Can't connect to local MySQL server through socket '/var/lib/mysql/mysql.sock分析

访客 阅读:161 2021-09-06 11:58:14 评论:0
本文章主要介绍了Can't connect to local MySQL server through socket '/var/lib/mysql/mysql.sock,具有不错的的参考价值,希望对您有所帮助,如解说有误或未考虑完全的地方,请您留言指出,谢谢!
[root@localhost template_c]# mysql -uroot -p 
Enter password: 
ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/var/lib/mysql/mysql.sock' (111) 
[root@localhost template_c]# mysql -uroot -p 
Enter password: 
ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/var/lib/mysql/mysql.sock' (111) 
[root@localhost template_c]# mysql -uroot -p 
Enter password: 
ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/var/lib/mysql/mysql.sock' (111) 
[root@localhost template_c]# ps -ef |grep mariadb 
root     11089  6586  0 18:24 pts/2    00:00:00 grep --color=auto mariadb 
[root@localhost template_c]# systemctl restart mariadb.service 
Job for mariadb.service failed because the control process exited with error code. See "systemctl status mariadb.service" and "journalctl -xe" for details.

出现以上错误,不难看出,mariadb没有启动成功,让我去查看systemctl status mariadb.service 这个mariadb.service的服务状态。根据提示就进去了:

[root@localhost systemd]# systemctl status mariadb.service 
● mariadb.service - MariaDB database server 
   Loaded: loaded (/usr/lib/systemd/system/mariadb.service; enabled; vendor preset: disabled) 
   Active: failed (Result: exit-code) since 一 2017-06-26 18:29:27 CST; 8s ago 
  Process: 11525 ExecStartPost=/usr/libexec/mariadb-wait-ready $MAINPID (code=exited, status=1/FAILURE) 
  Process: 11524 ExecStart=/usr/bin/mysqld_safe --basedir=/usr (code=exited, status=0/SUCCESS) 
  Process: 11496 ExecStartPre=/usr/libexec/mariadb-prepare-db-dir %n (code=exited, status=0/SUCCESS) 
 Main PID: 11524 (code=exited, status=0/SUCCESS) 
 
626 18:29:26 localhost.localdomain systemd[1]: Starting MariaDB database server... 
626 18:29:26 localhost.localdomain mysqld_safe[11524]: 170626 18:29:26 mysqld_safe Logging to '/var/log/mariadb/mariadb.log'. 
626 18:29:26 localhost.localdomain mysqld_safe[11524]: 170626 18:29:26 mysqld_safe Starting mysqld daemon with databases from /.../mysql 
626 18:29:27 localhost.localdomain systemd[1]: mariadb.service: control process exited, code=exited status=1 
626 18:29:27 localhost.localdomain systemd[1]: Failed to start MariaDB database server. 
626 18:29:27 localhost.localdomain systemd[1]: Unit mariadb.service entered failed state. 
626 18:29:27 localhost.localdomain systemd[1]: mariadb.service failed. 
Hint: Some lines were ellipsized, use -l to show in full. 

服务启动失败,叫我去看mariadb的日志:

cat /var/log/mariadb/mariadb.log 

结果如下:

InnoDB: Cannot continue operation. 
170626 18:25:21 mysqld_safe mysqld from pid file /var/run/mariadb/mariadb.pid ended 
170626 18:29:26 mysqld_safe Starting mysqld daemon with databases from /var/lib/mysql 
170626 18:29:26 [Note] /usr/libexec/mysqld (mysqld 5.5.52-MariaDB) starting as process 11801 ... 
170626 18:29:26 [Warning] Can't create test file /var/lib/mysql/localhost.lower-test 
170626 18:29:26 InnoDB: The InnoDB memory heap is disabled 
170626 18:29:26 InnoDB: Mutexes and rw_locks use GCC atomic builtins 
170626 18:29:26 InnoDB: Compressed tables use zlib 1.2.7 
170626 18:29:26 InnoDB: Using Linux native AIO 
170626 18:29:26 InnoDB: Initializing buffer pool, size = 128.0M 
170626 18:29:26 InnoDB: Completed initialization of buffer pool 
170626 18:29:26  InnoDB: Operating system error number 13 in a file operation. 
InnoDB: The error means mysqld does not have the access rights to 
InnoDB: the directory. 
InnoDB: File name ./ibdata1 
InnoDB: File operation call: 'open'. 
InnoDB: Cannot continue operation. 
170626 18:29:26 mysqld_safe mysqld from pid file /var/run/mariadb/mariadb.pid ended 
170626 18:41:48 mysqld_safe Starting mysqld daemon with databases from /var/lib/mysql 
170626 18:41:48 [Note] /usr/libexec/mysqld (mysqld 5.5.52-MariaDB) starting as process 12394 ... 
170626 18:41:48 [Warning] Can't create test file /var/lib/mysql/localhost.lower-test 
170626 18:41:48 InnoDB: The InnoDB memory heap is disabled 
170626 18:41:48 InnoDB: Mutexes and rw_locks use GCC atomic builtins 
170626 18:41:48 InnoDB: Compressed tables use zlib 1.2.7 
170626 18:41:48 InnoDB: Using Linux native AIO 
170626 18:41:48 InnoDB: Initializing buffer pool, size = 128.0M 
170626 18:41:48 InnoDB: Completed initialization of buffer pool 
170626 18:41:48  InnoDB: Operating system error number 13 in a file operation. 
InnoDB: The error means mysqld does not have the access rights to 

说明mariadb的权限不够,不能写数据库,之前好像一不小心全改成www-data了,进去一看还真是,修改一下权限:
查看所有的用户和组,找出mariad所属的组和对应的用户。

 cat /etc/group 
 cat /etc/passwd 

确定为用户和组分别为:mysql:mysql ,修改一下权限:

 chown -R mysql:mysql /var/lib/mysql 
 chmod -R 755 /var/lib/mysql 

启动mariadb:systemctl start mariadb.service ,启动成功!登陆一下看看能不能登陆进去。

这里写图片描述

已经搞好了,问题就是这么一步一步解决的。

报这个错还有一个原因,那就是mysql密码错了,解决方法如下:

IT虾米网


标签:mysql
声明

1.本站遵循行业规范,任何转载的稿件都会明确标注作者和来源;2.本站的原创文章,请转载时务必注明文章作者和来源,不尊重原创的行为我们将追究责任;3.作者投稿可能会经我们编辑修改或补充。

发表评论
搜索
KIKK导航

KIKK导航

排行榜
关注我们

一个IT知识分享的公众号