Search code examples
linuxgomariadb

dial unix /var/lib/mysql/mysql.sock: connect: no such file or directory; ONLY while connected using database/sql


I'm pulling my hairs out here and I have no clue how to debug. I think this is a Linux problem and not a Go problem, but I have no idea how to fix it. Any indications on how to proceed would be highly appreciated.

I connect to MariaDB 10.3.28 over a UNIX socket on a CentOS Stream 8 (I know...), I even disabled fapolicyd or SELinux, the usual suspects, but the following happens: before connecting to the socket I can call os.Stat("/var/lib/mysql/mysql.sock") and it returns fine, also the database connects fine and I run a query and get data back. All good so far.

However, right after connecting, calling os.Stat again will return stat /var/lib/mysql/mysql.sock: no such file or directory. This is evident when after SetConnMaxLifetime expired, the connection pool can't reconnect and all queries fail (same error). Then after I call db.Close, suddenly all os.Stat have no problem finding /var/lib/mysql/mysql.sock again!

How is this possible? The socket file keeps existing during the whole time, somehow the system blocks access to the socket for this process while the database is connected?

Socket file permission is 0777 and all parent directories have o+x. MariaDB has among other settings:

max_connections=200
wait_timeout=120
open_files_limit=1200

local_infile=0
skip_name_resolve
skip_show_database
symbolic_links=0

Weird! Any help on debugging this highly appreciated.


Solution

  • I'm sorry to have wasted your time. It wasn't right after connecting that I couldn't reach the socket file, but after chroot which I totally forgot. This became apparent while using strace like @danblack suggested. Thanks for your time.