如何在Linux中重設MySQL root密碼

不言
發布: 2019-03-01 11:10:39
原創
6095 人瀏覽過

MySQL是一種廣泛用於資料儲存的開源資料庫軟體。有時我們忘記了MySQL root密碼,但不需要緊張,本篇文章將介紹如何透過簡單的步驟重設MySQL root密碼。

如何在Linux中重設MySQL root密碼

(相關推薦:MySQL教學

步驟1:在安全模式下啟動MySQL

首先,需要停止執行mysql伺服器。我們使用以下命令之一在Linux系統上停止MySQL伺服器。

# service mysql stop           //对于基于SysVinit的系统
# systemctl stop mysql.service     //对于基于Systemd的系统
登入後複製

現在在安全模式下使用--skip grant tables選項啟動mysql伺服器。使用以下命令以安全模式啟動MySQL。在安全模式下,MySQL不提示輸入登入密碼。

# mysqld_safe --skip-grant-tables &
登入後複製

步驟2:重設mysql root密碼

現在以root使用者身分登入mysql伺服器,並使用下列指令變更密碼。這將重置系統上的mysql root密碼。

對於MySQL5.6或更低版本

# mysql -u root
 mysql>USE mysql;
 mysql>UPDATE user SET password=PASSWORD("NEW-PASSWORD") WHERE User='root';
 mysql>FLUSH PRIVILEGES;
 mysql>quit
登入後複製

對於MySQL5.7或更高版本

# mysql -u root
mysql> SET PASSWORD FOR 'root'@'localhost' = PASSWORD("NEW-PASSWORD");
mysql>FLUSH PRIVILEGES;
mysql>quit
登入後複製

步驟3:重啟mysql伺服器

#更改密碼後,停止mysql(在安全模式下執行)服務,並使用下面的命令重新啟動它。

//基于SysVinit的系统
# service mysql stop 
# service mysql start

//基于Systemd的系统 
# systemctl stop mysql.service
# systemctl start mysql.service
登入後複製

步驟4:驗證新密碼

重新設定mysql root帳號密碼並重新啟動後,只需登入驗證新密碼即可。

# mysql -u root -p

Enter password: **********
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 29
Server version: 5.5.57 MySQL Community Server (GPL)

Copyright (c) 2000, 2017, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql>
登入後複製

這篇文章到這裡就已經全部結束了,更多精彩內容大家可以關注php中文網的其他相關欄位教學! ! !

以上是如何在Linux中重設MySQL root密碼的詳細內容。更多資訊請關注PHP中文網其他相關文章!

相關標籤:
來源:php.cn
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
作者最新文章
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板
關於我們 免責聲明 Sitemap
PHP中文網:公益線上PHP培訓,幫助PHP學習者快速成長!