共计 1566 个字符,预计需要花费 4 分钟才能阅读完成。
MySQL 忘记 root 密码,而且不能使用操作系统认证直接进入数据库修改密码时,需要重置 root 密码。
1、在配置文件 /etc/my.cnf 添加 skip-grant-tables 一行,跳过密码验证。
2、重启 mysql 数据库主进程# /etc/init.d/mysqld restart(也可以直接先停掉 MySQL 进程后使用 skip-grant-tables 参数重启 MySQL)
3、登录数据库修改密码。
mysql> update user set authentication_string=password(”) where user=’root’ and host=’localhost’;
Query OK, 1 row affected (0.00 sec)
Rows matched: 1 Changed: 1 Warnings: 0
mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)
mysql>
mysql> exit
这里需要修改的字段是 authentication_string,这点和之前的版本不同。
4、这个时候,如果你设置的密码太简单,则在数据库执行任何命令都会报类似如下错误:
mysql> show databases;
ERROR 1820 (HY000): You must reset your password using ALTER USER statement before executing this statement.
mysql> show database;
ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ‘database’ at line 1
mysql> update user set authentication_string=password(‘mysql’) where user=’root’ and host=’localhost’;
ERROR 1046 (3D000): No database selected
mysql> use mysql;
ERROR 1820 (HY000): You must reset your password using ALTER USER statement before executing this statement.
5、注意:如果只想设置简单密码需要修改两个全局参数:
mysql> set global validate_password_policy=0;
mysql> set global validate_password_length=1;
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> set password=password(“mysql”);
Query OK, 0 rows affected, 1 warning (0.00 sec)
mysql> use mysql;
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A
Database changed
本文永久更新链接地址 :http://www.linuxidc.com/Linux/2017-07/145607.htm