mysqli::mysqli(): (HY000/2002): Can't connect to local MySQL server through socket 'MySQL' (2)
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')
Can't connect to local MySQL server through socket '/var/lib/mysql/mysql.sock' (2)
Are you connecting to "localhost" or "127.0.0.1" ? I noticed that when you connect to "localhost" the socket connector is used, but when you connect to "127.0.0.1" the TCP/IP connector is used. You could try using "127.0.0.1" if the socket connector is not enabled/working.
PHP Error mysqli::mysqli(): (HY000/2002)
Finally got the solution to this problem, I'm sharing it because I can't find a solution anywhere else. The $host should be linked to "127.0.0.1" but for an unknown reason my host only worked when it was "127.0.0.1:3307" or the name of your port for mysql at the end, don't forget the ":". PS I changed my db connection back to using the procedural way despite what the Ashu said and it worked.
MySQL connection not working: 2002 No such file or directory
If you use Linux: the path to the mysql.sock file is wrong. This is usually because you are using (LAMPP) XAMPP and it isn't in /tmp/mysql.sock
Open the php.ini file and find this line:
mysql.default_socket
And make it
mysql.default_socket = /path/to/mysql.sock
Related Topics
How to Get Count of Rows in MySQL Table Using PHP
Simple Jquery, PHP and Jsonp Example
Isset() and Empty() - What to Use
How to Increase the Execution Timeout in PHP
Fatal Error: Call to Undefined Function MySQL_Connect()
PHP.Ini & Smtp= - How to Pass Username & Password
How to Use a JavaScript Variable as a PHP Variable
PHPexcel Runs Out of 256, 512 and Also 1024Mb of Ram
Fastest Way to Check If a String Is Json in PHP
PHP Function to Make Slug (Url String)
"Cannot Send Session Cache Limiter - Headers Already Sent"
Using $_Post to Get Select Option Value from Html
Html Element Array, Name="Something[]" or Name="Something"
How to Convert a Number to a Word in PHP