Can’t connect to local MySQL server through socket [Solved]

  • 11,000
  • Tác giả: admin
  • Ngày đăng:
  • Lượt xem: 11
  • Tình trạng: Còn hàng

If you run rẩy into this error message while trying to lớn connect to lớn a local MySQL Server:

ERROR 2002 (HY000): Can't connect to lớn local MySQL server through socket '/var/lib/mysql/mysql.sock'

It means either the MySQL server is not installed/running, or the file mysql.sock doesn’t exist in /var/lib/mysql/.

There are a couple of solutions for this error.

1. First, kiểm tra to lớn see whether mysqld service is running or not. If not, start it:

Then try to lớn connect again.

2. Try to lớn connect to lớn 127.0.0.1 instead of localhost

If you connect to lớn localhost, it will use the socket connector, but if you connect to lớn 127.0.0.1 the TCP/IP connector will be used. So when the socket connector is not working, try connecting to lớn 127.0.0.1 instead.

3. Edit tệp tin my.cnf

Find the tệp tin my.cnf (usually in /etc/) to lớn edit and add these following line:

[mysqld]

socket=/var/lib/mysql/mysql.sock 

[client]

socket=/var/lib/mysql/mysql.sock

Restart mysql and connect again.

4. Symlink

In some cases, you mysql.sock might be located in another thư mục. You have to lớn find and symlink it. You might find it in /tmp/mysql.sock or /data/mysql_datadir/mysql.sock

For example, your tệp tin is located as in /tmp/mysql.sock

Then symlink it:

ln -s /var/lib/mysql/mysql.sock /tmp/mysql.sock

Restart mysql and connect again.


Need a good GUI Tool for MySQL? TablePlus is a modern, native tool with an elegant UI that allows you to lớn simultaneously manage multiple databases such as MySQL, PostgreSQL, SQLite, Microsoft SQL Server and more.


Download TablePlus for Mac.

Not on Mac? Download TablePlus for Windows.

On Linux? Download TablePlus for Linux

Need a quick edit on the go? Download TablePlus for iOS.

TablePlus GUI Tool MySQL