登陆时遇到错误:ERROR 2002 (HY000): Can’t connect to local MySQL server through socket ‘/tmp/mysql.sock’ (2)
解决:
$ /etc/init.d/mysql.server restart
[ ok ] Restarting mysql.server (via systemctl): mysql.server.service.
如果重启还是没有解决,还有一个原因:
由于我们修改了mysql配置“/etc/my.cnf”引起的。比如我们修改了配置文件中“[mysql]”选项下的“socket”参数,而未指定“[client]”、“[mysql]”选项的“socket”参数,导致mysql使用默认的socket文件位置去寻找socket文件,从而导致未找到socket文件而引发此错误。
解决方法: 修改“/etc/my.cnf”配置文件,在配置文件中添加“[client]”选项和“[mysql]”选项,并使用这两个选项下的“socket”参数值,与“[mysqld]”选项下的“socket”参数值,指向的socket文件路径完全一致。如下:
[mysqld]
datadir=/storage/db/mysql
socket=/storage/db/mysql/mysql.sock
...省略n行(爱E族)...
[client]
default-character-set=utf8
socket=/storage/db/mysql/mysql.sock
[mysql]
default-character-set=utf8
socket=/storage/db/mysql/mysql.sock
具体请参考:http://aiezu.com/article/mysql_cant_connect_through_socket.html
理论知识:
MYSQL的连接方式有两种:
- 通过本地socket进行连接 【mysql服务器和客户端在同一台机器上】
[mysql]> show variables like 'socket%'; --查看mysql socket文件在哪里
+---------------+-----------------+
| Variable_name | Value |
+---------------+-----------------+
| socket | /tmp/mysql.sock |
+---------------+-----------------+
$ mysql -uroot -p123456 -S/tmp/mysql.sock 【本地连接,-S/tmp/mysql.sock可以省略】
[mysql]> \s
--------------
mysql Ver 8.0.13 for linux-glibc2.12 on x86_64 (MySQL Community Server - GPL)
Connection id: 20
Current database:
Current user: root@localhost
SSL: Not in use -------本地传输,没有网络传输,而ssl是为了防止窃听,因此没有使用ssl
Current pager: stdout
Using outfile: ''
Using delimiter: ;
Server version: 8.0.13 MySQL Community Server - GPL
Protocol version: 10
Connection: Localhost via UNIX socket --当前的连接方式是socket
Server characterset: utf8mb4
Db characterset: utf8mb4
Client characterset: utf8mb4
Conn. characterset: utf8mb4
UNIX socket: /tmp/mysql.sock
Uptime: 2 hours 42 min 28 sec
可以在配置文件中$ sudo gedit /etc/my.cnf添加:
[client]
socket=/tmp/mysql.sock
- 通过TCP/IP协议连接【mysql服务器和客户端不在同一台机器上,也可以在一台机器上】
$ mysql -h127.0.0.1 -uroot -p
> show variables like '%ssl%';
+--------------------+-----------------+
| Variable_name | Value |
+--------------------+-----------------+
| have_openssl | YES |
| have_ssl | YES | ---- 说明已经启用了ssl
+ --------------------------------------+
[mysql]> status
--------------
mysql Ver 8.0.13 for linux-glibc2.12 on x86_64 (MySQL Community Server - GPL)
Connection id: 17
Current database:
Current user: root@localhost
SSL: Cipher in use is DHE-RSA-AES128-GCM-SHA256 --启用了ssl
Current pager: stdout
Using outfile: ''
Using delimiter: ;
Server version: 8.0.13 MySQL Community Server - GPL
Protocol version: 10
Connection: 127.0.0.1 via TCP/IP --当前的连接方式是TCP/IP
Server characterset: utf8mb4
Db characterset: utf8mb4
Client characterset: utf8mb4
Conn. characterset: utf8mb4
TCP port: 3306
Uptime: 2 hours 41 min 15 sec
Threads: 2 Questions: 211 Slow queries: 0 Opens: 267 Flush tables: 2 Open tables: 243 Queries per second avg: 0.021
--------------
启动mysql 报错:
ERROR 2002 (HY000): Can’t connect to local MySQL server through socket ‘/var/lib/mysql/mysql.sock’ (2)
1.先查看mysql启动状态
#/etc/rc.d/init.d/mysqld status
2.mysql.sock位置查找与验证?
# find / -name mysql.sock
/Data/mydata/mysql.sock
验证目录是否正确
#mysql -u root -p -S /Data/mydata/mysql.sock
备注:-S参数socket file文件验证
3.验证mysql.sock文件权限和用户是否正确?
如果是权限问题,则先改变权限
#chown -R mysql:mysql /Data/mydata/mysql.sock
[root@localhost ~]# /etc/init.d/mysqld start
启动 MySQL: [ OK]
4.永久解决
修改/etc/my.cnf
添加以下内容到/etc/my.conf:
[client]
socket = /Data/mydata/mysql.sock
[mysqld]
socket = /Data/mydata/mysql.sock