出自 Arch Linux 中文维基

MariaDB 是一個可靠的、高性能的、功能全面的資料庫,旨在為用戶提供長期免費、向下兼容能直接替代MySQL的資料庫服務。自2013年起,MariaDB就被Arch Linux當作官方默認的MySQL實現[1]

安裝

Archlinux 選擇的 MySQL 默認實現被稱為 MariaDB

安裝 mariadbmariadb-libs

提示:
  • 如果資料庫 (位於 /var/lib/mysql) 運行在 Btrfs 分區之上, 你應當在創建資料庫之前禁用 Copy-on-Write 特性。
  • 如果資料庫運行在 ZFS 分區之上, 你應該在創建資料庫之前參閱 ZFS#Databases

安裝 mariadb 軟體包之後,你必須在啟動 mariadb.service 之前運行下面這條命令:

# mariadb-install-db --user=mysql --basedir=/usr --datadir=/var/lib/mysql

然後 enable 或者 start mariadb.service

提示:如果數據目錄使用的不是 /var/lib/mysql,需要在 /etc/my.cnf.d/server.cnf 文件的 [mysqld] 部分設置 datadir=<數據目錄>

配置

啟動 mariadb 伺服器,並添加 root 維護帳號後,可以登錄伺服器進行進一步配置:

  1. mariadb -u root -p
注意: 默認密碼為空,直接敲回車鍵登錄

添加新用戶

以下是創建一個密碼為'some_pass'的'monty'用戶的示例,並賦予 mydb 完全操作權限:

$ mariadb -u root -p
MariaDB> CREATE USER 'monty'@'localhost' IDENTIFIED BY 'some_pass';
MariaDB> GRANT ALL PRIVILEGES ON mydb.* TO 'monty'@'localhost';
MariaDB> quit

配置文件

MariaDB 會按照以下順序讀取配置文件 (根據 mysqld --help --verbose | tail -20 的輸出):

/etc/my.cnf /etc/my.cnf.d/ ~/.my.cnf

請根據需要的作用範圍(對系統, 對用戶...)修改對應的配置文件。 點擊這裡了解更多信息。

啟用自動補全

注意: 啟用這項功能會增加客戶端啟動時間。

MySQL 默認禁用客戶端自動補全功能。要在整個系統中啟用它,編輯 /etc/my.cnf.d/mysql-clients.cnf,在mysql下 添加 auto-rehash。注意:不要將auto-rehash寫在mysqld下,下次客戶端啟動時就會啟用自動補全。

使用 UTF8MB4

警告: 在改變字符集之前務必先創建備份!
注意: mariadb 包已經使用 utf8mb4 作為字符集且使用 utf8mb4_unicode_ci 作為校對規則. 使用默認(字符集)設置的用戶可以跳過本節。
  • 推薦使用UTF8MB4而不是UTF-8,因為它允許完整的Unicode支持 [2] [3].

將以下內容追加/etc/my.cnf.d/my.cnf文件中:

[client]
default-character-set = utf8mb4

[mariadb]
collation_server = utf8mb4_unicode_ci
character_set_server = utf8mb4

[mariadb-client]
default-character-set = utf8mb4

重啟 mariadb.service 以應用更改。更改字符集不會改變現有表格格式,只會影響新創建的表格以及檢索數據的協議交互。

參見#Maintenance 以優化和檢查資料庫健康狀況。

使用內存作為臨時文件存放點

MySQL 存儲臨時文件的目錄名是 tmpdir

創建一個臨時目錄:

# mkdir -pv /var/lib/mysqltmp
# chown mysql:mysql /var/lib/mysqltmp

通過命令找出 mysql 的id和gid:

$ id mysql
uid=27(mysql) gid=27(mysql) groups=27(mysql)

添加 tmpfs/etc/fstab 中。

 tmpfs   /var/lib/mysqltmp   tmpfs   rw,gid=27,uid=27,size=100M,mode=0750,noatime   0 0

