centos7.5下yum 安裝mariadb10.3詳解
目錄
3)、檔案/etc/my.cnf.d/mysql-clients.cnf
前言
mariadb 和mysql就像親兄弟的關係,各種語法、驅動啥的,在mysql上能上的,在mariadb上基本都可以直接使用。更多的細節在此不多說。
1、刪除舊版本
centos7下預設安裝有mariadb資料庫,但是是舊版本,在安裝新版本前需要先把舊版本刪除,有些系統還預設安裝mysql,也必須刪除,否則與mariadb會產生衝突,如下命令過程:
rpm -qa | grep mariadb
結果如下:
用命令yum刪除以上三個:
yum remove mariadb-server-5.5.60-1.el7_5.x86_64
yum remove mariadb-5.5.60-1.el7_5.x86_64
yum remove mariadb-libs-5.5.60-1.el7_5.x86_64
2,建立 MariaDB.repo
在目錄下 /etc/yum.repos.d/ 建立檔案: MariaDB.repo
並把以下內容新增到所建檔案中
# MariaDB 10.3 CentOS repository list - created 2018-10-16 15:18 UTC
# http://downloads.mariadb.org/mariadb/repositories/
[mariadb]
name = MariaDB
baseurl = http://yum.mariadb.org/10.3/centos7-amd64
gpgkey=https://yum.mariadb.org/RPM-GPG-KEY-MariaDB
gpgcheck=1
以上是官方源,這裡我們用阿里源,內容如下:
[mariadb]
name = MariaDB
baseurl = http://mirrors.aliyun.com/mariadb/yum/10.3/centos7-amd64/
gpgkey = http://mirrors.aliyun.com/mariadb/yum/RPM-GPG-KEY-MariaDB
gpgcheck = 1
3、安裝 install MariaDB
sudo yum install MariaDB-server MariaDB-client
4、 安裝完成MariaDB,首先啟動MariaDB
systemctl start mariadb
5、 設定開機啟動
systemctl enable mariadb
systemctl 常用命令:
systemctl start mariadb #啟動服務
systemctl enable mariadb #設定開機啟動
systemctl restart mariadb #重新啟動
systemctl stop mariadb.service #停止MariaDB
6、 接下來進行MariaDB的相關簡單配置
輸入以下命令:
mysql_secure_installation
先是設定密碼,會提示先輸入密碼
Enter current password for root (enter for none):<–初次執行直接回車
設定密碼
Set root password? [Y/n] <– 是否設定root使用者密碼,輸入y並回車或直接回車
New password: <– 設定root使用者的密碼
Re-enter new password: <– 再輸入一次你設定的密碼
其他配置
Remove anonymous users? [Y/n] <– 是否刪除匿名使用者,Y,回車
Disallow root login remotely? [Y/n] <–是否禁止root遠端登入,N,回車,
Remove test database and access to it? [Y/n] <– 是否刪除test資料庫,n,回車
Reload privilege tables now? [Y/n] <– 是否重新載入許可權表,回車
初始化MariaDB完成,接下來測試登入
這裡我設定的密碼:luxxxxxx
7.測試登入
mysql -u root -p
成功登入後顯示如下:
8、配置mariaDB相關字符集
1)、檔案/etc/my.cnf
vi /etc/my.cnf
新增如下內容:
[mysqld]
init_connect='SET collation_connection = utf8_general_ci'
init_connect='SET NAMES utf8'
character-set-server=utf8
collation-server=utf8_general_ci
skip-character-set-client-handshake
2)、檔案/etc/my.cnf.d/client.cnf
vi /etc/my.cnf.d/client.cnf
在[client]中新增
default-character-set=utf8
3)、檔案/etc/my.cnf.d/mysql-clients.cnf
vi /etc/my.cnf.d/mysql-clients.cnf
在[mysql]中新增
default-character-set=utf8
4)、全部配置完成,重啟mariadb
systemctl restart mariadb
5)、之後進入MariaDB檢視字符集
mysql> show variables like "%character%";show variables like "%collation%";
9、新增使用者、設定許可權
建立使用者命令
mysql>create user [email protected] identified by 'luxxxxxx';
或 直接建立使用者並授權的命令
mysql>
grant all on *.* to [email protected] indentified by 'luxxxxxx';
或 授予外網登陸許可權
mysql>grant all privileges on *.* to [email protected]'%' identified by 'password';
或 授予許可權並且可以授權
mysql>grant all privileges on *.* to [email protected]'192.168.1.10' identified by 'lu5896848' with grant option;
mysql>flush privileges;
簡單的使用者和許可權配置基本就這樣了。
其中只授予部分許可權把 其中 all privileges或者all改為select,insert,update,delete,create,drop,index,alter,grant,references,reload,shutdown,process,file其中一部分。
10、遠端訪問資料庫
遠端訪問MySQL,需開放預設埠號3306,方式有兩種:
1)、centos6或更早前的版本系統
vi /etc/sysconfig/iptables
修改
*filter
:INPUT ACCEPT [0:0]
:FORWARD ACCEPT [0:0]
:OUTPUT ACCEPT [0:0]
-A INPUT -m state --state RELATED,ESTABLISHED -j ACCEPT
-A INPUT -p icmp -j ACCEPT
-A INPUT -i lo -j ACCEPT
-A INPUT -p tcp -m state --state NEW -m tcp --dport 22 -j ACCEPT
-A INPUT -j REJECT --reject-with icmp-host-prohibited
-A FORWARD -j REJECT --reject-with icmp-host-prohibited
COMMIT
#在裡面加入這2行:
-A RH-Firewall-1-INPUT -m state –state NEW -m tcp -p tcp –dport 3306 -j ACCEPT
-A RH-Firewall-1-INPUT -m state –state NEW -m udp -p udp –dport 3306 -j ACCEPT
#改為
*filter
:INPUT ACCEPT [0:0]
:FORWARD ACCEPT [0:0]
:OUTPUT ACCEPT [0:0]
-A INPUT -m state --state RELATED,ESTABLISHED -j ACCEPT
-A INPUT -p icmp -j ACCEPT
-A INPUT -i lo -j ACCEPT
-A INPUT -p tcp -m state --state NEW -m tcp --dport 22 -j ACCEPT
-A RH-Firewall-1-INPUT -m state –state NEW -m tcp -p tcp –dport 3306 -j ACCEPT
-A RH-Firewall-1-INPUT -m state –state NEW -m udp -p udp –dport 3306 -j ACCEPT
-A INPUT -j REJECT --reject-with icmp-host-prohibited
-A FORWARD -j REJECT --reject-with icmp-host-prohibited
COMMIT
如果該 iptables 配置檔案 不存在,先執行yum install iptables-services安裝
執行 iptables 重啟生效
service iptables restart
2)、centos7
執行
firewall-cmd --permanent --zone=public --add-port=3306/tcp
firewall-cmd --permanent --zone=public --add-port=3306/udp
這樣就開放了相應的埠。
執行
firewall-cmd --reload
最後,如果你是用的國外伺服器,記得還要設定一個時區 default-time-zone = '+8:00'
另外,如果外部訪問不了,嘗試把防火牆關閉看一下。