CentOS 7 安装配置 MySQL 及 MariaDB 替代方案
一、系统环境
执行 yum update 升级后,当前系统版本确认为 CentOS Linux release 7.1.1503 (Core)。
[root@yl-web yl]# cat /etc/redhat-release
CentOS Linux release 7.1.1503 (Core)
二、mysql安装
网络上常见的教程通常建议直接使用 yum 命令进行安装。
#yum install mysql
#yum install mysql-server
#yum install mysql-devel
在实际操作中,安装 mysql 和 mysql-devel 可以成功,但尝试安装 mysql-server 时会出现错误,具体报错信息如下所示。
[root@yl-web yl]# yum install mysql-server
Loaded plugins: fastestmirror
Loading mirror speeds from cached hostfile
* base: mirrors.sina.cn
* extras: mirrors.sina.cn
* updates: mirrors.sina.cn
No package mysql-server available.
Error: Nothing to do
经查阅资料得知,CentOS 7 版本已将 MySQL 数据库软件从默认的程序列表中移除,转而采用 mariadb 作为默认数据库。
针对这一问题,主要有两种解决途径。
1、方法一:安装mariadb
MariaDB 数据库管理系统是 MySQL 的一个分支,主要由开源社区在维护,采用 GPL 授权许可。开发这个分支的原因之一是:甲骨文公司收购了 MySQL 后,有将 MySQL 闭源的潜在风险,因此社区采用分支的方式来避开这个风险。MariaDB 的目的是完全兼容 MySQL,包括 API 和命令行,使之能轻松成为 MySQL 的代替品。
通过 yum 安装 mariadb,安装包大小约为 59 M。
[root@yl-web yl]# yum install mariadb-server mariadb
管理 mariadb 数据库的相关 systemctl 命令包括:
systemctl start mariadb #启动MariaDB
systemctl stop mariadb #停止MariaDB
systemctl restart mariadb #重启MariaDB
systemctl enable mariadb #设置开机启动
首先需要使用 systemctl start mariadb 命令启动数据库服务。
[root@yl-web yl]# systemctl start mariadb
服务启动后,即可正常使用 mysql 客户端连接数据库。
[root@yl-web yl]# mysql -u root -p
Enter password:
Welcome to the MariaDB monitor. Commands end with ; or \g.
Your MariaDB connection id is 3
Server version: 5.5.41-MariaDB MariaDB Server
Copyright (c) 2000, 2014, Oracle, MariaDB Corporation Ab and others.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
MariaDB [(none)]> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| test |
+--------------------+
4 rows in set (0.00 sec)
MariaDB [(none)]>
安装 mariadb 后,登录界面显示的提示符也是 MariaDB [(none)]> ,用户可能需要适应一下。以下是第二种安装方法。
2、方法二:官网下载安装mysql-server
# wget http://dev.mysql.com/get/mysql-community-release-el7-5.noarch.rpm
# rpm -ivh mysql-community-release-el7-5.noarch.rpm
# yum install mysql-community-server
完成安装步骤后,需要重启 mysql 服务以使配置生效。
# service mysqld restart
初次安装 mysql 后,root 账户默认没有设置密码。
[root@yl-web yl]# mysql -u root
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 3
Server version: 5.6.26 MySQL Community Server (GPL)
Copyright (c) 2000, 2026, Oracle and/or its affiliates. All rights reserved.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| test |
+--------------------+
4 rows in set (0.01 sec)
mysql>
接下来需要为 root 账户设置密码。
mysql> set password for 'root'@'localhost' =password('password');
Query OK, 0 rows affected (0.00 sec)
mysql>
设置密码后无需重启数据库即可立即生效。
在安装过程中,系统会显示已安装的组件列表,包括 mysql-community-client、mysql-community-devel、mysql-community-libs 和 mysql-community-server 等。
Installed:
mysql-community-client.x86_64 0:5.6.26-2.el7 mysql-community-devel.x86_64 0:5.6.26-2.el7
mysql-community-libs.x86_64 0:5.6.26-2.el7 mysql-community-server.x86_64 0:5.6.26-2.el7
Dependency Installed:
mysql-community-common.x86_64 0:5.6.26-2.el7
Replaced:
mariadb.x86_64 1:5.5.41-2.el7_0 mariadb-devel.x86_64 1:5.5.41-2.el7_0 mariadb-libs.x86_64 1:5.5.41-2.el7_0
mariadb-server.x86_64 1:5.5.41-2.el7_0
因此,安装完 MySQL 后,原有的 mariadb 会被自动替换并停止生效。
[root@yl-web yl]# rpm -qa |grep mariadb
[root@yl-web yl]#
三、配置mysql
1、编码
MySQL 的主配置文件位于 /etc/my.cnf。
需要在配置文件的末尾添加编码相关配置。
[mysql]
default-character-set =utf8
这里的字符编码必须和 /usr/share/mysql/charsets/Index.xml 中定义的一致。

2、远程连接设置
执行 SQL 语句将所有数据库的所有表的所有权限赋值给位于所有 IP 地址的 root 用户。
mysql> grant all privileges on *.* to root@'%'identified by 'password';
如果是为新用户授权而不是 root,则要先新建用户。
mysql>create user 'username'@'%' identified by 'password';
完成上述权限设置后,就可以进行远程连接了。
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持本站。