Can't connect to local MySQL server through so

2018-12-31 17:49发布

I am getting the following error when I try to connect to mysql:

Can't connect to local MySQL server through socket '/var/lib/mysql/mysql.sock' (2)

Is there a solution for this error? What might be the reason behind it?

30条回答
余生无你
2楼-- · 2018-12-31 18:14

If you are on a recent RHEL, you may need to start mariadb (an open source mysql db) instead of the mysql db:

yum remove mysql
yum -y install mariadb-server mariadb
service mariadb start

You should then be able to access mysql in the usual fashion:

mysql -u root -p
查看更多
看风景的人
3楼-- · 2018-12-31 18:15

Make sure you have enough space left in /var. If Mysql demon is not able to write additional info to the drive the mysql server won't start and it leads to the error Can't connect to local MySQL server through socket '/var/lib/mysql/mysql.sock' (2)

Consider using

expire_logs_days = 10
max_binlog_size = 100M

This will help you keep disk usage down.

查看更多
只靠听说
4楼-- · 2018-12-31 18:16

In my case I have moved socket file to another location inside /etc/my.cnf from /var/lib/mysql/mysql.sock to /tmp/mysql.sock

Even after restarting the mysqld service, I still see the error message when I try to connect. ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/var/lib/mysql/mysql.sock' (2)

The problem is with the way that the client is configured. Running diagnostics will actually show the correct socket path. eg ps aux | grep mysqld

Works:

mysql -uroot -p -h127.0.0.1
mysql -uroot -p --socket=/tmp/mysql.sock

Does not Work:

mysql -uroot -p
mysql -uroot -p -hlocalhost

You can fix this problem by adding the same socket line under [client] section inside mysql config.

查看更多
明月照影归
5楼-- · 2018-12-31 18:17

Most likely mysql.sock does not exist in /var/lib/mysql/.

If you find the same file in another location then symlink it:

For ex: I have it in /data/mysql_datadir/mysql.sock

Switch user to mysql and execute as mentioned below:

su mysql

ln -s /data/mysql_datadir/mysql.sock /var/lib/mysql/mysql.sock

That solved my problem

查看更多
皆成旧梦
6楼-- · 2018-12-31 18:18

Make sure you started the server:

mysql.server start

Then connect with root user:

mysql -uroot
查看更多
临风纵饮
7楼-- · 2018-12-31 18:18

I had to disable explicit_defaults_for_timestamp from my.cnf.

查看更多
登录 后发表回答