As shown below, PHP connection MySQL error:
SQLSTATE[HY000] [2002] Can ' t connect to local MySQL server through socket ' MySQL ' (2)
The test code is as follows:
<?php
try
{
$dsn = ‘mysql:dbname=php-note;host=localhost;port=3306;charset=utf8‘;
$username = ‘root‘;
$password = ‘root‘;
new PDO( $dsn, $username, $password);
}
catch (\PDOException $e)
{
echo $e->getMessage();
}
" Solutions "
Change host=localhost to host=127.0.0.1 !
Solution to connect MySQL hint can ' t connect to local MySQL server through socket
mysql,mysqldump,mysqladmin,php connecting to MySQL service often prompts the following error:
ERROR 2002 (HY000): Can ' t connect to local MySQL server through socket '/var/lib/mysql/mysql.sock ' (2)
This is due to the mysql.sock location of the socket file that modifies the MySQL service, which is caused by the inability to connect to the MySQL service via the MySQL socket file, the following are the specific workarounds:
1. View the location of the socket file for the MySQL service:
The location of the MySQL socket file is set in/etc/my.cnf, and the CAT/ETC/MY.CNF content is as follows:
[Mysqld]
Datadir=/storage/db/mysql
Socket=/storage/db/mysql/mysql.sock
User=mysql
Where the socket is equal to the location of the socket file, we just modify the my.cnf file, tell mysql,mysqldump,mysqladmin MySQL service socket location where you can.
2. Modify the My.cnf file:
Add the following in the/etc/my.cnf file and restart the MYSQLS service to resolve the Mysql,mysqldump,mysqladmin "can ' t connect to local MySQL server through socket ' /var/lib/mysql/mysql.sock ' "Question:
[Mysqld]
Datadir=/storage/db/mysql
Socket=/storage/db/mysql/mysql.sock
[MySQL]
Socket=/storage/db/mysql/mysql.sock
[Mysqldump]
Socket=/storage/db/mysql/mysql.sock
[Mysqladmin]
Socket=/storage/db/mysql/mysql.sock
3. PHP Connection MySQL service tip "Can ' t connect to local MySQL server through socket ..." Workaround
Sometimes the MySQL service is working properly, the user name password is also correct, using PHP mysql_connect function is not connected to MySQL, call PHP mysql_error () function hint "Can ' t connect to local MySQL Server through socket '/var/lib/mysql/mysql.sock ', which is the/etc/php.ini file we need to modify.
Locate "Mysql.default_socket" under "[MySQL]" in the/etc/php.ini file and set its value to point to the correct MySQL service socket file, such as:
[MySQL]
... Omit n rows ...
Mysql.default_socket = "/storage/db/mysql/mysql.sock"
4. Python connection mysql hint "Can ' t connect to local MySQL server through socket ..." Workaround:
Specify the socket file in the Connect MySQL database function as follows:
#!/usr/bin/python
From MYSQLDB Import Connect
conn = Connect (db= "Pzy", user= "root", host= "localhost", unix_socket= "/storage/db/mysql/mysql.sock")
cur = conn.cursor ()
Count=cur.execute ("Show Databases")
print ' There has%s dbs '% count
Conn.commit ()
Conn.close ()
5. PHP PDO connection mysql hint "Can ' t connect to local MySQL server through socket ..." Workaround:
Also add the location of the MySQL socket file in the connection string, as follows:
<?php
$DSN = "Mysql:host=localhost;dbname=pzy;unix_socket=/storage/db/mysql/mysql.sock";
$db = new PDO ($dsn, ' root ', ');
$rs = $db->query ("SELECT * from Qrtest");
while ($row = $rs->fetch ()) {
Print_r ($row);
}
?>
Reference:
Http://www.aiezu.com/db/mysql_cant_connect_through_socket.html
PHP Connection MySQL Error: sqlstate[hy000] [2002] Can ' t connect to local MySQL server through socket ' MySQL ' (2)