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:03

Ensure that your mysql service is running

service mysqld start

Then, try the one of the following following:

(if you have not set password for mysql)

mysql -u root

if you have set password already

mysql -u root -p
查看更多
永恒的永恒
3楼-- · 2018-12-31 18:03

if you change files in /var/lib/mysql [ like copy or replace that ], you must set owner of files to mysql this is so important if mariadb.service restart has been faild

chown -R mysql:mysql /var/lib/mysql/*

chmod -R 700 /var/lib/mysql/*

查看更多
零度萤火
4楼-- · 2018-12-31 18:03

This might be a stupid suggestion but make 100% sure your DB is still hosted at localhost. For example, if a Network Admin chose (or changed to) Amazon DB hosting, you will need that hostname instead!

查看更多
何处买醉
5楼-- · 2018-12-31 18:04

ran into this issue while trying to connect mysql in SSH client, found adding the socket path to the command helpful when switching between sockets is necessary.

> mysql -u user -p --socket=/path/to/mysql5143.sock
查看更多
宁负流年不负卿
6楼-- · 2018-12-31 18:06

One way to reproduce this error: If you meant to connect to a foreign server but instead connect to the non existent local one:

eric@dev ~ $ mysql -u dev -p
Enter password:
ERROR 2002 (HY000): Can't connect to local MySQL server through 
socket '/var/lib/mysql/mysql.sock' (2)
eric@dev ~ $

So you have to specify the host like this:

eric@dev ~ $ mysql --host=yourdb.yourserver.com -u dev -p
Enter password:
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 235
Server version: 5.6.19 MySQL Community Server (GPL)

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql> show databases;
+-------------------------+
| Database                |
+-------------------------+
| information_schema      |
| mysql                   |
| performance_schema      |
+-------------------------+
3 rows in set (0.00 sec)

mysql> exit
Bye
eric@dev ~ $
查看更多
人间绝色
7楼-- · 2018-12-31 18:06

If you are in the shell of sf.net, try:

mysql --host=mysql-{LETTER} --user={LETTER}{GROUP ID}admin -p

Change {LETTER} and {GROUP ID} as shown in your MySQL Database of project admin profile.

查看更多
登录 后发表回答