MariaDB概述
MariaDB數據庫管理系統是MySQL的一個分支,主要由開源社區在維護,採用GPL受權許可 MariaDB的目的是徹底兼容MySQL,包括API和命令行,使之能輕鬆成爲MySQL的代替品。在存儲引擎方面,使用XtraDB(英語:XtraDB)來代替MySQL的InnoDB。 MariaDB由MySQL的創始人Michael Widenius(英語:Michael Widenius)主導開發 ----來源百度百科node
準備工做
名稱 | IP地址 |
---|---|
master-s1 | 192.168.1.10 |
slaver-s2 | 192.168.1.20 |
slaver-s3 | 192.168.1.30 |
以上系統的版本均爲:CentOS-7-x86_64-DVD-2003
防火牆以及selinux的設置
python
三臺均執行如下操做: [root@master-s1 ~]# systemctl stop firewalld [root@master-s1 ~]# systemctl disable firewalld [root@master-s1 ~]# setenforce 0
搭建過程以下
時間同步的設置mysql
在時間服務器以前,咱們修改本地hosts文件,添加以下內容: [root@master-s1 ~]# vim /etc/hosts //三臺均瑤執行 192.168.1.10 master-s1 192.168.1.20 slaver-s2 192.168.1.30 slaver-s3 master-s1 配置(做爲時間同步服務,讓其餘節點都與這一臺的時間同步) [root@master-s1 ~]# yum install -y ntp ntpdate //安裝時間同步工具以及時間服務器 [root@master-s1 ~]# ntpdate ntp1.aliyun.com //同步阿里雲的是否服務器 19 Oct 19:36:52 ntpdate[57124]: adjust time server 120.25.115.20 offset -0.003110 sec [root@master-s1 ~]# clock -w //保存時間 [root@master-s1 ~]# vim /etc/ntp.conf //編輯時間同步服務器的配置文件 17 restrict 192.168.1.0 mask 255.255.255.0 nomodify notrap //取消前面的註釋 18 server 127.127.1.0 //添加一行這個 21 #server 0.centos.pool.ntp.org iburst (註釋以server開頭的行) 22 #server 1.centos.pool.ntp.org iburst 23 #server 2.centos.pool.ntp.org iburst 24 #server 3.centos.pool.ntp.org iburst [root@master-s1 ~]# systemctl start ntpd //啓動時間服務器 [root@master-s1 ~]# systemctl enable ntpd slaver-s2 以及 slaver-s3的配置(配置均一致) [root@slaver-s2 ~]# yum install -y ntpdate //安裝時間同步工具 [root@slaver-s2 ~]# ntpdate master-s1 //同步節點一 19 Oct 19:51:25 ntpdate[70175]: adjust time server 192.168.1.10 offset 0.015520 sec [root@slaver-s2 ~]# clock -w //保存時間 [root@slaver-s2 ~]# crontab -e //設置週期計劃 */1 * * * * /usr/sbin/ntpdate master-s1 >> /var/log/ntpdate.log //每隔1分鐘自動同步節點一的時間 [root@slaver-s2 ~]# systemctl restart crond [root@slaver-s2 ~]# systemctl enable crond //設置爲開機自動啓動
配置本地yum源linux
默認的yum中沒有mariadb-server-galera galera這一些集羣包 [root@master-s1 ~]# yum install -y centos-release-openstack-rocky.noarch [root@master-s1 ~]# yum clean all //清除緩存 [root@master-s1 ~]# yum makecache //更新緩存
安裝數據庫以及集羣相關的軟件包sql
[root@master-s1 ~]# yum install mariadb mariadb-server python2-PyMySQL mariadb-server-galera mariadb-galera-common galera xinetd rsync -y [root@master-s1 ~]# cd /etc/my.cnf.d/ //進入這個目錄 [root@master-s1 my.cnf.d]# ls //這個目錄下會有這一些文件 auth_gssapi.cnf enable_encryption.preset mariadb-server.cnf tokudb.cnf client.cnf galera.cnf mysql-clients.cnf [root@master-s1 my.cnf.d]# vim test.cnf //咱們新建一個以.cnf 結尾的文件 [galera] #名稱 固定 wsrep_on=ON #是否開啓羣集 wsrep_provider=/usr/lib64/galera/libgalera_smm.so #存放認證的路徑 wsrep_cluster_address="gcomm://master-s1,slaver-s2,slaver-s3" #羣集IP地址,這裏須要注意,第一臺建立羣集的不需輸入任何IP地址,第二就須要輸入這個羣集裏面所包含的IP地址 binlog_format=row #不用管 default_storage_engine=InnoDB #使用的數據庫引擎爲INNODB innodb_autoinc_lock_mode=3 #羣集的節點數是多少 bind-address=0.0.0.0 #無需管 wsrep_cluster_name="MariaDB_Cluster" #羣集的名稱 wsrep_node_address="192.168.1.10" #節點的IP地址 wsrep_sst_method=rsync #羣集的狀態 [root@master-s1 my.cnf.d]# scp test.cnf root@slaver-s2:/etc/my.cnf.d/ //而後test.cnf 文件複製到其餘兩個節點中去,並修改wsrep_node_address選項爲本地IP地址 [root@master-s1 my.cnf.d]# scp test.cnf root@slaver-s3:/etc/my.cnf.d/
啓動各節點的數據庫數據庫
master-s1 節點的啓動 [root@master-s1 ~]# /usr/libexec/mysqld --wsrep-new-cluster --user=root & //節點一須要先執行這個命令來啓動集羣以及數據庫 [root@master-s1 ~]# ss -tan | grep 3306 LISTEN 0 80 *:3306 *:* slaver-s2 和 slaver-s3 啓動一致 [root@slaver-s2 ~]# systemctl start mariadb //從節點直接開啓數據庫便可,會自動加入到指定的集羣中 [root@slaver-s2 ~]# ss -tan | grep 3306 LISTEN 0 80 *:3306 *:*
各節點登陸數據庫測試是否實現同步vim
在登陸數據庫以前,各節點均須要先初始化數據庫纔可 [root@master-s1 ~]# mysql_secure_installation [root@master-s1 ~]# mysql -uroot -p //登陸數據庫 Enter password: MariaDB [(none)]> create database t1; //建立一個數據庫 Query OK, 1 row affected (0.00 sec) slaver-s2以及slaver-s3查看是否實現同步 MariaDB [(none)]> show databases; +--------------------+ | Database | +--------------------+ | information_schema | | mysql | | performance_schema | | t1 | +--------------------+ 4 rows in set (0.00 sec) 查看當前集羣的節點數以及狀態 MariaDB [(none)]> show status like'wsrep_%'; ...... | wsrep_cluster_size | 3 //當前集羣的節點數 | wsrep_local_state_comment | Synced //集羣的狀態爲同步狀態
解決數據庫中止掉之後,再次啓動啓動不上centos
[root@master-s1 ~]# ps -aux |grep mysqld //在主節點上查看當前mysqld的運行狀態 root 86820 0.4 12.7 1459076 127160 pts/0 Sl 23:02 0:00 /usr/libexec/mysqld --wsrep-new-cluster --user=root root 90009 0.0 0.0 112824 980 pts/0 R+ 23:05 0:00 grep --color=auto mysqld 22:53 0:00 grep --color=auto mysqld [root@master-s1 ~]# pkill -9 mysqld //殺掉mysqld主進程 [root@master-s1 ~]# [1]+ 已殺死 /usr/libexec/mysqld --wsrep-new-cluster --user=root [root@master-s1 ~]# rm -rf /var/lib/mysql/ * //刪除掉咱們以前運行mariadb產生的文件 [root@master-s1 ~]# chown -R mysql:mysql /var/run/mariadb/mariadb.pid //將mysql的進程號文件的所屬組以及用戶改成mysql [root@master-s1 ~]# systemctl start mariadb //而後再次啓動mariadb數據庫 便可 [root@master-s1 ~]# systemctl enable mariadb
安裝haproxy(虛擬端口)
安裝這個軟件,咱們能夠將數據庫的3306端口映射咱們指定的端口暴露在公網下,這樣就極大的提升的數據的安全性
api
三個節點都須要進行安裝 [root@master-s1 ~]# yum install -y haproxy [root@master-s1 ~]# vim /etc/haproxy/haproxy.cfg //修改配置文件 42 defaults 在這個項的下面添加以下內容便可 59 listen mariadb_haproxy 60 bind *:3307 61 mode tcp 62 server s1 192.168.1.10:3306 check inter 2000 rise 2 fall 5 63 server s2 192.168.1.20:3306 check inter 2000 rise 2 fall 5 64 server s3 192.168.1.30:3306 check inter 2000 rise 2 fall 5 而後將這個配置文件拷貝到其餘節點上便可 [root@master-s1 ~]# scp /etc/haproxy/haproxy.cfg root@slaver-s2:/etc/haproxy/haproxy.cfg [root@master-s1 ~]# scp /etc/haproxy/haproxy.cfg root@slaver-s3:/etc/haproxy/haproxy.cfg 三個節點均須要執行 [root@master-s1 ~]# systemctl start haproxy //啓動服務 [root@master-s1 ~]# systemctl enable haproxy Created symlink from /etc/systemd/system/multi-user.target.wants/haproxy.service to /usr/lib/systemd/system/haproxy.service. [root@master-s1 ~]# ss -tan | grep 3307 LISTEN 0 128 *:3307 *:* 而後咱們使用-P 指定端口的參數來登陸數據庫測試 [root@master-s1 ~]# mysql -uroot -p -P3307 Enter password: Welcome to the MariaDB monitor. Commands end with ; or \g. Your MariaDB connection id is 4 Server version: 10.1.20-MariaDB MariaDB Server Copyright (c) 2000, 2016, Oracle, MariaDB Corporation Ab and others. Type 'help;' or '\h' for help. Type '\c' to clear the current input statement. MariaDB [(none)]>
安裝keepalive
使用keepalive軟件,會自動給咱們生成一個虛擬的IP地址,這個IP地址是對外開放的,全部外部訪問都會請求這個IP地址,而後在返回給咱們的數據庫進行處理,至關於集羣的IP地址,主要用於管理整個集羣的
緩存
三個節點均要執行 [root@master-s1 ~]# yum install -y keepalived //安裝這個軟件 [root@master-s1 ~]# vim /etc/keepalived/keepalived.conf //修改配置文件 將配置文件裏面的內容所有清空,添加以下內容便可 vrrp_instance VI_1 { state MASTER #通常主節點的狀態爲MASTER 其餘節點都爲BACKUP interface ens33 #指定網卡名稱 virtual_router_id 51 priority 100 #優先級 advert_int 1 authentication { #認證方式 auth_type PASS auth_pass 1111 } virtual_ipaddress { 192.168.1.100 dev ens33 #設置集羣IP地址 } } [root@master-s1 ~]# scp /etc/keepalived/keepalived.conf root@slaver-s2:/etc/keepalived/keepalived.conf [root@master-s1 ~]# scp /etc/keepalived/keepalived.conf root@slaver-s3:/etc/keepalived/keepalived.conf 注:拷貝過去的文件須要進行修改 state BACKUP (兩個節點的狀態均要改成 BACKUP 備份) priority ? (優先級兩個節點不可以一致) 修改完成之後咱們就能夠啓動keepalive服務(三個節點均須要執行) [root@master-s1 ~]# systemctl start keepalived [root@master-s1 ~]# systemctl enable keepalived 啓動完成之後,通常狀況下只有咱們master-s1節點上會存在集羣的IP地址 [root@master-s1 ~]# ip a ens33: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000 ....... valid_lft forever preferred_lft forever inet 192.168.1.100/32 scope global ens33 ....... 測試,當咱們關閉主節點的keepalive服務,看看從節點上會不會出現集羣的IP地址(同一個時刻,集羣的IP地址會根據優先級來肯定存在於那一臺服務器上) [root@master-s1 ~]# systemctl stop keepalived [root@slaver-s2 ~]# ip a ens33: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000 link/ether 00:0c:29:bd:6e:43 brd ff:ff:ff:ff:ff:ff inet 192.168.1.20/24 brd 192.168.1.255 scope global noprefixroute ens33 valid_lft forever preferred_lft forever inet 192.168.1.100/32 scope global ens33 valid_lft forever preferred_lft forever
登陸測試
[root@master-s1 ~]# mysql -uroot -p -P3307 -h192.168.1.100 Enter password: ERROR 1130 (HY000): Host 'master-s1' is not allowed to connect to this MariaDB server //若是在登陸的時候遇到這個問題 解決方法以下: [root@master-s1 ~]# mysql -uroot -p //登陸數據庫 Enter password: MariaDB [(none)]> grant all privileges on *.* to 'root'@'%' identified by '123' with grant option; Query OK, 0 rows affected (0.00 sec) MariaDB [(none)]> flush privileges; Query OK, 0 rows affected (0.00 sec) 而後咱們在登陸測試便可 [root@master-s1 ~]# mysql -uroot -p -P3307 -h192.168.1.100 //輸入對應的端口號以及集羣IP地址便可登陸成功 Enter password: Welcome to the MariaDB monitor. Commands end with ; or \g. Your MariaDB connection id is 15 Server version: 10.1.20-MariaDB MariaDB Server Copyright (c) 2000, 2016, Oracle, MariaDB Corporation Ab and others. Type 'help;' or '\h' for help. Type '\c' to clear the current input statement. MariaDB [(none)]>
至此,mariadb數據庫高可用就此搭建完成了,若有錯誤,請指出改正