Shell script restarts MySQL password
Note: Remember to give this script the Execute permission Oh. (chmod u+x reset_mysql_root_password.sh)
This shell script is as follows:
The code is as follows |
Copy Code |
#!/bin/bash Path=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:~/bin Export PATH # Check If user is root If [$ (id-u)!= "0"]; Then printf "error:you must is root to run this script!\n" Exit 1 Fi echo "=========================================================================\n" printf "Reset MySQL root Password for Lnmp, written by licess\n" printf "=========================================================================\n" printf "LNMP is a tool to Auto-compile & install nginx+mysql+php on linux\n" printf "This script are a tool to reset MySQL root password for lnmp\n" printf "For more information please visit http://www.111cn.net\n" printf "\ n" printf "Usage:sh reset_mysql_root_password.sh\n" printf "=========================================================================\n" Mysql_root_password= "" Read-p "(Please input New MySQL root password):" Mysql_root_password If ["$mysql _root_password" = ""]; Then echo "Error:password can" t be null!! \ n " Exit 1 Fi printf "stoping MySQL ... \ n " /etc/init.d/mysql stop printf "Starting MySQL with skip Grant tables\n" /usr/local/mysql/bin/mysqld_safe--skip-grant-tables >/dev/null 2>&1 & printf "using MySQL to flush privileges and reset password\n" Sleep 10 printf "Update user Set Password = password (' $mysql _root_password ') where user = ' root ' \ n" /usr/local/mysql/bin/mysql-u root MySQL << EOF Update user Set Password = password (' $mysql _root_password ') where user = ' root '; Eof Reset_status= ' echo $? ' if [$reset _status = "0"]; Then printf "Password Reset succesfully. Now killing Mysqld softly\n " Killall mysqld Sleep 10 printf "Restarting the actual MySQL service\n" /etc/init.d/mysql start printf "Password successfully reset to ' $mysql _root_password ' \ n" Else printf "Reset MySQL root password failed!\n" Fi |
Method Two, using the root administrator to manipulate
System administrator root into Shell
The code is as follows |
Copy Code |
#service mysqld Stop #mysqld_safe –skip-grant-tables & (My mysqld_safe in/usr/bin, if your mysqld_safe is not in the path, use absolute path here) This time the root password is empty, we will be free authentication login #mysql-U Root > |
Method Iii. use of Mysqladmin
The code is as follows |
Copy Code |
#./mysqladmin-u root password ' newpassword ' #./mysqladmin-u root-h host_name password ' newpassword ' Usually mysqladmin ' s path is/usr/bin, HOST_NAME is your real host name, e.g. Localhost.localdomain. The quotes behind the password are not required, but quotation marks are required if the password contains spaces or some special symbols. |
Method Four, use MySQL SET password command
The code is as follows |
Copy Code |
#./mysql-u Root mysql> SET PASSWORD for ' root ' @ ' localhost ' = PASSWORD (' NewPassword '); mysql> SET PASSWORD for ' root ' @ ' host_name ' = PASSWORD (' NewPassword '); |
Method v. Update the user table with the UPDATE statement reset root password
The code is as follows |
Copy Code |
#./mysql-u Root mysql> UPDATE mysql.user SET Password = Password (' newpassword ') WHERE user = ' root '; mysql> FLUSH privileges; |
Method Six, start MySQL safe mode reset root password
The code is as follows |
Copy Code |
1, Stop the MySQL process Execution:/etc/init.d/mysql stop, the exact location may vary depending on the system, or it may be a/etc/init.d/mysql,/etc/init.d/mysqld path, or directly below (preferably do not use the following mandatory statement): # killall-term mysqld 2, boot MySQL in safe mode # mysqld_safe– Skip-grant-tables or, # mysqld_safe--skip-grant-tables >/dev/null 2>&1/ Hint: mysqld_ Safe is generally in the/usr/local/mysql/bin/directory. 3, login MySQL to complete the above two steps after the password can not enter the MySQL # mysql-u root or, #/usr/local/mysql/bin/mysql-u Root MySQL 4, change root password The following sentences are executed sequentially: Use MySQL; Select host, user, password from user; Update user Set Password=password ("NewPassword") where user= "root" flush privileges; 5. Exit the console and restart the MySQL service Service mysqld Restart or, /etc/init.d/mysql restart |