System environment:
[root@db02 ~]# uname -m x86_64 [root@db02 ~]# uname -r 2.6.32-696.el6.x86_64 [root@db02 ~]# cat /etc/redhat-release CentOS release 6.9 (Final)
5.6 and 5.7 are a bit different, in 5.6 the column name is password, but 5.7 is authentication_string
Operation process:
1. Modify /etc/my.cnf and add skip-grant-tables in the line below [mysqld]
[root@db02 ~]# vim /etc/my.cnf [mysqld] skip-grant-tables ……………………
2. Start mysql
/etc/init.d/mysqld-5.7.17 start
3. Enter the database without password and change the password
mysql> use mysql; mysql> update user set authentication_string=PASSWORD('123yk') where user='root' and host='localhost'; mysql> flush privileges;
4. Modify /etc/my.cnf again and remove the skip-grant-tables just added
[root@db02 ~]# vim /etc/my.cnf [mysqld] # skip-grant-tables
5. Restart mysql
/etc/init.d/mysqld-5.7.17 restart
6. Log in with the newly modified password
/application/mysql-5.7.17/bin/mysql -uroot -p
Note: Users can modify and set passwords according to their own system environment and conditions. This blog post is for reference only.