目前的CentOS7默认可以安装MarialDB,本文主要针对默认版本。

1、如果要安装最新的版本,需要自己设置安装源

rpm -qa |grep mariadb
yum remove mysql mysql-server mysql-libs compat-mysql51
rpm -e --nodeps mariadb-*

vim /etc/yum.repos.d/MariaDB.repo
# MariaDB 10.1 CentOS repository list - created 2017-02-15 20:31 UTC
# http://downloads.mariadb.org/mariadb/repositories/
[mariadb]
name = MariaDB
baseurl = http://yum.mariadb.org/10.1/centos7-amd64
gpgkey=https://yum.mariadb.org/RPM-GPG-KEY-MariaDB
gpgcheck=1

2、开始安装

yum -y install mariadb-server
# 或 
yum -y install MariaDB-server MariaDB-client

安装执行

yum -y install mariadb-server
Loaded plugins: fastestmirror, langpacks
epel                                                                                | 4.7 kB  00:00:00     
extras                                                                              | 3.4 kB  00:00:00     
os                                                                                  | 3.6 kB  00:00:00     
updates                                                                             | 3.4 kB  00:00:00     
(1/7): epel/7/x86_64/group_gz                                                       |  88 kB  00:00:00     
(2/7): epel/7/x86_64/updateinfo                                                     | 951 kB  00:00:00     
(3/7): extras/7/x86_64/primary_db                                                   | 156 kB  00:00:00     
(4/7): os/7/x86_64/group_gz                                                         | 166 kB  00:00:00     
(5/7): updates/7/x86_64/primary_db                                                  | 1.3 MB  00:00:00     
(6/7): os/7/x86_64/primary_db                                                       | 6.0 MB  00:00:00     
(7/7): epel/7/x86_64/primary_db                                                     | 6.6 MB  00:00:01     
Determining fastest mirrors
Resolving Dependencies
--> Running transaction check
---> Package mariadb-server.x86_64 1:5.5.60-1.el7_5 will be installed
--> Processing Dependency: mariadb-libs(x86-64) = 1:5.5.60-1.el7_5 for package: 1:mariadb-server-5.5.60-1.el7_5.x86_64
--> Processing Dependency: mariadb(x86-64) = 1:5.5.60-1.el7_5 for package: 1:mariadb-server-5.5.60-1.el7_5.x86_64
--> Processing Dependency: perl-DBI for package: 1:mariadb-server-5.5.60-1.el7_5.x86_64
--> Processing Dependency: perl-DBD-MySQL for package: 1:mariadb-server-5.5.60-1.el7_5.x86_64
--> Processing Dependency: perl(Data::Dumper) for package: 1:mariadb-server-5.5.60-1.el7_5.x86_64
--> Processing Dependency: perl(DBI) for package: 1:mariadb-server-5.5.60-1.el7_5.x86_64
--> Running transaction check
---> Package mariadb.x86_64 1:5.5.60-1.el7_5 will be installed
---> Package mariadb-libs.x86_64 1:5.5.52-1.el7 will be updated
---> Package mariadb-libs.x86_64 1:5.5.60-1.el7_5 will be an update
---> Package perl-DBD-MySQL.x86_64 0:4.023-6.el7 will be installed
---> Package perl-DBI.x86_64 0:1.627-4.el7 will be installed
--> Processing Dependency: perl(RPC::PlServer) >= 0.2001 for package: perl-DBI-1.627-4.el7.x86_64
--> Processing Dependency: perl(RPC::PlClient) >= 0.2000 for package: perl-DBI-1.627-4.el7.x86_64
---> Package perl-Data-Dumper.x86_64 0:2.145-3.el7 will be installed
--> Running transaction check
---> Package perl-PlRPC.noarch 0:0.2020-14.el7 will be installed
--> Processing Dependency: perl(Net::Daemon) >= 0.13 for package: perl-PlRPC-0.2020-14.el7.noarch
--> Processing Dependency: perl(Net::Daemon::Test) for package: perl-PlRPC-0.2020-14.el7.noarch
--> Processing Dependency: perl(Net::Daemon::Log) for package: perl-PlRPC-0.2020-14.el7.noarch
--> Processing Dependency: perl(Compress::Zlib) for package: perl-PlRPC-0.2020-14.el7.noarch
--> Running transaction check
---> Package perl-IO-Compress.noarch 0:2.061-2.el7 will be installed
--> Processing Dependency: perl(Compress::Raw::Zlib) >= 2.061 for package: perl-IO-Compress-2.061-2.el7.noarch
--> Processing Dependency: perl(Compress::Raw::Bzip2) >= 2.061 for package: perl-IO-Compress-2.061-2.el7.noarch
---> Package perl-Net-Daemon.noarch 0:0.48-5.el7 will be installed
--> Running transaction check
---> Package perl-Compress-Raw-Bzip2.x86_64 0:2.061-3.el7 will be installed
---> Package perl-Compress-Raw-Zlib.x86_64 1:2.061-4.el7 will be installed
--> Finished Dependency Resolution

