一. 在配置文件/etc/my.cnf添加一行: skip-grant-tables, 跳过密码验证.
二. 重启mysql数据库主进程
/etc/init.d/mysqld restart
三. 不使用密码登录数据库, 然后修改密码
mysql> use mysql;
Database changed
mysql> update user set authentication_string=password('yourNewPassword') where user='root' and host='localhost';
Query OK, 1 row affected (0.00 sec)
mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)
mysql> exit
这里需要修改的字段是authentication_string, 这点和老版本不同. 旧版本为
set password=password(‘admin')
四. 如果你设置的密码太简单, 则在数据库执行任何命令都会报类似如下错误(需要跳到第1步重新开始)
mysql> show databases;
ERROR 1820 (HY000): You must reset your password using ALTER USER statement before executing this statement.
mysql> use mysql;
ERROR 1820 (HY000): You must reset your password using ALTER USER statement before executing this statement.
五. 如果只想设置简单密码需要修改两个全局参数
mysql> set global validate_password_policy=0;
Query OK, 0 rows affected (0.00 sec)
mysql> set global validate_password_length=1;
Query OK, 0 rows affected (0.00 sec)
mysql> update user set authentication_string=password('yourNewPassword') where user='root' and host='localhost';
Query OK, 1 row affected (0.00 sec)
mysql> use mysql;
Database changed