日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 运维知识 > 数据库 >内容正文

数据库

基于Multi Master MySQL(MMM)实现Mariadb10读写分离

發布時間:2025/3/20 数据库 34 豆豆
生活随笔 收集整理的這篇文章主要介紹了 基于Multi Master MySQL(MMM)实现Mariadb10读写分离 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

----本文大綱

  • 簡介

  • 資源配置

  • 拓撲圖

  • 實現過程


====================

一、簡介

MMM即Master-Master Replication Manager for MySQL(mysql主主復制管理器)關于mysql主主復制配置的監控、故障轉移和管理的一套可伸縮的腳本套件(在任何時候只有一個節點可以被寫入),這個套件也能對居于標準的主從配置的任意數量的從服務器進行讀負載均衡,所以你可以用它來在一組居于復制的服務器啟動虛擬ip,除此之外,它還有實現數據備份、節點之間重新同步功能的腳本。MySQL本身沒有提供replication failover的解決方案,通過MMM方案能實現服務器的故障轉移,從而實現mysql的高可用。MMM不僅能提供浮動IP的功能,更可貴的是如果當前的主服務器掛掉后,會將你后端的從服務器自動轉向新的主服務器進行同步復制,不用手工更改同步配置。這個方案是目前比較成熟的解決方案。

方案優缺點

優點:安全性、穩定性高,可擴展性好,高可用,當主服務器掛掉以后,另一個主立即接管,其他的從服務器能自動切換,不用人工干預。

缺點:至少三個節點,對主機的數量有要求,需要實現讀寫分離,對程序來說是個挑戰。

二、資源配置

  • 主機屬性

系統名字角色主機名ip地址關系
Centos6.5x86_64 DB1
Masteressun.mariadb1.com192.168.1.109與DB2互為主從
Centos6.5x86_64DB2Masteressun.mariadb2.com 192.168.1.112
與DB1互為主從
Centos6.5x86_64DB3Slaveessun.mariadb3.com 192.168.1.113
DB1的從庫
Centos6.5x86_64MonitorMonitoressun.monitor.com 192.168.1.116
監控所有主機

  • 虛擬ip(VIP)