Dependencies Resolved

===========================================================================================================
Package                             Arch               Version                       Repository      Size
===========================================================================================================
Installing:
mariadb-server                      x86_64             1:5.5.60-1.el7_5              os              11 M
Installing for dependencies:
mariadb                             x86_64             1:5.5.60-1.el7_5              os             8.9 M
perl-Compress-Raw-Bzip2             x86_64             2.061-3.el7                   os              32 k
perl-Compress-Raw-Zlib              x86_64             1:2.061-4.el7                 os              57 k
perl-DBD-MySQL                      x86_64             4.023-6.el7                   os             140 k
perl-DBI                            x86_64             1.627-4.el7                   os             802 k
perl-Data-Dumper                    x86_64             2.145-3.el7                   os              47 k
perl-IO-Compress                    noarch             2.061-2.el7                   os             260 k
perl-Net-Daemon                     noarch             0.48-5.el7                    os              51 k
perl-PlRPC                          noarch             0.2020-14.el7                 os              36 k
Updating for dependencies:
mariadb-libs                        x86_64             1:5.5.60-1.el7_5              os             758 k

Transaction Summary
===========================================================================================================
Install  1 Package  (+9 Dependent packages)
Upgrade             ( 1 Dependent package)

Total download size: 22 M
Downloading packages:
Delta RPMs disabled because /usr/bin/applydeltarpm not installed.
(1/11): mariadb-libs-5.5.60-1.el7_5.x86_64.rpm                                      | 758 kB  00:00:00     
(2/11): mariadb-5.5.60-1.el7_5.x86_64.rpm                                                                                                            | 8.9 MB  00:00:00     
(3/11): perl-Compress-Raw-Bzip2-2.061-3.el7.x86_64.rpm                                                                                               |  32 kB  00:00:00     
(4/11): perl-Compress-Raw-Zlib-2.061-4.el7.x86_64.rpm                                                                                                |  57 kB  00:00:00     
(5/11): perl-DBD-MySQL-4.023-6.el7.x86_64.rpm                                                                                                        | 140 kB  00:00:00     
(6/11): perl-DBI-1.627-4.el7.x86_64.rpm                                                                                                              | 802 kB  00:00:00     
(7/11): perl-Data-Dumper-2.145-3.el7.x86_64.rpm                                                                                                      |  47 kB  00:00:00     
(8/11): perl-IO-Compress-2.061-2.el7.noarch.rpm                                                                                                      | 260 kB  00:00:00     
(9/11): perl-Net-Daemon-0.48-5.el7.noarch.rpm                                                                                                        |  51 kB  00:00:00     
(10/11): perl-PlRPC-0.2020-14.el7.noarch.rpm                                                                                                         |  36 kB  00:00:00     
(11/11): mariadb-server-5.5.60-1.el7_5.x86_64.rpm                                                                                                    |  11 MB  00:00:01     
----------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Total                                                                                                                                        15 MB/s |  22 MB  00:00:01     
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
  Updating   : 1:mariadb-libs-5.5.60-1.el7_5.x86_64                                                                                                                    1/12
  Installing : perl-Data-Dumper-2.145-3.el7.x86_64                                                                                                                     2/12
  Installing : 1:mariadb-5.5.60-1.el7_5.x86_64                                                                                                                         3/12
  Installing : 1:perl-Compress-Raw-Zlib-2.061-4.el7.x86_64                                                                                                             4/12
  Installing : perl-Net-Daemon-0.48-5.el7.noarch                                                                                                                       5/12
  Installing : perl-Compress-Raw-Bzip2-2.061-3.el7.x86_64                                                                                                              6/12
  Installing : perl-IO-Compress-2.061-2.el7.noarch                                                                                                                     7/12
  Installing : perl-PlRPC-0.2020-14.el7.noarch                                                                                                                         8/12
  Installing : perl-DBI-1.627-4.el7.x86_64                                                                                                                             9/12
  Installing : perl-DBD-MySQL-4.023-6.el7.x86_64                                                                                                                      10/12
  Installing : 1:mariadb-server-5.5.60-1.el7_5.x86_64                                                                                                                 11/12
  Cleanup    : 1:mariadb-libs-5.5.52-1.el7.x86_64                                                                                                                     12/12
  Verifying  : 1:mariadb-server-5.5.60-1.el7_5.x86_64                                                                                                                  1/12
  Verifying  : perl-Compress-Raw-Bzip2-2.061-3.el7.x86_64                                                                                                              2/12
  Verifying  : perl-Net-Daemon-0.48-5.el7.noarch                                                                                                                       3/12
  Verifying  : perl-Data-Dumper-2.145-3.el7.x86_64                                                                                                                     4/12
  Verifying  : perl-DBD-MySQL-4.023-6.el7.x86_64                                                                                                                       5/12
  Verifying  : 1:mariadb-libs-5.5.60-1.el7_5.x86_64                                                                                                                    6/12
  Verifying  : 1:perl-Compress-Raw-Zlib-2.061-4.el7.x86_64                                                                                                             7/12
  Verifying  : 1:mariadb-5.5.60-1.el7_5.x86_64                                                                                                                         8/12
  Verifying  : perl-DBI-1.627-4.el7.x86_64                                                                                                                             9/12
  Verifying  : perl-IO-Compress-2.061-2.el7.noarch                                                                                                                    10/12
  Verifying  : perl-PlRPC-0.2020-14.el7.noarch                                                                                                                        11/12
  Verifying  : 1:mariadb-libs-5.5.52-1.el7.x86_64                                                                                                                     12/12

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   

