1 版本
ubuntu 18.04
mysql 5.7
2 步骤
1 查看默认用户与密码
sudo vim /etc/mysql/debian.cnf
得到这个:
在这里[client]
host = localhost
user = debian-sys-maint
password = X2gvwzo5nFbUJfTn
socket = /var/run/mysqld/mysqld.sock
[mysql_upgrade]
host = localhost
user = debian-sys-maint
password = X2gvwzo5nFbUJfTn
socket = /var/run/mysqld/mysqld.sock插入代码片
2 使用user与password登录mysql
mysql -u debian-sys-maint -pX2gvwzo5nFbUJfTn
3 修改root密码 = 123456
use mysql;
update user set authentication_string=PASSWORD("123456") where User="root";
4 刷新权限
flush privileges;
update user set plugin='mysql_native_password';
flush privileges;
5 退出重新登录
exit;
mysql -u root -p123456
过程中显示如下:
mysql> use mysql;
Database changed
mysql>
mysql> update user set authentication_string=PASSWORD("123456") where User="root";
Query OK, 1 row affected, 1 warning (0.01 sec)
Rows matched: 1 Changed: 1 Warnings: 1
mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)
mysql>
mysql> update user set plugin='mysql_native_password';
Query OK, 1 row affected (0.00 sec)
Rows matched: 4 Changed: 1 Warnings: 0
mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)
mysql> exit
Bye
3 mysql8.0
8.0以上版本指令有所改变
mysql> update user set authentication_string="" where user="root";
Query OK, 0 rows affected (0.00 sec)
Rows matched: 1 Changed: 0 Warnings: 0
mysql> alter user 'root'@'localhost' identified by "123456";
Query OK, 0 rows affected (0.04 sec)
或 ALTER USER 'root'@'localhost' IDENTIFIED WITH caching_sha2_password BY '123456';
mysql> flush privileges;
Query OK, 0 rows affected (0.01 sec)
mysql> exit
Bye