无法通过 socket '/var/mysql/mysql.sock' 连接到本地 MySQL 服务器 (38)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5376427/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-31 19:13:23  来源:igfitidea点击:

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

mysql

提问by Lambivist

I am having a big problem trying to connect to mysql. When I run:

我在尝试连接到 mysql 时遇到了一个大问题。当我运行时:

/usr/local/mysql/bin/mysql start

I have the following error :

我有以下错误:

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

I do have mysql.sockunder the /var/mysqldirectory.

mysql.sock/var/mysql目录下确实有。

In /etc/my.cnfI have:

/etc/my.cnf我有:

[client]
port=3306
socket=/var/mysql/mysql.sock

[mysqld]
port=3306
socket=/var/mysql/mysql.sock
key_buffer_size=16M
max_allowed_packet=8M

and in /etc/php.iniI have :

/etc/php.ini我有:

; Default socket name for local MySQL connects.  If empty, uses the built-in
; MySQL defaults.
mysql.default_socket = /var/mysql/mysql.sock

I have restarted apache using sudo /opt/local/apache2/bin/apachectl restart

我已经使用重新启动了apache sudo /opt/local/apache2/bin/apachectl restart

But I still have the error.

但我仍然有错误。

Otherwise, I don't know if that's relevant but when I do mysql_config --socketsI get

否则,我不知道这是相关的,但是当我这样做mysql_config --sockets,我得到

--socket         [/tmp/mysql.sock]

采纳答案by marimaf

If your file my.cnf (usually in the /etc/mysql/folder) is correctly configured with

如果您的文件 my.cnf(通常在/etc/mysql/文件夹中)正确配置为

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 755 /var/lib/mysql/

that solved it for me

这为我解决了

回答by Waqas

are you sure you installed mysql as well as mysql server..

你确定你安装了mysql以及mysql服务器..

For example to install mySql server I'll use yum or apt to install both mysql command line tool and the server:

例如,要安装 mySql 服务器,我将使用 yum 或 apt 来安装 mysql 命令行工具和服务器:

yum -y install mysql mysql-server (or apt-get install mysql mysql-server)

Enable the MySQL service:

启用 MySQL 服务:

/sbin/chkconfig mysqld on

Start the MySQL server:

启动 MySQL 服务器:

/sbin/service mysqld start

afterwards set the MySQL root password:

然后设置 MySQL 根密码:

mysqladmin -u root password 'new-password' (with the quotes)

I hope it helps.

我希望它有帮助。

回答by Maurizio

A quick workaround that worked for me: try using the local ip address (127.0.0.1) instead of 'localhost' in mysql_connect(). This "forces" php to connect through TCP/IP instead of a unix socket.

一个对我有用的快速解决方法:尝试在 mysql_connect() 中使用本地 IP 地址 (127.0.0.1) 而不是“localhost”。这“强制”php 通过 TCP/IP 而不是 unix 套接字连接。

回答by AnupRaj

I got the following error

我收到以下错误

ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock' (111)

Tried several ways and finally solved it through the following way

尝试了几种方法,最后通过以下方式解决了

sudo gksu gedit /etc/mysql/my.cnf

modified

修改的

#bind-address       = 127.0.0.1

to

bind-address        = localhost

and restarted

并重新启动

sudo /etc/init.d/mysql restart

it worked

有效

回答by Zorayr

Make sure you are running mysqld : /etc/init.d/mysql start

确保您正在运行 mysqld : /etc/init.d/mysql start

回答by Ali Hashemi

To prevent the problem from occurring, you must perform a graceful shutdown of the server from the command line rather than powering off the server.

为防止出现此问题,您必须从命令行正常关闭服务器,而不是关闭服务器电源。

shutdown -h now

This will stop the running services before powering down the machine.

这将在关闭机器电源之前停止正在运行的服务。

Based on Centos, an additional method for getting it back up again when you run into this problem is to move mysql.sock:

基于Centos,当你遇到这个问题时,另一个让它重新备份的方法是移动mysql.sock:

mv /var/lib/mysql/mysql.sock /var/lib/mysql/mysql.sock.bak

service mysqld start

Restarting the service creates a new entry called mqsql.sock

重新启动服务会创建一个名为 mqsql.sock 的新条目

回答by Doulat Khan

I got this error when I set cron job for my file. I changed the permissions of file to 777but it still not worked for me. Finally I got the solution. May be it will be helpful for others.

为文件设置 cron 作业时出现此错误。我将文件的权限更改为777但它仍然对我不起作用。最后我得到了解决方案。可能对其他人有帮助。

Try with this command:

试试这个命令:

mysql -h 127.0.0.1 -P 3306 -u root -p

mysql -h 127.0.0.1 -P 3306 -u 根 -p

Remember that -h means host, -P means portand -p means password.

请记住, -h 表示主机, -P 表示端口, -p 表示密码。

回答by matt2000

As can be seen by the many answers here, there are lots of problems that can result in this error message when you start the MySQL service. The thing is, MySQL will generally tell you exactly what's wrong, if you just look in the appropriate log file.

从这里的许多答案可以看出,当您启动 MySQL 服务时,有很多问题可能会导致此错误消息。问题是,如果您只查看相应的日志文件,MySQL 通常会准确地告诉您出了什么问题。

For example, on Ubuntu, you should check /var/log/syslog. Since lots of other things might also be logging to this file, you probably want to use grepto look at mysql messages, and tailto look at only the most recent. All together, that might look like:

例如,在 Ubuntu 上,您应该检查/var/log/syslog. 由于许多其他内容也可能会记录到此文件中,因此您可能希望grep用来查看 mysql 消息,并且tail只查看最新的消息。总之,这可能看起来像:

grep mysql /var/log/syslog | tail -50

grep mysql /var/log/syslog | tail -50

Don't blindly make changes to your configuration because someone else said 'This worked for my system.' Figure out what is actually wrong with your system and you'll get a better result much faster.

不要因为有人说“这对我的系统有效”就盲目地更改您的配置。弄清楚您的系统究竟出了什么问题,您会更快地获得更好的结果。

回答by theshadow

Another workaround is to edit /etc/my.cnf and include host in the section [client]

另一种解决方法是编辑 /etc/my.cnf 并在 [client] 部分中包含主机

 [client]
 #password       = your_password
 host            = 127.0.0.1
 port            = 3306
 socket          = /var/run/mysql/mysql.sock

And then restarting the mysql service.

然后重启mysql服务。

This workaround was tested in: Server version: 5.5.25a-log Source distribution

此解决方法已在以下环境中进行测试:服务器版本:5.5.25a-log 源分发

回答by fustaki

I had the same problem and it has been caused by an update of mysql drivers when mysql server was running. I fixed it just restarting both mysql and apache2:

我遇到了同样的问题,它是由 mysql 服务器运行时更​​新 mysql 驱动程序引起的。我修复它只是重新启动 mysql 和 apache2:

sudo service mysql stop

sudo service mysql start

sudo service apache2 stop

sudo service apache2 start

须藤服务 mysql 停止

须藤服务 mysql 启动

须藤服务 apache2 停止

须藤服务 apache2 启动