实验环境
操作系统:lsb_release -a
No LSB modules are available.
Distributor ID:Ubuntu
Description:Ubuntu 20.04.1 LTS
Release:20.04
Codename:focal
mysql数据库版本:mysql基本信息
具体步骤
1. 停止mysql (sudo service mysqld stop 或者 kill -TERM mysqld)
2. 使用参数 --skip-grant-tables启动mysql ( /usr/bin/mysqld_safe --skip-grant-tables &)
3. 需要看root用户使用的身份验证插件(这个步骤可以跳过)select host,user,plugin from mysql.user;
MariaDB [mysql]> select host,user,plugin from mysql.user ;
+-----------+-------------+-----------------------+
| Host | User | plugin |
+-----------+-------------+-----------------------+
| localhost | mariadb.sys | mysql_native_password |
| localhost | root | mysql_native_password |
| % | root | mysql_native_password |
+-----------+-------------+-----------------------+
3 rows in set (0.002 sec)
4. 使用修改root密码的命令,如果你用的是高版本的mysql,使用update去设置mysql.user 的表则会遇到Column 'Password' is not updatable,应该使用下面修改root密码的命令MariaDB [mysql]> set password for 'root'@'localhost'=password('123456');
Query OK, 0 rows affected (0.002 sec)
5. 如果遇到下面提示ERROR 1290 (HY000),则使用下面命令,先将数据库设置为可写,然后再设置为只读ERROR 1290 (HY000): The MariaDB server is running with the --skip-grant-tables option so it cannot execute this statement
set global read_only=1;
FLUSH PRIVILEGES;
set password for 'root'@'localhost'=password('123456');
FLUSH PRIVILEGES;
set global read_only=0;
FLUSH PRIVILEGES;
引用
[MySQL 连接方式](https://blog.csdn.net/qinglingls/article/details/96864122)
[mysql8 参考手册-- ALTER USER语句](http://www.1024sky.cn/blog/article/1214)