天天看點

在 Linux 中修改 MySQL 或 MariaDB 的 Root 密碼

其中的一個設定是資料庫的 root 密碼 —— 該密碼必須保密,并且隻在必要的時候使用。如果你需要修改它(例如,當資料庫管理者換了人 —— 或者被解雇了!)。

這篇文章遲早會派上用場的。我們講說明怎樣來在 linux 中修改 mysql 或 mariadb 資料庫伺服器的 root 密碼。

盡管我們會在本文中使用 mariadb 伺服器,但本文中的用法說明對 mysql 也有效。

<a target="_blank"></a>

你知道 root 密碼,但是想要重置它,對于這樣的情況,讓我們首先确定 mariadb 正在運作:

<code>------------- centos/rhel 7 and fedora 22+ -------------</code>

<code># systemctl is-active mariadb</code>

<code>------------- centos/rhel 6 and fedora -------------</code>

<code># /etc/init.d/mysqld status</code>

在 Linux 中修改 MySQL 或 MariaDB 的 Root 密碼

檢查 mysql 狀态

如果上面的指令傳回中沒有 <code>active</code> 這個關鍵詞,那麼該服務就是停止狀态,你需要在進行下一步之前先啟動資料庫服務:

<code># systemctl start mariadb</code>

<code># /etc/init.d/mysqld start</code>

接下來,我們将以 root 登入進資料庫伺服器:

<code># mysql -u root -p</code>

為了相容不同版本,我們将使用下面的聲明來更新 mysql 資料庫的使用者表。注意,你需要将<code>yourpasswordhere</code> 替換為你為 root 選擇的新密碼。

<code>mariadb [(none)]&gt; use mysql;</code>

<code>mariadb [(none)]&gt; update user set password=password('yourpasswordhere') where user='root' and host = 'localhost';</code>

<code>mariadb [(none)]&gt; flush privileges;</code>

要驗證是否操作成功,請輸入以下指令退出目前 mariadb 會話。

<code>mariadb [(none)]&gt; exit;</code>

然後,敲回車。你現在應該可以使用新密碼連接配接到伺服器了。

在 Linux 中修改 MySQL 或 MariaDB 的 Root 密碼

修改 mysql/mariadb root 密碼

在本文中,我們說明了如何修改 mariadb / mysql 的 root 密碼 —— 或許你知道目前所講的這個方法,也可能不知道。

原文釋出時間為:2017-03-22

本文來自雲栖社群合作夥伴“linux中國”