微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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

安装5.7版本的MysqL出现如下错误

root@iZufkfljcZ:~# MysqL -uroot -p
 
Enter password:
 
ERROR 2002 (HY000): Can't connect to local MysqL server through socket '/tmp/MysqL.sock' (2)
 

 出现的问题是:找不到MysqL.sock,如果你可以运行

find / -name MysqL.sock 

这条命令,并且能查到结果的话,只需将查到的结果做一个软连接到/tmp目录下即可解决(网上都是这么解决的)。

 

但是,我执行了这条语句之后,并没有任何反应,没有找到MysqL.sock文件

在这之前,需要明白MysqL.sock这个文件有什么用?

连接localhost通常通过一个Unix域套接文件进行,一般是/tmp/MysqL.sock。如果套接文件删除了,本地客户就不能连接。这可能发生在你的系统运行一个cron任务删除了/tmp下的临时文件

如果你因为丢失套接文件而不能连接,你可以简单地通过重启服务器重新创建得到它。因为服务器在启动时重新创建它。

如果和我一样,重启服务器还是没有任何变化,你可以先执行下面的语句:

# MysqL -uroot -h 127.0.0.1 -p 

不出意外,这句话应该是可以执行的,你现在不能用套接字建立连接因为它不见了,所以可以建立一个TCP/IP连接
如果套接文件一个cron任务删除,问题将重复出现,除非你修改cron任务或使用一个或使用一个不同的套接文件,我的解决办法是重新指定一个不同的套接字,或者说,我现在没有MysqL.sock文件,所以我要想办法生成一个

首先,更改my.cnf文件,我的服务器中的目录为/etc/my.cnf,如果没有的话可以用find去查找,

 

 

 

接下来就是保存退出,然后确保这个目录存在,并且将这个目录的权限修改一下

# chmod 777 /var/lib/MysqL

准备步骤做好,然后就是MysqLMysqLd服务重启

# service MysqL restart
 
# service MysqLd restart

 

 

我在重启MysqLd服务的时候,重启失败了,显示如下:

 
root@iZufkfljcZ:~# service MysqLd start
 
Job for MysqLd.service Failed because the control process exited with error code. See "systemctl status MysqLd.service" and "journalc
 
tl -xe" for details.

这个时候,提示可以输入systemctl status MysqLd.service去查看具体的失败原因,于是:

****************************************************************************************

root@iZufkfljcZ:~# systemctl status MysqLd.service
● MysqLd.service - LSB: start and stop MysqL
  Loaded: loaded (/etc/init.d/MysqLd; bad; vendor preset: enabled)
  Active: Failed(Result: exit-code) since 三 2017-12-20 10:38:30 CST; 45s ago
Docs: man:systemd-sysv-generator(8)
 Process: 2154 ExecStart=/etc/init.d/MysqLd start (code=exited,status=1/FAILURE)


12月 20 10:38:29 iZufkfljcZ systemd[1]: Starting LSB: start and stop MysqL...
12月 20 10:38:29 iZufkfljcZ MysqLd[2154]: Starting MysqL
12月 20 10:38:29 iZufkfljcZ MysqLd_safe[2689]:Logging to '/var/log/MysqL/error.log'.
12月 20 10:38:29 iZufkfljcZ MysqLd_safe[2693]:Directory '/var/run/MysqLd' for UNIX socket file don't exists.
12月 20 10:38:30 iZufkfljcZ MysqLd[2154]: . * The server quit without updating PID file (/var/run/MysqLd/MysqLd.pid).
12月 20 10:38:30 iZufkfljcZ systemd[1]: MysqLd.service: Control process exited,code=exited status=1
12月 20 10:38:30 iZufkfljcZ systemd[1]: Failed to start LSB: start and stop MysqL.
12月 20 10:38:30 iZufkfljcZ systemd[1]: MysqLd.service: Unit entered Failed state.
12月 20 10:38:30 iZufkfljcZ systemd[1]: MysqLd.service: Failed with result 'exit-code'.

 

****************************************************************************************

根据提示可知,/var/run/MysqLd目录不存在,也就是说MysqLd服务重启需要这个目录,那就建一个吧:

root@iZufkfljcZ:~# mkdir /var/run/MysqLd
 
root@iZufkfljcZ:~# chmod 777 /var/run/MysqLd/
 
root@iZufkfljcZ:~# service MysqLd start
 
root@iZufkfljcZ:~#

 建完目录后,重新运行MysqLd服务,发现重启成功了,那么我们再来看看为什么刚才要建这个目录呢?打开这个目录看看:

root@iZufkfljcZ# ls /var/run/MysqLd
 
MysqLd.pid MysqLd.sock MysqLd.sock.lock

发现了一个熟悉的东西,MysqLd.sock,但是这个是不是我们需要的东西呢?不管他,先用这个sock文件登下MysqL看看行不行:

 root@iZufkfljcZ:/var/run/MysqLd# MysqL -uroot -p -S /var/run/MysqLd/MysqLd.sock
 
Enter password:
 
Welcome to the MysqL monitor. Commands end with ; or \g.
 
Your MysqL connection id is 4
 
Server version: 5.7.20-0ubuntu0.16.04.1 (Ubuntu)
 
 
copyright (c) 2000,2017,Oracle and/or its affiliates. All rights reserved.
 
 
Oracle is a registered Trademark of Oracle Corporation and/or its
 
affiliates. Other names may be Trademarks of their respective
 
owners.
 
 
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
 
MysqL>

  

这么一运行,发现好像可以了,那接下来好办了,我们把之前改的配置改回来就行了,之前的目录应该是/tmp/MysqL.sock,我们可以建立一个软连接连上去就可以了,

 
root@iZufkfljcZ:~# ln -s /var/run/MysqLd/MysqLd.sock /tmp/MysqL.sock
 
root@iZufkfljcZ:~# ls /tmp/
 
MysqL.sock

 这样,tmp目录下就有了my.cnf配置文件中需要的MysqL.sock文件了,然后把my.cnf改回就行了,

 

 

 


到此为止,我们的MysqL应该已经完全修复了,那么我们再测试一下吧:

 OK,搞定收工,用了快一天时间在解决这个问题上,感觉收获还是挺多的,Linux的学习任重而道远啊!

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