When you use just “localhost” the MySQL client library tries to use a Unix domain socket for the connection instead of a TCP/IP connection. The error is telling you that the socket, called MySQL
, cannot be used to make the connection, probably because it does not exist (error number 2).
From the MySQL Documentation:
On Unix, MySQL programs treat the host name localhost specially, in a
way that is likely different from what you expect compared to other
network-based programs. For connections to localhost, MySQL programs
attempt to connect to the local server by using a Unix socket file.
This occurs even if a –port or -P option is given to specify a port
number. To ensure that the client makes a TCP/IP connection to the
local server, use –host or -h to specify a host name value of
127.0.0.1, or the IP address or name of the local server. You can also specify the connection protocol explicitly, even for localhost, by
using the –protocol=TCP option.
There are a few ways to solve this problem.
- You can just use TCP/IP instead of the Unix socket. You would do this by using
127.0.0.1
instead oflocalhost
when you connect. The Unix socket might by faster and safer to use, though. - You can change the socket in
php.ini
: open the MySQL configuration filemy.cnf
to find where MySQL creates the socket, and set PHP’smysqli.default_socket
to that path. On my system it’s/var/run/mysqld/mysqld.sock
. -
Configure the socket directly in the PHP script when opening the connection. For example:
$db = new MySQLi('localhost', 'kamil', '***', '', 0, '/var/run/mysqld/mysqld.sock')