MySQL service provides different connection methods. We generally prefer TCP/IP way where connection is established over Network Socket. There is alternative way named Socket which is connected over local system. During connection we may get error like Can’t connect to local MySQL server through socket ‘/var/run/mysqld/mysqld.sock’ . In this tutorial we will look to solve this problem.

MySQL服务提供了不同的连接方法。 通常,我们更喜欢通过网络套接字建立连接的TCP / IP方式。 还有另一种名为Socket的方法,它通过本地系统连接。 在连接期间,我们可能会收到类似无法通过套接字'/var/run/mysqld/mysqld.sock'连接到本地MySQL服务器的错误 在本教程中,我们将寻求解决此问题。

解决方案1 ​​–启动服务 (Solution 1 – Start The Service)

Socket is created by the MySQL service and if the service is not the started the socket will not created. So starting the service is the first step to solve this problem. We will use systemctl  command.

套接字是由MySQL服务创建的,如果服务不是启动的,则不会创建套接字。 因此,启动服务是解决此问题的第一步。 我们将使用systemctl命令。

$ sudo systemctl start mysql.service

AND check service status

并检查服务状态

$ sudo systemctl status mysql.service
Solution 1 - Start The Service
Solution 1 – Start The Service
解决方案1 ​​–启动服务

解决方案2 –检查套接字路径(Solution 2 – Check The Socket Path)

As we try to connect to the socket the path of the Socket should be correctly specified. By the default the socket is /var/run/mysqld/mysqld.sock and this may be changed. We can check current path configuration of the socket from config file which is /etc/mysql/debian.cnf .

当我们尝试连接到套接字时,应正确指定套接字的路径。 默认情况下,套接字为/var/run/mysqld/mysqld.sock并且可以更改。 我们可以从配置文件/etc/mysql/debian.cnf检查套接字的当前路径配置。

$ sudo cat /etc/mysql/debian.cnf

Solution 2 - Check The Socket Path
Solution 2 – Check The Socket Path
解决方案2 –检查套接字路径

解决方案3 –检查数据目录权限(Solution 3 – Check Data Directory Permissions)

One of the another reason to get related error is MySQL server do not started. This may be cause with wrong directory permissions. We should check MySQL service data path permissions with the following command and make them writeable by the MySQL service.

出现相关错误的另一个原因之一是MySQL服务器未启动。 这可能是由于目录权限错误引起的。 我们应该使用以下命令检查MySQL服务数据路径权限,并使它们可被MySQL服务写入。

$ sudo chmod -R 755 /var/lib/mysql/
LEARN MORE  Linux Netstat Command With Examples
了解更多带有示例Linux Netstat命令

翻译自: https://www.poftut.com/error-2002-hy000-cant-connect-local-mysql-server-socket-var-run-mysqld-mysqld-sock-error-solution/

Logo

魔乐社区(Modelers.cn) 是一个中立、公益的人工智能社区,提供人工智能工具、模型、数据的托管、展示与应用协同服务,为人工智能开发及爱好者搭建开放的学习交流平台。社区通过理事会方式运作,由全产业链共同建设、共同运营、共同享有,推动国产AI生态繁荣发展。

更多推荐