在Centos 7下安装MariaDB简明教程

引言: 本文将简要介绍如何在CentOS 7的环境下,安装Mariadb,配置用户和访问权限等类似操作。

  1. 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访问

>> 是否禁止 test数据库的访问

>> 是否禁用匿名用户

>> 是否重新加载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是比较容易的,但是将其配置好,并允许其正常访问确实一个繁琐的过程,希望本文中涉及到的若干技术点,大家都可以有一个直观的认知和后续的了解使用。

如果您有什么问题,还请大家给我留言。

--------------------------------------- 罪恶的分割线--------------------------------

本文系CDSN的博主《木小鱼的笔记》个人原创,如无允许,请勿转载。如要转载,请保留原始链接和原作者信息,支持原创,尊重原创,让知识的世界更美好。

作者本人也维护了一个今日头条上的头条号:程序加油站,欢迎大家关注。

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


#使用军哥的lnmp配置虚拟主机,需要注意的是要配置hosts文件(这一点官方没有讲到)具体方法:1、修改hosts文件sudo vim /etc/hosts2、在hosts文件中新增一行(这里类似于windows上配置hosts文件)127.0.0.1 www.baidu.com3、保存并重启网络
一、配置编译环境 yum update && yum upgrade yum groupinstall "Development Tools" yum install gcc gcc-c++ pcre* openssl* gd-devel* zlib-devel
一、下载repo文件 二、备份并替换系统的repo文件 三、执行yum源更新操作
LAMP -- Linux Apache MySQL PHP 在CentOS安装的顺序,我一般是Apache -> MySQL -> PHP 第一步、安装并配置Apache 1、使用yum安装 [plain] view plaincopy
一、下载nginx安装包,官网下载中心http://nginx.org/download [root@localhost software]# wget http://nginx.org/download/nginx-1.10.1.tar.gz 二、解压nginx安装包 [root@localhos
1.下载composer curl -sS https://getcomposer.org/installer | php ps:如果出现php无法运行的情况,请先把PHP加入环境变量,具体操作参考《centOS安装PHP》 2.移动或复制composer到环境下可执行 cp composer.ph
一、卸载旧版本的vim 二、安装终端字符处理库ncurses 三、下载Vim8 四、编译安装 五、添加到系统环境变量 六、安装成功
引言 我们在日常的开发过程中,很多时候需要连接服务器查看日志或者在服务器上调试代码。但是,使用 ssh 命令登录服务器每次都需要输出密码,就比较繁琐。因此我们可以使用 sshpass 通过参数指定密码进行登录。解决了这个问题之后,我发现登录服务器之后,还需要手动 cd 切换到项目目录或者日志目录。为
iptables basics Introduction iptables 是一个配置 Linux 防火墙的工具,是 netfilter 项目的一部分。 在 Linux Kernel 3.13 及以上版本中, nftables 将作为后继者取代 iptables 以成为更强大易用的防火墙工具 Bas
一、下载tengine 二、安装tenginx(nginx)的模块依赖库 三、解压并指定安装路径 四、编译安装 五、启动(tengine)nginx 六、访问(tengine)nginx
这篇文章介绍了如何快速部署一套Kubernetes集群,下面就快速开始吧! 准备工作 机器部署规划 主机 | IP | 部署组件 | | master主节点 | 192.168.199.206 | etcd、kube apiserver、kube controller manager、kube sc
一、pip下载 二、解压安装 三、安装完成
1、WebBench安装: yum install ctags(先安装依赖库) mkdir /usr/local/man (创建一个目录不然会报错) wget http://home.tiscali.cz/~cz210552/distfiles/webbench-1.5.tar.gz tar zxv
1:查看环境: [root@10-4-14-168 html]# cat /etc/redhat-release CentOS release 6.5 (Final) 2:关掉防火墙 [root@10-4-14-168 html]# chkconfig iptables off 3:配置CentOS
安装 1.检查当前是否有安装php rpm -qa|grep php 如果有安装PHP,那么请先删除这些安装包: yum remove php* 2.安装php源 Centos 5 安装php源: rpm -ivh http://mirro
一、安装 二、配置 三、修改配置文件 四、program 配置 五、启动
Sed 日常使用介绍 简介 是 环境下常用的流处理工具, 可以处理字符流, 文件或者二进制文件流。 各个 发行版都会配备 及其衍生的命令工具, 因此, 掌握该工具的基本用法, 足以满足我们日常的许多文本处理的任务。 以下采用示例的方式对 进行基本用法介绍. 如果有兴趣了解更高级的使用技巧, 本文采用
linux下开机自启: 在/etc/init.d目录下新建文件elasticsearch 并敲入shell脚本: 注意, 前两行必须填写,且要注释掉。 第一行为shell前行代码,目的告诉系统使用shell。 第二行分别代表运行级别、启动优先权、关闭优先权,且后面添加开机服务会用到。 shell脚本
1、因为在centos7中/etc/rc.d/rc.local的权限被降低了,所以需要赋予其可执行权 chmod +x /etc/rc.d/rc.local 2、赋予脚本可执行权限假设/usr/local/script/autostart.sh是你的脚本路径,给予执行权限 chmod +x /usr
最简单的查看方法可以使用ls -ll、ls-lh命令进行查看,当使用ls -ll,会显示成字节大小,而ls- lh会以KB、MB等为单位进行显示,这样比较直观一些。 通过命令du -h –max-depth=1 *,可以查看当前目录下各文件、文件夹的大小,这个比较实用。 查询当前目录总大小可以使用d