Reputation: 63
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
mysqli.default_socket = /tmp/mysql.sock
[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?
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.
Upvotes: 3
Views: 5811
Reputation: 168
In my case solution was to change localhost to 127.0.0.1
I was connecting from WSL to Windows Instance of MySQL Server
Upvotes: 3
Reputation: 123
In my case I used strace /usr/bin/php test.php
and found more info:
connect(3, {sa_family=AF_UNIX, sun_path="/var/run/mysqld/mysqld.sock"}, 29) = -1 ENOENT (No such file or directory)
This was due to me running inside a jail with no access to the local socket file so I was forced to use TCP protocol by changing localhost to 127.0.0.1 in the php code.
Upvotes: 0
Reputation: 802
You want to connect to mysql via socket in your code
$dblink = mysqli_connect('localhost', 'root', 'rootpass', 'testdb', 3306, '/tmp/mysql.sock');
try to replace this with:
$dblink = mysqli_connect('/tmp/mysql.sock', 'root', 'rootpass', 'testdb', 3306);
there is a solution available here as well https://serverfault.com/questions/673854/how-to-let-php-connect-to-database-by-using-unix-socket-joomla hope this helps.
Upvotes: 0
Reputation: 13
This could be a simple permissions problem. Seems like only root/current user/sudo has rights to the tmp folder (which is why terminal works and php doesn't).
Also, as a reference to my own Centos 7 mysql setup:
my datadir and socket locations are identical hence why mysql has access to the .sock file
--datadir=/var/lib/mysql --socket=/var/lib/mysql/mysql.sock
Upvotes: 0