CentOS7環(huán)境安裝MariaDB(MySQL開源版)
一、MariaDB概述
1). 說明
MariaDB數(shù)據(jù)庫管理系統(tǒng)是MySQL的一個(gè)分支,主要由開源社區(qū)在維護(hù),采用GPL授權(quán)許可。
2). 預(yù)準(zhǔn)備
- 有的centos7已經(jīng)默認(rèn)安裝了Mariadb,可以查看自己的有沒有安裝,沒有安裝的再進(jìn)行安裝,已經(jīng)安裝了可以不用安裝也可以卸載了重裝。
[root@hadoop000 ~]# yum remove mariadb-server
[root@hadoop000 ~]# cat /etc/redhat-release
CentOS Linux release 7.4.1708 (Core)
二、安裝配置Mariadb
1).安裝MariaDB
通過yum安裝就行了。簡單快捷,安裝mariadb-server,默認(rèn)依賴安裝mariadb,一個(gè)是服務(wù)端、一個(gè)是客戶端。
[root@hadoop000 ~]# yum install mariadb-server
2).制作系統(tǒng)服務(wù)
- 安裝完成后首先要把MariaDB服務(wù)開啟,并設(shè)置為開機(jī)啟動
[root@hadoop000 ~]# systemctl start mariadb # 開啟服務(wù)
[root@hadoop000 ~]# systemctl enable mariadb # 設(shè)置為開機(jī)自啟動服務(wù)
3).MariaDB初始化配置
- 首次安裝需要進(jìn)行數(shù)據(jù)庫的配置,命令都和mysql的一樣
[root@hadoop000 ~]# mysql_secure_installation
- 配置時(shí)出現(xiàn)的各個(gè)選項(xiàng)以及建議的操作
Enter current password for root (enter for none): # 輸入數(shù)據(jù)庫超級管理員root的密碼(注意不是系統(tǒng)root的密碼),第一次進(jìn)入還沒有設(shè)置密碼則直接回車
Set root password? [Y/n] # 設(shè)置密碼,y
New password: # 新密碼
Re-enter new password: # 再次輸入密碼
Remove anonymous users? [Y/n] # 移除匿名用戶, y
Disallow root login remotely? [Y/n] # 拒絕root遠(yuǎn)程登錄,n,不管y/n,都會拒絕root遠(yuǎn)程登錄
Remove test database and access to it? [Y/n] # 刪除test數(shù)據(jù)庫,y:刪除。n:不刪除,數(shù)據(jù)庫中會有一個(gè)test數(shù)據(jù)庫,一般不需要
Reload privilege tables now? [Y/n] # 重新加載權(quán)限表,y?;蛘咧貑⒎?wù)也許
- 測試是否能夠登錄成功
出現(xiàn)
MariaDB [(none)]>就表示已經(jīng)能夠正常登錄使用MariaDB數(shù)據(jù)庫了
[root@hadoop000 ~]# mysql -u root -p
Enter password:
Welcome to the MariaDB monitor. Commands end with ; or \g.
Your MariaDB connection id is 8
Server version: 5.5.60-MariaDB 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)]>
4).設(shè)置MariaDB字符集為utf-8
建議使用vim工具進(jìn)行配置
- 配置
/etc/my.cnf文件,在[mysqld]標(biāo)簽下添加如下內(nèi)容
init_connect='SET collation_connection = utf8_unicode_ci'
init_connect='SET NAMES utf8'
character-set-server=utf8
collation-server=utf8_unicode_ci
skip-character-set-client-handshake
- 配置
/etc/my.cnf.d/client.cnf文件,在[client]標(biāo)簽下添加
default-character-set=utf8
- 配置
/etc/my.cnf.d/mysql-clients.cnf文件,在[mysql]標(biāo)簽下添加
default-character-set=utf8
- 重啟服務(wù)
[root@hadoop000 ~]# systemctl restart mariadb
- 進(jìn)入mariadb查看字符集
配置字符集后的內(nèi)容
MariaDB [(none)]> show variables like "%character%";show variables like "%collation%";
+--------------------------+----------------------------+
| Variable_name | Value |
+--------------------------+----------------------------+
| character_set_client | utf8 |
| character_set_connection | utf8 |
| character_set_database | utf8 |
| character_set_filesystem | binary |
| character_set_results | utf8 |
| character_set_server | utf8 |
| character_set_system | utf8 |
| character_sets_dir | /usr/share/mysql/charsets/ |
+--------------------------+----------------------------+
8 rows in set (0.00 sec)
+----------------------+-----------------+
| Variable_name | Value |
+----------------------+-----------------+
| collation_connection | utf8_unicode_ci |
| collation_database | utf8_unicode_ci |
| collation_server | utf8_unicode_ci |
+----------------------+-----------------+
3 rows in set (0.00 sec)
MariaDB [(none)]>
5).禁用防火墻配置(開發(fā)階段建議)
- 查看防火墻狀態(tài)
enabled表示可以啟用;running表示已經(jīng)在啟用
[hadoop@hadoop000 ~]$ systemctl status firewalld
● firewalld.service - firewalld - dynamic firewall daemon
Loaded: loaded (/usr/lib/systemd/system/firewalld.service; enabled; vendor preset: enabled)
Active: active (running) since Thu 2019-11-28 19:13:33 CST; 1min 41s ago
Docs: man:firewalld(1)
Main PID: 18478 (firewalld)
CGroup: /system.slice/firewalld.service
└─18478 /usr/bin/python -Es /usr/sbin/firewalld --nofork --nopid
[hadoop@hadoop000 ~]$
- 關(guān)閉防火墻
[hadoop@hadoop000 ~]$ systemctl stop firewalld
==== AUTHENTICATING FOR org.freedesktop.systemd1.manage-units ===
Authentication is required to manage system services or units.
Authenticating as: root
Password:
==== AUTHENTICATION COMPLETE ===
[hadoop@hadoop000 ~]$
- 禁用防火墻,開機(jī)不自動啟動
[hadoop@hadoop000 ~]$ systemctl disable firewalld
==== AUTHENTICATING FOR org.freedesktop.systemd1.manage-unit-files ===
Authentication is required to manage system service or unit files.
Authenticating as: root
Password:
==== AUTHENTICATION COMPLETE ===
Removed symlink /etc/systemd/system/multi-user.target.wants/firewalld.service.
Removed symlink /etc/systemd/system/dbus-org.fedoraproject.FirewallD1.service.
==== AUTHENTICATING FOR org.freedesktop.systemd1.reload-daemon ===
Authentication is required to reload the systemd state.
Authenticating as: root
Password:
==== AUTHENTICATION COMPLETE ===
[hadoop@hadoop000 ~]$
- 再次檢查狀態(tài)
[hadoop@hadoop000 ~]$ systemctl status firewalld
● firewalld.service - firewalld - dynamic firewall daemon
Loaded: loaded (/usr/lib/systemd/system/firewalld.service; disabled; vendor preset: enabled)
Active: inactive (dead)
Docs: man:firewalld(1)
[hadoop@hadoop000 ~]$
6).防火墻配置開放端口(生產(chǎn)環(huán)境)
在不關(guān)閉防火墻的情況下,允許某端口的外來鏈接。步驟如下,開啟3306端口,重啟防火墻
[root@hadoop000 ~]# systemctl stop firewalld
[root@hadoop000 ~]# firewall-cmd --query-port=3306/tcp # 查看3306端口是否開啟
[root@hadoop000 ~]# firewall-cmd --zone=public --add-port=3306/tcp --permanent # 開啟3306端口
[root@hadoop000 ~]# firewall-cmd --reload # 重啟防火墻
success
[root@hadoop000 ~]# firewall-cmd --query-port=3306/tcp # 查看3306端口是否開啟
yes
7). 配置遠(yuǎn)程登錄
MariaDB默認(rèn)是拒絕root遠(yuǎn)程登錄的。
- 查看mysql數(shù)據(jù)庫中的user表
[root@hadoop000 ~]# mysql -u root -p # 先通過本地鏈接進(jìn)入數(shù)據(jù)庫
MariaDB [(none)]> use mysql;
MariaDB [mysql]> select host, user from user;
+-----------+------+
| host | user |
+-----------+------+
| 127.0.0.1 | root |
| ::1 | root |
| hadoop000 | root |
+-----------+------+
3 rows in set (0.00 sec)
- 將與主機(jī)名相等的字段改為
%,我的主機(jī)名為hadoop000;
MariaDB [mysql]> update user set host='%' where host='hadoop000';
Query OK, 1 row affected (0.00 sec)
Rows matched: 1 Changed: 1 Warnings: 0
MariaDB [mysql]> select host, user from user;
+-----------+------+
| host | user |
+-----------+------+
| % | root |
| 127.0.0.1 | root |
| localhost | root |
+-----------+------+
3 rows in set (0.00 sec)
- 刷新權(quán)限表,或重啟MariaDB服務(wù)
MariaDB [mysql]> flush privileges;
Query OK, 0 rows affected (0.00 sec)
[root@hadoop000 ~]# systemctl restart mariadb
注意:刷新權(quán)限表是在數(shù)據(jù)庫中,重啟服務(wù)是在外部命令行中
三、添加用戶并授權(quán)
root用戶太危險(xiǎn),APP應(yīng)用建議使用其他普通用戶,設(shè)置權(quán)限!
1). 添加新用戶并授權(quán)
[hadoop@hadoop000 ~]$ mysql -u root -p
Enter password:
...
MariaDB [(none)]> create user 'hadoop'@'%' identified by '^_^_^_^';
MariaDB [(none)]> grant ALL HADOOP_HIVE.* to 'hadoop'@'%';
2). 遠(yuǎn)程登錄測試
略