MySQL connection failure in Linux: Can't connect to local MySQL server through socket '/var/lib/MySQL/mysq

Source: Internet
Author: User

Recently, I used Django to write a small website on Linux, and MySQL was used for the database. But one day, a strange problem occurred, and an error was reported:

MySQL error 2002 (hy000): Can't connect to local MySQL server through socket '/var/lib/MySQL. Sock' (2)

Then I will go to the file '/var/lib/MySQL. sock ', but I couldn't find it, so I couldn't understand it. Then I remembered that MySQL service mysqld was not started, because MySQL-u root-P can still be used.

So I quickly checked the status of service mysqld status to see that it was not started, and then there was no problem.

 

 

 

 

MySQL startup error:

Error 2002 (hy000): Can't connect to local MySQL server through socket '/var/lib/MySQL. Sock' (2)

1. Check/etc/rc. d/init. d/mysqld status to see if MySQL has been started.
Check whether the permission is correct.

2. Check whether your MySQL. Sock is in that location,
Mysql-u your MySQL username-p-S/var/lib/MySQL. Sock

3. Try: Service mysqld start

4. If the permission problem occurs, change the permission first # chown-r MYSQL: MySQL/var/lib/MySQL

[Root @ localhost ~] #/Etc/init. d/mysqld start
Start MYSQL: [OK]
[Root @ localhost ~] # Mysql-u root-P

Error 2002 (hy000): Can't connect to local MySQL server through socket '/var/lib/MySQL. Sock' (2)
The reason is that the access permission of/var/lib/MySQL is incorrect.

Shell> chown-r MYSQL: MySQL/var/lib/MySQL

Start the server.

Shell>/etc/init. d/MySQL start

After the server is started normally, check/var/lib/MySQL to automatically generate the mysql. Sock file.

However, my problem is still not solved.

The problem is finally solved:
Method: Modify/etc/My. conf:
[Mysqld]
Datadir =/usr/local/MySQL/Data
Socket =/var/lib/MySQL. Sock

[Mysql. Server]
User = MySQL
Basedir =/usr/local/MySQL

If there is not currently a section called [client], add one at the bottom of the file and copy the socket = line under the [mysqld] Section such:

[Client]
Socket =/var/lib/MySQL. Sock

The error "Starting mysqlcouldn't find MySQL manager or server" is returned when you run/etc/init. d/MySQL start.

If the mysqld service is not enabled, run/usr/local/MySQL/bin/mysqld_safe &

Solve the problem.

When PHP is used, the following warning messages are displayed:

Warning: MySQL Connection Failed: Can't connect to local MySQL server through socket '/tmp/MySQL. sock '(111) in/home/httpd/html/Show. PHP on line 9

Link/tmp/MySQL. Sock to/var/lib/MySQL:

Linux # ln-S/var/lib/MySQL. Sock/tmp/MySQL. Sock

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.