將以下配置添加到 /etc/mysql/my.cnfmysqld 組下:

 tmpdir      = /var/lib/mysqltmp

Stop mariadb.service, mount /var/lib/mysqltmp/啟動 mariadb.service.

Time zone tables

Although time zone tables are created during the installation, they are not automatically populated. They need to be populated if you are planning on using CONVERT_TZ() in SQL queries.

To populate the time zone tables with all the time zones:

$ mariadb-tzinfo-to-sql /usr/share/zoneinfo | mariadb -u root -p mysql

Optionally, you may populate the table with specific time zone files:

$ mariadb-tzinfo-to-sql timezone_file timezone_name | mariadb -u root -p mysql

安全

提高初始的安全性

使用 mariadb-secure-installation 命令會交互式地引導您完成一些推薦的安全措施,比如移除匿名帳戶和測試資料庫,從而提高初始安全性。

# mariadb-secure-installation
警告: 運行之後,請注意TCP埠3306仍將保持打開狀態,但會拒絕連接並顯示錯誤消息。要防止MySQL監聽外部接口,請參閱#僅在迴環地址上監聽#僅通過Unix套接字本地訪問的部分。

只監聽本地迴環地址

默認情況下, MariaDB 會監聽 0.0.0.0 這個地址, 它包括了所有的網絡接口。 為了限制 MariaDB 只監聽迴環地址, 請在 /etc/my.cnf.d/server.cnf文件中添加如下行:

[mariadb]
bind-address = localhost

這將綁定到地址 127.0.0.1 和 ::1,並使 MariaDB 能夠接收 IPv4 和 IPv6 的連接。

啟用僅通過 Unix 套接字在本地啟用訪問

By default, MariaDB is accessible via both Unix sockets and the network. If MariaDB is only needed for the localhost, you can improve security by not listening on TCP port 3306, and only listening on Unix sockets instead. To do this, add the following line in

默認情況下,MariaDB 可通過 Unix sockets 和網絡訪問。如果只需要在本地主機上使用 MariaDB,則可以通過不在 TCP 埠 3306 上監聽,而是僅在 Unix sockets 上監聽來提高安全性。要實現這一點,請在/etc/my.cnf.d/server.cnf文件中添加以下行:

[mariadb]
skip-networking

您仍然可以像以前一樣在本地登錄,但只能使用 Unix 套接字。

授權遠程訪問

警告: 這不被視為最佳實踐,因為可能會引起安全問題。如果你想要從網絡內/外的另一台主機上管理 MariaDB 伺服器,請考慮使用安全外殼協議(Secure Shell),虛擬網絡計算機(VNC)虛擬專用網絡(VPN)

要允許遠程訪問MariaDB伺服器,請確保MariaDB已啟用網絡正在適當的接口上進行偵聽.

授予任何MariaDB用戶遠程訪問權限(以root用戶為例):

# mariadb -u root -p

檢查當前具有遠程訪問權限特權的用戶:

SELECT User, Host FROM mysql.user WHERE Host <> 'localhost';

現在為您的用戶(這裡是 root)授予遠程訪問權限:

GRANT ALL PRIVILEGES ON *.* TO 'root'@'192.168.1.%' IDENTIFIED BY 'my_optional_remote_password' WITH GRANT OPTION;

如果願意,您可以將「%」通配符更改為特定的主機。密碼可以與用戶的主密碼不同。

配置主目錄訪問

注意: 出於安全考慮,systemd 的 .service 文件通過 ProtectHome=true 禁止 MariaDB 訪問 /home/root/run/user 目錄內的文件。datadir 必須要放在以上文件夾之外,並且由 mysql 用戶和用戶組 所有。 如果要改變這個設置,可以根據以下連結創建一個替代的 service 文件:[4]

維護

升級

mariadb大版本升級的時候(例如 mariadb-10.3.10-1 到 mariadb-10.9.4-1),最好更新一下資料庫:

# mariadb-upgrade -u root -p

要從 10.3.x 更新到 10.9.x:

  • 停止 10.3.x 伺服器
  • 更新軟體包
  • 啟動新服務並用執行新軟體包的 mariadb_upgrade

