如果有其他系統部署需求可以參考原文
https://doc.janettr.com/install/manual/
MariaDB 10 是 Ambari 及大數據平臺的常見數據庫方案。本文適配 Rocky Linux 8.10,涵蓋 MariaDB 10.11 推薦安裝、YUM
源配置、參數優化、初始化和安全設置,幫助你一步到位完成兼容性和安全性部署。
本文基于 Rocky 8.10 做的適配,其他 el8 產品(如
CentOS8/Alma8)配置和包名可能會有出入,使用時請留意版本兼容性。如遇疑難可通過 VX 或 QQ 群與作者交流。
1. 為什么選用 MariaDB 10?🤔
- 兼容性好:Ambari、Hadoop 等組件官方推薦 10.x 及以上,支持 utf8mb4,避免表結構或字符集出錯。
- 性能與安全提升:10.11 版本 InnoDB 優化、并發性能提升、慢查詢日志等企業特性更豐富。
- 主流社區活躍,國內鏡像源豐富,便于快速部署。
2. 配置阿里云 MariaDB 10.11 YUM 源
Rocky 8 官方源不自帶高版本 MariaDB,建議用阿里云或官方倉庫。
sudo tee /etc/yum.repos.d/mariadb.repo <<'EOF'
[mariadb]
name = MariaDB
baseurl = https://mirrors.aliyun.com/mariadb/yum/10.11/rocky8-amd64/
gpgkey = https://mirrors.aliyun.com/mariadb/yum/RPM-GPG-KEY-MariaDB
gpgcheck = 1
enabled = 1
module_hotfixes = 1
EOFsudo dnf clean all
sudo dnf makecache
如需最新版,可至 阿里云 MariaDB 鏡像 查詢對應 Rocky8 最新路徑。
3. 安裝 MariaDB 服務端與客戶端
先卸載老版本殘留,避免沖突:
sudo dnf remove -y mariadb mariadb-server mariadb-libs
安裝 10.11 版:
sudo dnf install -y MariaDB-server MariaDB-client
驗證版本:
mariadb --version
# mariadb Ver 15.1 Distrib 10.11.x-MariaDB, for Linux (x86_64)
4. 配置字符集、性能參數與遠程訪問
編輯 /etc/my.cnf
(如無則創建):
[client]
default-character-set=utf8mb4[mysqld]
user=mysql
port=3306
basedir=/usr
datadir=/var/lib/mysql
socket=/var/lib/mysql/mysql.sock
pid-file=/var/run/mysqld/mysqld.pidcharacter-set-server=utf8mb4
collation-server=utf8mb4_unicode_ci# InnoDB 性能優化
innodb_buffer_pool_size=1G
innodb_log_file_size=256M
innodb_flush_log_at_trx_commit=1
innodb_flush_method=O_DIRECT
innodb_thread_concurrency=8# 允許遠程連接
bind-address=0.0.0.0
skip-name-resolve
max_connections=500# 啟用慢查詢日志
slow_query_log=1
slow_query_log_file=/var/log/mysql/slow-query.log
long_query_time=2
如有業務節點多、內存充足,可適當調高 innodb_buffer_pool_size
和 max_connections
,按需調整。
5. 初始化數據庫并啟動服務
sudo mariadb-install-db --user=mysql --datadir=/var/lib/mysql
sudo systemctl restart mariadb
sudo systemctl enable mariadb
查看服務狀態:
systemctl status mariadb
6. MariaDB 安全初始化
執行安全加固腳本:
sudo mariadb-secure-installation
根據提示:
- Enter current password for root (enter for none): 回車
- Switch to unix_socket authentication [Y/n]: n
- Change the root password? [Y/n]: y(建議設定強密碼)
- Remove anonymous users? [Y/n]: y
- Disallow root login remotely? [Y/n]: n(需要遠程請選 n)
- Remove test database and access to it? [Y/n]: y
- Reload privilege tables now? [Y/n]: y
7. 配置 root 遠程訪問與業務用戶授權
登錄 MariaDB:
sudo mariadb -uroot -p
在 SQL 提示符下:
CREATE USER IF NOT EXISTS 'root'@'%' IDENTIFIED BY 'root';
GRANT ALL PRIVILEGES ON *.* TO 'root'@'%' WITH GRANT OPTION;
FLUSH PRIVILEGES;
如需 Ambari/Hive 業務庫,預建用戶:
CREATE DATABASE IF NOT EXISTS ambari;
CREATE DATABASE IF NOT EXISTS hive;
CREATE USER IF NOT EXISTS 'ambari'@'%' IDENTIFIED BY 'ambari';
CREATE USER IF NOT EXISTS 'hive'@'%' IDENTIFIED BY 'hive';
GRANT ALL PRIVILEGES ON *.* TO 'ambari'@'%' WITH GRANT OPTION;
GRANT ALL PRIVILEGES ON *.* TO 'hive'@'%' WITH GRANT OPTION;
FLUSH PRIVILEGES;
8. 測試遠程連接
在其他節點執行:
mysql -uroot -p'root' -h 192.168.3.1 -e "SELECT 1;"
出現 1
說明配置成功。