Centos7 Mysql Forgot Login Password

强制修改密码

注意:此方式存在安全隐患

// 停止mysql服务
[root~] > systemctl stop mysqld

//设置mysql跳过权限安全检查(存在安全隐患)
[root~] > systemctl set-environment MYSQLD_OPTS="--skip-grant-tables"

//启动mysql服务
[root~] > systemctl start mysqld

//登录mysql,此时不需要输入密码,直接登录
[root~] > mysql -u root

//修改root密码,host为'%'表示全部
mysql > UPDATE mysql.user SET authentication_string = PASSWORD('MyNewPassword') WHERE User = 'root' AND Host = '%';

Query OK, 0 rows affected, 1 warning (0.00 sec)
Rows matched: 1  Changed: 0  Warnings: 1

//刷新权限
mysql > flush privileges;
Query OK, 0 rows affected (0.00 sec)

//退出
mysql> quit;
Bye

//设置mysql权限安全检查(这步很重要)
[root~] > systemctl set-environment MYSQLD_OPTS=""

//重启mysql
[root~] > systemctl restart mysqld

//登录需要密码,修改成功
[root~] > mysql -u root -p
ENTER PASSWORD: 

//退出
mysql > quit;

你可能感兴趣的:(mysql)