Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

localhost: socket: mysqli_connect(): (HY000/2002): No such file or directory

When I attempt to use a socket (not TCP) connection to connect to MySQL 5.7.21 using PHP 7.2.4 mysqli_connect() on Centos 7.4 I get an error:

Warning: mysqli_connect(): (HY000/2002): No such file or directory

php.ini

mysqli.default_socket = /tmp/mysql.sock

my.cnf

[mysqld]
socket=/tmp/mysql.sock
...
[client]
socket=/tmp/mysql.sock

MySQL connection from command line works fine:

/usr/local/mysql57/bin/mysql --socket tmp/mysql.sock --host localhost -u root -p  

Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 3
Server version: 5.7.21 MySQL Community Server (GPL)

ls -l /tmp/my*  

srwxrwxrwx 1 mysql mysql 0 Apr 19 22:01 /tmp/mysql.sock
-rw------- 1 mysql mysql 5 Apr 19 22:01 /tmp/mysql.sock.lock

ps -edf | grep mysql  

mysql 3593 3329 0 22:01 ? 00:00:03 /usr/local/mysql57/bin/mysqld --defaults-file=/usr/local/mysql57/my.cnf --basedir=/usr/local/mysql57 --datadir=/mysql57 --plugin-dir=/usr/local/mysql57/lib/plugin --log-error=myhost.err --pid-file=myhost.pid --socket=/tmp/mysql.sock --port=3306

$dblink = mysqli_connect('localhost', 'root', 'rootpass', 'testdb', 3306, '/tmp/mysql.sock');
if (mysqli_connect_errno($dblink)) {
    echo "Failed to connect to MySQL: " . mysqli_connect_error();
}

MySQL generic tar.gz was installed. I'm using nginx 1.12.2. If I use 127.0.0.1 or locahost:3306 such that TCP is used it works fine. Why doesn't socket work?

Update #1

I have another Centos 7.4 image with MySQL 5.7.11 which works with socket. However, I don't start it using systemctl. I also noticed that even though it's running there is no /tmp/mysql.sock.lock file, just /tmp/mysql.sock
Other than that it's mostly the same, in fact, /tmp/mysql.sock isn't specified anywhere in php.ini nor my.cnf files.

like image 463
Hiyo Avatar asked Nov 24 '25 06:11

Hiyo


1 Answers

In my case solution was to change localhost to 127.0.0.1

I was connecting from WSL to Windows Instance of MySQL Server

like image 50
Nikita Bulanakov Avatar answered Nov 25 '25 19:11

Nikita Bulanakov



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!