Solution to connect MySQL hint can ' t connect to local MySQL server through socket

Source: Internet
Author: User
Tags dsn

mysql,mysqldump,mysqladmin,php connecting to MySQL service often prompts the following error:

1 < Span class= "crayon-i" >error 2002 (hy000: can ' t connect to local MySQL server through Socket ' /var/lib< Span class= "Crayon-o" >/mysql/mysqlsock ' (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:

1234 < Span class= "Crayon-sy" >[mysqld datadir=/storage /db/mysql socket=/storage/db /mysql/mysqlsock user=mysql

Where the socket equals the path is the socket file location, 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 MySQL,mysqldump,mysqladmin "can ' t connect to local MySQL server through Socket '/var/lib/mysql/mysql.sock ' "Problem:

123456789101112 [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:

123 [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:

12345678 #!/usr/bin/pythonfrom 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:

12345678 <?php$dsn = "Mysql:host=localhost;dbname=pzy;unix_socket=/storage/db/mysql/mysql.sock"; $db = new PDO($dsn, ' root ', ' ); $rs = query("select * from Qrtest") , $db while($row = fetch() $rs){ print_r($row); }?>

Solution to connect MySQL hint can ' t connect to local MySQL server through socket

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.