mysql(mariadb)无法打开,报错:找不到mysqld.sock且Can‘t connect to MySQL server on 127.0.0.1(111)

5 篇文章 2 订阅
4 篇文章 0 订阅


网络上太多该问题来自转载且格式混乱,查阅他人经验并没有解决我的问题,这里分享下自己解决后的经验,以免后人少走弯路。
标题因为长度限制,很多关键信息无法打出,这里详细描述下我重启mysql(是 mariadb的,以下简称mysql)所遇到的问题:
1: MariaDB: ERROR 2003 (HY000): Can’t connect to MySQL server on ‘127.0.0.1’ (111 “Connection refused”)
2: Job for mariadb.service failed because the control process exited with error code. See “systemctl status mariadb.service” and “journalctl -xe” for details.
3: Can’t connect to local MySQL server through socket ‘/var/run/mysqld/mysqld.sock’ (2)
直接上结论: 你是不是修改过配置文件,修改了/etc/my.cnf ,如果是,那么请继续向下看,如果没有,那请继续查阅其他资料。

1:问题描述

一开始mysql是正常的,修改过mysql的配置文件后,打算通过重启mysql后报错,内容如下:

systemctl restart mariadb

报错:Job for mariadb.service failed because the control process exited with error code. See “systemctl status mariadb.service” and “journalctl -xe” for details.
顺着报错给的指令,

systemctl status mariadb.service

查看mysql的运行状态

● mariadb.service - MariaDB database server
   Loaded: loaded (/usr/lib/systemd/system/mariadb.service; enabled; vendor preset: disabled)
   Active: failed (Result: exit-code) since 日 2022-05-29 14:42:18 CST; 13s ago
  Process: 3531 ExecStartPost=/usr/libexec/mariadb-wait-ready $MAINPID (code=exited, status=1/FAILURE)
  Process: 3530 ExecStart=/usr/bin/mysqld_safe --basedir=/usr (code=exited, status=0/SUCCESS)
  Process: 3496 ExecStartPre=/usr/libexec/mariadb-prepare-db-dir %n (code=exited, status=0/SUCCESS)
 Main PID: 3530 (code=exited, status=0/SUCCESS)

529 14:42:12 localhost systemd[1]: Starting MariaDB database server...
529 14:42:12 localhost mariadb-prepare-db-dir[3496]: Database MariaDB is probably initialized in /var/lib/mysql already, nothing is done.
529 14:42:12 localhost mysqld_safe[3530]: 220529 14:42:12 mysqld_safe Logging to '/var/log/mariadb/mariadb.log'.
529 14:42:12 localhost mysqld_safe[3530]: 220529 14:42:12 mysqld_safe Starting mysqld daemon with databases from /var/lib/mysql
529 14:42:18 localhost systemd[1]: mariadb.service: control process exited, code=exited status=1
529 14:42:18 localhost systemd[1]: Failed to start MariaDB database server.
529 14:42:18 localhost systemd[1]: Unit mariadb.service entered failed state.
529 14:42:18 localhost systemd[1]: mariadb.service failed.

这里我们需要注意,Failed to start MariaDB database server表明mysql根本没有启动,通过ps aux | grep mysql也发现没有mysql的进程,所以网上相关的kill这个进程然后重启就是于事无补的。
接着尝试通过常用的socket方式启动mysql

mysql -uroot -p12345678

报错如下:
Can’t connect to local MySQL server through socket ‘/var/lib/mysql/mysql.sock’ (2)
顾名思义,mysql服务器无法通过这个mysqld.sock连接,那么我们会习惯去看看这个文件的状态,结果发现这个目录下没有这个文件
原因是这个文件是需要mysql正常启动后才会出现的,此时因为重启失败了,自然找不到这个文件,因此重启机器自然也解决不了。
接着,通过socket无法连接mysql,那么我换种方式

mysql -u root -h 127.0.0.1 -p 3306

输入密码后出现如下报错

**MariaDB: ERROR 2003 (HY000): Can't connect to MySQL server on '127.0.0.1' (111 "Connection refused")**

2:问题分析与解决

这时,我们回到最初的报错上,我们发现启动的日志会被记录在log中,路径为:/var/log/mariadb/mariadb.log
查询日志后发现如下重要信息

220529 14:42:12 mysqld_safe Starting mysqld daemon with databases from /var/lib/mysql
220529 14:42:12 InnoDB: The InnoDB memory heap is disabled
220529 14:42:12 InnoDB: Mutexes and rw_locks use GCC atomic builtins
220529 14:42:12 InnoDB: Compressed tables use zlib 1.2.7
220529 14:42:12 InnoDB: Using Linux native AIO
220529 14:42:12 [Note] /usr/libexec/mysqld (mysqld 5.5.68-MariaDB) starting as process 3828 ...
220529 14:42:12 InnoDB: Initializing buffer pool, size = 128.0M
220529 14:42:12 InnoDB: Completed initialization of buffer pool
220529 14:42:12 InnoDB: highest supported file format is Barracuda.
220529 14:42:12  InnoDB: Waiting for the background threads to start
220529 14:42:13 Percona XtraDB (http://www.percona.com) 5.5.61-MariaDB-38.13 started; log sequence number 18423221
220529 14:42:13 [Note] Plugin 'FEEDBACK' is disabled.
220529 14:42:13 [ERROR] /usr/libexec/mysqld: unknown variable 'default-character-set=utf8'
220529 14:42:13 [ERROR] Aborting

220529 14:42:13  InnoDB: Starting shutdown...
220529 14:42:17  InnoDB: Shutdown completed; log sequence number 18423221
220529 14:42:17 [Note] /usr/libexec/mysqld: Shutdown complete

220529 14:42:17 mysqld_safe mysqld from pid file /var/run/mariadb/mariadb.pid ended

显而易见,有问题的地方出现在ERROR处,unknown variable ‘default-character-set=utf8’
这里我才恍然大悟,自己刚刚修改过配置文件,/etc/my.cnf,所以将配置复原后,重启成功,下面的操作一气呵成

[root@localhost ~]# systemctl restart mariadb  #这里没有输出是正常的
[root@localhost ~]#   
[root@localhost ~]# mysql -uroot -p12345678
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 2
Server version: 5.5.68-MariaDB MariaDB Server

Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

MariaDB [(none)]>

顺利通过socket方式进入mysql,同时,mysqld.sock自然也回来了,毕竟启动成功了

[root@localhost mysql]# ll
总用量 53288
-rw-rw----. 1 mysql mysql    16384 529 14:42 aria_log.00000001
-rw-rw----. 1 mysql mysql       52 529 14:42 aria_log_control
-rw-rw----. 1 mysql mysql 44040192 529 14:52 ibdata1
-rw-rw----. 1 mysql mysql  5242880 529 14:52 ib_logfile0
-rw-rw----. 1 mysql mysql  5242880 529 14:52 ib_logfile1
drwx------. 2 mysql mysql     4096 727 2021 mysql
srwxrwxrwx. 1 mysql mysql        0 529 14:44 mysql.sock    《-----------在这里
drwx------. 2 mysql mysql     8192 529 14:52 neutron
drwx------. 2 mysql mysql     4096 727 2021 performance_schema
[root@localhost mysql]# 

3:反思与总结

1:重启能够解决80%的问题,这80%中不包括你修改了配置
2:原来的服务正常,突然某个时间不好使了,要注意是不是期间你误删了重要的文件,或者修改了什么配置。
3:日志是最重要的参考。
4:不到万不得已,不要删库重建。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值