阿里雲服務器使用二進制方式安裝Mariadb

軟件包的安裝方式有三種:源碼包編譯安裝、二進制包安裝、rpm包安裝。這三種安裝方法都各有優劣。RPM安裝:適合小環境,核心功能都具備,快速搭建環境,但它的版本一般都不會太高;二進制安裝:它是發佈出來時預先編譯過的,既避免了編譯的麻煩,又提供了增強功能;源碼包編譯安裝:它的要求高,要有編譯環境,編譯時可指定幾乎所有選項,根據需求進行定製,可滿足你的所有選擇。

由於centos7系統RPM包自帶Mariadb包版本過低,不適合學習使用,因此今天就通過二進制包安裝的方式來解決版本問題。

安裝步驟如下:

1,下載二進制包;

2,解壓到安裝目錄,安裝相應的依賴包;

3、準備配置文件;

4、設置啓動服務項;

5、測試是否可以正常的登錄;

6、數據庫安全加固;

7、安全初始化;

安裝過程如下:

1,下載二進制包:

二進制包的下載可以從官方網站下載,也可以從國內清華大學官網下載,下載地址如下:  

這裏下載的Mariadb包的版本爲mariadb-10.2.23-linux-x86_64.tar.gz

2,解壓到安裝目錄,安裝相應的依賴包:

(1)安裝相應的依賴包:

  1. [root@localhost ~]# yum -ql libaio-devel numactl-devel #環境依賴包
  2. [root@localhost ~]# yum install lrzsz-0.12.20-36.el7.x86_64 #負責文件的上傳和下載

(2)創建mysql用戶,創建/data/mysql文件夾,計劃將數據庫安裝到/data/mysql文件夾下面:

  1. # useradd -r -s /sbin/nologin -d /data/mysql mysql #創建mysql的系統賬號,
  2. 指定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的創建軟連接並修改權限:

  1. [root@host7 local]# ln -s mariadb-10.2.25-linux-x86_64/ mysql
  2. [root@localhost local]# ll
  3. lrwxrwxrwx 1 root root 29 Jul 3 22:28 mysql -> mariadb-10.2.25-linux-x86_64/
  4. [root@localhost local]#chown -R root.root mysql/ #將文件夾的權限修改爲root
  5. [root@localhost mysql]# ll
  6. total 176
  7. drwxr-xr-x 2 root root 4096 Jul 4 09:06 bin
  8. -rw-r--r-- 1 root root 17987 Jun 15 02:29 COPYING
  9. -rw-r--r-- 1 root root 2354 Jun 15 02:29 CREDITS
  10. drwxrwxr-x 3 root root 18 Jun 15 23:35 data
  11. -rw-r--r-- 1 root root 8245 Jun 15 02:29 EXCEPTIONS-CLIENT
  12. drwxrwxr-x 3 root root 19 Jun 15 23:35 include
  13. -rw-r--r-- 1 root root 8694 Jun 15 02:29 INSTALL-BINARY
  14. drwxr-xr-x 5 root root 335 Mar 15 01:07 lib
  15. drwxrwxr-x 4 root root 30 Jun 15 23:35 man
  16. drwxrwxr-x 11 root root 4096 Jun 15 23:35 mysql-test
  17. -rw-r--r-- 1 root root 2843 Jun 15 02:29 README.md
  18. -rw-r--r-- 1 root root 19477 Jun 15 02:29 README-wsrep
  19. drwxrwxr-x 2 root root 30 Jul 3 22:50 scripts
  20. drwxrwxr-x 32 root root 4096 Jun 15 23:35 share
  21. drwxrwxr-x 4 root root 4096 Jun 15 23:35 sql-bench
  22. drwxrwxr-x 3 root root 275 Jun 15 23:35 support-files
  23. -rw-r--r-- 1 root root 86263 Jun 15 02:29 THIRDPARTY