如果新服務未啟動,請參考 MariaDB 未啟動,無法執行 mariadb_upgrade。

備份

有各種各樣的工具和策略可以備份你的資料庫。

如果你正在使用默認的 InnoDB 存儲引擎,建議的在線備份所有資料庫並為時序恢復(也稱為「向前滾動」,當你需要恢復舊備份並重放自那個備份以來發生的更改時)的方法是執行以下命令:

$ mariadb-dump --single-transaction --flush-logs --events --routines --master-data=2 --all-databases -u root -p > all_databases.sql

這將提示輸入 MariaDB 的 root 用戶密碼,該密碼是在資料庫配置期間定義的。

強烈建議不在命令行中指定密碼,因為這會使密碼通過 ps aux 或其他技術暴露給其他用戶發現。相反,上述命令會提示輸入指定用戶的密碼,並將其隱藏起來。

Compression

As SQL tables can get pretty large, it is recommended to pipe the output of the aforementioned command in a compression utility like gzip:

$ mariadb-dump --single-transaction --flush-logs --events --routines --master-data=2 --all-databases -u root -p | gzip > all_databases.sql.gz

Decompressing the backup thus created and reloading it in the server is achieved by doing:

$ zcat all_databases.sql.gz | mariadb -u root -p

This will recreate and repopulate all the databases previously backed up (see this or this).

Non-interactive

If you want to setup non-interactive backup script for use in cron jobs or systemd timers, see option files and this illustration for mariadb-dump.

Basically you should add the following section to the relevant configuration file:

[mariadb-dump]
user=mysqluser
password=secret

Mentioning a user here is optional, but doing so will free you from having to mention it on the command line. If you want to set this for all tools, including mariadb-client, use the [client] group.

Example script

The database can be dumped to a file for easy backup. The following shell script will do this for you, creating a db_backup.gz file in the same directory as the script, containing your database dump:

#!/bin/sh

THISDIR=$(dirname $(readlink -f "$0"))

mariadb-dump --single-transaction --flush-logs --events --routines --master-data=2 --all-databases \
 | gzip > $THISDIR/db_backup.gz
echo 'purge master logs before date_sub(now(), interval 7 day);' | mariadb

See also the official mariadb-dump page in the MariaDB manuals.

Holland Backup

A python-based software package named Holland Backup is available in AUR to automate all of the backup work. It supports direct mysqldump, LVM snapshots to tar files (mysqllvm), LVM snapshots with mysqldump (mysqldump-lvm), and xtrabackup methods to extract the data. The Holland framework supports a multitude of options and is highly configurable to address almost any backup situation.