DB1 ? ?192.168.1.109 ?`192.168.1.24

DB2 ? ?192.168.1.112 ? ?192.168.1.24,192.168.1.22

DB3 ? ?192.168.1.113 ? ?192.168.1.23

三、拓撲圖

四、實現過程

1、配置DB1

修改配置文件/etc/my.cnf,添加如下語句

1 2 3 4 5 6 7 server-id=1 log_bin=/mariadb/data/mysql-bin binlog_format=row log-slave-updates sync_binlog=1 auto_increment_increment=2 auto_increment_offset=1

授權用戶

1 2 3 4 MariaDB [(none)]>?grant?replication slave,replication client?on?*.*?to?'repluser'@'192.168.1.112'?identified?by?'replpass'; Query OK, 0?rows?affected (0.12 sec) MariaDB [(none)]>?grant?replication slave,replication client?on?*.*?to?'repluser'@'192.168.1.113'?identified?by?'replpass'; Query OK, 0?rows?affected (0.00 sec)

查看binlog日志標記

1 2 3 4 5 6 7 MariaDB [(none)]> show master status; +------------------+----------+--------------+------------------+ | File???????????? | Position | Binlog_Do_DB | Binlog_Ignore_DB | +------------------+----------+--------------+------------------+ | mysql-bin.000003 |????? 756 |????????????? |????????????????? | +------------------+----------+--------------+------------------+ 1 row?in?set?(0.00 sec)

2、配置DB2

修改配置文件/etc/my.cnf,添加如下語句

1 2 3 4 5 6 7 log-bin=mysql-bin binlog_format=ROW log-slave-updates sync_binlog=1 auto_increment_increment=2 auto_increment_offset=2 server-id=2

授權用戶

1 2 MariaDB [(none)]>?grant?replication slave,replication client?on?*.*?to?'repluser'@'192.168.1.109'?identified?by?'replpass'; Query OK, 0?rows?affected (0.15 sec)

查看binlog日志標記

1 2 3 4 5 6 7 MariaDB [(none)]> show master status; +------------------+----------+--------------+------------------+ | File???????????? | Position | Binlog_Do_DB | Binlog_Ignore_DB | +------------------+----------+--------------+------------------+ | mysql-bin.000007 |????? 548 |????????????? |????????????????? | +------------------+----------+--------------+------------------+ 1 row?in?set?(0.00 sec)

連接DB1

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 MariaDB [(none)]> change master?to?master_host='192.168.1.109',master_user='repluser',master_password='replpass',master_log_file='mysql-bin.000003',master_log_pos=756; Query OK, 0?rows?affected (0.06 sec) MariaDB [(none)]> start slave; Query OK, 0?rows?affected (0.06 sec) MariaDB [(none)]> show slave status\G *************************** 1. row *************************** ???????????????Slave_IO_State: Waiting?for?master?to?send event ??????????????????Master_Host: 192.168.1.109 ??????????????????Master_User: repluser ??????????????????Master_Port: 3306 ????????????????Connect_Retry: 60 ??????????????Master_Log_File: mysql-bin.000003 ??????????Read_Master_Log_Pos: 756 ???????????????Relay_Log_File: essun-relay-bin.000002 ????????????????Relay_Log_Pos: 535 ????????Relay_Master_Log_File: mysql-bin.000003 ?????????????Slave_IO_Running: Yes ????????????Slave_SQL_Running: Yes ??????????????Replicate_Do_DB: ??????????Replicate_Ignore_DB: ???????????Replicate_Do_Table: ???????Replicate_Ignore_Table: ??????Replicate_Wild_Do_Table: ??Replicate_Wild_Ignore_Table: ???????????????????Last_Errno: 0 ???????????????????Last_Error: ?????????????????Skip_Counter: 0 ??????????Exec_Master_Log_Pos: 756 ??????????????Relay_Log_Space: 832 ??????????????Until_Condition: None ???????????????Until_Log_File: ????????????????Until_Log_Pos: 0 ???????????Master_SSL_Allowed: Yes ???????????Master_SSL_CA_File: /etc/slave/cacert.pem ???????????Master_SSL_CA_Path: ??????????????Master_SSL_Cert: /etc/slave/mysql.crt ????????????Master_SSL_Cipher: ???????????????Master_SSL_Key: /etc/slave/mysql.key ????????Seconds_Behind_Master: 0 Master_SSL_Verify_Server_Cert:?No ????????????????Last_IO_Errno: 0 ????????????????Last_IO_Error: ???????????????Last_SQL_Errno: 0 ???????????????Last_SQL_Error: ??Replicate_Ignore_Server_Ids: ?????????????Master_Server_Id: 1 ???????????????Master_SSL_Crl: /etc/slave/cacert.pem ???????????Master_SSL_Crlpath: ???????????????????Using_Gtid:?No ??????????????????Gtid_IO_Pos: 1 row?in?set?(0.00 sec)

3、配置DB3

修改配置文件/etc/my.cnf添加如下語句

1 2 3 4 server-id=3 log-bin=mysql-bin log-slave-updates relay-log=relay-log-bin

連接DB1

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 MariaDB [(none)]> change master?to?master_host='192.168.1.109',master_user='repluser',master_password='replpass',master_log_file='mysql-bin.000003',master_log_pos=756; Query OK, 0?rows?affected (0.03 sec) MariaDB [(none)]> start slave; Query OK, 0?rows?affected (0.00 sec) MariaDB [(none)]> show slave status\G *************************** 1. row *************************** ???????????????Slave_IO_State: Waiting?for?master?to?send event ??????????????????Master_Host: 192.168.1.109 ??????????????????Master_User: repluser ??????????????????Master_Port: 3306 ????????????????Connect_Retry: 60 ??????????????Master_Log_File: mysql-bin.000003 ??????????Read_Master_Log_Pos: 756 ???????????????Relay_Log_File: relay-log-bin.000002 ????????????????Relay_Log_Pos: 535 ????????Relay_Master_Log_File: mysql-bin.000003 ?????????????Slave_IO_Running: Yes ????????????Slave_SQL_Running: Yes ??????????????Replicate_Do_DB: ??????????Replicate_Ignore_DB: ???????????Replicate_Do_Table: ???????Replicate_Ignore_Table: ??????Replicate_Wild_Do_Table: ??Replicate_Wild_Ignore_Table: ???????????????????Last_Errno: 0 ???????????????????Last_Error: ?????????????????Skip_Counter: 0 ??????????Exec_Master_Log_Pos: 756 ??????????????Relay_Log_Space: 830 ??????????????Until_Condition: None ???????????????Until_Log_File: ????????????????Until_Log_Pos: 0 ???????????Master_SSL_Allowed: Yes ???????????Master_SSL_CA_File: /etc/slave/cacert.pem ???????????Master_SSL_CA_Path: ??????????????Master_SSL_Cert: /etc/slave/mysql.crt ????????????Master_SSL_Cipher: ???????????????Master_SSL_Key: /etc/slave/mysql.key ????????Seconds_Behind_Master: 0 Master_SSL_Verify_Server_Cert:?No ????????????????Last_IO_Errno: 0 ????????????????Last_IO_Error: ???????????????Last_SQL_Errno: 0 ???????????????Last_SQL_Error: ??Replicate_Ignore_Server_Ids: ?????????????Master_Server_Id: 1 ???????????????Master_SSL_Crl: /etc/slave/cacert.pem ???????????Master_SSL_Crlpath: ???????????????????Using_Gtid:?No ??????????????????Gtid_IO_Pos: 1 row?in?set?(0.00 sec)

DB1連接DB2

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 MariaDB [(none)]> change master?to?master_host='192.168.1.112',master_user='repluser',master_password='replpass',master_log_file='mysql-bin.000007',master_log_pos=548; Query OK, 0?rows?affected (0.03 sec) MariaDB [(none)]> start slave; Query OK, 0?rows?affected (0.00 sec) MariaDB [(none)]> show slave status\G *************************** 1. row *************************** ???????????????Slave_IO_State: Waiting?for?master?to?send event ??????????????????Master_Host: 192.168.1.112 ??????????????????Master_User: repluser ??????????????????Master_Port: 3306 ????????????????Connect_Retry: 60 ??????????????Master_Log_File: mysql-bin.000007 ??????????Read_Master_Log_Pos: 548 ???????????????Relay_Log_File: essun-relay-bin.000002 ????????????????Relay_Log_Pos: 535 ????????Relay_Master_Log_File: mysql-bin.000007 ?????????????Slave_IO_Running: Yes ????????????Slave_SQL_Running: Yes ??????????????Replicate_Do_DB: ??????????Replicate_Ignore_DB: ???????????Replicate_Do_Table: ???????Replicate_Ignore_Table: ??????Replicate_Wild_Do_Table: ??Replicate_Wild_Ignore_Table: ???????????????????Last_Errno: 0 ???????????????????Last_Error: ?????????????????Skip_Counter: 0 ??????????Exec_Master_Log_Pos: 548 ??????????????Relay_Log_Space: 832 ??????????????Until_Condition: None ???????????????Until_Log_File: ????????????????Until_Log_Pos: 0 ???????????Master_SSL_Allowed:?No ???????????Master_SSL_CA_File: ???????????Master_SSL_CA_Path: ??????????????Master_SSL_Cert: ????????????Master_SSL_Cipher: ???????????????Master_SSL_Key: ????????Seconds_Behind_Master: 0 Master_SSL_Verify_Server_Cert:?No ????????????????Last_IO_Errno: 0 ????????????????Last_IO_Error: ???????????????Last_SQL_Errno: 0 ???????????????Last_SQL_Error: ??Replicate_Ignore_Server_Ids: ?????????????Master_Server_Id: 2 ???????????????Master_SSL_Crl: ???????????Master_SSL_Crlpath: ???????????????????Using_Gtid:?No ??????????????????Gtid_IO_Pos: 1 row?in?set?(0.00 sec)

4、測試

在DB2中建立一個數據庫testdb

1 2 3 4 5 6 7 8 MariaDB [mysql]>?create?database?testdb; Query OK, 1 row affected (0.02 sec) MariaDB [mysql]> use testdb; Database?changed MariaDB [testdb]>?create?table?t1 (name?char(40)?not?null,age?int?not?null); Query OK, 0?rows?affected (0.17 sec) MariaDB [testdb]>?insert?t1?values?('king',24); Query OK, 1 row affected (0.01 sec)

在DB1中對testdb,插入一條數據

1 2 MariaDB [testdb]>?insert?t1?values?('tom',24); Query OK, 1 row affected (0.01 sec)

在DB3中查看結果

1 2 3 4 5 6 7 8 9 MariaDB [(none)]>?select?*?from?testdb.t1; +------+-----+ |?name?| age | +------+-----+ | tom? |? 24 | | king |? 24 | +------+-----+ 2?rows?in?set?(0.00 sec) MariaDB [(none)]>
OK三臺DB的主從配置正常工作

5、安裝mysql-mmm-agent

在DB1~3上安裝mysql-mmmo-agent

注:mysql-mmm-agent是在epel源中,所以要下載EPEL源安裝包即可http://download.fedoraproject.org/pub/epel/6/i386/repoview/epel-release.html

下載對應的版本就可以的。

1 2 #rpm -ivh epel-release-6-8.noarch.rpm yum -y?install?mysql-mmm-agent

每一個節點都要安裝

在每一個節點上要給Monitor授權用戶

1 2 3 4 5 6 MariaDB [(none)]> GRANT SUPER, REPLICATION CLIENT, PROCESS ON *.* TO?'mmm_agent'@'192.168.1.116'???IDENTIFIED BY?'123456'; Query OK, 0 rows affected (0.01 sec) MariaDB [(none)]> GRANT REPLICATION CLIENT ON *.* TO?'mmm_monitor'@'192.168.1.116'?IDENTIFIED BY?'123456'; Query OK, 0 rows affected (0.01 sec) MariaDB [(none)]> GRANT REPLICATION CLIENT ON *.* TO?'repluser'@'192.168.1.116'?IDENTIFIED BY?'replpass'; Query OK, 0 rows affected (0.01 sec)

6、在Monitor節點上要安裝

1 #yum -y install mysql-mmm*

此包同樣也在epel源中

7、在Monitor端的設置/etc/mysql-mmm/mmm_common.conf

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 active_master_role????? writer ???????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????? <host default> ????cluster_interface?????? eth0 ????pid_path????????????????/var/run/mysql-mmm/mmm_agentd.pid ????bin_path????????????????/usr/libexec/mysql-mmm/ ????replication_user??????? repluser?#復制用戶 ????replication_password??? replpass?#復制密碼 ????agent_user????????????? mmm_agent?#代理用戶 ????agent_password????????? 123456?#代理用戶的密碼 </host> ???????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????? <host db1> ????ip????? 192.168.1.109 ????mode??? master ????peer??? db2 </host> ???????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????? <host db2> ????ip????? 192.168.1.112 ????mode??? master ????peer??? db1 </host> ???????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????? <host db3> ????ip????? 192.168.1.113 ????mode??? slave </host> ???????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????? <role writer> ????hosts?? db1, db2 ????ips???? 192.168.1.24 ????mode??? exclusive?#排它 </role> ???????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????? <role reader> ????hosts??? db2, db3 ????ips????? 192.168.1.22, 192.168.1.23 ????mode??? balanced?#均衡 </role>

將此文件分發到各DB1~3中的/etc/mysql-mmm/下

8、每一個DB中都會有mmm_agent的配置文件,編輯mmm_agent.conf

在數據庫服務器上,還有一個mmm_agent.conf需要修改,其內容是:

1 2 3 4 5 include mmm_common.conf # The 'this' variable refers to this server.? Proper operation requires # that 'this' server (db1 by default), as well as all other servers, have the # proper IP addresses set in mmm_common.conf. this db2

第一行表示:將之前Monitor中的mmm_common.conf文件載入到此文件中,供此文件中的參數設用。

最后一行標記此主機的角色(引用mmm_common.conf中的host段)在不同的數據庫服務器上要分別改為db1和db3否則代理就會無法啟動。

9、編輯mmm_mon.confg

在Monitor上,修改mmm_mon.conf文件,修改后內容為:

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 include mmm_common.conf <monitor> ????ip????????????????? 192.168.1.116?#當前monitor主機地址 ????pid_path????????????/var/run/mysql-mmm/mmm_mond.pid ????bin_path????????????/usr/libexec/mysql-mmm ????status_path?????????/var/lib/mysql-mmm/mmm_mond.status ????ping_ips??????????? 192.168.1.109, 192.168.1.112?#真實DB地址 ????auto_set_online???? 10 ????# The kill_host_bin does not exist by default, though the monitor will ????# throw a warning about it missing.? See the section 5.10 "Kill Host ????# Functionality" in the PDF documentation. ????# ????# kill_host_bin???? /usr/libexec/mysql-mmm/monitor/kill_host ????# </monitor> <host default> ????monitor_user??????? mmm_monitor?#監控DB的用戶名 ????monitor_password??? 123456?#密碼 </host> debug 0?#關閉debug功能,如果程序無法監控得到,可以使用debug 1查錯

10、啟動MMM

在各DB端啟動mmm-agent

1 2 #/etc/init.d/mysql-mmm-agent start #echo "/etc/init.d/mysql-mmm-agent start" >> /etc/rc.local

在Monitor端啟動監控程序

1 2 3 #cd /etc/init.d/ # chkconfig mysql-mmm-monitor on # service mysql-mmm-monitor start

過幾秒鐘,就可以使用mmm_control show查看在線監控端(DB)了

1 2 3 4 5 6 [root@essun ~]# service mysql-mmm-monitor status mmm_mond (pid? 5395) is running... [root@essun ~]# mmm_control show ??db1(192.168.1.109) master/ONLINE. Roles: ??db2(192.168.1.112) master/ONLINE. Roles: reader(192.168.1.22), writer(192.168.1.24) ??db3(192.168.1.113) slave/ONLINE. Roles: reader(192.168.1.23)

注:可以使用

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 [root@essun ~]# mmm_control --help Invalid?command?'--help' Valid commands are: ????help????????????????????????????? - show this message ????ping??????????????????????????????-?ping?monitor ????show????????????????????????????? - show status ????checks [<host>|all [<check>|all]] - show checks status ????set_online <host>???????????????? -?set?host <host> online ????set_offline <host>??????????????? -?set?host <host> offline ????mode????????????????????????????? - print current mode. ????set_active??????????????????????? - switch into active mode. ????set_manual??????????????????????? - switch into manual mode. ????set_passive?????????????????????? - switch into passive mode. ????move_role [--force] <role> <host> - move exclusive role <role> to host <host> ????????????????????????????????????????(Only use --force?if?you know what you are doing!) ????set_ip <ip> <host>??????????????? -?set?role with ip <ip> to host <host>

查看mmm_control的可用參數

11、模擬DB2下線

Monitor當前狀態

讓DB2下線,當前可寫主機是db1,db3

db2沒有下線之前還可以讀寫,當下線之后,可寫的切換到DB1上了,所有讀的都到了db3上了

當DB2重新上線后的情況如下

注:DB1、DB同時只能一有個寫,一個讀!

========================================== Mariadb高可用演示完畢========================










本文轉自 jinlinger 51CTO博客,原文鏈接:http://blog.51cto.com/essun/1403294,如需轉載請自行聯系原作者

總結

以上是生活随笔為你收集整理的基于Multi Master MySQL(MMM)实现Mariadb10读写分离的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。