軟件包的安裝方式有三種:源碼包編譯安裝、二進制包安裝、rpm包安裝。這三種安裝方法都各有優劣。RPM安裝:適合小環境,核心功能都具備,快速搭建環境,但它的版本一般都不會太高;二進制安裝:它是發佈出來時預先編譯過的,既避免了編譯的麻煩,又提供了增強功能;源碼包編譯安裝:它的要求高,要有編譯環境,編譯時可指定幾乎所有選項,根據需求進行定製,可滿足你的所有選擇。
由於centos7系統RPM包自帶Mariadb包版本過低,不適合學習使用,因此今天就通過二進制包安裝的方式來解決版本問題。
安裝步驟如下:
1,下載二進制包;
2,解壓到安裝目錄,安裝相應的依賴包;
3、準備配置文件;
4、設置啓動服務項;
5、測試是否可以正常的登錄;
6、數據庫安全加固;
7、安全初始化;
安裝過程如下:
1,下載二進制包:
二進制包的下載可以從官方網站下載,也可以從國內清華大學官網下載,下載地址如下:
- 項目官方:https://downloads.mariadb.org/mariadb/repositories/
- 國內鏡像:https://mirrors.tuna.tsinghua.edu.cn/mariadb/yum/
這裏下載的Mariadb包的版本爲mariadb-10.2.23-linux-x86_64.tar.gz。
2,解壓到安裝目錄,安裝相應的依賴包:
(1)安裝相應的依賴包:
- [root@localhost ~]# yum -ql libaio-devel numactl-devel #環境依賴包
-
- [root@localhost ~]# yum install lrzsz-0.12.20-36.el7.x86_64 #負責文件的上傳和下載
(2)創建mysql用戶,創建/data/mysql文件夾,計劃將數據庫安裝到/data/mysql文件夾下面:
- # useradd -r -s /sbin/nologin -d /data/mysql mysql #創建mysql的系統賬號,
- 指定shell類型爲nologin,指定安裝目錄爲/data/mysql
(3)將下載的二進制包進行解壓,解壓到/usr/local/目錄下面:
#tar xvf mariadb-10.2.25-linux-x86_64.tar.gz -C /usr/local/ #注意一定要將包解壓到/usr/local/目錄下面。
3、準備配置文件:
(1)將解壓到/usr/local的創建軟連接並修改權限:
- [root@host7 local]# ln -s mariadb-10.2.25-linux-x86_64/ mysql
-
- [root@localhost local]# ll
-
- lrwxrwxrwx 1 root root 29 Jul 3 22:28 mysql -> mariadb-10.2.25-linux-x86_64/
-
- [root@localhost local]#chown -R root.root mysql/ #將文件夾的權限修改爲root
-
- [root@localhost mysql]# ll
- total 176
- drwxr-xr-x 2 root root 4096 Jul 4 09:06 bin
- -rw-r--r-- 1 root root 17987 Jun 15 02:29 COPYING
- -rw-r--r-- 1 root root 2354 Jun 15 02:29 CREDITS
- drwxrwxr-x 3 root root 18 Jun 15 23:35 data
- -rw-r--r-- 1 root root 8245 Jun 15 02:29 EXCEPTIONS-CLIENT
- drwxrwxr-x 3 root root 19 Jun 15 23:35 include
- -rw-r--r-- 1 root root 8694 Jun 15 02:29 INSTALL-BINARY
- drwxr-xr-x 5 root root 335 Mar 15 01:07 lib
- drwxrwxr-x 4 root root 30 Jun 15 23:35 man
- drwxrwxr-x 11 root root 4096 Jun 15 23:35 mysql-test
- -rw-r--r-- 1 root root 2843 Jun 15 02:29 README.md
- -rw-r--r-- 1 root root 19477 Jun 15 02:29 README-wsrep
- drwxrwxr-x 2 root root 30 Jul 3 22:50 scripts
- drwxrwxr-x 32 root root 4096 Jun 15 23:35 share
- drwxrwxr-x 4 root root 4096 Jun 15 23:35 sql-bench
- drwxrwxr-x 3 root root 275 Jun 15 23:35 support-files
- -rw-r--r-- 1 root root 86263 Jun 15 02:29 THIRDPARTY
(2)進入/mysql/scripts 目錄,運行裏面的 腳本,設置數據庫在哪個目錄創建:
- 數據庫創建腳本:
-
- [root@localhost mysql]# cd scripts/
- [root@localhost scripts]# ll
- total 20
- -rwxr-xr-x 1 root root 19257 Jun 15 02:34 mysql_install_db #運行該腳本可創建數據庫
-
- 注意運行該目錄使用相對路徑的方法,否者運行過程會報錯:
-
- [root@localhost mysql]# ./scripts/mysql_install_db --datadir=/data/mysql --user=mysql #指定安裝路徑爲/data/myssql,以mysql的用戶進行運行。
(3)將 /usr/local/mysql/suppert-files/my-huge.cnf文件下數據庫配置大小文件拷貝到/data/mysql/my.cnf目錄下並修改路徑:
- 將包內配置文件拷貝到指定目錄下,並修改文件名稱:
- [root@localhost ~]# cp /usr/local/mysql/support-files/my-huge.cnf /etc/mysql/my.cnf
-
- 修改/etc/myaql/my.cnf文件:
-
- [root@localhost ~]# vim /etc/mysql/my.cnf
-
- # The MySQL server
- [mysqld]
- datadir=/data/mysql #將路徑修改爲/data/mysql
- port = 3306
- socket = /tmp/mysql.sock
-
4,設置啓動服務項:
(1) 將/usr/local/mysql/suppert-files下的mysql.server 拷貝到 /etc/init.d/mysql目錄下,並設置成啓動服務項:
- [root@localhost ~]# cp /usr/local/mysql//support-files/mysql.server /etc/init.d/mysqld
-
- 檢查服務項,沒有Mysqldq啓動服務:
- [root@localhost ~]# chkconfig --list
-
- Note: This output shows SysV services only and does not include native
- systemd services. SysV configuration data might be overridden by native
- systemd configuration.
-
- If you want to list systemd services use 'systemctl list-unit-files'.
- To see services enabled on particular target use
- 'systemctl list-dependencies [target]'.
-
- netconsole 0:off 1:off 2:off 3:off 4:off 5:off 6:off
- network 0:off 1:off 2:on 3:on 4:on 5:on 6:off
-
- 將mysqld的服務項加入到服務啓動腳本里:
- [root@localhost ~]# chkconfig --add mysqld
-
- 啓動mysql服務:
- [root@localhost ~]# service mysqld start
-
- [root@localhost ~]# chkconfig --list
-
- Note: This output shows SysV services only and does not include native
- systemd services. SysV configuration data might be overridden by native
- systemd configuration.
-
- If you want to list systemd services use 'systemctl list-unit-files'.
- To see services enabled on particular target use
- 'systemctl list-dependencies [target]'.
-
- mysqld 0:off 1:off 2:on 3:on 4:on 5:on 6:off #服務已經啓動
- netconsole 0:off 1:off 2:off 3:off 4:off 5:off 6:off
- network 0:off 1:off 2:on 3:on 4:on 5:on 6:off
(2)將Mysql的bin路徑添加到全局路徑下,並使其生效:
- [root@localhost mysql]# echo 'PATH=/usr/local/mysql/bin:$PATH' > /etc/profile.d/mysql.sh #將路徑添加到全局路徑下。
-
- [root@localhost mysql]# . etc/profile.d/mysql.sh 使該設置生效
-
-
5,測試是否可以正常的登錄:
- # mysql
- Welcome to the MariaDB monitor. Commands end with ; or \g.
- Your MariaDB connection id is 34
- Server version: 10.2.25-MariaDB-log 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)]> MariaDB [(none)]> create database testdb; #測試創建數據庫;
-
- MariaDB [(none)]> show databases;
- +--------------------+
- | Database |
- +--------------------+
- | db1 | |
- | information_schema |
- | mysql |
- | performance_schema |
- | testdb |
- +--------------------+
- 5 rows in set (0.00 sec)
6,數據庫安全初始化:
- 使用 /usr/local/mysql/bin 下面的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): 輸入新密碼,並確認新密碼,後面選項採用默認即可。
7,測試登陸:
- [root@localhost ~]# mysql -uroot -p #測試是否可用密碼正確登陸
- Enter password:
- Welcome to the MariaDB monitor. Commands end with ; or \g.
- Your MariaDB connection id is 10
- Server version: 10.2.25-MariaDB-log 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)]>
至此,Mariadb的二進制安裝過程已經完成,可以使用該數據庫了。