The main hollandAUR and holland-commonAUR packages provide the core framework; one of the sub-packages (holland-mysqldumpAUR, holland-mysqllvmAUR and/or holland-xtrabackupAUR must be installed for full operation. Example configurations for each method are in the /usr/share/doc/holland/examples/ directory and can be copied to /etc/holland/backupsets/, as well as using the holland mk-config command to generate a base configuration for a named provider.

故障排除

執行 mysql_upgrade 後 MySQL 不能啟動

試試安全模式下運行的 MySQL:

# mariadbd-safe --datadir=/var/lib/mysql/

然後再運行:

# mariadb-upgrade -u root -p

重置 root 密碼

  1. 停止 mariadb.service.
  2. 用安全方式啟動服務:
    # mariadbd-safe --skip-grant-tables --skip-networking &
  3. 連接伺服器:
    # mariadb -u root
  4. 修改 root 密碼:
    MariaDB [mysql]> FLUSH PRIVILEGES;
    MariaDB [mysql]> ALTER USER 'root'@'localhost' IDENTIFIED BY 'new_password';
    MariaDB [mysql]> exit
    
  5. 停掉 mariadbd* 進程:
    # kill $(cat /var/lib/mysql/$HOSTNAME.pid)
  6. 啟動 mariadb.service.

檢查並修復所有數據表

檢查並自動修復所有資料庫中的所有表,查看更多

# mariadb-check -A --auto-repair -u root -p

優化所有數據表

強制優化所有數據表,自動修復可能出現的數據表錯誤

# mariadb-check -A --auto-repair -f -o -u root -p

OS error 22 when running on ZFS

如果您正在使用 ZFS 並且遇見了如下錯誤

InnoDB: Operating system error number 22 in a file operation.

那麼就需要修改 /etc/mysql/my.cnf 中的設置來禁用 aio_writes

[mariadb]
...
innodb_use_native_aio = 0

無法通過命令行登錄, 但是 phpmyadmin 正常工作

當使用了超長 (>70-75) 的密碼後,這個問題有可能發生。 mariadb 的命令行不能在 readline 模式中處理那麼多的字符。 所以如果打算使用推薦的密碼輸入方式:

$ mariadb -u user -p
Password:

不妨考慮更換一個長度短一點的密碼。

注意: 您依然可以通過在命令行參數中指定密碼來登錄
$ mysql -u <user> -p"some-very-strong-password"
警告: 但這樣做很危險,因為您的密碼很可能會洩漏到某個地方,例如,日誌。只有當遇到緊急情況才能考慮這麼做,並且事後不要忘記更改密碼。

MySQL 日誌文件占用太多空間

默認情況下, mysqld 會在 /var/lib/mysql 下創建二進位日誌文件。這在某些場景下是很有用的。但是這些日誌文件也可能耗光您的硬碟空間。如果需要,您可以在 /etc/mysql/my.cnf 中注釋掉以下兩行來禁用日誌:

#log-bin=mysql-bin
#binlog_format=mixed

或者限制 logfile 的大小:

OpenRC fails to start MariaDB

To use MariaDB with OpenRC you need to add the following lines to the [mariadb] section in the MySQL configuration file, located at /etc/my.cnf.d/my.cnf.

user = mysql
basedir = /usr
datadir = /var/lib/mysql
pid-file = /run/mysqld/mysql.pid

You should now be able to start MariaDB using:

# rc-service mysql start

Specified key was too long

See #Increase character limit.

Changed limits warning on max_open_files/table_open_cache

Increase the number of file descriptors by creating a systemd drop-in, e.g.:

/etc/systemd/system/mariadb.service.d/limit_nofile.conf
[Service]
LimitNOFILE=8192

10.4 to 10.5 upgrade crash: "InnoDB: Upgrade after a crash is not supported. The redo log was created with MariaDB 10.4.x"

Before MariaDB 10.5, redo log was unnecessarily split into multiple files.[5]

Do NOT ever remove the old binary logs /var/lib/mysql/ib_logfile* out of the way.

To resolve this, install MariaDB 10.4. Start it and let it undergo a clean shutdown. After that happens you can upgrade to 10.5 again. Same applies if another version of MariaDB was specified.

Table 'mysql.xxx' does not exist in engine

Symptom: When running mariadb-upgrade or mariadb-check, it return one or more error like these:

Table 'mysql.xxx' does not exist in engine

Where "xxx" usually is the system table inside the mysql database.

Steps to fix this,

  1. Create backup directory outside of MariaDB ${DATADIR}</nowiki>, for example in $HOME/mariadb_backup.
  2. Copy the offending files from ${DATADIR}/mysql/xxx.{frm,ibd}</nowiki> to backup directory. The xxx.ibd may not exist.
  3. Drop the tables with DROP TABLE mysql.xxx on the mariadb prompt.
  4. Run the mariadb-check. On success, the file xxx.frm and xxx.ibd should be created again.
  5. Re-run mariadb-upgrade if necessary. You may need the --force option.
expire_logs_days = 10
max_binlog_size  = 100M

另外,您也可以執行以下命令來清除 /var/lib/mysql 裡的一些日誌文件來釋放硬碟空間:

#mysql -u root -p"PASSWORD" -e "PURGE BINARY LOGS TO 'mysql-bin.0000xx';"

更多資源