Can 't connect to local MySQL server through socket '/tmp/mysql.sock '(2)解决
[p=26, 2, left][size=18px]把mysql的套接字文件不小心删除了,然后无论怎么卸载重装,都出现了这个问题:[/size][/p][p=26, 2, left][/p][list=1][*][float=left]
[/float]
[float=left]root@iZufkfljcZ:~[i]# mysql -uroot -p[/i]
[/float]
[*][float=left]
[/float]
[float=left]Enter password:
[/float]
[*][float=left]
[/float]
[float=left]ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/tmp/mysql.sock' (2)
[/float]
[*][float=left]
[/float]
[float=left]
[/float]
[/list][font=-apple-system, "] 网上找了好久,搜到的全部是重复的文章而且也没讲明为什么出现这个问题,找了一天才找到一篇讲解mysql.sock文件作用的文章,然后分析一下为什么出现这个问题,现在讲之总结一下,以备后人查阅。[/font]
[p=26, 2, left][size=18px] 我出现的问题是:找不到mysql.sock,如果你可以运行[/size][/p]find / -name mysql.sock[font=-apple-system, "]这条命令,并且能查到结果的话,只需将查到的结果做一个软连接到/tmp目录下即可解决(网上都是这么解决的)。[/font]
[p=26, 2, left][size=18px]但是,我执行了这条语句之后,并没有任何反应,没有找到mysql.sock文件。[/size][/p][p=26, 2, left][size=18px]在这之前,需要明白mysql.sock这个文件有什么用?[/size][/p][p=26, 2, left][size=18px][font=Verdana, Arial, Helvetica, sans-serif]连接localhost通常通过一个Unix域套接字文件进行,一般是/tmp/mysql.sock。如果套接字文件被删除了,本地客户就不能连接。这可能发生在你的系统运行一个cron任务删除了/tmp下的临时文件。[/font][/size][/p][p=26, 2, left][size=18px][font=Verdana, Arial, Helvetica, sans-serif]如果你因为丢失套接字文件而不能连接,你可以简单地通过重启服务器重新创建得到它。因为服务器在启动时重新创建它。
[/font][/size][/p][p=26, 2, left][size=18px][font=Verdana, Arial, Helvetica, sans-serif]如果和我一样,重启服务器还是没有任何变化,你可以先执行下面的语句:[/font][/size][/p][p=26, 2, left][size=18px][font=Verdana, Arial, Helvetica, sans-serif]
[/font][/size][/p]# mysql -uroot -h 127.0.0.1 -p [font=-apple-system, "]不出意外,这句话应该是可以执行的,[/font][font=Verdana, Arial, Helvetica, sans-serif]你现在不能用套接字建立连接因为它不见了,所以可以建立一个TCP/IP连接[/font]
[font=Verdana, Arial, Helvetica, sans-serif]如果套接字文件被一个cron任务删除,问题将重复出现,除非你修改cron任务或使用一个或使用一个不同的套接字文件,我的解决办法是重新指定一个不同的套接字,或者说,我现在没有mysql.sock文件,所以我要想办法生成一个。[/font]
[p=26, 2, left][size=18px][font=Verdana, Arial, Helvetica, sans-serif]首先,更改my.cnf文件,我的服务器中的目录为/etc/my.cnf,如果没有的话可以用find去查找,
[/font][/size][/p]
[img]https://img-blog.csdn.net/20171220111132201?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvaGpmMTYxMTA1/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/SouthEast[/img]
[p=26, 2, left][size=18px]接下来就是保存退出,然后确保这个目录存在,并且将这个目录的权限修改一下[/size][/p][p=26, 2, left]
[/p][i]# chmod 777 /var/lib/mysql[/i][font=-apple-system, "]准备步骤做好,然后就是mysql和mysqld服务重启[/font]
[p=26, 2, left]
[/p][list=1][*][float=left]
[/float]
[float=left]# service mysql restart
[/float]
[*][float=left]
[/float]
[float=left]# service mysqld restart
[/float]
[/list][font=-apple-system, "]我在重启mysqld服务的时候,重启失败了,显示如下:[/font][list=1][*][float=left]
[/float]
[float=left]root@iZufkfljcZ:~[i]# service mysqld start[/i]
[/float]
[*][float=left]
[/float]
[float=left]Job for mysqld.service failed because the control process exited with error code. See "systemctl status mysqld.service" and "journalc
[/float]
[*][float=left]
[/float]
[float=left]tl -xe" for details.
[/float]
[/list][p=26, 2, left]这个时候,提示可以输入systemctl status mysqld.service去查看具体的失败原因,于是:[/p][p=26, 2, left]****************************************************************************************[/p][p=26, 2, left][size=14px]
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'.[/size]
[/p][p=26, 2, left]
[/p][p=26, 2, left]****************************************************************************************[/p][p=26, 2, left]根据提示可知,/var/run/mysqld目录不存在,也就是说mysqld服务重启需要这个目录,那就建一个吧:[/p][p=26, 2, left]
[/p][list=1][*][float=left]
[/float]
[float=left]root@iZufkfljcZ:~[i]# mkdir /var/run/mysqld[/i]
[/float]
[*][float=left]
[/float]
[float=left]root@iZufkfljcZ:~[i]# chmod 777 /var/run/mysqld/[/i]
[/float]
[*][float=left]
[/float]
[float=left]root@iZufkfljcZ:~[i]# service mysqld start[/i]
[/float]
[*][float=left]
[/float]
[float=left]root@iZufkfljcZ:~[i]# [/i]
[/float]
[/list][font=-apple-system, "]建完目录后,重新运行mysqld服务,发现重启成功了,那么我们再来看看为什么刚才要建这个目录呢?打开这个目录看看:[/font]
[p=26, 2, left]
[/p][list=1][*][float=left]
[/float]
[float=left]root@iZufkfljcZ[i]# ls /var/run/mysqld[/i]
[/float]
[*][float=left]
[/float]
[float=left]mysqld.pid mysqld.sock mysqld.sock.lock
[/float]
[/list][font=-apple-system, "]发现了一个熟悉的东西,mysqld.sock,但是这个是不是我们需要的东西呢?不管他,先用这个sock文件登下mysql看看行不行:[/font]
[p=26, 2, left]
[/p][list=1][*][float=left]
[/float]
[float=left]root@iZufkfljcZ:/var/run/mysqld# mysql -uroot -p -S /var/run/mysqld/mysqld.sock
[/float]
[*][float=left]
[/float]
[float=left]Enter password:
[/float]
[*][float=left]
[/float]
[float=left]Welcome to the MySQL monitor. Commands end with ; or \g.
[/float]
[*][float=left]
[/float]
[float=left]Your MySQL connection id is 4
[/float]
[*][float=left]
[/float]
[float=left]Server version: 5.7.20-0ubuntu0.16.04.1 (Ubuntu)
[/float]
[*][float=left]
[/float]
[float=left]
[/float]
[*][float=left]
[/float]
[float=left]Copyright (c) 2000, 2017, Oracle and/or its affiliates. All rights reserved.
[/float]
[*][float=left]
[/float]
[float=left]
[/float]
[*][float=left]
[/float]
[float=left]Oracle is a registered trademark of Oracle Corporation and/or its
[/float]
[*][float=left]
[/float]
[float=left]affiliates. Other names may be trademarks of their respective
[/float]
[*][float=left]
[/float]
[float=left]owners.
[/float]
[*][float=left]
[/float]
[float=left]
[/float]
[*][float=left]
[/float]
[float=left]Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
[/float]
[*][float=left]
[/float]
[float=left]
[/float]
[*][float=left]
[/float]
[float=left]mysql>
[/float]
[/list]
[font=-apple-system, "]这么一运行,发现好像可以了,那接下来好办了,我们把之前改的配置改回来就行了,之前的目录应该是/tmp/mysql.sock,我们可以建立一个软连接连上去就可以了,[/font]
[p=26, 2, left]
[/p][list=1][*][float=left]
[/float]
[float=left]root@iZufkfljcZ:~[i]# ln -s /var/run/mysqld/mysqld.sock /tmp/mysql.sock[/i]
[/float]
[*][float=left]
[/float]
[float=left]root@iZufkfljcZ:~[i]# ls /tmp/[/i]
[/float]
[*][float=left]
[/float]
[float=left]mysql.sock
[/float]
[/list][font=-apple-system, "]这样,tmp目录下就有了my.cnf配置文件中需要的mysql.sock文件了,然后把my.cnf改回就行了,[/font]
[p=26, 2, left][size=18px][img]https://img-blog.csdn.net/20171220113111870?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvaGpmMTYxMTA1/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/SouthEast[/img]
到此为止,我们的mysql应该已经完全修复了,那么我们再测试一下吧:[/size][/p][p=26, 2, left]
[/p][list=1][*][float=left]
[/float]
[float=left]root@iZufkfljcZ:/var/run/mysqld# mysql -uroot -p
[/float]
[*][float=left]
[/float]
[float=left]Enter password:
[/float]
[*][float=left]
[/float]
[float=left]Welcome to the MySQL monitor. Commands end with ; or \g.
[/float]
[*][float=left]
[/float]
[float=left]Your MySQL connection id is 5
[/float]
[*][float=left]
[/float]
[float=left]Server version: 5.7.20-0ubuntu0.16.04.1 (Ubuntu)
[/float]
[*][float=left]
[/float]
[float=left]
[/float]
[*][float=left]
[/float]
[float=left]Copyright (c) 2000, 2017, Oracle and/or its affiliates. All rights reserved.
[/float]
[*][float=left]
[/float]
[float=left]
[/float]
[*][float=left]
[/float]
[float=left]Oracle is a registered trademark of Oracle Corporation and/or its
[/float]
[*][float=left]
[/float]
[float=left]affiliates. Other names may be trademarks of their respective
[/float]
[*][float=left]
[/float]
[float=left]owners.
[/float]
[*][float=left]
[/float]
[float=left]
[/float]
[*][float=left]
[/float]
[float=left]Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
[/float]
[*][float=left]
[/float]
[float=left]
[/float]
[*][float=left]
[/float]
[float=left]mysql>
[/float]
[/list][font=-apple-system, "] OK。[/font]
页:
[1]