MySQL數(shù)據(jù)庫(kù)實(shí)現(xiàn)MMM高可用群集架構(gòu)
概念
MMM(Master-Master replication managerfor Mysql,Mysql主主復(fù)制管理器)是一套靈活的腳本程序,基于perl實(shí)現(xiàn),用來(lái)對(duì)mysql replication進(jìn)行監(jiān)控和故障遷移,并能管理mysql Master-Master復(fù)制的配置(同一時(shí)間只有一個(gè)節(jié)點(diǎn)是可寫(xiě)的)。
MMM高可用架構(gòu)說(shuō)明
- mmm_mond:監(jiān)控進(jìn)程,負(fù)責(zé)所有的監(jiān)控工作,決定和處理所有節(jié)點(diǎn)角色活動(dòng)。此腳本需要在監(jiān)管機(jī)上運(yùn)行。
- mmm_agentd:運(yùn)行在每個(gè)mysql服務(wù)器上的代理進(jìn)程,完成監(jiān)控的探針工作和執(zhí)行簡(jiǎn)單的遠(yuǎn)端服務(wù)設(shè)置。此腳本需要在被監(jiān)管機(jī)上運(yùn)行。
- mmm_control:一個(gè)簡(jiǎn)單的腳本,提供管理mmm_mond進(jìn)程的命令。
- mysql-mmm的監(jiān)管端會(huì)提供多個(gè)虛擬IP(VIP),包括一個(gè)可寫(xiě)VIP,多個(gè)可讀VIP,通過(guò)監(jiān)管的管理,這些IP會(huì)綁定在可用mysql之上,當(dāng)某一臺(tái)mysql宕機(jī)時(shí),監(jiān)管會(huì)將VIP遷移至其他mysql。在整個(gè)監(jiān)管過(guò)程中,需要在mysql中添加相關(guān)授權(quán)用戶(hù),以便讓mysql可以支持監(jiān)理機(jī)的維護(hù)。授權(quán)的用戶(hù)包括一個(gè)mmm_monitor用戶(hù)和一個(gè)mmm_agent用戶(hù)。
MMM的優(yōu)缺點(diǎn)
優(yōu)點(diǎn): 高可用性,擴(kuò)展性好,出現(xiàn)故障自動(dòng)切換,對(duì)于主主同步,在同一時(shí)間只提供一臺(tái)數(shù)據(jù)庫(kù)寫(xiě)操作,保證的數(shù)據(jù)的一致性。
缺點(diǎn): Monitor節(jié)點(diǎn)是單點(diǎn),可以結(jié)合Keepalived實(shí)現(xiàn)高可用,對(duì)主機(jī)的數(shù)量有要求,需要實(shí)現(xiàn)讀寫(xiě)分離,對(duì)程序來(lái)說(shuō)是個(gè)挑戰(zhàn)。
實(shí)驗(yàn)環(huán)境部署
第一步:在四臺(tái)服務(wù)器上都需要安裝MySQL數(shù)據(jù)庫(kù)
1.配置ALI云源,然后安裝epel-release源
[root@localhost ~]# wget -O /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-7.repo [root@localhost ~]# yum -y install epel-release [root@localhost ~]# yum clean all && yum makecache
2.搭建本地yum源
#安裝數(shù)據(jù)庫(kù) [root@localhost ~]# yum -y install mariadb-server mariadb #關(guān)閉防火墻和安全功能 [root@localhost ~]# systemctl stop firewalld.service [root@localhost ~]# setenforce 0 #開(kāi)啟數(shù)據(jù)庫(kù) [root@localhost ~]# systemctl start mariadb.service
3.修改ml主配置文件
[root@localhost ~]# vim /etc/my.cnf #刪除前9行,添加以下內(nèi)容 [mysqld] log_error=/var/lib/mysql/mysql.err #錯(cuò)誤日志的文件位置 log=/var/lib/mysql/mysql_log.log #訪問(wèn)日志的文件位置 log_slow_queries=/var/lib/mysql_slow_queris.log #man日志的文件位置 binlog-ignore-db=mysql,information_schema #mysql,information_schema不生成二進(jìn)制日志文件 character_set_server=utf8 #字符集 log_bin=mysql_bin #二進(jìn)制日志文件功能開(kāi)啟 server_id=1 #不同主機(jī)id不同 log_slave_updates=true #授權(quán)同步 sync_binlog=1 #二進(jìn)制日志文件功能開(kāi)啟 auto_increment_increment=2 #自增量 auto_increment_offset=1 #起始值 [root@localhost ~]# systemctl restart mariadb.service [root@localhost ~]# netstat -natp | grep 3306
4.復(fù)制配置文件到其他三臺(tái)數(shù)據(jù)庫(kù)服務(wù)器,注意修改server_id
[root@localhost ~]# scp /etc/my.cnf root@192.168.142.134:etc/
5.進(jìn)入數(shù)據(jù)庫(kù),并查看日志文件信息
[root@localhost ~]# mysql #查看記錄日志文件名稱(chēng)和位置值 MariaDB [(none)]> show master status; +------------------+----------+--------------+--------------------------+ | File | Position | Binlog_Do_DB | Binlog_Ignore_DB | +------------------+----------+--------------+--------------------------+ | mysql_bin.000001 | 245| | mysql,information_schema | +------------------+----------+--------------+--------------------------+ 1 row in set (0.00 sec)
6.在m1和m2上互相授予訪問(wèn)的權(quán)限,并授權(quán)同步日志
#在m1和m2上互相授予訪問(wèn)的權(quán)限 MariaDB [(none)]> grant replication slave on *.* to 'replication'@'192.168.142.%' identified by '123456'; #在m1上指定m2的日志文件名和位置參數(shù) MariaDB [(none)]> change master to master_host='192.168.142.134',master_user='replication',master_password='123456',master_log_file='mysql_bin.000001',master_log_pos=245; #在m2上指定m1的日志文件名和位置參數(shù) MariaDB [(none)]> change master to master_host='192.168.142.131',master_user='replication',master_password='123456',master_log_file='mysql_bin.000001',master_log_pos=245;
7.在m1上開(kāi)啟同步功能
MariaDB [(none)]> start slave;
8.查看同步狀態(tài),兩臺(tái)主服務(wù)器都要看到Y(jié)es
MariaDB [(none)]> show slave status\G; Slave_IO_Running: Yes Slave_SQL_Running: Yes
9.在m1上創(chuàng)建數(shù)據(jù)庫(kù)
MariaDB [(none)]> create database school;
10.在m2上查看同步數(shù)據(jù)庫(kù)
MariaDB [(none)]> show databases; +--------------------+ | Database | +--------------------+ | information_schema | | mysql | | performance_schema | | school | | test | +--------------------+ 5 rows in set (0.00 sec)
11.在兩臺(tái)從上做-注意日志文件和位置參數(shù)的改變(都指向m1)
MariaDB [(none)]> change master to master_host='192.168.142.131',master_user='replication',master_password='123456',master_log_file='mysql_bin.000001',master_log_pos=245;
12.開(kāi)啟同步功能
MariaDB [(none)]> start slave; #在從服務(wù)器上查看同步數(shù)據(jù)信息 MariaDB [(none)]> show databases; +--------------------+ | Database | +--------------------+ | information_schema | | mysql | | performance_schema | | school | | test | +--------------------+ 5 rows in set (0.00 sec)
13.在四臺(tái)服務(wù)器上安裝MMM相關(guān)軟件
[root@localhost ~]# yum -y install mysql-mmm*
14.配置mmm_common.conf配置文件
[root@localhost ~]# vim /etc/mysql-mmm/mmm_common.conf <host default> #修改網(wǎng)卡為ens33 cluster_interface ens33 pid_path /run/mysql-mmm-agent.pid bin_path /usr/libexec/mysql-mmm/ replication_user replicantion #修改授權(quán)密碼 replication_password 123456 agent_user mmm_agent #修改代理授權(quán)密碼 agent_password 123456 </host> #指定四臺(tái)服務(wù)器的角色與IP地址 <host db1> ip 192.168.142.131 mode master peer db2 </host> <host db2> ip 192.168.142.134 mode master peer db1 </host> <host db3> ip 192.168.142.130 mode slave </host> <host db4> ip 192.168.142.135 mode slave </host> #設(shè)定主服務(wù)器虛擬IP <role writer> hosts db1, db2 ips 192.168.142.250 mode exclusive </role> #設(shè)定從服務(wù)器虛擬IP <role reader> hosts db3, db4 ips 192.168.142.251, 192.168.142.252 mode balanced </role>
15.從m1上面復(fù)制配置文件到其他三臺(tái)服務(wù)器
[root@localhost ~]# scp mmm_common.conf/mmm_common.conf root@192.168.142.134:/etc/mysql-mmm/mmm_common.conf [root@localhost ~]# scp mmm_common.conf/mmm_common.conf root@192.168.142.130:/etc/mysql-mmm/mmm_common.conf [root@localhost ~]# scp mmm_common.conf/mmm_common.conf root@192.168.142.135:/etc/mysql-mmm/mmm_common.conf
第二步:配置monitor監(jiān)控服務(wù)器
1.安裝epel-release源以及MMM相關(guān)軟件
[root@localhost ~]# systemctl stop firewalld.service [root@localhost ~]# setenforce 0 [root@localhost ~]# wget -O /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-7.repo [root@localhost ~]# yum -y install epel-release [root@localhost ~]# yum clean all && yum makecache [root@localhost ~]# yum -y install mysql-mmm*
2.從m1上面復(fù)制配置文件到監(jiān)控服務(wù)器
[root@localhost ~]# scp mmm_common.conf root@192.168.142.134:/etc/mysql-mmm/
3.配置mmm_common.conf配置文件
[root@localhost ~]# vim /etc/mysql-mmm/mmm_mon.conf <monitor> ip 127.0.0.1 pid_path /run/mysql-mmm-monitor.pid bin_path /usr/libexec/mysql-mmm status_path /var/lib/mysql-mmm/mmm_mond.status #指向四臺(tái)服務(wù)器的IP地址 ping_ips 192.168.142.131,192.168.142.134,192.168.142.130,192.168.142.135 auto_set_online 10
4.在所有數(shù)據(jù)庫(kù)服務(wù)器上為mmm_agent授權(quán)
MariaDB [(none)]> grant super, replication client, process on *.* to 'mmm_agent'@'192.168.142.%' identified by '123456'; Query OK, 0 rows affected (0.02 sec)
5.在所有數(shù)據(jù)庫(kù)服務(wù)器上為mmm_moniter授權(quán)
MariaDB [(none)]> grant replication client on *.* to 'mmm_monitor'@'192.168.18.%' identified by '123456'; Query OK, 0 rows affected (0.02 sec)
6.刷新數(shù)據(jù)庫(kù)、在所有數(shù)據(jù)庫(kù)服務(wù)器上設(shè)定代理名稱(chēng)
[root@localhost ~]# vim /etc/mysql-mmm/mmm_agent.conf #修改代理配置文件 #m1中默認(rèn)名稱(chēng) this db1 #m2中名稱(chēng) this db2 #s1中名稱(chēng) this db3 #s2中名稱(chēng) this db4
7.所有數(shù)據(jù)庫(kù)服務(wù)器上啟動(dòng)代理功能并設(shè)定開(kāi)機(jī)自啟動(dòng)
#啟動(dòng)代理功能 [root@localhost ~]# systemctl start mysql-mmm-agent.service #設(shè)置開(kāi)機(jī)自啟動(dòng) [root@localhost ~]# systemctl enable mysql-mmm-agent.service
8.在monitor服務(wù)器開(kāi)啟監(jiān)控服務(wù)、查看各節(jié)點(diǎn)的情況
[root@localhost mysql-mmm]# systemctl start mysql-mmm-monitor.service [root@localhost ~]# mmm_control show db1(192.168.142.131) master/ONLINE. Roles: writer(192.168.142.250) db2(192.168.142.134) master/ONLINE. Roles: db3(192.168.142.130) slave/ONLINE. Roles: reader(192.168.142.252) db4(192.168.142.135) slave/ONLINE. Roles: reader(192.168.142.251)
9、檢測(cè)所有服務(wù)器狀態(tài)
[root@localhost ~]# mmm_control checks all db4 ping [last change: 2019/11/25 18:23:03] OK db4 mysql [last change: 2019/11/25 18:23:03] OK db4 rep_threads [last change: 2019/11/25 18:23:03] OK db4 rep_backlog [last change: 2019/11/25 18:23:03] OK: Backlog is null db2 ping [last change: 2019/11/25 18:23:03] OK db2 mysql [last change: 2019/11/25 18:59:01] OK db2 rep_threads [last change: 2019/11/25 18:59:01] OK db2 rep_backlog [last change: 2019/11/25 18:59:01] OK: Backlog is null db3 ping [last change: 2019/11/25 18:23:03] OK db3 mysql [last change: 2019/11/25 18:59:01] OK db3 rep_threads [last change: 2019/11/25 18:59:01] OK db3 rep_backlog [last change: 2019/11/25 18:59:01] OK: Backlog is null db1 ping [last change: 2019/11/25 18:23:03] OK db1 mysql [last change: 2019/11/25 18:59:01] OK db1 rep_threads [last change: 2019/11/25 18:59:01] OK db1 rep_backlog [last change: 2019/11/25 18:59:01] OK: Backlog is null
第三、故障測(cè)試
1、模擬m1服務(wù)器宕機(jī),停止服務(wù)
[root@localhost ~]# systemctl stop mariadb.service
2.當(dāng)m1服務(wù)器宕機(jī)后,m2接收虛擬IP繼續(xù)提供服務(wù)
[root@localhost ~]# mmm_control show db1(192.168.142.131) master/ONLINE. Roles: db2(192.168.142.134) master/ONLINE. Roles: writer(192.168.142.250) db3(192.168.142.130) slave/ONLINE. Roles: reader(192.168.142.252) db4(192.168.142.135) slave/ONLINE. Roles: reader(192.168.142.251)
3.當(dāng)s1服務(wù)器宕機(jī)后,s2接收虛擬IP繼續(xù)提供服務(wù)
[root@localhost ~]# mmm_control show db1(192.168.142.131) master/ONLINE. Roles: writer(192.168.142.250) db2(192.168.142.134) master/ONLINE. Roles: db3(192.168.142.130) slave/HARD_OFFLINE. Roles: db4(192.168.142.135) slave/ONLINE. Roles: reader(192.168.142.251), reader(192.168.142.252
4.在m1服務(wù)器上為監(jiān)控服務(wù)器地址授權(quán)登錄
MariaDB [(none)]> grant all on *.* to 'root'@'192.168.142.136' identified by '123456'; Query OK, 0 rows affected (0.00 sec)
5.在監(jiān)控服務(wù)器上安裝數(shù)據(jù)庫(kù)客戶(hù)端
[root@localhost ~]# yum -y install mariadb
6.在監(jiān)控服務(wù)器上使用指定用戶(hù)登錄數(shù)據(jù)庫(kù),并創(chuàng)建數(shù)據(jù)信息
[root@localhost ~]# mysql -u root -p -h 192.168.142.250 Enter password: #輸入密碼即可 #創(chuàng)建一個(gè)數(shù)據(jù)庫(kù) MariaDB [(none)]> create database BDQN; Query OK, 1 row affected (0.01 sec)
7.在所有數(shù)據(jù)庫(kù)上都能查看及時(shí)同步到的數(shù)據(jù)信息
MariaDB [(none)]> show databases; #查看數(shù)據(jù)庫(kù) +--------------------+ | Database | +--------------------+ | information_schema | | BDQN | #同步到的BDQN數(shù)據(jù)庫(kù) | mysql | | performance_schema | | school | | test | +--------------------+ 6 rows in set (0.00 sec)
到此MMM群集架構(gòu)已完成,謝謝閱讀!!!
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
詳解mysql中的concat相關(guān)函數(shù)
這篇文章主要介紹了mysql中的concat相關(guān)函數(shù),本文給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2018-11-11MySQL之臨時(shí)表的實(shí)現(xiàn)示例
MySQL臨時(shí)表是存儲(chǔ)在內(nèi)存或者磁盤(pán)上的臨時(shí)數(shù)據(jù)表,它們的生命周期只限于當(dāng)前數(shù)據(jù)庫(kù)會(huì)話,臨時(shí)表的創(chuàng)建和使用方式與普通表類(lèi)似,本文就詳細(xì)的介紹了MySQL之臨時(shí)表,感興趣的可以了解一下2023-08-08MYSQL必知必會(huì)讀書(shū)筆記第六章之過(guò)濾數(shù)據(jù)
本文給大家分享MYSQL必知必會(huì)讀書(shū)筆記第六章之過(guò)濾數(shù)據(jù)的相關(guān)知識(shí),非常實(shí)用,特此分享到腳本之家平臺(tái),供大家參考2016-05-05計(jì)算機(jī)二級(jí)考試MySQL知識(shí)點(diǎn) mysql alter命令
這篇文章主要為大家詳細(xì)介紹了計(jì)算機(jī)二級(jí)考試MySQL知識(shí)點(diǎn),詳細(xì)介紹了mysql中alter命令的使用方法,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-08-08Mysql數(shù)據(jù)庫(kù)連接失敗SSLException: Unsupported record
這篇文章主要介紹了Mysql數(shù)據(jù)庫(kù)連接失敗SSLException: Unsupported record version Unknown-0.0問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-06-06mysql 8.0.16 Win10 zip版本安裝配置圖文教程
這篇文章主要為大家詳細(xì)介紹了mysql 8.0 Win10 zip版本安裝配置圖文教程,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-06-06MYSQL數(shù)據(jù)庫(kù)表結(jié)構(gòu)優(yōu)化方法詳解
這篇文章主要介紹了MYSQL數(shù)據(jù)庫(kù)表結(jié)構(gòu)優(yōu)化方法,總結(jié)分析了mysql針對(duì)表結(jié)構(gòu)優(yōu)化的數(shù)據(jù)類(lèi)型選擇、范式化操作、表的拆分等相關(guān)使用技巧,需要的朋友可以參考下2019-08-08