欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

MySQL错误 ERROR 2002 (HY000): Can't connect to local MySQL server through socket 的解决办法

程序员文章站 2022-08-04 15:37:59
原因分析: 1、没有 /var/lib/mysql/mysql.sock 的目录权限 添加权限 chown -r mysql:mysql /var/lib/mysql/ 2、没有/var/lib/m...

原因分析:

1、没有 /var/lib/mysql/mysql.sock 的目录权限

添加权限 chown -r mysql:mysql /var/lib/mysql/

2、没有/var/lib/mysql/mysql.sock文件

通过 find / -name mysql.sock

在 /tmp//mysql.sock

用 ln -s 命令建立连接

或者修改 /etc/my.cnf配置文件,下边有,然后重启

#查看mysql实例的状态
[root@szdb mysqldata]# netstat -ntlp  | grep 3306
tcp        0      0 :::3306                     :::*                        listen      13001/mysqld
#查看my.cnf关于socket的配置
[root@szdb mysqldata]# more /etc/my.cnf |grep sock
socket = /tmp/mysql.sock
#由上可知my.cnf中定义的为/tmp目录下,而错误提示为/data/mysqldata/目录下
#也就是说mysqld已经声称了正确的sock文件,但客户端连接还是从初始目录去找sock文件
#下面查看后台日志,有个error,是关于满查询日志的,是由于目录不存在而产生的错误,与当前故障无关
[root@szdb mysqldata]# more szdb.err
             ............
2014-10-11 13:17:21 13001 [note] innodb: 5.6.12 started; log sequence number 1625997
/app/soft/mysql/bin/mysqld: file '/log/mysql_logs/slowquery.log' not found (errcode: 2 - no such file or directory)
2014-10-11 13:17:21 13001 [error] could not use /log/mysql_logs/slowquery.log for logging (error 2). turning logging off for the who
le duration of the mysql server process. to turn it on again: fix the cause, shutdown the mysql server and restart it.
2014-10-11 13:17:21 13001 [note] server hostname (bind-address): '*'; port: 3306
2014-10-11 13:17:21 13001 [note] ipv6 is available.
2014-10-11 13:17:21 13001 [note]   - '::' resolves to '::';
2014-10-11 13:17:21 13001 [note] server socket created on ip: '::'.
2014-10-11 13:17:21 13001 [note] event scheduler: loaded 0 events
2014-10-11 13:17:21 13001 [note] /app/soft/mysql/bin/mysqld: ready for connections.
version: '5.6.12-log'  socket: '/tmp/mysql.sock'  port: 3306  source distribution
3、解决故障
通过配置my.cnf mysql选项socket文件位置解决
复制代码 代码如下:
#先停止mysql服务器
[root@szdb mysqldata]# service mysqld stop
shutting down mysql.[  ok  ]
#修改my.cnf,如下
[root@szdb mysqldata]# vi /etc/my.cnf
[mysql]
no-auto-rehash
socket = /tmp/mysql.sock  #添加该行
[mysql.server]
user=mysql#添加
basedir=/usr/local/mysql#添加
#if there is not currently a section called [client], add one at the bottom of the file and copy the socket= line under the [mysqld] section such as:
[client]#添加
socket=/var/lib/mysql/mysql.sock#添加
#重启mysql服务器
[root@szdb mysqldata]# service mysqld start
starting mysql..[  ok  ]
#再次连接正常
[root@szdb mysqldata]# mysql -uroot -p
enter password: 
mysql> show variables like 'version';
+---------------+------------+
| variable_name | value      |
+---------------+------------+
| version       | 5.6.12-log |
+---------------+------------+
为socket文件建立链接方式
复制代码 代码如下:
[root@szdb mysqldata]# ln -s /tmp/mysql.sock /data/mysqldata/mysql.sock 
ln: creating symbolic link `/data/mysqldata/mysql.sock' to `/tmp/mysql.sock': file exists
[root@szdb mysqldata]# rm mysql.sock    #上面提示文件存在,所以删除之前的mysql.sock文件
[root@szdb mysqldata]# ln -s /tmp/mysql.sock /data/mysqldata/mysql.sock
[root@szdb mysqldata]# ls -hltr mysql.sock
lrwxrwxrwx 1 root root 15 oct 11 14:00 mysql.sock -> /tmp/mysql.sock
[root@szdb mysqldata]# mysql -uroot -p
enter password: 
mysql> show variables like 'socket';
+---------------+-----------------+
| variable_name | value           |
+---------------+-----------------+
| socket        | /tmp/mysql.sock |
+---------------+-----------------+