随着mysql被oracle公司收购之后,越来越多的公司都把自己的数据库转移到了mariadb数据库上面,其实mysql数据库和mariadb数据库的区别只是换个名字而已,内在的东西一模一样,下面我们就来说一下在centos6和centos7上如何正确的安装mariadb吧


centos7上:

   centos7上的yum源里是自带mariadb数据库的,所以我们就给自己的虚拟机装上mariadb的服务器端和客户端,再设置一下密码就可以使用啦。

[root@J-7 ~]# yum -y groupinstall mariadb mariadb-client      #安装mariadb的服务器端和客户端
Loaded plugins: fastestmirror
There is no installed groups file.
Maybe run: yum groups mark convert (see man yum)
base                                                                    | 3.6 kB  00:00:00     
elpl                                                                    | 4.3 kB  00:00:00     
Loading mirror speeds from cached hostfile
Resolving Dependencies
--> Running transaction check
……
  Verifying  : perl-IO-Compress-2.061-2.el7.noarch                                       14/14 

Installed:
  MySQL-python.x86_64 0:1.2.5-1.el7           mariadb.x86_64 1:5.5.52-1.el7                   
  mariadb-server.x86_64 1:5.5.52-1.el7        mysql-connector-odbc.x86_64 0:5.2.5-6.el7       

Dependency Installed:
  libtool-ltdl.x86_64 0:2.4.2-21.el7_2          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             unixODBC.x86_64 0:2.3.1-11.el7                

Complete!
[root@J-7 ~]# systemctl  start mariadb.service          #开启服务
[root@J-7 ~]# systemctl  enable mariadb.service          #设置为开机启动
Created symlink from /etc/systemd/system/multi-user.target.wants/mariadb.service to /usr/lib/systemd/system/mariadb.service.
[root@J-7 ~]# ss -nutl                                             #查看一下Mariadb的3306端口开启了没有
Netid State      Recv-Q Send-Q Local Address:Port               Peer Address:Port              
udp   UNCONN     0      0                 *:12518                         *:*                  
udp   UNCONN     0      0                 *:29118                         *:*                  
udp   UNCONN     0      0                 *:68                            *:*                  
udp   UNCONN     0      0                 *:68                            *:*                  
udp   UNCONN     0      0                :::12518                        :::*                  
udp   UNCONN     0      0                :::24061                        :::*                  
tcp   LISTEN     0      50                *:3306                          *:*                  
tcp   LISTEN     0      128               *:22                            *:*                  
tcp   LISTEN     0      100       127.0.0.1:25                            *:*                  
tcp   LISTEN     0      128              :::22                           :::*                  
tcp   LISTEN     0      100             ::1:25                           :::*                  
[root@J-7 ~]# 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):         #没有密码要创建,按回车即可
ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: YES)
Enter current password for root (enter for none): 
ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: YES)
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] no                #关闭远程登录?
 ... 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!
[root@J-7 ~]# mysql -uroot -p                          #登录
Enter password:                                        #输入密码
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 12
Server version: 5.5.52-MariaDB MariaDB Server

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

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

MariaDB [(none)]>                                      #正常使用

centos6:

     centos6上默认的数据库是mysql5.5的,这里我们要安装mariadb10.0的就得自己去Mariadb数据库的官方网站http://mariadb.org  下一个,来进行安装,如果你已经安装了自己卸载干净就行,我这里环境是最小安装的,这里我以二进制编译安装为例:

[root@localhost ~]# rz                                                                     #把自己下载的包传上来

