引言: 本文将简要介绍如何在CentOS 7的环境下,安装Mariadb,配置用户和访问权限等类似操作。
CentOS
Cent OS是业界中大名鼎鼎的RedHat红帽系统的开源编译版本,站在巨人的肩上是可以看得更高更远, 由于RedHat的Linux系统非常稳定和强大,且已经得到了业界的普遍认可,这一点从Red Hat的公司业绩就一目了然得感觉到。
由于Red Hat Linux系统是基于开源的Linux系统而来,故其的代码需要进行开源,所以我们才可以见得到Centos系统,其是基于Red Hat Linux开源的源代码编译而来, 目前绝大多数的业界Linux系统都是基于CentOS的衍生系统。
目前Centos的最新版本为7.2, 感兴趣的童鞋可以访问其官网: www.centos.org, 支持开源,人人受益。
2. MaraDB
MariaDB数据库管理系统是MysqL的一个分支,采用GPL授权许可 MariaDB的目的是完全兼容MysqL,包括API和命令行,使之能轻松成为MysqL的代替品;其目前版本开源,相对于Oracle旗下的MysqL而言,无法确认某一天MysqL是否会转换为商业产品,且目前这个迹象已经非常明显。在大名鼎鼎的Ubuntu Linux发行版中,已经将MysqL数据库替换为了MariaDB数据库,由此可见开源社区对Oracle不明朗态度的担忧。
MariaDB由MysqL的创始人Michael Widenius主导开发,其目前的版本为10.1/10.2 Series,兼容MysqL的5.6, 5.7版本。 简单而言,大家可以把其当作一模一样的MysqL来用,完全没有问题。当然MariaDB没有停留在MysqL的基础上,其依然开发了若干很好的特性,比如集群的解决方案,高可用性等等技术方案,感兴趣的童鞋可以访问其官网获取更多信息。
官网:www.mariadb.org
3. Yum
Yum(全称为 Yellow dog Updater,Modified)是一个在Fedora和RedHat以及CentOS中的Shell前端软件包管理器。基于RPM包管理,能够从指定的服务器自动下载RPM包并且安装,可以自动处理依赖性关系,并且一次安装所有依赖的软件包,无须繁琐地一次次下载、安装。
基本上每个Linux发行版都会开发一套自己的包管理工具,故这个也是在使用Linux之时非常让人头痛的地方,目前几个主流的包管理工具有:
>> RPM 同样来自 Red Hat的贡献
>> APT 大规模使用在Ubuntu的发行版本上
4. 安装步骤
a.1 检查当下操作系统的版本
>>> uname -a
Linux AY131203102210033c39Z 3.10.0-327.22.2.el7.x86_64 #1 SMP Thu Jun 23 17:05:11 UTC 2016 x86_64 x86_64 x86_64 GNU/Linux
>>> cat /proc/version
Linux version 3.10.0-327.22.2.el7.x86_64 (builder@kbuilder.dev.centos.org) (gcc version 4.8.3 20140911 (Red Hat 4.8.3-9) (GCC) ) #1 SMP Thu Jun 23 17:05:11 UTC 2016
>>> lsb_release
LSB Version: :core-4.1-amd64:core-4.1-noarch
针对Centos或者Red Hat的系统,可以基于如下命令来检查其版本:
>> cat /etc/redhat-release
CentOS Linux release 7.2.1511 (Core)
a.2 检查MaraiDB是否已经安装
>> yum search mariadb
[root@xxxxxx~]# yum search mariadb
Loaded plugins: fastestmirror
Loading mirror speeds from cached hostfile
* base: mirrors.aliyuncs.com
* epel: mirrors.aliyuncs.com
* extras: mirrors.aliyuncs.com
* updates: mirrors.aliyuncs.com
======================================================================================= N/S matched: mariadb ========================================================================================
mariadb-bench.x86_64 : MariaDB benchmark scripts and data
mariadb-devel.i686 : Files for development of MariaDB/MysqL applications
mariadb-devel.x86_64 : Files for development of MariaDB/MysqL applications
mariadb-embedded.i686 : MariaDB as an embeddable library
mariadb-embedded.x86_64 : MariaDB as an embeddable library
mariadb-embedded-devel.i686 : Development files for MariaDB as an embeddable library
mariadb-embedded-devel.x86_64 : Development files for MariaDB as an embeddable library
mariadb-libs.i686 : The shared libraries required for MariaDB/MysqL clients
mariadb-libs.x86_64 : The shared libraries required for MariaDB/MysqL clients
mariadb-server.x86_64 : The MariaDB server and related files
mariadb.x86_64 : A community developed branch of MysqL
mariadb-test.x86_64 : The test suite distributed with MariaD
percona-xtrabackup.x86_64 : Online backup for InnoDB/XtraDB in MysqL,Percona Server and MariaDB
Name and summary matches only,use "search all" for everything.
>> yum info mariadb
可以获取到mariadb的安装状态信息
a.3 安装
>> yum install mariadb mariadb-server
[root@xxxxx ~]# yum install mariadb mariadb-server
Loaded plugins: fastestmirror
base | 3.6 kB 00:00:00
epel | 4.3 kB 00:00:00
extras | 3.4 kB 00:00:00
updates | 3.4 kB 00:00:00
(1/2): epel/x86_64/updateinfo | 738 kB 00:00:00
(2/2): epel/x86_64/primary_db | 4.5 MB 00:00:04
Loading mirror speeds from cached hostfile
* base: mirrors.aliyuncs.com
* epel: mirrors.aliyuncs.com
* extras: mirrors.aliyuncs.com
* updates: mirrors.aliyuncs.com
Package 1:mariadb-5.5.52-1.el7.x86_64 already installed and latest version
Resolving Dependencies
--> Running transaction check
........................................................
Verifying : perl-DBD-MysqL-4.023-5.el7.x86_64 9/10
Verifying : perl-IO-Compress-2.061-2.el7.noarch 10/10
Installed:
mariadb-server.x86_64 1:5.5.52-1.el7
Dependency Installed:
libaio.x86_64 0:0.3.109-13.el7 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
Complete!
以上就安装成功了。
a.4 启动mariadb,并检查其启动状态
systemctl start mariadb
启动完成后,测试启动启动状态:
systemctl status mariadb
以下是启动过程信息,可以查看其失败的原因和成功的过程信息
● mariadb.service - MariaDB database server
Loaded: loaded (/usr/lib/systemd/system/mariadb.service; disabled; vendor preset: disabled)
Active: active (running) since Mon 2017-02-20 13:01:25 CST; 7s ago
Process: 32136 ExecStartPost=/usr/libexec/mariadb-wait-ready $MAINPID (code=exited,status=0/SUCCESS)
Process: 32056 ExecStartPre=/usr/libexec/mariadb-prepare-db-dir %n (code=exited,status=0/SUCCESS)
Main PID: 32134 (MysqLd_safe)
CGroup: /system.slice/mariadb.service
├─32134 /bin/sh /usr/bin/MysqLd_safe --basedir=/usr
└─32292 /usr/libexec/MysqLd --basedir=/usr --datadir=/var/lib/MysqL --plugin-dir=/usr/lib64/MysqL/plugin --log-error=/var/log/mariadb/mariadb.log --pid-file=/var/run/mariadb/mariadb.p...
Feb 20 13:01:21 AY131203102210033c39Z mariadb-prepare-db-dir[32056]: The latest information about MariaDB is available at http://mariadb.org/.
Feb 20 13:01:21 AY131203102210033c39Z mariadb-prepare-db-dir[32056]: You can find additional information about the MysqL part at:
Feb 20 13:01:21 AY131203102210033c39Z mariadb-prepare-db-dir[32056]: http://dev.MysqL.com
Feb 20 13:01:21 AY131203102210033c39Z mariadb-prepare-db-dir[32056]: Support MariaDB development by buying support/new features from MariaDB
Feb 20 13:01:21 AY131203102210033c39Z mariadb-prepare-db-dir[32056]: Corporation Ab. You can contact us about this at sales@mariadb.com.
Feb 20 13:01:21 AY131203102210033c39Z mariadb-prepare-db-dir[32056]: Alternatively consider joining our community based development effort:
Feb 20 13:01:21 AY131203102210033c39Z mariadb-prepare-db-dir[32056]: http://mariadb.com/kb/en/contributing-to-the-mariadb-project/
Feb 20 13:01:21 AY131203102210033c39Z MysqLd_safe[32134]: 170220 13:01:21 MysqLd_safe Logging to '/var/log/mariadb/mariadb.log'.
Feb 20 13:01:21 AY131203102210033c39Z MysqLd_safe[32134]: 170220 13:01:21 MysqLd_safe Starting MysqLd daemon with databases from /var/lib/MysqL
Feb 20 13:01:25 AY131203102210033c39Z systemd[1]: Started MariaDB database server.
看到最后一句的时候,就知道其已经正常启动了.....
另外大家也可以访问/var/log/mariadb/mariadb.log中的日志信息,非常有帮助的。
将mariadb设置为开机启动:
systemctl enable mariadb
输出信息为:
Created symlink from /etc/systemd/system/multi-user.target.wants/mariadb.service to /usr/lib/systemd/system/mariadb.service.
5. 设置Root 用户的登陆密码
MysqL_secure_installation
根据命令提示,主要是做以下几件事情:
>> 设置root密码
>> 是否禁止远程 root访问
>> 是否禁用匿名用户
>> 是否重新加载privilleges-table信息
具体的执行信息如下:
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
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.
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!
6. 创建用户以及设置权限
创建用户:
MysqL>create user username@localhost identified by 'password';
授予用户权利:
MysqL>grant all on *.* to username@localhost indentified by 'password';
授予用户访问外网的权利:
MysqL>grant all privileges on *.* to username@'%' identified by 'password';
授予权限并且可以授权
grant all privileges on *.* to username@'hostname' identified by 'password' with grant option;
刷新权限表,让各种设置立即生效。
flush privileges;
验证用户是否被正确创建, 访问MysqL.user 表,看看其中是否存在bladestone用户。
7. 配置Mariadb数据库的字符设置
>> 在/etc/my.cnf中的MysqLd标签下,新增字符设置:
init_connect='SET collation_connection = utf8_unicode_ci' init_connect='SET NAMES utf8' character-set-server=utf8
>> 在/etc/my.cnf.d/client.cnf中的client标签下,新增字符设置:
default-character-set=utf8
>> 在/etc/my.cnf.d/MysqL-clients.cnf下的[MysqLd]标签下,新增字符设置:
设置完成后,重新启动mariadb-server,将设置生效。
>> systemctl restart mariadb
>> systemctl status mariadb # 检查启动状态是否正确,详细信息参照本文中的样例
>> vi /var/log/mariadb/mariadb.log # 查看其中是否有错误信息
如果木有错误信息,则设置成功生效了。
那我们来查看一下是否真的生效了:
登陆MysqL命令行, 输入命令:
show variables like "%character%";show variables like "%collation%";输出结果信息:
MariaDB [(none)]> show variables like "%character%";show variables like "%collation%";
+--------------------------+----------------------------+
| Variable_name | Value |
+--------------------------+----------------------------+
| character_set_client | utf8 |
| character_set_connection | utf8 |
| character_set_database | utf8 |
| character_set_filesystem | binary |
| character_set_results | utf8 |
| character_set_server | utf8 |
| character_set_system | utf8 |
| character_sets_dir | /usr/share/MysqL/charsets/ |
+--------------------------+----------------------------+
8 rows in set (0.00 sec)
+----------------------+-----------------+
| Variable_name | Value |
+----------------------+-----------------+
| collation_connection | utf8_unicode_ci |
| collation_database | utf8_unicode_ci |
| collation_server | utf8_unicode_ci |
+----------------------+-----------------+
3 rows in set (0.00 sec)
8. 测试连接
在目的主机上通过MysqL的命令行是可以直接访问的,但是基于远程的主机访问模式,则无法被正确的访问和数据读取,那问题出在什么地方呢?
基于ping命令测试ip地址,可以正确访问。 基于telnet测试,结果确认估计是端口未曾打开,或者是防火墙将其进行了端口屏蔽。
故首先确认,其端口是否正常的工作:
>> netstat -anp | grep 3306
结果发现,其应用正常启动,故确认其为防火墙的问题以及规则设置。
在Centos 7中的防火墙规则为firewall-cmd, iptables在centos 7中将被替代了。
新增3306的端口开发规则:
firewall-cmd --zone=public --add-port=3306/tcp --permanen
重新加载规则:
firewall-cmd --reload
重新测试连接状态,显示 可以正确远程访问了。
9. 总结
在Cent OS上安装mariadb是比较容易的,但是将其配置好,并允许其正常访问确实一个繁琐的过程,希望本文中涉及到的若干技术点,大家都可以有一个直观的认知和后续的了解使用。
如果您有什么问题,还请大家给我留言。
--------------------------------------- 罪恶的分割线--------------------------------
@H_788_502@本文系CDSN的博主《木小鱼的笔记》个人原创,如无允许,请勿转载。如要转载,请保留原始链接和原作者信息,支持原创,尊重原创,让知识的世界更美好。@H_788_502@作者本人也维护了一个今日头条上的头条号:程序加油站,欢迎大家关注。