在 Debian 12、11 或 10 上安裝 MariaDB 的兩種方法

在 Debian 上安裝 MariaDB 非常簡單,並為您的環境帶來強大、靈活且高效的資料庫管理系統。本指南將引導您完成安裝過程,確保您可以存取 MariaDB 的最新穩定版本。

以下是使用 MariaDB 的功能和優點的快速概述:

  • 高性能: MariaDB 為資料庫操作提供卓越的速度和效率。
  • 可擴充性: 輕鬆處理大型資料集和高流量負載。
  • 安全: 高級安全功能,包括加密和存取控制。
  • 相容性: 與 MySQL 的無縫相容使切換或整合變得容易。
  • 靈活性: 支援不同應用程式的各種儲存引擎和插件。
  • 社區支持: 廣泛的文檔和強大的支持社區。

MariaDB 的跨平台功能可確保流暢的體驗,讓您可以在任何系統上利用其強大的功能。讓我們深入了解在 Debian 系統上安裝 MariaDB 的技術步驟。

安裝 MariaDB 的先決條件

若要在 Debian 上成功安裝 MariaDB,請確保符合以下要求:

系統需求和支援的 Debian 版本

成分最低要求
處理器2 GHz 或更快的雙核心處理器
記憶體4 GB 或更多
磁碟空間25 GB 可用空間
網路高速網路連線
支援的版本Debian 12(書呆子), Debian 11(靶心), Debian 10(巴斯特)

其他要求

要求描述
網路連線下載 MariaDB 和應用程式更新所必需的。
終端約定所有終端命令都應以普通用戶身份執行 sudo 特權。
CLI 指令使用命令列介面 (CLI) 進行安裝和設定。
資料庫管理工具建議使用 phpMyAdmin 等工具來更好地管理資料庫。

方法1:透過預設儲存庫安裝MariaDB

更新包索引

首先,在您的終端機中,更新軟體包索引以確保您擁有有關可用軟體包的最新資訊:

sudo apt update

安裝 MariaDB 伺服器

從預設儲存庫安裝 MariaDB 伺服器套件:

sudo apt install mariadb-server

此命令將在您的 Debian Linux 發行版上安裝預設的 MariaDB 版本。要升級到滿足您要求的最新 MariaDB,請查看下面的方法 2。

方法2:透過PPA安裝MariaDB

導入 MariaDB.org APT 儲存庫

安裝 MariaDB 安裝所需的初始軟體包

第一步是安裝必要的依賴項。在終端機中執行以下命令。

sudo apt install dirmngr ca-certificates software-properties-common apt-transport-https curl -y

繼續新增 MariaDB APT 儲存庫

本節介紹導入建議用於生產用途的長期支援 (LTS) 分支。對於那些喜歡在 WordPress 等內容管理系統 (CMS) 上使用最新版本的 MariaDB 並且不介意每 6-12 個月升級一次版本的用戶來說,短期版本是一個選擇。

在安裝之前,請使用下列命令匯入 GPG 金鑰以驗證 MariaDB 套件。

curl -fsSL http://mirror.mariadb.org/PublicKey_v2 | sudo gpg --dearmor | sudo tee /usr/share/keyrings/mariadb.gpg > /dev/null

導入 GPG 金鑰後,下一步是導入儲存庫。這些命令適用於 Debian 11 和 Debian 10 發行版。

10.5(長期版本)EOL

壽命結束:2025 年 6 月 24 日

echo "deb [signed-by=/usr/share/keyrings/mariadb.gpg] http://mirror.mariadb.org/repo/10.5/debian/ $(lsb_release -cs) main" | sudo tee /etc/apt/sources.list.d/mariadb.list
10.6(長期發布)

壽命結束:2026 年 7 月 6 日

echo "deb [signed-by=/usr/share/keyrings/mariadb.gpg] http://mirror.mariadb.org/repo/10.6/debian/ $(lsb_release -cs) main" | sudo tee /etc/apt/sources.list.d/mariadb.list
10.11(長期版本)

壽命結束:2028 年 2 月 16 日

echo "deb [signed-by=/usr/share/keyrings/mariadb.gpg] http://mirror.mariadb.org/repo/10.11/debian/ $(lsb_release -cs) main" | sudo tee /etc/apt/sources.list.d/mariadb.list
11.1(短期發布)

壽命結束:2024 年 8 月 21 日

echo "deb [arch=amd64,arm64,ppc64el signed-by=/usr/share/keyrings/mariadb.gpg] http://mirror.mariadb.org/repo/11.1/debian/ $(lsb_release -cs) main" | sudo tee /etc/apt/sources.list.d/mariadb.list
11.2(短期發布)

壽命終止:2024 年 11 月 21 日