[root@localhost ~]# ls
anaconda-ks.cfg  install.log  install.log.syslog  mariadb-5.5.57-linux-x86_64.tar.gz
[root@localhost ~]# getent passwd mysql                                                    #检查系统是否已经有了mysql用户的信息
[root@localhost ~]# useradd -r -d /app/data -s /sbin/nologin -u 36 mysql                   #创建mysql用户,指定家目录和默认shell
[root@localhost ~]# id mysql                                                               #查看一下用户信息
uid=36(mysql) gid=36(mysql) groups=36(mysql)
[root@localhost ~]# ll /app/data -d                                                        #看一下我们计划存放数据库的目录信息
drwxr-xr-x 2 root root 4096 Jul 28 00:34 /app/data                           
[root@localhost ~]# chown mysql:mysql /app/data/                                           #修改目录的属组和属主
[root@localhost ~]# tar xf mariadb-5.5.57-linux-x86_64.tar.gz -C /usr/local/               #将二进制包解压放到指定位置
[root@localhost ~]# cd /usr/local/
[root@localhost local]# ll                                
total 44
drwxr-xr-x.  2 root root 4096 Sep 23  2011 bin
drwxr-xr-x.  2 root root 4096 Sep 23  2011 etc
drwxr-xr-x.  2 root root 4096 Sep 23  2011 games
drwxr-xr-x.  2 root root 4096 Sep 23  2011 include
drwxr-xr-x.  2 root root 4096 Sep 23  2011 lib
drwxr-xr-x.  2 root root 4096 Sep 23  2011 lib64
drwxr-xr-x.  2 root root 4096 Sep 23  2011 libexec
drwxr-xr-x  12 root root 4096 Jul 28 00:38 mariadb-5.5.57-linux-x86_64
drwxr-xr-x.  2 root root 4096 Sep 23  2011 sbin
drwxr-xr-x.  5 root root 4096 Jul 27 20:51 share
drwxr-xr-x.  2 root root 4096 Sep 23  2011 src
[root@localhost local]# ln -s mariadb-5.5.57-linux-x86_64/  mysql/                          #给mariadb写一个软连接,切记不要后面的/
ln: target `mysql/' is not a directory: No such file or directory
[root@localhost local]# ln -s mariadb-5.5.57-linux-x86_64/  mysql
[root@localhost local]# ll                                                                  #查看一下
total 44
drwxr-xr-x.  2 root root 4096 Sep 23  2011 bin
drwxr-xr-x.  2 root root 4096 Sep 23  2011 etc
drwxr-xr-x.  2 root root 4096 Sep 23  2011 games
drwxr-xr-x.  2 root root 4096 Sep 23  2011 include
drwxr-xr-x.  2 root root 4096 Sep 23  2011 lib
drwxr-xr-x.  2 root root 4096 Sep 23  2011 lib64
drwxr-xr-x.  2 root root 4096 Sep 23  2011 libexec
drwxr-xr-x  12 root root 4096 Jul 28 00:38 mariadb-5.5.57-linux-x86_64
lrwxrwxrwx   1 root root   28 Jul 28 00:39 mysql -> mariadb-5.5.57-linux-x86_64/
drwxr-xr-x.  2 root root 4096 Sep 23  2011 sbin
drwxr-xr-x.  5 root root 4096 Jul 27 20:51 share
drwxr-xr-x.  2 root root 4096 Sep 23  2011 src
[root@localhost local]# ll mysql/                                                           #看一下mysql文件夹的属组和属主                                                    
total 192
drwxr-xr-x  2 root root   4096 Jul 28 00:38 bin
-rw-r--r--  1 wang wang  17987 Jul 19 04:33 COPYING
drwxr-xr-x  3 root root   4096 Jul 28 00:38 data
-rw-r--r--  1 wang wang   8245 Jul 19 04:33 EXCEPTIONS-CLIENT
drwxr-xr-x  3 root root   4096 Jul 28 00:38 include
-rw-r--r--  1 wang wang   8694 Jul 19 04:33 INSTALL-BINARY
drwxr-xr-x  3 root root   4096 Jul 28 00:38 lib
drwxr-xr-x  4 root root   4096 Jul 28 00:38 man
drwxr-xr-x 11 root root   4096 Jul 28 00:37 mysql-test
-rw-r--r--  1 wang wang 108813 Jul 19 04:33 README
drwxr-xr-x  2 root root   4096 Jul 28 00:38 scripts
drwxr-xr-x 27 root root   4096 Jul 28 00:38 share
drwxr-xr-x  4 root root   4096 Jul 28 00:38 sql-bench
drwxr-xr-x  3 root root   4096 Jul 28 00:38 support-files
[root@localhost local]# chgrp -R mysql mysql/                                              #修改mysql的属组
[root@localhost local]# ll mysql/
total 192
drwxr-xr-x  2 root mysql   4096 Jul 28 00:38 bin
-rw-r--r--  1 wang mysql  17987 Jul 19 04:33 COPYING
drwxr-xr-x  3 root mysql   4096 Jul 28 00:38 data
-rw-r--r--  1 wang mysql   8245 Jul 19 04:33 EXCEPTIONS-CLIENT
drwxr-xr-x  3 root mysql   4096 Jul 28 00:38 include
-rw-r--r--  1 wang mysql   8694 Jul 19 04:33 INSTALL-BINARY
drwxr-xr-x  3 root mysql   4096 Jul 28 00:38 lib
drwxr-xr-x  4 root mysql   4096 Jul 28 00:38 man
drwxr-xr-x 11 root mysql   4096 Jul 28 00:37 mysql-test
-rw-r--r--  1 wang mysql 108813 Jul 19 04:33 README
drwxr-xr-x  2 root mysql   4096 Jul 28 00:38 scripts
drwxr-xr-x 27 root mysql   4096 Jul 28 00:38 share
drwxr-xr-x  4 root mysql   4096 Jul 28 00:38 sql-bench
drwxr-xr-x  3 root mysql   4096 Jul 28 00:38 support-files
[root@localhost local]# ll /etc/my.cnf                                                        #这个是系统自带的配置文件
-rw-r--r--. 1 root root 251 Jan 27 06:25 /etc/my.cnf
[root@localhost local]# rpm -qf /etc/my.cnf                                                  #这个配置文件来自mysql的包
mysql-libs-5.1.73-8.el6_8.x86_64
[root@localhost local]# cat /etc/my.cnf                                                       #看一下这个配置的内容
[mysqld]
datadir=/var/lib/mysql
socket=/var/lib/mysql/mysql.sock
user=mysql
# Disabling symbolic-links is recommended to prevent assorted security risks
symbolic-links=0

[mysqld_safe]
log-error=/var/log/mysqld.log
pid-file=/var/run/mysqld/mysqld.pid
[root@localhost local]# cd mysql                                                               #进入到mysql文件夹
[root@localhost mysql]# ls
bin      data               include         lib  mysql-test  scripts  sql-bench
COPYING  EXCEPTIONS-CLIENT  INSTALL-BINARY  man  README      share    support-files
[root@localhost mysql]# cd support-files/                                                      #查看一下support-files文件夹,里面全部都是模板文件
[root@localhost support-files]# ls
binary-configure  my-innodb-heavy-4G.cnf  my-small.cnf         mysql.server
magic             my-large.cnf            mysqld_multi.server  SELinux
my-huge.cnf       my-medium.cnf           mysql-log-rotate
[root@localhost support-files]# mkdir /etc/mysql                                                #创建一个存放我们自己配置的文件夹
[root@localhost support-files]# cp my-huge.cnf /etc/mysql/my.cnf                       #复制一个模板配置修改成适合自己用的
[root@localhost support-files]#  vim /etc/mysql/my.cnf
……
[mysqld]
datadir         = /app/data                                 #存放目录
innodb_file_per_table = on                                      #使用引擎(可不写)
skip_name_resolve = on                                       #禁止解析(可不写)
port            = 3306
socket          = /tmp/mysql.sock
skip-external-locking
key_buffer_size = 384M
max_allowed_packet = 1M
table_open_cache = 512
sort_buffer_size = 2M
read_buffer_size = 2M
……
[root@localhost support-files]# cd /usr/local/mysql/                                          #进入到mysql文件夹
[root@localhost mysql]# scripts/mysql_install_db  --user=mysql --datadir=/app/data/         #执行包带的脚本并指定存放地址和执行用户
Installing MariaDB/MySQL system tables in '/app/data/' ...                                 #一些关键说明
170728  4:33:26 [Warning] 'THREAD_CONCURRENCY' is deprecated and will be removed in a future release.
170728  4:33:26 [Note] ./bin/mysqld (mysqld 5.5.57-MariaDB) starting as process 1872 ...
OK
Filling help tables...
170728  4:33:27 [Warning] 'THREAD_CONCURRENCY' is deprecated and will be removed in a future release.
170728  4:33:27 [Note] ./bin/mysqld (mysqld 5.5.57-MariaDB) starting as process 1881 ...
OK

To start mysqld at boot time you have to copy
support-files/mysql.server to the right place for your system

PLEASE REMEMBER TO SET A PASSWORD FOR THE MariaDB root USER !
To do so, start the server, then issue the following commands:

'./bin/mysqladmin' -u root password 'new-password'
'./bin/mysqladmin' -u root -h localhost.localdomain password 'new-password'

Alternatively you can run:
'./bin/mysql_secure_installation'

which will also give you the option of removing the test
databases and anonymous user created by default.  This is
strongly recommended for production servers.

See the MariaDB Knowledgebase at http://mariadb.com/kb or the
MySQL manual for more instructions.

You can start the MariaDB daemon with:
cd '.' ; ./bin/mysqld_safe --datadir='/app/data/'

You can test the MariaDB daemon with mysql-test-run.pl
cd './mysql-test' ; perl mysql-test-run.pl
…………

https://mariadb.org/get-involved/

[root@localhost mysql]# ls /app/data/                                                                 #查看一下我们存放数据库的文件夹
aria_log.00000001  mysql             mysql-bin.000002  performance_schema
aria_log_control   mysql-bin.000001  mysql-bin.index   test
[root@localhost mysql]# cd support-files/
[root@localhost mysql]# pwd
/usr/local/mysql
[root@localhost mysql]# ll /app/data/
total 1084
-rw-rw---- 1 mysql mysql   16384 Jul 28 04:33 aria_log.00000001
-rw-rw---- 1 mysql mysql      52 Jul 28 04:33 aria_log_control
drwx------ 2 mysql root     4096 Jul 28 04:33 mysql
-rw-rw---- 1 mysql mysql   30379 Jul 28 04:33 mysql-bin.000001
-rw-rw---- 1 mysql mysql 1038814 Jul 28 04:33 mysql-bin.000002
-rw-rw---- 1 mysql mysql      38 Jul 28 04:33 mysql-bin.index
drwx------ 2 mysql mysql    4096 Jul 28 04:33 performance_schema
drwx------ 2 mysql root     4096 Jul 28 04:33 test
[root@localhost mysql]# ls
bin      data               include         lib  mysql-test  scripts  sql-bench
COPYING  EXCEPTIONS-CLIENT  INSTALL-BINARY  man  README      share    support-files
[root@localhost mysql]# cp support-files/mysql.server  /etc/init.d/mysqld                              #复制一个启动服务脚本
[root@localhost mysql]# chkconfig  --list mysqld                                                       #查看服务
service mysqld supports chkconfig, but is not referenced in any runlevel (run 'chkconfig --add mysqld')
[root@localhost mysql]# chkconfig  --add mysqld                                                             #把mysqld服务加进去
[root@localhost mysql]# chkconfig  mysqld on                                                           #开机自启
[root@localhost mysql]# chkconfig  --list mysqld                                                       #看一下服务
mysqld         	0:off	1:off	2:on	3:on	4:on	5:on	6:off
[root@localhost mysql]# service mysqld start                                                            #启动服务
Starting MySQL.170728 04:40:26 mysqld_safe Logging to '/var/log/mysqld.log'.                          #因没有日志文件夹报错
170728 04:40:26 mysqld_safe Starting mysqld daemon with databases from /app/data
/usr/local/mysql/bin/mysqld_safe_helper: Can't create/write to file '/var/log/mysqld.log' (Errcode: 13)
 ERROR! 
[root@localhost mysql]# cat /etc/my.cnf                                                                 #看一下系统自带的mysql配置
[mysqld]
datadir=/var/lib/mysql
socket=/var/lib/mysql/mysql.sock
user=mysql
# Disabling symbolic-links is recommended to prevent assorted security risks
symbolic-links=0

[mysqld_safe]                                                                       
log-error=/var/log/mysqld.log                                                                         #日志正确存放地址
pid-file=/var/run/mysqld/mysqld.pid
[root@localhost mysql]# touch /var/log/mysqld.log                                                  #创建一个日志文件
[root@localhost mysql]# chown mysql /var/log/mysqld.log                                          #修改属主,给它个写的权限
[root@localhost mysql]# server mysqld start             
-bash: server: command not found
[root@localhost mysql]# service mysqld start                                                      #启动服务
Starting MySQL.170728 04:45:01 mysqld_safe Logging to '/var/log/mysqld.log'.
170728 04:45:01 mysqld_safe Starting mysqld daemon with databases from /app/data
. SUCCESS! 
[root@localhost mysql]# ss -ntl                                                                    #查看一下端口,3306开启了
State       Recv-Q Send-Q               Local Address:Port                 Peer Address:Port 
LISTEN      0      50                               *:3306                            *:*     
LISTEN      0      128                             :::22                             :::*     
LISTEN      0      128                              *:22                              *:*     
LISTEN      0      100                            ::1:25                             :::*     
LISTEN      0      100                      127.0.0.1:25                              *:*     
[root@localhost mysql]# mysql                                                                       #直接登录使用,报错命令没找到
-bash: mysql: command not found
[root@localhost mysql]# pwd
/usr/local/mysql
[root@localhost mysql]# ls
bin      data               include         lib  mysql-test  scripts  sql-bench
COPYING  EXCEPTIONS-CLIENT  INSTALL-BINARY  man  README      share    support-files
[root@localhost mysql]# cd bin/
[root@localhost bin]# vim /etc/profile.d/mysql.sh                                                  #写一个配置文件,把mysql放到bin下
export PATH=/usr/local/mysql/bin:$PATH
[root@localhost bin]# .  /etc/profile.d/mysql.sh                                                  #执行一下
[root@localhost bin]# mysql                                                                         #登录一下,可以匿名登录肯定不行啊,出去设置一下
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 2
Server version: 5.5.57-MariaDB MariaDB Server

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

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

MariaDB [(none)]> exit
Bye
[root@localhost bin]# mysql_se
mysql_secure_installation  mysql_setpermission        
[root@localhost bin]# mysql_secure_installation                                                     #执行脚本,完成基本的安全设置
/usr/local/mysql/bin/mysql_secure_installation: line 393: find_mysql_client: command not found

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] n                                                             #禁止root远程登录?
 ... 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.

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!
[root@localhost bin]# mysql -uroot -p                                                              #登录
Enter password:                                                                                      #输入密码
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 10
Server version: 5.5.57-MariaDB MariaDB Server

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

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

MariaDB [(none)]>                                                                                     #安装完成