閱讀602 返回首頁    go 阿裏雲 go 技術社區[雲棲]


如何在 Linux 中重置 MySQL 或者 MariaDB 的 root 密碼

如果你是第一次設置 MySQL 或 MariaDB 數據庫,你可以直接運行 mysql_secure_installation 來實現基本的安全設置。

其中一項是設置數據庫 root 帳戶的密碼 - 你必須保持私密,並僅在絕對需要時使用。如果你忘記了密碼或需要重置密碼(例如,當數據庫管理員換人或被裁員!),這篇文章會派上用場。我們將解釋如何在 Linux 中重置或恢複 MySQL 或 MariaDB 的 root 密碼。

建議閱讀: 更改 MySQL 或 MariaDB 的 root 密碼

雖然我們將在本文中使用 MariaDB,但這些說明同樣也適用於 MySQL。

恢複 MySQL 或者 MariaDB 的 root 密碼

開始之前,先停止數據庫服務並檢查服務狀態,我們應該可以看到先前設置的環境變量:


  1. ------------- SystemD -------------
  2. # systemctl stop mariadb
  3. ------------- SysVinit -------------
  4. # /etc/init.d/mysqld stop

接下來,用 --skip-grant-tables 選項啟動服務:


  1. ------------- SystemD -------------
  2. # systemctl set-environment MYSQLD_OPTS="--skip-grant-tables"
  3. # systemctl start mariadb
  4. # systemctl status mariadb
  5. ------------- SysVinit -------------
  6. # mysqld_safe --skip-grant-tables &

使用 skip tables 啟動 MySQL/MariaDB

使用 skip tables 啟動 MySQL/MariaDB

這可以讓你不用 root 密碼就能連接到數據庫(你也許需要切換到另外一個終端上):


  1. # mysql -u root

接下來,按照下麵列出的步驟來。


  1. MariaDB [(none)]> USE mysql;
  2. MariaDB [(none)]> UPDATE user SET password=PASSWORD('YourNewPasswordHere') WHERE User='root' AND Host = 'localhost';
  3. MariaDB [(none)]> FLUSH PRIVILEGES;

最後,停止服務,取消環境變量設置並再次啟動服務:


  1. ------------- SystemD -------------
  2. # systemctl stop mariadb
  3. # systemctl unset-environment MYSQLD_OPTS
  4. # systemctl start mariadb
  5. ------------- SysVinit -------------
  6. # /etc/init.d/mysql stop
  7. # /etc/init.d/mysql start

這可以讓先前的改變生效,允許你使用新的密碼連接到數據庫。

原文發布時間為:2017-03-17

本文來自雲棲社區合作夥伴“Linux中國”

最後更新:2017-05-24 16:02:14

  上一篇:go  《STM32庫開發實戰指南:基於STM32F4》----第3章 初識STM32 3.1 什麼是STM32
  下一篇:go  free:一個在 Linux 中檢查內存使用情況的標準命令