linux重設mysql root密碼的6種方法

來源:互聯網
上載者:User

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
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

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.