CentOS 7中使用Yum安装MariaDB数据库

  自CentOS 7.0起,CentOS中使用MariaDB代替以前版本的MySQL。MariaDB是MySQL的一个分支,由开源社区维护。MariaDB采用GPL授权许可,MariaDB的完全兼容MySQL,包括API、SQL语法、命令行,使之轻松成为MySQL代替品。
 
一、实验环境:
  • 操作系统:CentOS 7.0 64位(最小安装)
  • MariaDB版本:系统自带(mariadb-5.5.50-1.el7_2)

 
二、安装前准备:
  爱E族提醒您,安装前需检查是否已经安装了Mysql数据库或者MariaDB数据库,如果已经安装,需先卸载。并需要检查3306端口是否已经被占用:
检查是否安装mysql服务:
#检查是否安装mysql
rpm -qa|grep mysql-server
#如果已经安装,则卸载(爱E族aiezu.com)
yum remove mysql
yum remove mysql-server
检查是否已安装mariadb:
#检查是否安装mariadb
rpm -qa|grep mariadb
#如果已经安装,则卸载
yum remove mariadb-server
yum remove mariadb
检查3306端口(MariaDB服务端口)是否已被占有:
lsof -i :3306
爱E族提醒:如果未来到lsof命令,请运行“yum -y install lsof”安装“lsof”命令;
 
三、开始安装MariaDB:
1、更新系统:
安装前运行“yum -y update”更新下系统是非常有必要的,有利于减少系统漏洞。
yum -y update
2、安装mariadb数据库:
yum -y install mariadb mariadb-server
如果一切顺利,mariadb就已经安装好了,下步就是配置mariadb了(爱E族)
 
四、配置MariaDB:
1、启动MariaDB数据库:
//启动服务
service mariadb start
//查看运行状态(爱E族)
service mariadb status
2、设置MariaDB数据库的root密码:
//设置密码:输入下面命令,回车后输入两次新密码
mysqladmin password
//修改密码:输入下面命令,回车后输入旧密码,再回车输入两次新密码
mysqladmin -p password
3、还可以运行“mysql_secure_installation”配置MariaDB,运行命令后,屏幕输出以下内容,要求交互选择“Y/n”,具体的交互包括:1、输入当前密码(默认为空直接回车);2、是否修改当前密码,建议修改;3、是否删除匿名帐号,建议删除;4、是否禁止root帐号远程登录,默认只运行root本机登录,建议禁止,确保安全;5、是否删除“test”数据库,看个人自己选择;6、重新加载表权限,选Y。
/usr/bin/mysql_secure_installation:行379: find_mysql_client: 未找到命令

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(aiezu.com).

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.

You already have a root password set, so you can safely answer 'n'.

Change the root password? [Y/n] n
 ... skipping.

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, (http://aiezu.com)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...
ERROR 1008 (HY000) at line 1: Can't drop database 'test'; database doesn't exist
 ... Failed!  Not critical, keep moving...
 - 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!
 
五、登录数据库:
1、登录到mariadb数据库:
//运行下面命令,并输入刚才修改的密码
mysql -uroot -p
2、添加一个名称为"aiezu"的帐号:
--创建一个只允许本机登录的帐号“aiezu”(爱E族),其中“your_password”为帐号的密码;
--如果创建允许远程登录的帐号,请将“localhost”改成“%”
CREATE USER 'aiezu'@'localhost' IDENTIFIED BY 'your_password';
--CREATE USER 'aiezu'@'%' IDENTIFIED BY 'your_password';
3、创建一个名称为“aiezu_db”的数据库:
create database aiezu_db default character set 'utf8';
4、将库“aiezu_db”的所有权限给予用户“aiezu”:
--授权给aiezu(aiezu.com)
grant all privileges on aiezu_db.* to 'aiezu'@'localhost';
--刷新权限
flush privileges;
5、退出到bash shell,使用创建的用户“aiezu”登录到“aiezu_db”库:
//输入下面一条命令回车后输入创建用时使用的密码,这里上面使用的是“your_password”
mysql -u aiezu -p
//登录后use aiezu_db
 
六、防火墙开启数据库3306端口:
CentOS 7默认防火墙使用的是“firewalld”。运行下面代码,在防火墙“firewalld”中开启公网访问MariaDB数据库3306端口:
systemctl restart firewalld
firewall-cmd --zone=public --add-port=3306/tcp --permanent
firewall-cmd --reload

0 个评论

要回复文章请先登录注册