echo "deb [arch=amd64,arm64,ppc64el signed-by=/usr/share/keyrings/mariadb.gpg] http://mirror.mariadb.org/repo/11.2/debian/ $(lsb_release -cs) main" | sudo tee /etc/apt/sources.list.d/mariadb.list
MariaDB 11.3(短期版)

生命終止日期:2025 年 2 月。

echo "deb [arch=amd64,arm64,ppc64el signed-by=/usr/share/keyrings/mariadb.gpg] http://mirror.mariadb.org/repo/11.2/ubuntu/ $(lsb_release -cs) main" | sudo tee /etc/apt/sources.list.d/mariadb.list
MariaDB 11.4(長期版本)

生命終止日期:2030 年 2 月。

echo "deb [arch=amd64,arm64,ppc64el signed-by=/usr/share/keyrings/mariadb.gpg] http://mirror.mariadb.org/repo/11.4/ubuntu/ $(lsb_release -cs) main" | sudo tee /etc/apt/sources.list.d/mariadb.list

新增了版本支援的終止日期。請務必使用 LTS 版本進行生產,除非您準備好處理變更為較新版本的更新。

刷新APT指數

匯入儲存庫後,使用提供的命令更新您的 APT 儲存庫。

sudo apt update

完成 MariaDB 安裝

MariaDB.org 設定完成後,繼續在 Debian 上安裝 MariaDB

使用充分配置的儲存庫和 GPG 金鑰安裝 MariaDB 用戶端和伺服器套件。使用以下命令安裝 MariaDB:

sudo apt install mariadb-server mariadb-client

確認 MariaDB 安裝

透過檢查版本和建置來確認 MariaDB 的安裝。

mariadb --version

安裝了 MariaDB 10.10 的輸出範例:

mariadb  Ver x.x Distrib x.x.x-MariaDB, for debian-linux-gnu (x86_64) using  EditLine wrapper

啟用 MariaDB 服務和狀態檢查

驗證 MariaDB 服務狀態

安裝MariaDB後,您可以使用下列systemctl指令檢查資料庫軟體的狀態:

systemctl status mariadb

如果您碰巧尚未啟動該服務,請使用以下命令立即啟用它並在系統重新啟動時啟用它:

sudo systemctl enable mariadb --now

MariaDB Systemd 服務指令

以下是在 Debian 系統上使用 MariaDB 時需要注意的最常見的 systemd 服務命令。

啟動 MariaDB:

sudo systemctl start mariadb

停止 MariaDB:

sudo systemctl stop mariadb

在系統啟動時啟用 MariaDB:

sudo systemctl enable mariadb

在系統啟動時停用 MariaDB:

sudo systemctl disable mariadb

重新啟動 MariaDB 服務:

sudo systemctl restart mariadb

安裝後:保護 MariaDB

安裝新版本的 MariaDB 後,了解開箱即用的設定可能無法提供強大的安全性至關重要。因此,經驗豐富的資料庫管理員經常建議加強這些設定以防止漏洞。

運行安全腳本

MariaDB 提供了一個方便的腳本,名為 mysql_secure_installation 幫助管理員保護他們的安裝。執行此腳本不僅是最佳實踐,而且是最佳實踐。這是增強資料庫抵禦潛在威脅的主動步驟。

執行以下命令運行腳本:

sudo mysql_secure_installation

或者

sudo mariadb-secure-installation

配置 MariaDB 以增強安全性

啟動後 mysql_secure_installation 腳本中,您將被引導完成幾個步驟來增強資料庫的安全性:

  • 設定根密碼:為 root 使用者選擇一個強且唯一的密碼對於確保只有授權人員才能進行關鍵變更至關重要​​。
  • 限制遠端存取:拒絕從外部來源直接 root 登入有利於增強安全性。這最大限度地減少了來自遠端位置的未經授權的存取嘗試的風險。
  • 刪除匿名用戶:預設情況下,MariaDB 具有任何人都可以用來登入的匿名使用者帳戶。
  • 刪除測試資料庫:MariaDB 有一個每個人都可以存取的測試資料庫。雖然它可能對初始測試有幫助,但在生產環境中刪除它更安全,可以避免任何潛在的誤用。

您可以在自己的終端機中執行類似操作的範例:

NOTE: RUNNING ALL PARTS OF THIS SCRIPT IS RECOMMENDED FOR ALL MariaDB
      SERVERS IN PRODUCTION USE!  PLEASE READ EACH STEP CAREFULLY!

In order to log into MariaDB to secure it, we'll need the current
password for the root user. If you've just installed MariaDB, and
haven't set the root password yet, you should just press enter here.

Enter current password for root (enter for none): 
OK, successfully used password, moving on...

Setting the root password or using the unix_socket ensures that nobody
can log into the MariaDB root user without the proper authorisation.

