elixir-ecto / myxql

MySQL 5.5+ driver for Elixir
Apache License 2.0
271 stars 66 forks source link

Conection failed on a Windows PC #158

Closed belgoros closed 1 year ago

belgoros commented 1 year ago

I created a dummy Phoenix app and set up the MySQL as database option. So the generated dev.exs settings look like that:

config :mysql_app, MysqlApp.Repo,
  username: "mysql",
  password: "password",
  hostname: "localhost",
  port: "33306",
  database: "my-db",
  stacktrace: true,
  show_sensitive_data_on_connection_error: true,
  pool_size: 10

The mix.exs has the following declared deps:

defp deps do
    [
      {:phoenix, "~> 1.6.12"},
      {:phoenix_ecto, "~> 4.4"},
      {:ecto_sql, "~> 3.6"},
      {:myxql, "~> 0.6.3"},
      {:phoenix_html, "~> 3.0"},
      {:phoenix_live_reload, "~> 1.2", only: :dev},
      {:phoenix_live_view, "~> 0.17.5"},
      {:floki, ">= 0.30.0", only: :test},
      {:phoenix_live_dashboard, "~> 0.6"},
      {:esbuild, "~> 0.4", runtime: Mix.env() == :dev},
      {:swoosh, "~> 1.3"},
      {:telemetry_metrics, "~> 0.6"},
      {:telemetry_poller, "~> 1.0"},
      {:gettext, "~> 0.18"},
      {:jason, "~> 1.2"},
      {:plug_cowboy, "~> 2.5"}
    ]
  end

After successfully installing the dependencies and starting the IEX console, it failed to connect to the database:

C:\Users\scambour\myprojects\elixir\phoenix\mysql_app>iex -S mix
warning: the :gettext compiler is no longer required in your mix.exs.

Please find the following line in your mix.exs and remove the :gettext entry:

    compilers: [..., :gettext, ...] ++ Mix.compilers(),

  (gettext 0.20.0) lib/mix/tasks/compile.gettext.ex:5: Mix.Tasks.Compile.Gettext.run/1
  (mix 1.14.0) lib/mix/task.ex:421: anonymous fn/3 in Mix.Task.run_task/4
  (mix 1.14.0) lib/mix/tasks/compile.all.ex:92: Mix.Tasks.Compile.All.run_compiler/2
  (mix 1.14.0) lib/mix/tasks/compile.all.ex:72: Mix.Tasks.Compile.All.compile/4
  (mix 1.14.0) lib/mix/tasks/compile.all.ex:59: Mix.Tasks.Compile.All.with_logger_app/2
  (mix 1.14.0) lib/mix/tasks/compile.all.ex:33: Mix.Tasks.Compile.All.run/1

Compiling 14 files (.ex)
Generated mysql_app app
[error] GenServer #PID<0.527.0> terminating
** (stop) an exception was raised:
    ** (FunctionClauseError) no function clause matching in :inet_tcp.getserv/1
        (kernel 8.4.2) inet_tcp.erl:58: :inet_tcp.getserv("33306")
        (kernel 8.4.2) gen_tcp.erl:230: :gen_tcp.connect1/4
        (kernel 8.4.2) gen_tcp.erl:214: :gen_tcp.connect/4
        (myxql 0.6.3) lib/myxql/client.ex:296: MyXQL.Client.do_connect/1
        (myxql 0.6.3) lib/myxql/client.ex:90: MyXQL.Client.connect/1
        (myxql 0.6.3) lib/myxql/connection.ex:25: MyXQL.Connection.connect/1
        (db_connection 2.4.2) lib/db_connection/connection.ex:82: DBConnection.Connection.connect/2
        (connection 1.1.0) lib/connection.ex:622: Connection.enter_connect/5
        (stdlib 4.0.1) proc_lib.erl:240: :proc_lib.init_p_do_apply/3
    (kernel 8.4.2) gen_tcp.erl:219: :gen_tcp.connect/4
    (myxql 0.6.3) lib/myxql/client.ex:296: MyXQL.Client.do_connect/1
    (myxql 0.6.3) lib/myxql/client.ex:90: MyXQL.Client.connect/1
    (myxql 0.6.3) lib/myxql/connection.ex:25: MyXQL.Connection.connect/1
    (db_connection 2.4.2) lib/db_connection/connection.ex:82: DBConnection.Connection.connect/2
    (connection 1.1.0) lib/connection.ex:622: Connection.enter_connect/5
    (stdlib 4.0.1) proc_lib.erl:240: :proc_lib.init_p_do_apply/3
Last message: nil
....
[notice] Application mysql_app exited: shutdown
iex(1)>

Adding protocol: :tcp to the Db connection settings didn't fix the error:

config :mysql_app, MysqlApp.Repo,
  username: "mysql",
  password: "password",
  hostname: "127.0.0.1",
  protocol: :tcp,
  port: "33306",
  database: "my-db",
  stacktrace: true,
  show_sensitive_data_on_connection_error: true,
  pool_size: 10

I can connect to the same DB from any DB client or even from a simple Ruby script using ActiveRecord settings. The database is running inside a Docker container locally on the same PC.

wojtekmach commented 1 year ago

please try changing your port from "33306" to 3306 (integer) :)

belgoros commented 1 year ago

@wojtekmach the port is correct (even if the default one used by MySQL is 3306). As I pointed it out earlier, I can connect to this DB from MySQLWorkbench and using a simple Ruby script.

wojtekmach commented 1 year ago

Ok. Can you try changing it from a string to an integer?

belgoros commented 1 year ago

Yep, changing to integer made it work:

port: 33306,