使用yum方式在CentOS7上安装MariaDB(MySQL)数据库

使用yum方式在CentOS7上安装MariaDB(MySQL)数据库
自从MySQL被甲骨文收购过后,MariaDB数据库就变得越来越受欢迎,成为世界上最受欢迎的数据库之一。MariaDB不仅保持开源,而且还免费。它由MySQL的原班开发人员开发维护,作为MySQL的一个分支,完全兼容MySQL,包括API和命令行,因此,习惯MySQL的人很容易就能切换到MariaDB上来。现如今MariaDB已经被很多大公司使用,如Wikipedia,WordPress和Google等。

说明:默认情况下CentOS7提供的MariaDB版本为5.5,如果要安装较新的版本,如10.3,需要修改yum源,修改方式如下:

[root@localhost ~]#  vim /etc/yum.repos.d/MariaDB.repo

然后加入如下内容:

# MariaDB 10.3 CentOS repository list - created 2019-10-14 03:38 UTC
# http://downloads.mariadb.org/mariadb/repositories/
[mariadb]
name = MariaDB
baseurl = http://yum.mariadb.org/10.3/centos7-ppc64le
gpgkey=https://yum.mariadb.org/RPM-GPG-KEY-MariaDB
gpgcheck=1

更多版本可以参考官网文档:https://downloads.mariadb.org/mariadb/repositories/#distro=CentOS&distro_release=centos7-ppc64le--centos7&mirror=ulakbim&version=10.3
mariadb-yum-source

安装

[root@localhost ~]# yum install mariadb-server

启动服务

[root@localhost ~]# service mariadb start

配置

首次安装需要使用命令mysql_secure_installation进行初始化

[root@localhost ~]# 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):

首次安装,密码为空,直接回车就行,然后进行如下配置:

Set root password? [Y/n] <– 是否设置root用户密码

New password: <– 设置root用户的密码

Re-enter new password: <– 确认你设置的密码

其他配置
Remove anonymous users? [Y/n] <– 是否删除匿名用户,建议Y

Disallow root login remotely? [Y/n] <–是否禁止root远程登录

Remove test database and access to it? [Y/n] <– 是否删除test数据库

Reload privilege tables now? [Y/n] <– 是否重新加载权限表

新增用户并授权

登录数据库:

[root@localhost ~]# mysql -u root -p 
Enter password:  //输入密码,回车
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 11
Server version: 5.5.64-MariaDB MariaDB Server

Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

切换到mysql数据库,创建用户并授权

MariaDB [(none)]> use mysql //使用mysql数据库
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A

Database changed
MariaDB [mysql]>     //数据库切换到了mysql数据库

新增用户SQL格式:CREATE USER 'username'@'%' IDENTIFIED BY 'password';

说明:username指用户名, password指密码,%代表可以在任何客户端机器上使用该用户登录连接MariaDB

示例:

 MariaDB [mysql]> CREATE USER 'test'@'%' IDENTIFIED BY '123456';

grant语句授权
授权某个用户管理MariaDB中某个数据库的权限
格式为:grant all privileges on 数据库名称 to 用户名;
示例:

MariaDB [mysql]> grant all privileges on mysql to test;

授权用户管理MariaDB中所有数据库的权限
格式为:grant all on *.* to 用户名;
示例:

MariaDB [mysql]> grant all on *.* to test;

权限刷新到数据库

MariaDB [mysql]> flush privileges;­

退出MariaDB

MariaDB [mysql]> exit或Ctrl+C

重启MariaDB

[root@localhost ~]# service mariadb restart

设置开机启动

[root@localhost ~]# systemctl enable mariadb

the end

热门文章