如何在 CentOS 7 上安装 MariaDB 10.2


本文将介绍如何在 CentOS 7/RHEL 上安装 MariaDB 10.2。这篇文章可以作为初学者的指南或参考。请注意,这也可以作为 MySQL 的替代方案。

MariaDB 的特性

  • MariaDB 是一款开源的替代型关系数据库管理软件。
  • MariaDB 具有强大的性能,快速且可扩展,并拥有丰富的存储引擎。
  • MariaDB 具有 GIS 和 JSON 等新特性。

服务器信息和软件包

  • 包含的软件包:MariaDB、MariaDB-server、MariaDB-libs。
  • 使用的守护进程名称为:mariadb。
  • 端口号:3306。
  • 配置文件路径:/etc/my.cnf。

在 CentOS 7 / RHEL 7 上安装 MariaDB 10.2

通常,MariaDB 软件包随安装介质一起在本地存储库中提供,我们可以直接安装。但是,如果需要安装最新软件包,可以通过将存储库添加到 yum 本地存储库列表来实现。我们使用以下命令和代码将存储库添加到 yum。

将 MariaDB 安装的 Repo 添加到 Yum

为 CentOS 7 64 位添加 Repo

[mariadb]
name = MariaDB
baseurl = http://yum.mariadb.org/10.2/centos7-amd64
gpgkey=https://yum.mariadb.org/RPM-GPG-KEY-MariaDB
gpgcheck=1

为 RHEL 7 64 位添加 Repo

[mariadb10]
name = MariaDB
baseurl = http://yum.mariadb.org/10.2/rhel7-amd64
gpgkey=https://yum.mariadb.org/RPM-GPG-KEY-MariaDB
gpgcheck=1

使用 Yum Repo 安装 MariaDB

我们可以使用 Yum Repo 安装 MariaDB,因为我们刚刚将存储库添加到本地 yum 中,以下是使用 Yum 安装 MariaDB 的命令

# yum groupinstall mariadb*
Loaded plugins: fastestmirror, refresh-packagekit, security
Loading mirror speeds from cached hostfile
* base: ftp.iitm.ac.in
* extras: ftp.iitm.ac.in
* updates: ftp.iitm.ac.in
....
....
.....
....
....
...
....
Downloading packages:
--------------------------------------------------------------------------------------------------------------------------------------------
Total 68 MB/s | 22 MB 00:00:00
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
Warning: RPMDB altered outside of yum.
Installing : perl-Data-Dumper-2.145-3.el7.x86_64 1/13
Installing : unixODBC-2.3.1-10.el7.x86_64 2/13
Installing : perl-Net-Daemon-0.48-5.el7.noarch 3/13
Installing : 1:perl-Compress-Raw-Zlib-2.061-4.el7.x86_64 4/13
Installing : perl-Compress-Raw-Bzip2-2.061-3.el7.x86_64 5/13
Installing : perl-IO-Compress-2.061-2.el7.noarch 6/13
Installing : perl-PlRPC-0.2020-14.el7.noarch 7/13
Installing : perl-DBI-1.627-4.el7.x86_64 8/13
Installing : perl-DBD-MySQL-4.023-5.el7.x86_64 9/13
Installing : 1:mariadb-5.5.41-2.el7_0.x86_64 10/13
Installing : 1:mariadb-server-5.5.41-2.el7_0.x86_64 11/13
Installing : mysql-connector-odbc-5.2.5-6.el7.x86_64 12/13
Installing : MySQL-python-1.2.3-11.el7.x86_64 13/13
Verifying : 1:mariadb-5.5.41-2.el7_0.x86_64 1/13
Verifying : perl-Compress-Raw-Bzip2-2.061-3.el7.x86_64 2/13
Verifying : mysql-connector-odbc-5.2.5-6.el7.x86_64 3/13
Verifying : perl-Data-Dumper-2.145-3.el7.x86_64 4/13
Verifying : MySQL-python-1.2.3-11.el7.x86_64 5/13
Verifying : 1:mariadb-server-5.5.41-2.el7_0.x86_64 6/13
Verifying : 1:perl-Compress-Raw-Zlib-2.061-4.el7.x86_64 7/13
Verifying : perl-PlRPC-0.2020-14.el7.noarch 8/13
Verifying : perl-Net-Daemon-0.48-5.el7.noarch 9/13
Verifying : perl-DBI-1.627-4.el7.x86_64 10/13
Verifying : unixODBC-2.3.1-10.el7.x86_64 11/13
Verifying : perl-DBD-MySQL-4.023-5.el7.x86_64 12/13
Verifying : perl-IO-Compress-2.061-2.el7.noarch 13/13
Installed:
MySQL-python.x86_64 0:1.2.3-11.el7 mariadb.x86_64 1:5.5.41-2.el7_0 mariadb-server.x86_64 1:5.5.41-2.el7_0
mysql-connector-odbc.x86_64 0:5.2.5-6.el7
Dependency Installed:
perl-Compress-Raw-Bzip2.x86_64 0:2.061-3.el7 perl-Compress-Raw-Zlib.x86_64 1:2.061-4.el7 perl-DBD-MySQL.x86_64 0:4.023-5.el7
perl-DBI.x86_64 0:1.627-4.el7 perl-Data-Dumper.x86_64 0:2.145-3.el7 perl-IO-Compress.noarch 0:2.061-2.el7
perl-Net-Daemon.noarch 0:0.48-5.el7 perl-PlRPC.noarch 0:0.2020-14.el7 unixODBC.x86_64 0:2.3.1-10.el7
Complete!

