Ecto MyXQL error - (DBConnection.ConnectionError) (/tmp/mysql.sock) no such file or directory

So my application was working fine for the last couple of months. Started working on releasing it on our app server, ran into this problem. At first I thought it was just an issue with the mix release. But later went back and discovered it is happening on my workstation in dev env now too. This is the error that occurs as soon as the application starts:

error] MyXQL.Connection (#PID<0.498.0>) failed to connect: ** (DBConnection.ConnectionError) (/tmp/mysql.sock) no such file or directory - :enoent

It repeats this over and over. I have tried everything I can think of… and here is the weird part. I created a whole new phoenix app, copied over just the database configs and dependencies, then coded a single db query to display on the home page and it works fine!!!

The error doesn’t make sense either. I don’t understand why I am getting an error about a mysql socket when it is setup as a :tcp connection – the database is on another host.

I’ve been pulling my hair out for almost two days on this and have made zero inroads in discovering anything at all as to why this is happening. This keeps up for much longer I’m going to get kicked in the rear by my boss and forced to use Java/Tomcat and JSP. For the love of God, please no!

This is my config/dev.exs.

config :nai, Nai.Repo,
  database: "db/dev.db",
  priv: "priv/repo"

config :nai, Nai.Fish.Repo,
  database: "NAI",
  username: "xxx",
  password: "xxx",
  hostname: "appsnh",
  port: 3305,
  protocol: :tcp,
  priv: "/dev/null",
  #socket_options: [:inet6]  (not with myxql)
  stacktrace: true,
  pool_size: 10,
  show_sensitive_data_on_connection_error: true,
  read_only: true

And my repo modules:

defmodule Nai.Repo do
  use Ecto.Repo,
    otp_app: :nai,
    adapter: Ecto.Adapters.SQLite3

defmodule Nai.Fish.Repo do
  use Ecto.Repo,
    otp_app: :nai,
    adapter: Ecto.Adapters.MyXQL,
    read_only: true

For the most part I am using the most recent releases of all dependencies… snippet from my mix.exs.

      {:phoenix, "~> 1.6.0"},
      {:phoenix_ecto, "~> 4.4"},
      {:ecto_sql, "~> 3.8"},
      {:ecto_sqlite3, "~> 0.8.0"},
      #{:postgrex, ">= 0.0.0"},
      {:myxql, "~> 0.6.2"},

Please help!

Basic sanity check: Is /tmp/mysql.sock the right path? Does that file exist if you look for it in your terminal? Does your application have permission to read from /tmp?

1 Like

It really sounds like the correct config is not being used. You should try logging Application.get_env(:nai, Nai.Fish.Repo) and see what it is

1 Like

Check that “appsnh” will resolve to the IP address of the MySQL server. If not, I’m willing to bet that’s your issue.

You can use either host or nslookup (even ping will work) on your workstation to check it. If it’s not resolving to a usable IP address, the adapter might fall back to looking for a socket at a default location.

I can say after years of supporting web hosting w/PHP etc., if you use “localhost” for :hostname, many adapters, drivers, etc., will automatically look for the socket on the file system because it’s generally faster.

If the IP address of the MySQL server is static, do yourself a favor and just use the IP address instead.

MyXQL is going to pick that path if it sees a socket option in its config:

What is printed when you run Application.get_env(:nai, Nai.Fish.Repo)? (beware pasting the credentials)

My suspicion is that a socket is sneaking in there from another config file or runtime.exs.

My suspicion is that a socket is sneaking in there from another config file or runtime.exs.

That is a good suspicion!!! As that’s exactly what was going on … and why it was so hard to figure out. (See next post for explanation.)

It’s a good kick in the pants for me to take the time to learn OTP better so I can analyze what’s going on behind the scenes.

Thanks @benwilson512 @joey_the_snake @l00ker and @al2o3cr. I was able to figure it out – well I should say it “dawned on me”, b/c I’m not sure how one would actually “figure” this out.

The problem is with a separate project that I’m pulling in as a dependency. That project houses all the schema for the “Fish” database (there’s a lot of them 300+, and I wanted some SoC). That project uses the database too but only for testing purposes. So somehow when I add it to my dependencies and run my main application, that dependency is automatically added to the supervision tree and it tries to connect to the database, though as of yet I have no idea where it’s pulling its config from – but clearly it trying to use a socket :wink:

I had circumvented this issue before (basically by accident)… I used runtime: false with the dependency entry in the mix.exs file. And that prevented the problem.

But, when I create a release something goes awry, because the release application starts exhibiting the same behavior as if the runtime: false isn’t there. I would assume that this setting might prevent the dependency from being included in the release at all, but that doesn’t seem to be so b/c then why would I be getting the mysql socket error?

Trying to fix my release issue, I had removed the runtime: false option not realizing that I was causing my dev env to break in the same way.

So I still don’t fully understand what’s going on. But at least now I have some idea as to why. I need to figure out how to workaround this. I am not sure how I can both include the Fish dependency in my release and keep it from being added to the supervision tree. On the other hand, maybe I can just make the dependency responsible for the database connection, instead of doing it in my main app, and then I shouldn’t have to worry about it.