MariaDB 是 MySQL 的兄弟,有些人擔心 MySQL 被甲骨文買走之後未來會變得越來越貴,就像後來的 Java 那樣,會想要轉移到 MariaDB,而這兩者在第 10 版之後細部差異也越來越多,但就基本的 SQL 語句都還是可以無痛轉換的,更多資訊可以參閱 MariaDB 文件

這篇紀錄一下本人安裝 MariaDB 的筆記,大體上和 MySQL 差不多。

版本

MariaDB 也有分為短期支援與長期支援版,目前的版本可以參考 〈MariaDB general release maintenance periods〉。

這邊選用 10.6,到 2026 都不用擔心升級問題。

安裝

參閱 〈Installing MariaDB .deb Files〉。

Ubuntu 22.04

安裝 Ubuntu 22.04 自帶 APT 庫的 mariadb-server,版次正是 10.6,此套件會一併安裝下列套件:

  • galera-4
  • maraidb-client-10.6
  • mariadb-client-core-10.6
  • mariadb-common
  • mariadb-server-10.6
  • mariadb-server-core-10.6
  • mysql-common
  • socat

其中 Galera 是 MariaDB 的叢集系統,socat 則是 Galera 的依賴套件。

雖然安裝 mariadb-server 會一併安裝 Galera,但並不會自動啟用叢集,本文也不會提到叢集。

Ubuntu 20.04

參閱 〈MariaDB Package Repository Setup and Usage〉。

如果是 Ubuntu 20.04,自帶的 mariadb-server 是 10.3,可以外加 MariaDB Package Repositoty 加上 MariaDB 10.6,只要一行指令:

> curl -LsS https://r.mariadb.com/downloads/mariadb_repo_setup | sudo bash -s -- --mariadb-server-version="mariadb-10.6"

加上 APT 庫後,後續作法同上。

安裝後配置

安裝後服務會自動跑起來,也可以用 root 免密碼直接登入。

MariaDB 建議不需要跑 mysql_secure_installation

MariaDB 主配置文件在 /etc/mysql/mariadb.cnf,裡面部分內容:

# The MariaDB/MySQL tools read configuration files in the following order:
# 0. "/etc/mysql/my.cnf" symlinks to this file, reason why all the rest is read.
# 1. "/etc/mysql/mariadb.cnf" (this file) to set global defaults,
# 2. "/etc/mysql/conf.d/*.cnf" to set global options.
# 3. "/etc/mysql/mariadb.conf.d/*.cnf" to set MariaDB-only options.
# 4. "~/.my.cnf" to set user-specific options.
#
# If the same option is defined multiple times, the last one will apply.

實際上主要的配置都在 /etc/mysql/mariadb.conf.d/ 內,如果是 server 方面的配置就改 50-server.cnf,如果是 client 就改 50-client.cnf。

UTF-8 問題

參閱 〈Setting Character Sets and Collations〉。

開 50-client.cnf,確認 [client] 區段有以下設定:

[client]

default-character-set = utf8mb4

開 50-server.cnf,確認 [mysqld] 區段有以下設定:

[mysqld]

# MySQL/MariaDB default is Latin1, but in Debian we rather default to the full
# utf8 4-byte character set. See also client.cnf
character-set-server  = utf8mb4
collation-server      = utf8mb4_general_ci

如果有改動,改完之後,重啟服務:

> sudo systemctl restart mariadb

進去 MySQL 看一下現在的編碼:

SHOW VARIABLES LIKE 'character%';
SHOW VARIABLES LIKE 'collation%';

結果如下:

+--------------------------+----------------------------+
| Variable_name            | Value                      |
+--------------------------+----------------------------+
| character_set_client     | utf8mb4                    |
| character_set_connection | utf8mb4                    |
| character_set_database   | utf8mb4                    |
| character_set_filesystem | binary                     |
| character_set_results    | utf8mb4                    |
| character_set_server     | utf8mb4                    |
| character_set_system     | utf8mb3                    |
| character_sets_dir       | /usr/share/mysql/charsets/ |
+--------------------------+----------------------------+
8 rows in set (0.001 sec)
+----------------------+--------------------+
| Variable_name        | Value              |
+----------------------+--------------------+
| collation_connection | utf8mb4_general_ci |
| collation_database   | utf8mb4_general_ci |
| collation_server     | utf8mb4_general_ci |
+----------------------+--------------------+
3 rows in set (0.001 sec)

可以看到 MariaDB 預設就是 uft8mb4 了,貼心,不會有部份文字漏字的問題。

帳號管理

裝完預設帳號是 root@localhost,使用 unix socket 認證,簡單說就是用 Linux 認證,只要我能化身為 Linux root,我也就能用 root@localhost 登入 MariaDB。

但對於連接 MariaDB 的應用,就有需要另外開一組帳號了,並且改用 mysql_native_password 認證。

注意,MariaDB 與 MySQL 在此處的手法略有不同。

先用 root 登入 MySQL,開新帳號:

SET old_passwords=0;
CREATE USER 'ap1'@'192.168.0.3' IDENTIFIED BY 'ap-password';

則帳號 ap1 就開好啦,而且它只能從 192.168.0.3 連入,否則會被拒絕。

如果想要自由連入,用百分比符號表示:

SET old_passwords=0;
CREATE USER 'ap'@'%' IDENTIFIED BY 'ap-password';

看有沒有建成功:

SELECT Host, User FROM mysql.global_priv;

剛建的新帳號已經能登入了,但還沒有權限,可以賦予它們某些資料庫的權限,以 ap 為例:

GRANT ALL ON *.* TO 'ap'@'%';

上面的 *.* 表示所有的 database 與所有的 table,可視需要自行縮減。

最後一樣記得測一下。