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

 1、今天安装了一个mysql 5.7 的二进制安装方式,在连接mysql数据库的时候报了如下错误:

[root@iccsdb01 mysql]# mysql 

ERROR 2002 (HY000): Can’t connect to local MySQL server through socket ‘/tmp/mysql.sock’ (2)

2、根据提示应该是sock文件不在,通过查看进程配置,发现如下

[root@iccsdb01 bin]# ps -ef | grep mysql

root      9196  3125  0 11:18 pts/2    00:00:00 tail -f mysqld.log

root     11404     1  0 14:13 pts/1    00:00:00 /bin/sh /home/mysql/sysinfo/mysql/bin/mysqld_safe –datadir=/my_data/sysinfo –pid-file=/my_data/sysinfo/iccsdb01.pid

mysql    11616 11404  0 14:13 pts/1    00:00:00 /home/mysql/sysinfo/mysql/bin/mysqld –basedir=/home/mysql/sysinfo/mysql –datadir=/my_data/sysinfo –plugin-dir=/home/mysql/sysinfo/mysql/lib/plugin –user=mysql –log-error=/var/log/mysqld.log –pid-file=/my_data/sysinfo/iccsdb01.pid –socket=/var/lib/mysql/mysql.sock

由上可知mysql 默认的mysql.sock 是在/var/lib/mysql/mysql.sock,

但Linux系统总是去/tmp/mysql.sock查找,所以会报错

解决方法一

1、创建连接文件到指定目录上

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

2、测试连接成功登陆

[root@iccsdb01 /]# mysql -u root -p

Enter password: 

Welcome to the MySQL monitor.  Commands end with ; or \g.

Your MySQL connection id is 5

Server version: 5.7.18 MySQL Community Server (GPL)

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> 

解决方法二

  修改“/etc/my.cnf”配置文件,在配置文件中添加“[client]”选项和“[mysql]”选项,并使用这两个选项下的“socket”参数值,与“[mysqld]”选项下的“socket”参数值,指向的socket文件路径完全一致。如下: 

1 2 3 4 5 6 7 8 9 10 11 12 [mysqld] datadir = / my_data / sysinfo socket = / var / lib / mysql / mysql.sock ...省略n行...    [client] default - character - set = utf8 socket = / var / lib / mysql / mysql.sock    [mysql] default - character - set = utf8 socket = / var / lib / mysql / mysql.sock

修改完后,重启mysqld服务,即可解决此问题。

本文转自 yuri_cto 51CTO博客,原文链接:http://blog.51cto.com/laobaiv1/1919251,如需转载请自行联系原作者

点赞