登录 主页

运行 mysql 却报错说:ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock' (2)

2023-11-29 05:33PM

我今天安装了 DVWA 之后,我的 mysql 就无法启动了,并且一直报错说

$ sudo mysql -u root -p
ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock' (2)

在命令行里面启动 mysql,报错说

$ sudo systemctl restart mysql
Job for mysql.service failed because the control process exited with error code.
See "systemctl status mysql.service" and "journalctl -xeu mysql.service" for details.

 解决方法:

注意:这个方法会删除数据库的所有东西,如果想要使用这个方法,请注意备份

1. 先查看 mysql 都有什么

$ sudo dpkg -l | grep mysql
ii  mysql-client-8.0                           8.0.35-0ubuntu0.22.04.1                 amd64        MySQL database client binaries
ii  mysql-client-core-8.0                      8.0.35-0ubuntu0.22.04.1                 amd64        MySQL database core client binaries
ii  mysql-common                               5.8+1.0.8                               all          MySQL database common files, e.g. /etc/mysql/my.cnf
ii  mysql-server                               8.0.35-0ubuntu0.22.04.1                 all          MySQL database server (metapackage depending on the latest version)
ii  mysql-server-8.0                           8.0.35-0ubuntu0.22.04.1                 amd64        MySQL database server binaries and system database setup
ii  mysql-server-core-8.0                      8.0.35-0ubuntu0.22.04.1                 amd64        MySQL database server binaries
ii  php8.1-mysql                               8.1.2-1ubuntu2.14                       amd64        MySQL module for PHP

2. 重新安装 mysql

1)删除 mysql

$ sudo apt purge mysql-server mysql-client mysql-common mysql-server-core-* mysql-client-core-* 

2)运行玩上面的命令,会发现 

ii  php8.1-mysql                               8.1.2-1ubuntu2.14                       amd64        MySQL module for PHP

变成了 

rc  php8.1-mysql                               8.1.2-1ubuntu2.14                        amd64        MySQL module for PHP  

  我这里是把  rc  php8.1-mysql 也删除了,如果不想要删除,可以不使用下面这个命令

$ sudo dpkg -P php8.1-mysql

3)安装 mysql

$ sudo apt install mysql-server

  

安装了 mysql 之后发现问题还是没有解决,依然存在

$ sudo mysql -u root -p
ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock' (2)

4)这主要是你的配置文件出了问题,也需要删除

$ sudo rm /var/lib/mysql -r
$ sudo rm -r /etc/mysql/my.cnf
$ sudo rm -r /usr/bin/mysql
$ sudo rm -r /etc/mysql

我这里把这四个文件夹都删除了 

5)然后把 mysql 再次删除,重新安装

$ sudo apt purge mysql-server mysql-client mysql-common mysql-server-core-* mysql-client-core-* 

安装

$  sudo apt install mysql-server
Reading package lists... Done
Building dependency tree... Done
Reading state information... Done
The following additional packages will be installed:
  mysql-client-8.0 mysql-client-core-8.0 mysql-common mysql-server-8.0 mysql-server-core-8.0
Suggested packages:
  mailx tinyca
The following NEW packages will be installed:
  mysql-client-8.0 mysql-client-core-8.0 mysql-common mysql-server mysql-server-8.0 mysql-server-core-8.0
0 upgraded, 6 newly installed, 0 to remove and 47 not upgraded.
Need to get 0 B/21.8 MB of archives.
After this operation, 185 MB of additional disk space will be used.
Do you want to continue? [Y/n]
Preconfiguring packages ...
Selecting previously unselected package mysql-common.
(Reading database ... 259562 files and directories currently installed.)
Preparing to unpack .../mysql-common_5.8+1.0.8_all.deb ...
Unpacking mysql-common (5.8+1.0.8) ...
Selecting previously unselected package mysql-client-core-8.0.
Preparing to unpack .../mysql-client-core-8.0_8.0.35-0ubuntu0.22.04.1_amd64.deb ...
Unpacking mysql-client-core-8.0 (8.0.35-0ubuntu0.22.04.1) ...
Selecting previously unselected package mysql-client-8.0.
Preparing to unpack .../mysql-client-8.0_8.0.35-0ubuntu0.22.04.1_amd64.deb ...
Unpacking mysql-client-8.0 (8.0.35-0ubuntu0.22.04.1) ...
Selecting previously unselected package mysql-server-core-8.0.
Preparing to unpack .../mysql-server-core-8.0_8.0.35-0ubuntu0.22.04.1_amd64.deb ...
Unpacking mysql-server-core-8.0 (8.0.35-0ubuntu0.22.04.1) ...
Setting up mysql-common (5.8+1.0.8) ...
update-alternatives: using /etc/mysql/my.cnf.fallback to provide /etc/mysql/my.cnf (my.cnf) in auto mode
Selecting previously unselected package mysql-server-8.0.
(Reading database ... 259760 files and directories currently installed.)
Preparing to unpack .../mysql-server-8.0_8.0.35-0ubuntu0.22.04.1_amd64.deb ...
Unpacking mysql-server-8.0 (8.0.35-0ubuntu0.22.04.1) ...
Selecting previously unselected package mysql-server.
Preparing to unpack .../mysql-server_8.0.35-0ubuntu0.22.04.1_all.deb ...
Unpacking mysql-server (8.0.35-0ubuntu0.22.04.1) ...
Setting up mysql-client-core-8.0 (8.0.35-0ubuntu0.22.04.1) ...
Setting up mysql-server-core-8.0 (8.0.35-0ubuntu0.22.04.1) ...
Setting up mysql-client-8.0 (8.0.35-0ubuntu0.22.04.1) ...
Setting up mysql-server-8.0 (8.0.35-0ubuntu0.22.04.1) ...
update-alternatives: using /etc/mysql/mysql.cnf to provide /etc/mysql/my.cnf (my.cnf) in auto mode
Renaming removed key_buffer and myisam-recover options (if present)
Cannot stat file /proc/5067/fd/1023: Permission denied
mysqld will log errors to /var/log/mysql/error.log
mysqld is running as pid 75646 #这里可以看到 mysql 已经启动了
Created symlink /etc/systemd/system/multi-user.target.wants/mysql.service → /lib/systemd/system/mysql.service.
Setting up mysql-server (8.0.35-0ubuntu0.22.04.1) ...
Processing triggers for man-db (2.10.2-1) ...

6)进入 mysql,就发现已经可以了

$ sudo mysql -u root
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 9
Server version: 8.0.35-0ubuntu0.22.04.1 (Ubuntu)

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>

返回>>

登录

请登录后再发表评论。

评论列表:

目前还没有人发表评论