(2)進入/mysql/scripts 目錄,運行裏面的 腳本,設置數據庫在哪個目錄創建:

  1. 數據庫創建腳本:
  2. [root@localhost mysql]# cd scripts/
  3. [root@localhost scripts]# ll
  4. total 20
  5. -rwxr-xr-x 1 root root 19257 Jun 15 02:34 mysql_install_db #運行該腳本可創建數據庫
  6. 注意運行該目錄使用相對路徑的方法,否者運行過程會報錯:
  7. [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目錄下並修改路徑:

  1. 將包內配置文件拷貝到指定目錄下,並修改文件名稱:
  2. [root@localhost ~]# cp /usr/local/mysql/support-files/my-huge.cnf /etc/mysql/my.cnf
  3. 修改/etc/myaql/my.cnf文件:
  4. [root@localhost ~]# vim /etc/mysql/my.cnf
  5. # The MySQL server
  6. [mysqld]
  7. datadir=/data/mysql #將路徑修改爲/data/mysql
  8. port = 3306
  9. socket = /tmp/mysql.sock

     4,設置啓動服務項:

(1)   將/usr/local/mysql/suppert-files下的mysql.server 拷貝到 /etc/init.d/mysql目錄下,並設置成啓動服務項:

  1. [root@localhost ~]# cp /usr/local/mysql//support-files/mysql.server /etc/init.d/mysqld
  2. 檢查服務項,沒有Mysqldq啓動服務:
  3. [root@localhost ~]# chkconfig --list
  4. Note: This output shows SysV services only and does not include native
  5. systemd services. SysV configuration data might be overridden by native
  6. systemd configuration.
  7. If you want to list systemd services use 'systemctl list-unit-files'.
  8. To see services enabled on particular target use
  9. 'systemctl list-dependencies [target]'.
  10. netconsole 0:off 1:off 2:off 3:off 4:off 5:off 6:off
  11. network 0:off 1:off 2:on 3:on 4:on 5:on 6:off
  12. 將mysqld的服務項加入到服務啓動腳本里:
  13. [root@localhost ~]# chkconfig --add mysqld
  14. 啓動mysql服務:
  15. [root@localhost ~]# service mysqld start
  16. [root@localhost ~]# chkconfig --list
  17. Note: This output shows SysV services only and does not include native
  18. systemd services. SysV configuration data might be overridden by native
  19. systemd configuration.
  20. If you want to list systemd services use 'systemctl list-unit-files'.
  21. To see services enabled on particular target use
  22. 'systemctl list-dependencies [target]'.
  23. mysqld 0:off 1:off 2:on 3:on 4:on 5:on 6:off #服務已經啓動
  24. netconsole 0:off 1:off 2:off 3:off 4:off 5:off 6:off
  25. network 0:off 1:off 2:on 3:on 4:on 5:on 6:off

    (2)將Mysql的bin路徑添加到全局路徑下,並使其生效:

  1. [root@localhost mysql]# echo 'PATH=/usr/local/mysql/bin:$PATH' > /etc/profile.d/mysql.sh #將路徑添加到全局路徑下。
  2. [root@localhost mysql]# . etc/profile.d/mysql.sh 使該設置生效

              

   5,測試是否可以正常的登錄:

  1. # mysql
  2. Welcome to the MariaDB monitor. Commands end with ; or \g.
  3. Your MariaDB connection id is 34
  4. Server version: 10.2.25-MariaDB-log MariaDB Server
  5. Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.
  6. Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
  7. MariaDB [(none)]> MariaDB [(none)]> create database testdb; #測試創建數據庫;
  8. MariaDB [(none)]> show databases;
  9. +--------------------+
  10. | Database |
  11. +--------------------+
  12. | db1 | |
  13. | information_schema |
  14. | mysql |
  15. | performance_schema |
  16. | testdb |
  17. +--------------------+
  18. 5 rows in set (0.00 sec)

6,數據庫安全初始化:

  1. 使用 /usr/local/mysql/bin 下面的mysql_secure_installation腳本進行安全加固:
  2. [root@localhost ~]# mysql_secure_installation
  3. NOTE: RUNNING ALL PARTS OF THIS SCRIPT IS RECOMMENDED FOR ALL MariaDB
  4. SERVERS IN PRODUCTION USE! PLEASE READ EACH STEP CAREFULLY!
  5. In order to log into MariaDB to secure it, we'll need the current
  6. password for the root user. If you've just installed MariaDB, and
  7. you haven't set the root password yet, the password will be blank,
  8. so you should just press enter here.
  9. Enter current password for root (enter for none): 輸入新密碼,並確認新密碼,後面選項採用默認即可。

7,測試登陸:

  1. [root@localhost ~]# mysql -uroot -p #測試是否可用密碼正確登陸
  2. Enter password:
  3. Welcome to the MariaDB monitor. Commands end with ; or \g.
  4. Your MariaDB connection id is 10
  5. Server version: 10.2.25-MariaDB-log MariaDB Server
  6. Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.
  7. Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
  8. MariaDB [(none)]>

至此,Mariadb的二進制安裝過程已經完成,可以使用該數據庫了。

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