Can't connect to local MySQL server through socket ‘/var/run/mysqld/mysqld.sock‘ (2)解决思路

首先说明一下mysql.sock文件的作用:

连接mysql有两种方式,第一种是TCP/IP,第二种就是直接使用unix domain socket,它比TCP/IP块。

mysql.sock是在mysql-server和client在同一服务器上时,发起本地连接时可用,而无需定义-h参数指定具体的IP。mysql.sock是随每次mysql server启动时生成,通常配置参数是将mysql.sock生成在/tmp/目录下。即在/etc/my.cnf文件中指定socket=/tmp/mysql.sock

因此如果出现的是ERROR 2002 (HY000): Can‘t connect to local MySQL server through socket ‘/tmp/mysql.sock‘

(1) 首先确认配置参数是否正确,尝试重启mysql。

(2) 如果不成功,可以用

find / -name mysql.sock

定位文件在哪里。是不是socket被误放在其他地方,参考链接

(3) 会不会是权限设置问题,

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

参考链接

(4) 或者...其他情况

但是注意看,这里我出现的问题是 ERROR 2002 (HY000): Can‘t connect to local MySQL server through socket ‘/var/run/mysqld/mysqld.sock‘ (2)

排查:

(1)find / -name mysql.sock 看到mysql.sock在/tmp/路径下,/etc/my.cnf 的socket路径也是/tmp/mysql.sock;

(2) 用

ps -ef |grep mysql

看,似乎也没问题。

root       806     1  0  3月05 ?      00:00:00 /bin/sh /usr/local/mysql/bin/mysqld_safe --datadir=/usr/local/mysql/var --pid-file=/usr/local/mysql/var/linpeisong-PC.pid

mysql     1233   806  0  3月05 ?      00:00:27 /usr/local/mysql/bin/mysqld --basedir=/usr/local/mysql --datadir=/usr/local/mysql/var --plugin-dir=/usr/local/mysql/lib/plugin --user=mysql --log-error=/usr/local/mysql/var/linpeisong-PC.err --pid-file=/usr/local/mysql/var/linpeisong-PC.pid --socket=/tmp/mysql.sock --port=3306

但是如果我们手动指定--socket参数,即

mysql --socket=/tmp/mysql.sock -uroot -ppassword

是可以正常连接的。

(3) 最后参考这篇文章找到根源:参考链接

因为mysql会按顺序读取配置文件/etc/my.conf,/etc/mysql/my.conf,/usr/etc/my.con,~/.my.conf,后面的会将前面的覆盖。而此时在我的/etc/mysql/my.conf文件中,配置的socket参数就是

socket = /var/run/mysqld/mysqld.sock

有一个做法可以创建链接文件解决,如

ln -s /var/lib/mysql/mysql.sock /tmp/mysql.sock

但是用这种方法后面可能会遇到php又找不到mysql.sock的问题。

因此直接将/etc/mysql/my.cnf重新命名

mv /etc/mysql/my.cnf /etc/mysql/my.cnf_backup

就可以利用默认/etc/my.cnf正常连接。

Can't connect to local MySQL server through socket ‘/var/run/mysqld/mysqld.sock‘ (2)解决思路

时间: 2024-10-25 18:07:37

Can't connect to local MySQL server through socket ‘/var/run/mysqld/mysqld.sock‘ (2)解决思路的相关文章

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

ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock' (2) 方案1. 1.#ps -A|grep mysql 显示类似: 1829 ?        00:00:00 mysqld_safe   1876 ?        00:00:31 mysqld 2.#kill -9 1829 3.#kill -9 1876 4.#/etc/init.d/

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

今天mysql不知道犯啥病,root登录的时候一直报这个错误. 在国内网站找了找,没什么合适的 答案,在stackoverflow和ubuntu的一个社区上找到了答案,搞定了,但是没太看懂,记录一下,以后再翻. That said, Here is what I did/ found out: 1. You don't have to change the tmp directory in the my.cnf file. Even adding a second directory, chan

mysql start error ERROR 2002 (HY000): Can’t connect to local MySQL server through socket ‘/var/run/mysqld/mysql.sock’ (2)

When I type mysql, it shows “ERROR 2002 (HY000): Can’t connect to local MySQL server through socket ‘/var/run/mysqld/mysql.sock’ (2)”. Then I try to start mysql service: $ service mysql start But then I get error “ No directory, logging in with HOME=

Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock'解决

安装上mysql后,报 Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock',试了网上的方法都没有用,于是打开/etc/my.cnf查看. 看到这么一段: # The MySQL server 26 [mysqld] 27 port            = 3306 28 socket          = /tmp/mysql.sock 29 skip-external-locking

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

First make a backup of your /var/lib/mysql/ directory just to be safe. sudo mkdir /home/<your username>/mysql/ cd /var/lib/mysql/ sudo cp * /home/<your username>/mysql/ -R Next purge MySQL (this will remove php5-mysql and phpmyadmin as well as

Can&#39;t connect to local MySQL server through socket &#39;/var/run/mysqld/mysqld.sock&#39; (2)

今天在ubuntu上连接mysql数据库时,遇到一个小问题 Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock' (2) 第一次使用时还可以使用的,突然就不好了,不知道是不是没吃药, 先查看数据库状态: [email protected]:~$ sudo /etc/init.d/mysql status (因为我使用的是普通用户,所以要sudo一下) 1.如果出现: [email prot

Can&#39;t connect to local MySQL server through socket &#39;/var/run/mysqld/mysqld.sock&#39;的解决

Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock'的解决 原因是目录 的访问权限问题,ubuntu下用下面的命令(其他发行版用管理员权限,把sudo去掉) sudo chown -R mysql:mysql /var/run/mysqld 然后启动服务器 sudo /etc/init.d/mysql start Can't connect to local MySQL server th

mysql之ERROR 2002 (HY000): Can&#39;t connect to local MySQL server through socket &#39;/var/run/mysqld/mysqld.sock&#39;解决方法

LAMPP安装完成之后,mysql -u root -p连不上,报这个错误: ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock' 在mysql的配置文件里,/opt/lampp/etc/my.cnf, 发现了这样的配置: [java] <SPAN style="FONT-SIZE: 24px"># The following

OperationalError: (2002, “Can&#39;t connect to local MySQL server through socket &#39;/var/run/mysqld/mysqld.sock&#39; (2)”)

OperationalError: (2002, “Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock' (2)”) python MySQLdb这个模块是独立的,有自己独立的socket,解决的方法就是找出mysql配置里的socket的地址(比如socket=/tmp/mysql.sock),然后做个软连接 ln -s /tmp/mysql.sock  /var/run/mysqld/