PHP connection MySQL Error: sqlstate[hy000] [2002] Can T connect to Local

Source: Internet
Author: User
Tags dsn ini socket mysql database

As shown below, PHP connection MySQL error:

SQLSTATE[HY000] [2002] Can ' t connect to the 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 ();
}

"Solution"

Change the host=localhost to host=127.0.0.1!


connection MySQL tips can ' t connect to the local MySQL server through socket solution

mysql,mysqldump,mysqladmin,php connection MySQL service often prompts the following error:
ERROR 2002 (HY000): Can ' t connect to the local MySQL server through socket '/var/lib/mysql/mysql.sock ' (2)
This is due to the modification of the MySQL service socket file Mysql.sock location, and caused by the MySQL socket file can not be connected to the MySQL service, the specific solution is as follows:

1, view the MySQL service socket file location:

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 path is the location of the socket file, we just modify my.cnf file, tell mysql,mysqldump,mysqladmin MySQL service socket location can be.

2, modify the My.cnf file:

Add the following content to the/etc/my.cnf file and restart the MYSQLS service to resolve the Mysql,mysqldump,mysqladmin "can" T connect to the 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 prompts "Can" T connect to the local MySQL server through socket ... "solution

Sometimes the MySQL service is running normally, username password is also completely correct, use PHP mysql_connect function but can't connect MySQL, call php mysql_error () function hint "Can" t connect to local MySQL Server through socket '/var/lib/mysql/mysql.sock ', which is what we need to modify/etc/php.ini file.
Locate "Mysql.default_socket" under "[MySQL]" in the/etc/php.ini file and set its value 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 the local MySQL server through socket ... "solution:

Specify the socket file in the connection to the 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 the local MySQL server through socket ... "solution:

The


also adds 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);
}
?

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.