ERROR 2002 (HY000) Can‘t connect to local MySQL server through socket ‘varrunmysqldmysqld.sock‘ (2)

1. 首先检查 MySQL 服务是否正在运行

systemctl status mysqld

2. MySQL 服务启动后很快就进入了失败状态

● mysqld.service - MySQL Server
   Loaded: loaded (/etc/systemd/system/mysqld.service; enabled; vendor preset: disabled)
   Active: failed (Result: exit-code) since Sun 2024-11-03 18:51:25 CST; 1min 33s ago
  Process: 3071 ExecStart=/usr/bin/mysqld --defaults-file=/etc/mysql/my.cnf --datadir=/var/lib/mysql --socket=/var/run/mysqld/mysqld.sock (code=exited, status=1/FAILURE)
 Main PID: 3071 (code=exited, status=1/FAILURE)

3. 尝试初始化MySQL

3.1 停止MySQL服务

systemctl stop mysqld

3.2 删除数据目录中的内容(谨慎操作!确保已备份数据)

sudo rm -rf /var/lib/mysql/*

3.3 重新初始化 MySQL

sudo mysqld --initialize --user=mysql

3.4 启动 MySQL 服务

systemctl start mysqld

4. 尝试登录MySQL

[root@hcss-ecs-8875 shuaijie]# mysql -u root -p
Enter password: 

4.1显示使用提供的密码尝试以root用户从localhost连接 MySQL 时被拒绝

ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: YES)

5. 重置密码

5.1 停止 MySQL 服务

systemctl stop mysqld

5.2 以特殊模式启动 MySQL,跳过权限表检查

mysqld --skip-grant-tables --user=mysql &

5.3 另开一个终端窗口,连接到 MySQL 而不需要密码

mysql -u root

5.4 更新密码

USE mysql;
UPDATE user SET authentication_string=PASSWORD('新密码') WHERE User='root';
FLUSH PRIVILEGES;

5.5 退出 MySQL 并停止特殊模式启动的 MySQL 进程

systemctl start mysqld

6. 正常启动 MySQL 服务

[root@hcss-ecs-8875 shuaijie]# mysql -u root -p
Enter password: 
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 4
Server version: 5.7.44

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> 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值