ERROR 2002 (HY000): Can‘t connect to local MySQL server through socket /var/lib/mysql/mysql.sock报错解决

概要

新建库时发现数据库一个不小心导致登不上去,查看报错说是缺少这个sock文件,去网上搜报错解决方法时发现,文章里写的/usr/local/mysql目录我这里没有,我这个数据库版本是5.7的,通过find也找不到mysql.server文件,解决此问题的核心办法是要重新启动mysql服务,让其自动在/tmp文件中生成这个sock文件,所以我用下边的操作方法成功启动了mysql服务。

报错信息

[root@localhost centos7]# mysql -uroot -p
Enter password: 
ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/var/lib/mysql/mysql.sock' (2)

不同之处

[root@localhost local]# find / -name mysql.server
find: ‘/run/user/1000/gvfs’: 权限不够
[root@localhost local]# find / -name mysql
find: ‘/run/user/1000/gvfs’: 权限不够
/etc/logrotate.d/mysql
/etc/selinux/targeted/active/modules/100/mysql
/etc/selinux/targeted/tmp/modules/100/mysql
/var/lib/mysql
/var/lib/mysql/mysql
/usr/bin/mysql
/usr/lib64/mysql
/usr/share/mysql
 

查看mysql状态

[root@localhost bin]# service mysqld status
Redirecting to /bin/systemctl status mysqld.service
● mysqld.service - MySQL Server
   Loaded: loaded (/usr/lib/systemd/system/mysqld.service; enabled; vendor preset: disabled)
   Active: inactive (dead) since 一 2024-01-22 09:19:23 CST; 15min ago
     Docs: man:mysqld(8)
           http://dev.mysql.com/doc/refman/en/using-systemd.html
  Process: 1979 ExecStart=/usr/sbin/mysqld --daemonize --pid-file=/var/run/mysqld/mysqld.pid $MYSQLD_OPTS (code=exited, status=0/SUCCESS)
  Process: 1260 ExecStartPre=/usr/bin/mysqld_pre_systemd (code=exited, status=0/SUCCESS)
 Main PID: 1987 (code=exited, status=0/SUCCESS)

1月 22 09:14:35 localhost.localdomain systemd[1]: Starting MySQL Server...
1月 22 09:14:40 localhost.localdomain systemd[1]: Started MySQL Server.
1月 22 09:19:21 localhost.localdomain systemd[1]: Stopping MySQL Server...
1月 22 09:19:23 localhost.localdomain systemd[1]: Stopped MySQL Server.
 

启动mysql服务

[root@localhost bin]# service mysqld start
Redirecting to /bin/systemctl start mysqld.service

[root@localhost bin]# service mysqld status
Redirecting to /bin/systemctl status mysqld.service
● mysqld.service - MySQL Server
   Loaded: loaded (/usr/lib/systemd/system/mysqld.service; enabled; vendor preset: disabled)
   Active: active (running) since 一 2024-01-22 09:34:59 CST; 4s ago
     Docs: man:mysqld(8)
           http://dev.mysql.com/doc/refman/en/using-systemd.html
  Process: 5336 ExecStart=/usr/sbin/mysqld --daemonize --pid-file=/var/run/mysqld/mysqld.pid $MYSQLD_OPTS (code=exited, status=0/SUCCESS)
  Process: 5308 ExecStartPre=/usr/bin/mysqld_pre_systemd (code=exited, status=0/SUCCESS)
 Main PID: 5339 (mysqld)
    Tasks: 27
   CGroup: /system.slice/mysqld.service
           └─5339 /usr/sbin/mysqld --daemonize --pid-file=/var/run/mysqld/mys...

1月 22 09:34:57 localhost.localdomain systemd[1]: Starting MySQL Server...
1月 22 09:34:59 localhost.localdomain systemd[1]: Started MySQL Server.
 

成功登录mysql

[root@localhost bin]# mysql -uroot -p
Enter password: 
Welcome to the MySQL monitor.  Commands end with ; or g.
Your MySQL connection id is 2
Server version: 5.7.43 MySQL Community Server (GPL)

Copyright (c) 2000, 2023, Oracle and/or its affiliates.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

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

mysql> 
 

参考:

MySQL 报错:ERROR 2002 (HY000): Can't connect to local MySQL server through socket-腾讯云开发者社区-腾讯云 (tencent.com)