centos6.9 centos7.3 正確安裝mariadb的姿勢

    隨着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)]>                                                                                     #安裝完成










發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章