在CentOS系统中安装MariaDB数据库是一个常见的任务,特别是在需要稳定、开源且具有良好社区支持的MySQL替代品时,以下是在CentOS 7和CentOS 8系统上安装MariaDB 10.5的详细步骤:
CentOS 8 系统安装 MariaDB 10.5

| 步骤 | 说明 |
| 1 | 添加 MariaDB YUM 存储库 运行以下命令将 MariaDB 提供的 YUM 存储库添加到 CentOS 8 系统。 sudo tee /etc/yum.repos.d/mariadb.repo<通过更新缓存来确认存储库正在工作。 sudo yum makecache列出可用的存储库。 sudo yum repolist |
| 2 | 安装 MariaDB 10.5 运行以下命令安装 MariaDB 服务器和客户端。 sudo yum install MariaDBserver MariaDBclient |
| 3 | 启动 MariaDB 服务 使用 systemctl 命令启动 MariaDB 服务。 sudo systemctl start mariadb要使服务器重新启动时能够启动服务。 sudo systemctl enable mariadb检查服务状态。 systemctl status mariadb |
| 4 | 配置 MariaDB 运行以下命令进行安全配置。 mysql_secure_installation设置 root 用户密码。 输入新密码并确认 删除匿名用户。 禁止 root 远程登录。 删除 test 数据库及其访问权限。 重新加载权限表。 |
| 5 | 配置字符集 编辑 /etc/my.cnf.d/mariadbserver.cnf 文件。在 [mysqld] 标签下添加以下内容。 init_connect='SET collation_connection = utf8_unicode_ci'init_connect='SET NAMES utf8'charactersetserver=utf8collationserver=utf8_unicode_ci跳过字符集客户端握手。 skipcharactersetclienthandshake重启 MariaDB 服务以应用更改。 systemctl restart mariadb |
| 6 | 添加用户并授予权限 创建新用户并授权。 create user 'username'@'localhost' identified by 'password';grant all on *.* to 'username'@'localhost';授予外网登录权限。 grant all privileges on *.* to 'username'@'%' identified by 'password';授予权限并且可以授权。 grant all privileges on *.* to 'username'@'hostname' identified by 'password' with grant option; |
CentOS 7 系统安装 MariaDB 10.5
| 步骤 | 说明 |
| 1 | 添加 MariaDB YUM 存储库 运行以下命令将 MariaDB 提供的 YUM 存储库添加到 CentOS 7 系统。 sudo tee /etc/yum.repos.d/MariaDB.repo<通过更新缓存来确认存储库正在工作。 sudo yum makecache列出可用的存储库。 sudo yum repolist |
| 2 | 安装 MariaDB 10.5 运行以下命令安装 MariaDB 服务器和客户端。 sudo yum install MariaDBserver MariaDBclient y |
| 3 | 启动 MariaDB 服务 使用 systemctl 命令启动 MariaDB 服务。 sudo systemctl start mariadb要使服务器重新启动时能够启动服务。 sudo systemctl enable mariadb检查服务状态。 systemctl status mariadb |
| 4 | 配置 MariaDB 运行以下命令进行安全配置。 mysql_secure_installation设置 root 用户密码。 输入新密码并确认 删除匿名用户。 禁止 root 远程登录。 删除 test 数据库及其访问权限。 重新加载权限表。 |
| 5 | 配置字符集 编辑 /etc/my.cnf.d/mariadbserver.cnf 文件。在 [mysqld] 标签下添加以下内容。 init_connect='SET collation_connection = utf8_unicode_ci'init_connect='SET NAMES utf8'charactersetserver=utf8collationserver=utf8_unicode_ci跳过字符集客户端握手。 skipcharactersetclienthandshake重启 MariaDB 服务以应用更改。 systemctl restart mariadb |
| 6 | 添加用户并授予权限 创建新用户并授权。 create user 'username'@'localhost' identified by 'password';grant all on *.* to 'username'@'localhost';授予外网登录权限。 grant all privileges on *.* to 'username'@'%' identified by 'password';授予权限并且可以授权。 grant all privileges on *.* to 'username'@'hostname' identified by 'password' with grant option; |
FAQs
1、问题:如何在 MariaDB 中设置字符集?
解答:在/etc/my.cnf.d/mariadbserver.cnf 文件中的 [mysqld] 标签下添加以下内容:
init_connect='SET collation_connection = utf8_unicode_ci'
init_connect='SET NAMES utf8'
charactersetserver=utf8
collationserver=utf8_unicode_ci
skipcharactersetclienthandshake 然后重启 MariaDB 服务以应用更改:systemctl restart mariadb。
2、问题:如何添加用户并授予权限?

解答:使用以下命令创建用户并授予权限:
create user 'username'@'localhost' identified by 'password';
grant all on *.* to 'username'@'localhost';
grant all privileges on *.* to 'username'@'%' identified by 'password';
grant all privileges on *.* to 'username'@'hostname' identified by 'password' with grant option;
