--skip-grant-tables 无效是因为 mysql 实例已在运行,该参数仅对全新启动的 mysqld 进程生效;需先停止服务、清理残留进程,再用完整路径配合 --skip-networking 启动。

启动 MySQL 时加 --skip-grant-tables 为什么没反应?
常见现象是执行 mysqld --skip-grant-tables 后进程直接退出,或客户端仍提示 Access denied。根本原因不是命令写错了,而是 MySQL 实例已在运行——--skip-grant-tables 必须作用于**全新启动的 mysqld 进程**,不能附加到已有服务上。
实操要点:
- 先停掉当前服务:
sudo systemctl stop mysql(Ubuntu/Debian)或sudo systemctl stop mysqld(CentOS/RHEL) - 确认无残留进程:
ps aux | grep mysqld,如有则kill -9掉所有非 root 的mysqld进程 - 用完整路径启动(避免环境变量干扰):
/usr/sbin/mysqld --skip-grant-tables --skip-networking &;--skip-networking很关键,防止远程未授权访问 - 此时
mysql -u root可直连,无需密码
MySQL 5.7+ 中 UPDATE user SET authentication_string=... 报错:Field 'password' doesn't exist
5.7 开始,mysql.user 表不再有 password 字段,改用 authentication_string 存储哈希值。但直接赋值明文字符串会失败,因为验证逻辑已绑定插件(如 caching_sha2_password 或 mysql_native_password)。
正确做法分两步:
- 先切回兼容性更高的认证插件:
UPDATE mysql.user SET plugin='mysql_native_password' WHERE User='root'; - 再重置密码:
UPDATE mysql.user SET authentication_string=PASSWORD('你的新密码') WHERE User='root'; - 刷新权限:
FLUSH PRIVILEGES;
注意:PASSWORD() 函数在 8.0 已废弃,若误用 8.0 环境,需改用 ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY '新密码';
重置后能登录但所有数据库都显示 Access denied for user 'root'@'localhost'
这不是密码问题,而是权限表未生效或用户 Host 匹配失败。MySQL 的权限判定基于 User + Host 组合,'root'@'localhost' 和 'root'@'127.0.0.1' 是两个不同账户。
检查与修复步骤:
- 进库后执行:
SELECT User, Host FROM mysql.user WHERE User='root';,确认是否存在'root'@'localhost' - 如果只有
'root'@'%',但本地连接走的是 socket(默认解析为localhost),就会匹配失败 - 补全缺失记录:
INSERT INTO mysql.user (Host,User,authentication_string,plugin) VALUES ('localhost','root',PASSWORD('新密码'),'mysql_native_password');,再FLUSH PRIVILEGES; - 更稳妥的方式是统一用
CREATE USER 'root'@'localhost' IDENTIFIED BY '新密码'; GRANT ALL PRIVILEGES ON *.* TO 'root'@'localhost' WITH GRANT OPTION;
重启服务后密码又失效,或 --skip-grant-tables 被自动覆盖
根本原因是配置文件(如 /etc/mysql/mysql.conf.d/mysqld.cnf 或 /etc/my.cnf)里存在 skip-grant-tables 残留项,或 systemd 服务单元文件中硬编码了该参数。
必须手动清理:
- 搜索配置文件:
grep -r "skip-grant-tables" /etc/mysql/ /etc/my.cnf - 检查 systemd 服务定义:
systemctl cat mysql或systemctl cat mysqld,看[Service]下是否有ExecStart=...带该参数 - 修改后务必重启服务:
sudo systemctl daemon-reload && sudo systemctl start mysql - 验证是否生效:
mysqladmin -u root -p ping,输入新密码应返回mysqld is alive
跳过权限验证只是临时通道,任何残留都会让系统处于高危状态;删不干净的配置比忘了密码更危险。











