在linux系统中重启root密码也是非常的简单并且方法多了去了,下面小编为各位整理了一些常用的root密码重置方法,希望例子能给大家带来帮助.
shell脚本重启mysql密码
注:记得给此脚本属于执行权限哦,chmod u+x reset_mysql_root_password.sh,此shell脚本如下:
- #!/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 be 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 is 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 //Vevb.com
- 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
方法二:利用root管理员来操作,系统管理员root进入shell,代码如下:
- #service mysqld stop
- #mysqld_safe –skip-grant-tables & (我的mysqld_safe在/usr/bin,如果你的mysqld_safe不在PATH路径里面,那么这里要使用绝对路径)
- 这个时候root的密码为空,我们就可以免认证登录了
- #mysql -u root
- >
方法三:使用mysqladmin,代码如下:
- # ./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.
password后面的引号不是必须的,不过如果密码包含空格或者一些特殊的符号,需要用引号.
方法四:利用mysql SET PASSWORD命令,代码如下:
- # ./mysql -u root
- mysql> SET PASSWORD FOR 'root'@'localhost' = PASSWORD('newpassword');
- mysql> SET PASSWORD FOR 'root'@'host_name' = PASSWORD('newpassword');
方法五:使用UPDATE语句更新user表重置ROOT密码,代码如下:
- # ./mysql -u root
- mysql> UPDATE mysql.user SET Password = PASSWORD('newpassword') WHERE User = 'root';
- mysql> FLUSH PRIVILEGES;
方法六:启动MYSQL的安全模式重置ROOT密码,代码如下:
1、停止MySQL进程
执行:/etc/init.d/mysql stop,具体位置可能随系统不同而不同,也可能是/etc/init.d/mysql,/etc/init.d/mysqld等路径,或下面直接终止,最好不要使用下面这个强制语句.
# killall -TERM mysqld
2、以安全模式启动MySQL.
- # mysqld_safe –skip-grant-tables &
- //或,
- # mysqld_safe --skip-grant-tables >/dev/null 2>&1 &
提示:mysqld_safe一般在/usr/local/mysql/bin/目录下.
3、登陆MYSQL
完成上述两步以后就可以不用密码进入MySQL了.
- # mysql -u root
- //或,
- # /usr/local/mysql/bin/mysql -u root mysql
4、更改ROOT密码,以下几句依次执行:
- use mysql;
- select host, user, password from user;
- update user set password=password(“newpassword”) where user=”root”
- flush privileges;
5.退出控制台,重启MYSQL服务.
- service mysqld restart
- //或,
- /etc/init.d/mysql restart
新闻热点
疑难解答