Dependency Updated:
  mariadb-libs.x86_64 1:5.5.60-1.el7_5                                                                                                                                      

Complete!

安装完成MariaDB,首先启动MariaDB

systemctl start mariadb

设置为开启启动

tsystemctl enable mariadb
Created symlink from /etc/systemd/system/multi-user.target.wants/mariadb.service to /usr/lib/systemd/system/mariadb.service.

3、初始化数据。接下来进行MariaDB的相关简单配置:mysql_secure_installation

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.

#设置密码,是否设置root用户密码,输入Y并回车或直接回车
Set root password? [Y/n] y
#设置root用户的密码
New password:
#再输入一次你设置的密码
Re-enter new password:

#root密码设置成功
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.

#是否删除匿名用户,输入Y并回车
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.

#是否禁止root远程登录,输入n并回车
Disallow root login remotely? [Y/n] n
... skipping.

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.

#是否删除test数据库,输入n并回车
Remove test database and access to it? [Y/n] n
... skipping.

Reloading the privilege tables will ensure that all changes made so far
will take effect immediately.

#是否重新加载权限表,回车
Reload privilege tables now? [Y/n]
... 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!

操作执行到这里,数据库的初始化已基本完成。现在测试登录

mysql -h127.0.01 -uroot -p
Enter password:
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 8
Server version: 5.5.60-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.

MariaDB [(none)]> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| test               |
+--------------------+
4 rows in set (0.00 sec)

4、配置数据库字符集

1)编辑文件/etc/my.cnf

vim /etc/my.cnf
#在[mysqld]标签下添加
init_connect='SET collation_connection = utf8_unicode_ci'
init_connect='SET NAMES utf8'
character-set-server=utf8
collation-server=utf8_unicode_ci
skip-character-set-client-handshake
#端口
port=3317

2)编辑文件/etc/my.cnf.d/client.cnf

vim /etc/my.cnf.d/client.cnf
#在[client]中添加
default-character-set=utf8

3)编辑文件/etc/my.cnf.d/mysql-clients.cnf

vim /etc/my.cnf.d/mysql-clients.cnf
#在[mysql]中添加
default-character-set=utf8

全部配置完成,重启mariadb

systemctl restart mariadb

 

登陆数据库,查看配置后的字符集信息

MariaDB [(none)]> show variables like "%character%";
+--------------------------+----------------------------+
| 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)

MariaDB [(none)]> show variables like "%collation%";
+----------------------+-----------------+
| 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)

MariaDB [(none)]> select version();
+----------------+
| version()      |
+----------------+
| 5.5.60-MariaDB |
+----------------+
1 row in set (0.00 sec)

 

5、添加用户,设置权限

创建用户:

#创建用户命令:用户名 kftest,密码1234567
create user kftest@localhost identified by '1234567';
#直接创建用户并授权的命令
grant all on *.* to kftest@localhost indentified by '1234567';

授权用户:

#授予外网登陆权限
grant all privileges on *.* to kftest@'%' identified by '1234567';
#授予权限并且可以授权
grant all privileges on *.* to kftest@'hostname' identified by '1234567' with grant option;

简单的用户和权限配置基本就这样了。其中只授予部分权限把 其中 all privileges或者all改为select,insert,update,delete,create,drop,index,alter,grant,references,reload,shutdown,process,file其中一部分。

6、其他
查看MariaDB启动状态:

systemctl status mariadb

systemd重载此配置文件,刚刚配置的服务需要让systemctl能识别,就必须刷新配置:

systemctl daemon-reload

如果没有权限可以使用sudo

sudo systemctl daemon-reload

停止MariaDB服务:

systemctl stop mariadb

禁止开机启动:

systemctl disable mariadb

设置防火墙远程端口

firewall-cmd --zone=public --add-port=3308/tcp --permanent

刷新重启防火墙

firewall-cmd --reload

标签: MySQL, CentOS 7.2安装MariaDB, CentOS 7.2安装MySQL, Linux安装MySQL, Linux安装MariaDB

仅有一条评论

  1. 增大网

    活到老,学到老,认真拜读咯!

添加新评论