ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/var/lib/mysql/mysql.sock' (2)
=====================================================
步骤: 以下可用。
(1)关闭mysql:
service mysqld stop
(2)查看mysql.sock的位置
(base) [root@VM-0-2-centos ~]# locate mysql.sock
/var/lib/mysql/mysql.sock
(3)查看my.cnf的位置
(base) [root@VM-0-2-centos ~]# locate my.cnf
/etc/.my.cnf.swd
/etc/.my.cnf.swk
/etc/.my.cnf.swn
/etc/my.cnf
/etc/my.cnf.d
(4)查看 /etc/my.cnf 的内容
(base) [root@VM-0-2-centos ~]# more /etc/my.cnf
#http:#//dev.mysql.com/doc/refman/5.6/en/server-configuration-defaults.html
#For advice on how to change setting please see
[mysqld]
#
# Remove leading # and set to the amount of RAM for the most important data
# cache in MySQL. Start at 70% of total RAM for dedicated server, else 10%.
# innodb_buffer_pool_size = 128M
#
# Remove leading # to turn on a very important data integrity option: logging
# changes to the binary log between backups.
# log_bin
#
# Remove leading # to set options mainly useful for reporting servers.
# The server defaults are faster for transactions and fast SELECTs.
# Adjust sizes as needed, experiment to find the optimal values.
# join_buffer_size = 128M
# sort_buffer_size = 2M
# read_rnd_buffer_size = 2M
datadir=/var/lib/mysql
socket=/var/lib/mysql/mysql.sock
# Disabling symbolic-links is recommended to prevent assorted security risks
symbolic-links=0
skip-grant-tables
# Recommended in standard MySQL setup
sql_mode=NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES
[mysqld_safe]
log-error=/var/log/mysqld.log
pid-file=/var/run/mysqld/mysqld.pid
(base) [root@VM-0-2-centos ~]#
(5)修改 /etc/my.cnf
vi /etc/my.cnf
(6)添加以下内容:
[client]
default-character-set=utf8
socket=/var/lib/mysql/mysql.sock
[mysql]
default-character-set=utf8
socket=/var/lib/mysql/mysql.sock
(7)修改完后,重启mysqld服务
service mysqld start
=====================================================
参考:
原因:
这是由于我们连接数据库使用的主机名参数为“localhost”,或者未使用主机名参数、服务器默认使用“localhost”做为主机名 。 使用主机名参数为“localhost”连接mysql服务端时,mysql客户端会认为是连接本机,所以会尝试以socket文件方式进行连接(socket文件连接方式,比“ip:端口”方式效率更高),这时根据配置文件“/etc/my.cnf”的路径,未找到相应的socket文件,就会引发此错误。
修复故障前准备:
1、看mysql服务是否在运行:
由于“socket”文件是由mysql服务运行时创建的,如果提示“ERROR 2002 (HY000): Can't connect to local MySQL server through socket '***' (2)”,找不到“socket”文件,我们首先要确认的是mysql服务是否正在运行。
# 1、 端口是否打开
[[email protected] ~] # lsof -i:3306
COMMAND PID USER FD TYPE DEVICE SIZE /OFF NODE NAME
mysqld 12207 mysql 14u IPv4 52350 0t0 TCP *:mysql (LISTEN)
# 2、mysqld服务是否正在运行
[[email protected] ~] # service mysqld status
mysqld (pid 4717) is running...
# 3、如果mariadb,同样方法查服务是否正在运行:
[[email protected] ~] # service mariadb status
Redirecting to /bin/systemctl status mariadb.service
● mariadb.service - MariaDB database server
Loaded: loaded ( /usr/lib/systemd/system/mariadb .service; disabled; vendor preset: disabled)
Active: active (running) since 四 2016-11-03 13:47:37 CST; 23min ago
|
2、确定“socket”文件正确位置:
确定mysql服务正常运行后,产生此错误的原因只剩下“socket”文件路径不正确了,我们可以使用“find”命令或者“lsof”命令来确定socket文件的正确路径:
[[email protected] ~] # lsof -c mysqld|grep sock$
mysqld 4717 mysql 12u unix 0xffff88010a655b80 0t0 77474827 /storage/db/mysql/mysql .sock
[[email protected] ~] # find / -name '*.sock'
/storage/db/mysql/mysql .sock
|
解决方法:
解决方案一:
修改“/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
|
修改完后,重启mysqld服务,即可解决此问题。
解决方案二:
使用“ln -s /storage/db/mysql/mysql.sock /var/lib/mysql/mysql.sock”命令,将正确的socket文件位置,软链接到提示错误的socket文件路径位置,即可解决此问题:
[[email protected] ~] # ls /var/lib/mysql/mysql.sock
ls : cannot access /var/lib/mysql/mysql .sock: No such file or directory
[[email protected] ~] # ln -s /storage/db/mysql/mysql.sock /var/lib/mysql/mysql.sock
[[email protected] ~] # ls /var/lib/mysql/mysql.sock
/var/lib/mysql/mysql .sock
|
REF
http://aiezu.com/article/45.html
标签:socket,lib,sock,mysqld,mysql,var From: https://www.cnblogs.com/emanlee/p/18021473