If you do not remember the root password of MySQL, then you can only force the password change, using the shell to automatically change the password:
#!/bin/bash#author Aikerpath=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:~/binexport Pathread-p "Please input your new password:" cpwd= $cservice mysqld stopmysqld_safe--skip-grant-tables&echo ' changing password ... '; Echo ' The set password ... '; sleep 6mysql-uroot-e "INSERT into Mysql.user (Select_priv,insert_priv,update_priv,delete_priv, Create_priv,drop_priv,reload_priv,shutdown_priv,process_priv,file_priv,grant_priv,references_priv,index_priv, Alter_priv,show_db_priv,super_priv,create_tmp_table_priv,lock_tables_priv,execute_priv,repl_slave_priv,repl_ Client_priv,create_view_priv,show_view_priv,create_routine_priv,alter_routine_priv,create_user_priv,event_priv , trigger_priv,create_tablespace_priv,user,password,host) VALUES (' Y ', ' y ', ' y ', ' y ', ' y ', ' y ', ' y ', ' y ', ' y ', ' y ', ' y ', ' y ' , ' y ', ' y ', ' y ', ' y ', ' y ', ' y ', ' y ', ' y ', ' y ', ' y ', ' y ', ' y ', ' y ', ' y ', ' y ', ' y ', ' y ', ' root ', password (' ${pwd} '), ' 127.0.0.1 ') " MYSQL-UROOT-E "INSERT INTO Mysql.user (select_priv,insert_priv,update_priv,delete_priv,create_priv,drop_pRiv,reload_priv,shutdown_priv,process_priv,file_priv,grant_priv,references_priv,index_priv,alter_priv,show_db_ Priv,super_priv,create_tmp_table_priv,lock_tables_priv,execute_priv,repl_slave_priv,repl_client_priv,create_ View_priv,show_view_priv,create_routine_priv,alter_routine_priv,create_user_priv,event_priv,trigger_priv, Create_tablespace_priv,user,password,host) VALUES (' Y ', ' y ', ' y ', ' y ', ' y ', ' y ', ' y ', ' y ', ' y ', ' y ', ' y ', ' y ', ' y ', ' y ', ' y ', ' Y ', ' y ', ' y ', ' y ', ' y ', ' y ', ' y ', ' y ', ' y ', ' y ', ' y ', ' y ', ' y ', ' y ', ' root ', password (' ${pwd} '), ' localhost ', ' mysql-uroot-e ' Update Mysql.user set Password=password (' ${pwd} ') WHERE user= ' root '; mysql-uroot-e ' update Mysql.user SET authenticatio N_string=password (' ${pwd} ') WHERE user= ' root '; MYSQL-UROOT-E "FLUSH privileges";p kill-9 mysqld_safepkill-9 Mysqldsleep 2service mysqld startecho ' =========================================== ' echo "MySQL root password successfully modified to: ${pwd}" echo "The root password set ${pwd} successuful"
Test Effect:
[[email protected] ~]# sh mysqlpwd.sh Please input your new password:12345678shutting down MySQL. success! changing password ... The Set Password ... 2018-03-14t18:19:51.793872z mysqld_safe Logging to '/data/mysql/aaa.err '. 2018-03-14t18:19:51.814649z mysqld_safe Starting mysqld daemon with databases From/data/mysqlerror 1054 (42S22) at line 1:unknown column ' Password ' in ' field Li St ' Error 1054 (42S22) at line 1:unknown column ' Password ' "field List ' Error 1054 (42S22) at line 1:unknown Column ' PA ssWOrd ' in ' Field List ' Mysqlpwd.sh:line 17:45426 killed Mysqld_safe--skip-grant-tablesstarting MySQL S uccess! ===========================================mysql root Password successfully modified to: 12345678The root password set 12345678 successuful[[ Email protected] ~]# mysql-uroot-p Enter password:welcome to the MySQL monitor. Commands End With; or \g.your MySQL connection ID is 2Server version:5.7.21-log Source distributioncopyright (c), 2018, Oracle and/or I TS affiliates. All Rights reserved. Oracle is a registered trademark of Oracle Corporation and/or itsaffiliates. Other names trademarks of their respectiveowners. Type ' help ', ' or ' \h ' for help. Type ' \c ' to clear the current input statement.mysql> Quitbye
One shell per day (11) MySQL Force automatic password change