Home >Database >Mysql Tutorial >Why Can't I Connect to My MySQL Server via Unix Socket?
When trying to establish a connection to a MySQL database through the PHP MySQLi class, an error message may appear: "Can't connect to local MySQL server through socket 'MySQL' (2)."
This error indicates that the MySQL client library is attempting to use a Unix domain socket for the connection, but the socket is not configured correctly or does not exist. By default, localhost connections use a Unix socket, which can be faster and more secure than TCP/IP. However, specifying 'localhost' may not work if the socket is not configured properly.
To resolve this issue, consider the following solutions:
$db = new MySQLi('localhost', 'kamil', '***', '', 0, '/var/run/mysqld/mysqld.sock');
The above is the detailed content of Why Can't I Connect to My MySQL Server via Unix Socket?. For more information, please follow other related articles on the PHP Chinese website!