在Centos 7上安装MariaDB

MariaDB是MySQL关系数据库管理系统的社区开发的,商业支持的分支,旨在保留GNU GPL下的免费和开源软件。开发由MySQL的某些原始开发人员领导,由于担心被Oracle Corporation收购,他们将其分叉。它旨在替代MySQL,使用一些引用MySQL的命令,并且是CentOS 7上的默认软件包。

在本教程中,我们将说明如何在CentOS 7服务器上安装最新版本的MariaDB。MariaDB是首选软件包,应该可以代替MySQL无缝运行。

第1步-安装MariaDB

我们将使用Yum安装MariaDB软件包,在出现提示以确认我们希望继续时按y键:

# yum install mariadb-server

输出:

================================================================================
Package                      Arch        Version               Repository          Size
================================================================================

Installing:
mariadb-server               x86_64      1:5.5.60-1.el7_5      base       11 M
Installing for dependencies:
mariadb                      x86_64      1:5.5.60-1.el7_5      base      8.9 M
perl-Compress-Raw-Bzip2      x86_64      2.061-3.el7           base       32 k
perl-Compress-Raw-Zlib       x86_64      1:2.061-4.el7         base       57 k
perl-DBD-MySQL               x86_64      4.023-6.el7           base      140 k
perl-DBI                     x86_64      1.627-4.el7           base      802 k
perl-Data-Dumper             x86_64      2.145-3.el7           base       47 k
perl-IO-Compress             noarch      2.061-2.el7           base      260 k
perl-Net-Daemon              noarch      0.48-5.el7            base       51 k
perl-PlRPC                   noarch      0.2020-14.el7         base       36 k
Transaction Summary
================================================================================
Installed:  mariadb-server.x86_64 1:5.5.60-1.el7_5                                                                                                            
Dependency Installed:
  mariadb.x86_64 1:5.5.60-1.el7_5              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-6.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               
Complete!

现在让我们启动MariaDB数据库

systemctl start mariadb

systemctl不会显示所有服务管理命令的结果。为了确保我们成功,我们将使用以下命令:

systemctl status mariadb

如果MariaDB已成功启动,则输出应包含“ Active:活动(运行) `

● mariadb.service - MariaDB database server
Loaded: loaded (/usr/lib/systemd/system/mariadb.service; disabled; vendor preset: disabled)
Active: active (running) since Mon 2019-03-18 11:01:12 IST; 7s ago

现在,我们将使用systemctl enable命令创建必要的符号链接,以确保MariaDB在启动时启动。

systemctl enable mariadb

创建了从/etc/systemd/system/multi-user.target.wants/mariadb.service到/usr/lib/systemd/system/mariadb.service的符号链接。

第2步-保护MariaDB服务器

MariaDB包含一个安全脚本,用于更改一些不太安全的默认选项,例如远程root登录和样本用户。使用此命令运行安全脚本:

 # mysql_secure_installation

该脚本为每个步骤提供了详细的说明。它首先提示输入root密码,在本例中尚未设置该密码,因为我们已经全新安装了MariaDB,因此将按其建议按ENTER键。接下来,将提示我们设置该根密码,我们将这样做。

然后,我们将接受所有安全建议,方法是按Y,然后按ENTER键以显示其余提示,这将删除匿名用户,禁止远程root登录,删除测试数据库并重新加载特权表。

# mysql_secure_installation

输出如下

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 authorisation.
Set root password? [Y/n] y
New password:
Re-enter new password:
Password updated successfully!
Reloading 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
  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
This is also intended only for testing, and should be removed before moving into a production environment.

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.
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现在应该是安全的。 

步骤3:创建一个新的MariaDB数据库

在下面的示例中,我们将创建一个名为“ testdb”的演示数据库。

首先,我们将登录MariaDB

mysql -u root -p

这将要求您输入密码。输入正确的密码后,将显示一个欢迎屏幕

输出:

欢迎使用MariaDB监视器。命令以;结尾; 或\ g。
您的MariaDB连接ID是2
服务器版本:5.5.60-MariaDB MariaDB服务器

版权所有(c)2000、2018 ,Oracle,MariaDB Corporation Ab等。

输入“帮助;” 或’\ h’寻求帮助。键入“ \ c”以清除当前输入语句。

 

现在,让我们创建一个测试数据库

MariaDB [(无)]>创建数据库testdb;

输出量

查询正常,1行受影响(0.00秒)

在这里,“ testdb”是我们创建的数据库的名称。

步骤4:在MariaDB数据库中创建新用户

MariaDB [(无)]>创建由“密码”标识的用户“ testuser”;

在此,用户名是“ testuser”,分配给它的密码是“ password”

步骤5:授予用户权限

授予访问和使用MariaDB服务器的权限。

MariaDB [(none)]>  GRANT USAGE ON *.* TO 'testuser'@'123.123.123.123' IDENTIFIED BY 'mypassword';

上面的命令仅允许从某个IP地址进行访问(这是一种非常安全且通用的配置)

注意:如果要限制用户对特定数据库的访问,则需要提及数据库名称,以代替’ *。* ‘

MariaDB [(none)]>  GRANT USAGE ON `testdb`.* TO 'testuser'@'123.123.123.123' IDENTIFIED BY 'password';

要允许从网络上的任何其他计算机访问MariaDB服务器,可以使用’%’符号

MariaDB [(none)]> GRANT USAGE ON *.* TO 'testuser'@'%' IDENTIFIED BY 'password';

在本教程中,我们已在CentOS 7服务器上安装并保护了MariaDB,还学习了如何在MariaDB中创建数据库和用户。