1. 程式人生 > >CentOS7 使用yum安裝MariaDB以及環境配置

CentOS7 使用yum安裝MariaDB以及環境配置


從CentOS7開始,linux系統中預設暗轉過的資料庫變成了MariaDB,此資料庫為MySQL的一個分支,使用起來跟MySQL並沒有什麼區別,而且功能比MySQL更全面一點,此文主要是通過yum安裝。


直接正文
1. 檢視系統是否有mariadb資料庫

[root@localhost /]# rpm -qa | grep -i mariadb
mariadb-libs-5.5.35-3.el7.x86_64

無需解除安裝,不影響,安裝時會覆蓋。
2.yum安裝,需要新增MariaDB的yum依賴倉庫
需要新建一個MariaDB.repo檔案

[root@localhost
/]# vi /etc/yum.repos.d/MariaDB.repo

新增以下內容,然後wq退出儲存。(vi的簡單編輯應該都會)
[mariadb]
name = MariaDB
baseurl = http://yum.mariadb.org/10.2/centos7-amd64
gpgkey=https://yum.mariadb.org/RPM-GPG-KEY-MariaDB
gpgcheck=1
3. 開始安裝
這裡我直接上命令

[root@localhost /]# yum install MariaDB-server MariaDB-client -y

這一步比較消耗時間,耐心等待

更新完畢:
  centos-release.x86_64 0:7-5.1804.4.el7.centos      dracut.x86_64 0:033-535.el7_5.1      initscripts.x86_64 0:9.49.41-1.el7_5.1     

作為依賴被升級:
  dracut-config-rescue.x86_64 0:033-535.el7_5.1     dracut-network.x86_64 0:033-535.el7_5.1     glib2.x86_64 0:2.54.2-2.el7        
  kmod.x86_64 0:20-21.el7                           libgudev1.x86_64 0
:219-57.el7_5.1 systemd.x86_64 0:219-57.el7_5.1 systemd-libs.x86_64 0:219-57.el7_5.1 systemd-sysv.x86_64 0:219-57.el7_5.1 替代: mariadb-libs.x86_64 1:5.5.35-3.el7 完畢!

顯示這樣的資訊,表示安裝完畢,然後將其加入系統程序就可以。

[root@localhost /]# systemctl start mariadb

設定 MariaDB 在作業系統重啟後自動啟動服務。

[root@localhost /]# systemctl enable mariadb

檢視 MariaDB 服務當前狀態。

[root@localhost /]# systemctl status mariadb

● mariadb.service - MariaDB 10.2.17 database server
   Loaded: loaded (/usr/lib/systemd/system/mariadb.service; enabled; vendor preset: disabled)
  Drop-In: /etc/systemd/system/mariadb.service.d
           └─migrated-from-my.cnf-settings.conf
   Active: active (running) since 一 2018-08-27 19:05:38 CST; 25s ago

systemctl status mariadb

4. MariaDB相關配置
首先進入安全配置模式

[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.

大體是第一次使用MariaDB之類的,不用管,回車開始設定
然後依次是如下五步,根據自己的規劃設計。
a. 設定root賬戶的密碼

Set root password? [Y/n] Y
New password: 
Re-enter new password: 
Password updated successfully!
Reloading privilege tables..
 ... Success!

b. 刪除匿名使用者

Remove anonymous users? [Y/n] Y
 ... Success!

c. 禁用 root 遠端登入

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] Y
 ... Success!

這樣只有本機可以連線該資料庫,下面有說如何在開啟遠端連線該資料庫
d. 刪除測試資料庫

Remove test database and access to it? [Y/n] Y
 - Dropping test database...
 ... Success!
 - Removing privileges on test database...
 ... Success!

e. 重新載入許可權表。

Reload privilege tables now? [Y/n] Y
 ... Success!

到這,簡單的安全配置完成。現在可以登陸本機的Maria DB資料庫
跟登陸MySQL一樣的操作

[root@localhost /]# mysql -u root -p

輸入你的上面設定的密碼,登陸。
登陸進去後,我們可以來驗證一下,此時的資料庫都有那些賬號/網址可以等裡
輸入下列命令,注意在資料庫操作介面輸入

MariaDB [(none)]> select User, host from mysql.user;
+------+-----------+
| User | host      |
+------+-----------+
| root | 127.0.0.1 |
| root | ::1       |
| root | localhost |
+------+-----------+
3 rows in set (0.08 sec)

很明顯,只有本地可以連線。
下面我們建立一個遠端賬戶,並給此賬戶分配遠端連線的許可權

MariaDB [(none)]> GRANT ALL PRIVILEGES ON *.* TO 'root'@'%' IDENTIFIED BY '遠端賬戶密碼' WITH GRANT OPTION;

該命令設定了一個root賬戶,密碼為“遠端賬戶密碼”的賬戶,你可以用該賬戶通過遠端工具,比如Navicat來對資料庫進行連線。其中的%表示允許任何ip地址對資料庫進行遠端連線,你也可以對其進行限制,比如‘192.168.25.%’就表示只允許這個網段的ip遠端連線該資料庫。
最後一步,重新整理許可權,使改動生效。

MariaDB [(none)]> FLUSH PRIVILEGES;
Query OK, 0 rows affected (0.00 sec)

5. MariaDB相關字符集編碼配置

1.設定資料庫字母大小寫不敏感

[root@localhost /]# vi /etc/my.cnf.d/server.cnf

進去後找到mysqld
在[mysqld]下加上】
lower_case_table_names=1
預設是等於0的,即大小寫敏感。1代表不敏感
2.設定MariaDB資料庫預設編碼
首先進入資料庫檢視現在的字符集

MariaDB [(none)]> SHOW VARIABLES LIKE 'character%';
+--------------------------+----------------------------+
| Variable_name            | Value                      |
+--------------------------+----------------------------+
| character_set_client     | utf8                       |
| character_set_connection | utf8                       |
| character_set_database   | latin1                     |
| character_set_filesystem | binary                     |
| character_set_results    | utf8                       |
| character_set_server     | latin1                     |
| character_set_system     | utf8                       |
| character_sets_dir       | /usr/share/mysql/charsets/ |
+--------------------------+----------------------------+
8 rows in set (0.00 sec)

可以看到有的編碼集為latin1,這樣的話插入中文會亂碼,因此需要將編碼改為utf8。
做法:修改的配置檔案
vi /etc/my.cnf.d/server.cnf
在[mysqld]欄位里加入
character-set-server=utf8
重啟 MariaDB 配置生效。
systemctl restart mariadb

補充:關於遠端連線時,可能涉及的防火牆問題
關閉防火牆:
systemctl stop firewalld.service #停止firewall
systemctl disable firewalld.service #禁止firewall開機啟動
開放防火牆的指定埠(開啟後要重啟防火牆)
firewall-cmd –zone=public –add-port=3306/tcp –permanent
firewall-cmd –reload