You already have your root account protected, so you can safely answer 'n'.

Switch to unix_socket authentication [Y/n] Y <---- Type Y then press the ENTER KEY.
Enabled successfully!
Reloading privilege tables..
 ... Success!


You already have your root account protected, so you can safely answer 'n'.

Change the root password? [Y/n] Y <---- Type Y then press the ENTER KEY.
New password: 
Re-enter new password: 
Password updated successfully!
Reloading privilege tables..
 ... Success!


By default, a MariaDB installation has an anonymous user, allowing anyone
to log into MariaDB without having to have a user account created for
them.  This is intended only for testing, and to make the installation
go a bit smoother.  You should remove them before moving into a
production environment.

Remove anonymous users? [Y/n] Y <---- Type Y then press the ENTER KEY.
 ... Success!

Normally, root should only be allowed to connect from 'localhost'.  This
ensures that someone cannot guess at the root password from the network.

Disallow root login remotely? [Y/n] Y <---- Type Y then press the ENTER KEY.
 ... Success!

By default, MariaDB comes with a database named 'test' that anyone can
access.  This is also intended only for testing, and should be removed
before moving into a production environment.

Remove test database and access to it? [Y/n] Y <---- Type Y then press the ENTER KEY.
 - Dropping test database...
 ... Success!
 - Removing privileges on test database...
 ... Success!

Reloading the privilege tables will ensure that all changes made so far
will take effect immediately.

Reload privilege tables now? [Y/n] Y <---- Type Y then press the ENTER KEY.
 ... Success!

Cleaning up...

All done!  If you've completed all of the above steps, your MariaDB
installation should now be secure.

Thanks for using MariaDB!

MariaDB 的附加指令

運行 MariaDB 資料庫表升級工具

如果您已從舊版的 MariaDB 過渡(例如從 10.5 遷移到 10.10),則應謹慎確保資料庫表的順利功能。 MariaDB 提供了一個專門的工具, mariadb-upgrade,專門用於檢查和糾正由於版本轉換而可能出現的任何差異。

透過鍵入以下內容來啟動程序或查看資料庫表的目前狀態:

sudo mariadb-upgrade

例如,如果您遇到以下輸出:

This installation of MariaDB is already upgraded to 10.10.2-MariaDB.
There is no need to run mysql_upgrade again for 10.10.2-MariaDB.
You can use --force if you still want to run mysql_upgrade

這表明昇級工具已經完成了上述版本的工作。但即使您最近沒有執行升級,運行此工具仍然是明智的。它精心列出和更新表,以確保它們與當前的 MariaDB 安裝保持相容。

卸載 MariaDB

在某些情況下,您可能會想要從 Debian 系統中解除安裝 MariaDB。如果這是您的選擇,那麼必須乾淨利落地執行此操作,以防止延遲檔案或設定。

首先刪除核心 MariaDB 元件:

sudo apt auto mariadb-server mariadb-client

然後,若要清除對 MariaDB 的所有引用,請刪除關聯的儲存庫和 GPG 金鑰。

sudo rm /etc/apt/sources.list.d/mariadb.list /usr/share/keyrings/mariadb.gpg

結論

最後,本文提供了有關使用最新的 MariaDB 穩定版本增強 Debian 系統的詳細逐步指南。我們探索了兩種方法:使用 Debian 預設儲存庫或官方 mariadb.org 儲存庫來取得最新支援的版本。透過匯入 mariadb.org 儲存庫,您可以安裝 10.x 或 11.x 版本系列中的自訂 MariaDB 版本。

此外,我們也介紹了基本的服務指令,可協助您有效管理 MariaDB 服務。該指南還提供了在安裝較新版本的 MariaDB 後順利升級資料庫的必要步驟。對於需要恢復或進行更改的人,我們討論了刪除 MariaDB 及其導入的 apt 儲存庫。有了這些知識,您就可以自信地管理、升級和優化 Debian 上的 MariaDB 安裝。

有用的連結

以下是一些與使用 MariaDB 相關的有價值的連結:

  • MariaDB GitHub 儲存庫:存取 MariaDB GitHub 儲存庫以查看原始程式碼、報告問題並為開發做出貢獻。
  • MariaDB 知識庫:探索知識庫,了解有關 MariaDB 的詳細文章、教學和使用者指南。
  • MariaDB 文檔:存取全面的文檔,以取得有關安裝、配置和使用 MariaDB 的詳細指南。
  • MariaDB 官方網站:請造訪 MariaDB 官方網站,以了解有關資料庫系統、其功能和下載選項的資訊。
  • MariaDB 產品頁面:了解 MariaDB 提供的各種產品和服務,包括社群伺服器和企業解決方案。
Joshua James
跟我來
Joshua James 的最新帖子 (看全部)

發佈留言