旧版的 mysql
我们可以通过update MySQL.user set authentication_string=password('xxxxxxxxxxx') where user='root';
来重置密码
新版 mysql
,通过以上方式重置密码,并不会生效
正确做法是,停止 mysqld
服务
通过 mysqld_safe --skip-grant-tables
启动服务
然后 新的终端里键入 mysql
可以直接进入
这时候 我们可以用ALTER user 'root'@'localhost' IDENTIFIED BY 'xxxxxxxxxxx';
或者SET PASSWORD FOR root = PASSWORD("xxxxxxxxxxx");
来重置密码
不过你会发现,直接这么输入会报错ERROR 1290 (HY000): The MariaDB server is running with the --skip-grant-tables option so it cannot execute this statement
最最关键的是, 需要先输入 flush privileges;
再输入上面修改密码的指令,就能成功了.
具体操作流程如下
[root@centos7 wwwroot]# mysql
Welcome to the MariaDB monitor. Commands end with ; or \g.
Your MariaDB connection id is 14
Server version: 10.3.16-MariaDB-log Source distribution
Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
MariaDB [(none)]> ALTER user 'root'@'localhost' IDENTIFIED BY 'xxxxxxxxxxx';
ERROR 1290 (HY000): The MariaDB server is running with the --skip-grant-tables option so it cannot execute this statement
MariaDB [(none)]> flush privileges;
Query OK, 0 rows affected (0.002 sec)
MariaDB [(none)]> ALTER user 'root'@'localhost' IDENTIFIED BY 'xxxxxxxxxxx';
Query OK, 0 rows affected (0.000 sec)