如何在CentOS 9上安装和配置Remi源的MariaDB?
美国、香港服务器
如何在CentOS 9上安装和配置Remi源的MariaDB?
05-28 来源:
一、先确认 & 准备环境
1. 卸载系统自带 MariaDB(避免冲突)
bash
运行
# 检查是否已装
rpm -qa | grep -i mariadb
# 卸载(有就执行)
sudo dnf remove -y mariadb*
2. 启用基础源(CRB + EPEL + Remi)
如果之前没装 Remi,先执行:
bash
运行
# 1. 启用 CRB
sudo dnf config-manager --set-enabled crb
# 2. 安装 EPEL
sudo dnf install -y epel-release epel-next-release
# 3. 安装 Remi 源(CentOS 9)
sudo dnf install -y https://mirrors.aliyun.com/remi/enterprise/remi-release-9.rpm
# 4. 替换为阿里镜像(加速)
sudo sed -i "s/http:\/\/rpms.remirepo.net/https:\/\/mirrors.aliyun.com\/remi/g" /etc/yum.repos.d/remi*
sudo sed -i "s/#baseurl/baseurl/g" /etc/yum.repos.d/remi*
sudo sed -i "s|^mirrorlist|#mirrorlist|" /etc/yum.repos.d/remi*
# 5. 刷新缓存
sudo dnf clean all
sudo dnf makecache
3. 查看 Remi 提供的 MariaDB 版本
bash
运行
dnf --enablerepo=remi list mariadb-server
会看到 remi 源里的 10.6/10.11/11.4 等新版。
二、安装 Remi 源的 MariaDB
1. 安装(默认装 remi 里最新稳定版,如 10.11)
bash
运行
sudo dnf install -y --enablerepo=remi \
mariadb-server \
mariadb \
php-mysqlnd # 如需 PHP 连接
2. 锁定版本(防止自动升级到更高版)
bash
运行
sudo dnf versionlock add mariadb*
三、启动 & 设置开机自启
bash
运行
# 启动
sudo systemctl start mariadb
# 开机自启
sudo systemctl enable mariadb
# 查看状态(显示 active 即成功)
systemctl status mariadb
四、安全初始化(必须做!)
bash
运行
sudo mysql_secure_installation
按提示操作(默认无密码,直接回车):
Set root password? → Y → 设强密码(如 Root@123)
Remove anonymous users? → Y
Disallow root login remotely? → 生产环境 Y,开发可 N
Remove test database and access to it? → Y
Reload privilege tables now? → Y
五、配置远程连接(可选)
1. 授权远程用户(允许 192.168.1.0/24 网段)
bash
运行
# 登录 MySQL
mysql -u root -p
# 执行授权(替换密码和网段)
CREATE USER "root"@"192.168.1.%" IDENTIFIED BY "你的强密码";
GRANT ALL PRIVILEGES ON *.* TO "root"@"192.168.1.%";
FLUSH PRIVILEGES;
EXIT;
2. 放行防火墙(3306 端口)
bash
运行
sudo firewall-cmd --add-port=3306/tcp --permanent
sudo firewall-cmd --reload
3. 配置监听所有地址(bind-address)
bash
运行
sudo vi /etc/my.cnf.d/mariadb-server.cnf
修改 / 添加:
ini
[mysqld]
bind-address = 0.0.0.0
重启生效:
bash
运行
sudo systemctl restart mariadb
六、验证版本 & 连接
bash
运行
# 查看版本(显示 remi 源的版本号)
mysql -V
# 本地连接测试
mysql -u root -p
三二互联专业提供香港VPS,美国VPS主机,香港云服务器租用等业务香港美国到大陆CN2 GIA速度最快