无法通过 socket '/var/lib/mysql/mysql.sock' 连接到本地 MySQL 服务器 (2)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4448467/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Can't connect to local MySQL server through socket '/var/lib/mysql/mysql.sock' (2)
提问by Atul
I am getting the following error when I try to connect to mysql:
当我尝试连接到 mysql 时出现以下错误:
Can't connect to local MySQL server through socket '/var/lib/mysql/mysql.sock' (2)
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?
这个错误有解决方案吗?背后的原因可能是什么?
采纳答案by Jan Thom?
Are you connecting to "localhost" or "127.0.0.1" ? I noticed that when you connect to "localhost" the socket connector is used, but when you connect to "127.0.0.1" the TCP/IP connector is used. You could try using "127.0.0.1" if the socket connector is not enabled/working.
您是连接到“localhost”还是“127.0.0.1”?我注意到当您连接到“localhost”时使用了套接字连接器,但是当您连接到“127.0.0.1”时使用了 TCP/IP 连接器。如果套接字连接器未启用/工作,您可以尝试使用“127.0.0.1”。
回答by Haja Peer Mohamed H
Ensure that your mysql service is running
确保您的 mysql 服务正在运行
service mysqld start
Then, try the one of the following following:
然后,尝试以下操作之一:
(if you have not set password for mysql)
(如果你还没有为mysql设置密码)
mysql -u root
if you have set password already
如果你已经设置了密码
mysql -u root -p
回答by marimaf
If your file my.cnf (usually in the etc folder) is correctly configured with
如果您的文件 my.cnf(通常在 etc 文件夹中)正确配置为
socket=/var/lib/mysql/mysql.sock
you can check if mysql is running with the following command:
您可以使用以下命令检查 mysql 是否正在运行:
mysqladmin -u root -p status
try changing your permission to mysql folder. If you are working locally, you can try:
尝试更改您对 mysql 文件夹的权限。如果您在本地工作,您可以尝试:
sudo chmod -R 777 /var/lib/mysql/
that solved it for me
这为我解决了
回答by Dan Grossman
The MySQL server is not running, or that is not the location of its socket file (check my.cnf).
MySQL 服务器没有运行,或者这不是它的套接字文件的位置(检查 my.cnf)。
回答by sreddy
Most likely mysql.sock
does not exist in /var/lib/mysql/
.
中很可能mysql.sock
不存在/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
例如:我有它 /data/mysql_datadir/mysql.sock
Switch user to mysql and execute as mentioned below:
将用户切换到 mysql 并执行如下所述:
su mysql
ln -s /data/mysql_datadir/mysql.sock /var/lib/mysql/mysql.sock
That solved my problem
那解决了我的问题
回答by duhaime
If you are on a recent RHEL, you may need to start mariadb (an open source mysql db) instead of the mysql db:
如果您使用的是最近的 RHEL,您可能需要启动 mariadb(一个开源 mysql db)而不是 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:
mysql -u root -p
回答by Dejan
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
在我来说,我已经搬到套接字文件到另一个位置,里面/etc/my.cnf
从/var/lib/mysql/mysql.sock
到/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)
即使在重新启动 mysqld 服务后,当我尝试连接时仍然看到错误消息。
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
问题在于客户端的配置方式。运行诊断实际上会显示正确的套接字路径。例如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 fixthis problem by adding the same socketline under [client]
section inside mysql config.
您可以通过在 mysql 配置中的部分下添加相同的套接字行来解决此问题。[client]
回答by minhas23
Just edit /etc/my.cnf
Add following lines to my.cnf
只需编辑/etc/my.cnf
以下行添加到my.cnf
[mysqld]
socket=/var/lib/mysql/mysql.sock
[client]
socket=/var/lib/mysql/mysql.sock
Restart mysql and connect again
重启mysql并再次连接
mysql -u user -p password database -h host;
mysql -u 用户 -p 密码数据库 -h 主机;
回答by vkGunasekaran
Check if your mysqld service is running or not, if not run, start the service.
检查你的mysqld服务是否正在运行,如果没有运行,启动该服务。
If your problem isn't solved, look for /etc/my.cnf
and modify as following, where you see a line starting with socket
. Take a backup of that file before doing this update.
如果您的问题没有解决,请查找/etc/my.cnf
并修改如下,您会看到一行以socket
. 在执行此更新之前备份该文件。
socket=/var/lib/mysql/mysql.sock
Change to
改成
socket=/opt/lampp/var/mysql/mysql.sock -u root
回答by Karthik
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)
确保您有足够的空间/var
。如果 Mysql Demon 无法向驱动器写入附加信息,则 mysql 服务器将无法启动并导致错误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.
这将帮助您降低磁盘使用率。