启动 MariaDB 服务

以下是启动 MariaDB 服务的命令

# systemctl start mariadb.services

我们需要在启动时添加服务以启动服务,可以使用以下命令在启动时启动 MariaDB 服务。

# systemctl enable mariadb.services

添加 MariaDB 端口以允许防火墙访问

# firewall-cmd –permanent –add-services=mysql
# firewall-cmd –permanent –add-port=3306/tcp
# firewall-cmd –reload

保护 mariadb 并设置 root 用户密码

默认情况下,mariadb 的 root 密码未设置,如果没有 root 密码,我们将无法登录数据库服务器。以下是设置 MariaDB 的 root 密码、删除匿名用户、通过限制 root 的远程登录来保护 MariaDB 以及删除测试数据库的命令。

# mysql_secure_installation
/usr/bin/mysql_secure_installation: line 379: find_mysql_client: command not found
NOTE: RUNNING ALL PARTS OF THIS SCRIPT IS RECOMMENDED FOR ALL MariaDB
SERVERS IN PRODUCTION USE! PLEASE READ EACH STEP CAREFULLY!
In order to log into MariaDB to secure it, we'll need the current
password for the root user. If you've just installed MariaDB, and
you haven't set the root password yet, the password will be blank,
so you should just press enter here.
Enter current password for root (enter for none):
OK, successfully used password, moving on...
Setting the root password ensures that nobody can log into the MariaDB
root user without the proper authorization.
Set root password? [Y/n] y
New password:
Re-enter new password:
Password updated successfully!
Reloading the privilege tables..
... Success!
By default, a MariaDB installation has an anonymous user, allowing anyone
to log into MariaDB without having to have a user account created for
them. This is intended only for testing, and to make the installation
go a bit smoother. You should remove them before moving into a
production environment.
Remove anonymous users? [Y/n] y
... Success!
Normally, root should only be allowed to connect from 'localhost'. This
ensures that someone cannot guess at the root password from the network.
Disallow root login remotely? [Y/n] y
... Success!
By default, MariaDB comes with a database named 'test' that anyone can
access. This is also intended only for testing, and should be removed
before moving into a production environment. Install MariaDB 10.2 RHEL 7
Remove test database and access to it? [Y/n] y
- Dropping test database...
... Success!
- Removing privileges on test database...
... Success!
Reloading the privilege tables will ensure that all changes made so far
will take effect immediately. Install MariaDB 10.2 RHEL 7
Reload privilege tables now? [Y/n] y
... Success!
Cleaning up...
All done! If you've completed all of the above steps, your MariaDB
installation should now be secure.
Thanks for using MariaDB!

测试 MariaDB 连接

完成安装并为 root 用户分配密码并保护连接后,我们需要通过登录数据库来测试 mariadb。以下是测试 MariaDB 的命令:

# mysql -uroot -p
Enter password:
Welcome to the MariaDB monitor. Commands end with; or \g.
Your MariaDB connection id is 10
Server version: 5.5.41-MariaDB MariaDB Server Install MariaDB 10.2 RHEL 7
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)]>

通过以上步骤,我们可以安装 CentOS 7/RHEL 7 上的 MariaDB 10.2,并通过删除匿名用户和测试数据库来保护 mariadb,这是一个很好的 MySQL 服务器替代方案。在以后的文章中,您将学习更多关于 MariaDB 的知识。

更新于:2019年10月18日

1K+ 浏览量

开启你的职业生涯

完成课程获得认证

开始学习
广告