【问题标题】:Erlang failing to connect to mysql/mariadbErlang 无法连接到 mysql/mariadb
【发布时间】:2020-10-18 19:59:18
【问题描述】:

我无法使用 erlang 连接到 mysql/mariadb。

根据这个 github-page 的要求: https://github.com/mysql-otp/mysql-otp

要求:

Erlang/OTP version R16B or later
MySQL database version 4.1 or later or MariaDB
GNU Make or Rebar or any other tool for building Erlang/OTP applications

版本:

14> erlang:system_info(otp_release).
"22"

我不确定是否需要这个要求,但我添加了这个:

[mysqld] default_authentication_plugin=mysql_native_password

到我的 /etc/my.cnf。但这可能无关紧要,因为错误是未定义的函数。 我可以编译代码,但我无法运行它。非常感谢您为这项工作提供任何帮助。

代码:

-module(mydatabase).
-compile(export_all).

connect_to_database() -> 
Conn = mysql:start_link([{host, "localhost"}, {user, "user"},
                       {password, "password"}, {database, "MyDatabase"}]) ,
           
  case Conn of
      {ok, Pid} -> io:fwrite("~w~w~n", [ok,Pid]);
      {error, ConnErr}  -> io:fwrite("error : ~p ~n", [ConnErr])
    end.
              
 start() -> connect_to_database().

mariadb 正在运行:

sudo systemctl status mariadb
[sudo] password for user: 
● mariadb.service - MariaDB 10.4.13 database server
   Loaded: loaded (/usr/lib/systemd/system/mariadb.service; disabled; vendor >
   Active: active (running) since Sun 2020-06-28 15:33:50 CEST; 1h 4min ago

错误信息:

12> c(mydatabase).     
mydatabase.erl:2: Warning: export_all flag enabled - all functions will be exported
{ok,mydatabase}
13> mydatabase:start().
** exception error: undefined function mysql:start_link/1
 in function  mydatabase:connect_to_database/0 (mydatabase.erl, line 1

【问题讨论】:

    标签: mysql erlang mariadb erlang-shell


    【解决方案1】:

    你忘记了这个要求:

    GNU Make 或 Rebar 或任何其他用于构建 Erlang/OTP 应用程序的工具

    根据mysql-otp docs

    MySQL/OTP 是将 Erlang/OTP 应用程序连接到 MySQL 的驱动程序 和 MariaDB 数据库。

    OTP application需要一定的架构,mysql驱动需要在应用中列为依赖。您的错误是由于 Erlang 中没有名为 mysql:start_link/1 的函数。相反,这是您的代码必须以某种方式访问​​的第三方函数,因此文档中的 Usage as a Dependency 部分。

    以下步骤可用于创建使用 mysql/mariaDB 作为依赖项的OTP application

    1. Install rebar3.

    2. 创建钢筋3 app:

      ~/erlang_programs/mysql1$ rebar3 new app myapp
      ===> Writing myapp/src/myapp_app.erl
      ===> Writing myapp/src/myapp_sup.erl
      ===> Writing myapp/src/myapp.app.src
      ===> Writing myapp/rebar.config
      ===> Writing myapp/.gitignore
      ===> Writing myapp/LICENSE
      ===> Writing myapp/README.md
      
    3. 在 rebar.config 文件中添加 mysql-otp 作为依赖项:

        ~/erlang_programs/mysql1$ cd myapp
        ~/erlang_programs/mysql1/myapp$ ls
        LICENSE       rebar.config
        README.md src
      

    像这样:

        {erl_opts, [debug_info]}.
        {deps, [
    
          {mysql, ".*", {
                          git, "https://github.com/mysql-otp/mysql-otp",
                          {tag, "1.6.0"}
                        }
          }
    
        ]}.
    
        {shell, [
          % {config, "config/sys.config"},
            {apps, [myapp]}
        ]}.
    
    1. 把你的源代码放在src目录下:

       ~/erlang_programs/mysql1/myapp$ cd src
       ~/erlang_programs/mysql1/myapp/src$ ls
       my_mysql.erl    myapp_app.erl
       myapp.app.src   myapp_sup.erl
      

    my_mysql.erl:

    -module(my_mysql).
    -compile(export_all).
    
    do_mysql(Name, Info) ->
        {ok, MysqlPid} = mysql:start_link(
                      [{host, "localhost"}, 
                       {user, "root"},
                       {password, ""}, 
                       {database, "mydb"}
                      ]
                    ),
    
         ok = mysql:query(
               MysqlPid, 
               "INSERT INTO people (name, info) VALUES (?, ?)", [Name, Info]
            ),
    
         {ok, ColumnNames, Rows} = mysql:query(
                    MysqlPid, 
                    <<"SELECT * FROM people">>),
    
         io:format("ColumnNames: ~p~nRows: ~p~n", [ColumnNames, Rows]).
    
    1. 获取依赖并编译所有源代码:

       ~/erlang_programs/mysql1/myapp$ rebar3 compile
       ===> Verifying dependencies...
       ===> Fetching mysql ({git,"https://github.com/mysql-otp/mysql-otp",
                                        {tag,"1.6.0"}})
       ===> Compiling mysql
       ===> Compiling myapp
       src/my_mysql.erl:2: Warning: export_all flag enabled - all functions will be exported
      
    2. 启动 shell 并自动构建所有 .beam 文件的路径(在 rebar3 放置它们的深层嵌套目录中):

       ~/erlang_programs/mysql1/myapp$ rebar3 shell
       ===> Verifying dependencies...
       ===> Compiling myapp
       Erlang/OTP 20 [erts-9.3] [source] [64-bit] [smp:4:4] [ds:4:4:10] [async-threads:1] [hipe] [kernel-poll:false]
      
       ===> The rebar3 shell is a development tool; to deploy applications in production, consider using releases (http://www.rebar3.org/docs/releases)
       ===> Booted myapp
       Eshell V9.3  (abort with ^G)
      
    3. 执行你的函数:

       1> my_mysql:do_mysql("Jeffsy", "2.0").
       ColumnNames: [<<"id">>,<<"name">>,<<"info">>]
       Rows: [[1,<<"7stud">>,<<"abc">>],
              [2,<<"Beth">>,<<"xxx">>],
              [3,<<"Diane">>,<<"xyz">>],
              [4,<<"Kathy">>,<<"xyz">>],
              [5,<<"Kathy">>,<<"xyz">>],
              [6,<<"Dave">>,<<"efg">>],
              [7,<<"Tom">>,<<"zzz">>],
              [8,<<"David">>,<<"abc">>],
              [9,<<"Eloise">>,<<"abc">>],
              [10,<<"Jess">>,<<"xyz">>],
              [11,<<"Jeffsy">>,<<"2.0">>]]
       ok
      

    【讨论】:

      猜你喜欢
      • 2015-10-13
      • 2017-09-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-08-07
      • 1970-01-01
      • 2019-08-02
      • 2019-05-19
      相关资源
      最近更新 更多