目錄
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 username@'%' identified by 'password';
或 授予權限並且可以授權
mysql>grant all privileges on *.* to lhy@'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'
另外,如果外部訪問不了,嘗試把防火牆關閉看一下。