Home Database Mysql Tutorial MariaDB 10 (MySQL DB) 多主复制并实现读写分离_MySQL

MariaDB 10 (MySQL DB) 多主复制并实现读写分离_MySQL

Jun 01, 2016 pm 01:16 PM
mariadb none separation copy accomplish

MariaDB

二、资源配置

  • 主机属性

系统 名字 角色 主机名 ip地址 关系
Centos6.5x86_64 DB1 Master essun.mariadb1.com 192.168.1.109 与DB2互为主从
Centos6.5x86_64 DB2 Master essun.mariadb2.com 192.168.1.112 与DB1互为主从
Centos6.5x86_64 DB3 Slave essun.mariadb3.com 192.168.1.113 DB1的从库
Centos6.5x86_64 Monitor Monitor essun.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

三、拓扑图

wKiom1NbD-vBYTbaAAH9IMbCsBM351.jpg

四、实现过程

1、配置DB1

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

server-<span style="COLOR: rgb(0,0,255)">id</span>=<span style="COLOR: rgb(128,0,128)">1</span><span style="COLOR: rgb(0,0,0)">log_bin</span>=/mariadb/data/mysql-<span style="COLOR: rgb(0,0,0)">binbinlog_format</span>=<span style="COLOR: rgb(0,0,0)">rowlog</span>-slave-<span style="COLOR: rgb(0,0,0)">updates sync_binlog</span>=<span style="COLOR: rgb(128,0,128)">1</span><span style="COLOR: rgb(0,0,0)">auto_increment_increment</span>=<span style="COLOR: rgb(128,0,128)">2# <span>默认地,AUTO_INCREMENT 的开始值是 1,每条新记录递增 1。</span></span><span style="COLOR: rgb(0,0,0)">auto_increment_offset</span>=<span style="COLOR: rgb(128,0,128)">1 </span>
Copy after login

授权用户

MariaDB [(none)]> grant replication slave,replication client on *.* to <span style="COLOR: rgb(128,0,0)">'</span><span style="COLOR: rgb(128,0,0)">repluser</span><span style="COLOR: rgb(128,0,0)">'</span>@<span style="COLOR: rgb(128,0,0)">'</span><span style="COLOR: rgb(128,0,0)">192.168.1.112</span><span style="COLOR: rgb(128,0,0)">'</span> identified by <span style="COLOR: rgb(128,0,0)">'</span><span style="COLOR: rgb(128,0,0)">replpass</span><span style="COLOR: rgb(128,0,0)">'</span><span style="COLOR: rgb(0,0,0)">;Query OK, </span><span style="COLOR: rgb(128,0,128)">0</span> rows affected (<span style="COLOR: rgb(128,0,128)">0.12</span><span style="COLOR: rgb(0,0,0)"> sec)MariaDB [(none)]</span>> grant replication slave,replication client on *.* to <span style="COLOR: rgb(128,0,0)">'</span><span style="COLOR: rgb(128,0,0)">repluser</span><span style="COLOR: rgb(128,0,0)">'</span>@<span style="COLOR: rgb(128,0,0)">'</span><span style="COLOR: rgb(128,0,0)">192.168.1.113</span><span style="COLOR: rgb(128,0,0)">'</span> identified by <span style="COLOR: rgb(128,0,0)">'</span><span style="COLOR: rgb(128,0,0)">replpass</span><span style="COLOR: rgb(128,0,0)">'</span><span style="COLOR: rgb(0,0,0)">;Query OK, </span><span style="COLOR: rgb(128,0,128)">0</span> rows affected (<span style="COLOR: rgb(128,0,128)">0.00</span> sec)
Copy after login

查看binlog日志标记

MariaDB [(none)]><span style="COLOR: rgb(0,0,0)"> show master status;</span>+------------------+----------+--------------+------------------+| File | Position | Binlog_Do_DB | Binlog_Ignore_DB |+------------------+----------+--------------+------------------+| mysql-bin.<span style="COLOR: rgb(128,0,128)">000003</span> |<span style="COLOR: rgb(128,0,128)">756</span> |||+------------------+----------+--------------+------------------+<span style="COLOR: rgb(128,0,128)">1</span> row <span style="COLOR: rgb(0,0,255)">in</span> set (<span style="COLOR: rgb(128,0,128)">0.00</span> sec)
Copy after login

2、配置DB2

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

log-bin=mysql-<span style="COLOR: rgb(0,0,0)">binbinlog_format</span>=<span style="COLOR: rgb(0,0,0)">ROWlog</span>-slave-<span style="COLOR: rgb(0,0,0)">updatessync_binlog</span>=<span style="COLOR: rgb(128,0,128)">1</span><span style="COLOR: rgb(0,0,0)">auto_increment_increment</span>=<span style="COLOR: rgb(128,0,128)">2</span><span style="COLOR: rgb(0,0,0)">auto_increment_offset</span>=<span style="COLOR: rgb(128,0,128)">2</span><span style="COLOR: rgb(0,0,0)">server</span>-<span style="COLOR: rgb(0,0,255)">id</span>=<span style="COLOR: rgb(128,0,128)">2</span>
Copy after login

授权用户

MariaDB [(none)]> grant replication slave,replication client on *.* to <span style="COLOR: rgb(128,0,0)">'</span><span style="COLOR: rgb(128,0,0)">repluser</span><span style="COLOR: rgb(128,0,0)">'</span>@<span style="COLOR: rgb(128,0,0)">'</span><span style="COLOR: rgb(128,0,0)">192.168.1.109</span><span style="COLOR: rgb(128,0,0)">'</span> identified by <span style="COLOR: rgb(128,0,0)">'</span><span style="COLOR: rgb(128,0,0)">replpass</span><span style="COLOR: rgb(128,0,0)">'</span><span style="COLOR: rgb(0,0,0)">;Query OK, </span><span style="COLOR: rgb(128,0,128)">0</span> rows affected (<span style="COLOR: rgb(128,0,128)">0.15</span> sec)
Copy after login

查看binlog日志标记

MariaDB [(none)]><span style="COLOR: rgb(0,0,0)"> show master status;</span>+------------------+----------+--------------+------------------+| File | Position | Binlog_Do_DB | Binlog_Ignore_DB |+------------------+----------+--------------+------------------+| mysql-bin.<span style="COLOR: rgb(128,0,128)">000007</span> |<span style="COLOR: rgb(128,0,128)">548</span> |||+------------------+----------+--------------+------------------+<span style="COLOR: rgb(128,0,128)">1</span> row <span style="COLOR: rgb(0,0,255)">in</span> set (<span style="COLOR: rgb(128,0,128)">0.00</span> sec)
Copy after login
Copy after login

连接DB1

MariaDB [(none)]><span style="COLOR: rgb(0,0,0)"> show master status;</span>+------------------+----------+--------------+------------------+| File | Position | Binlog_Do_DB | Binlog_Ignore_DB |+------------------+----------+--------------+------------------+| mysql-bin.<span style="COLOR: rgb(128,0,128)">000007</span> |<span style="COLOR: rgb(128,0,128)">548</span> |||+------------------+----------+--------------+------------------+<span style="COLOR: rgb(128,0,128)">1</span> row <span style="COLOR: rgb(0,0,255)">in</span> set (<span style="COLOR: rgb(128,0,128)">0.00</span> sec)
Copy after login
Copy after login

3、配置DB3

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

server-<span style="COLOR: rgb(0,0,255)">id</span>=<span style="COLOR: rgb(128,0,128)">3</span><span style="COLOR: rgb(0,0,0)">log</span>-bin=mysql-<span style="COLOR: rgb(0,0,0)">binlog</span>-slave-<span style="COLOR: rgb(0,0,0)">updates<br></span><span>relay</span>-log=relay-log-bin
Copy after login

连接DB1

MariaDB [(none)]> change master to master_host=<span style="COLOR: rgb(128,0,0)">'</span><span style="COLOR: rgb(128,0,0)">192.168.1.109</span><span style="COLOR: rgb(128,0,0)">'</span>,master_user=<span style="COLOR: rgb(128,0,0)">'</span><span style="COLOR: rgb(128,0,0)">repluser</span><span style="COLOR: rgb(128,0,0)">'</span>,master_password=<span style="COLOR: rgb(128,0,0)">'</span><span style="COLOR: rgb(128,0,0)">replpass</span><span style="COLOR: rgb(128,0,0)">'</span>,master_log_file=<span style="COLOR: rgb(128,0,0)">'</span><span style="COLOR: rgb(128,0,0)">mysql-bin.000003</span><span style="COLOR: rgb(128,0,0)">'</span>,master_log_pos=<span style="COLOR: rgb(128,0,128)">756</span><span style="COLOR: rgb(0,0,0)">;Query OK, </span><span style="COLOR: rgb(128,0,128)">0</span> rows affected (<span style="COLOR: rgb(128,0,128)">0.03</span><span style="COLOR: rgb(0,0,0)"> sec)MariaDB [(none)]</span>><span style="COLOR: rgb(0,0,0)"> start slave;Query OK, </span><span style="COLOR: rgb(128,0,128)">0</span> rows affected (<span style="COLOR: rgb(128,0,128)">0.00</span><span style="COLOR: rgb(0,0,0)"> sec)MariaDB [(none)]</span>><span style="COLOR: rgb(0,0,0)"> show slave status/G</span>*************************** <span style="COLOR: rgb(128,0,128)">1</span>. row ***************************<span style="COLOR: rgb(0,0,0)"> Slave_IO_State: Waiting </span><span style="COLOR: rgb(0,0,255)">for</span><span style="COLOR: rgb(0,0,0)"> master to send eventMaster_Host: </span><span style="COLOR: rgb(128,0,128)">192.168</span>.<span style="COLOR: rgb(128,0,128)">1.109</span><span style="COLOR: rgb(0,0,0)">Master_User: repluserMaster_Port: </span><span style="COLOR: rgb(128,0,128)">3306</span><span style="COLOR: rgb(0,0,0)">Connect_Retry: </span><span style="COLOR: rgb(128,0,128)">60</span><span style="COLOR: rgb(0,0,0)">Master_Log_File: mysql</span>-bin.<span style="COLOR: rgb(128,0,128)">000003</span><span style="COLOR: rgb(0,0,0)">Read_Master_Log_Pos: </span><span style="COLOR: rgb(128,0,128)">756</span><span style="COLOR: rgb(0,0,0)"> Relay_Log_File: relay</span>-log-bin.<span style="COLOR: rgb(128,0,128)">000002</span><span style="COLOR: rgb(0,0,0)">Relay_Log_Pos: </span><span style="COLOR: rgb(128,0,128)">535</span><span style="COLOR: rgb(0,0,0)">Relay_Master_Log_File: mysql</span>-bin.<span style="COLOR: rgb(128,0,128)">000003</span><span style="COLOR: rgb(0,0,0)"> Slave_IO_Running: YesSlave_SQL_Running: YesReplicate_Do_DB:Replicate_Ignore_DB: Replicate_Do_Table: Replicate_Ignore_Table:Replicate_Wild_Do_Table:Replicate_Wild_Ignore_Table: Last_Errno: </span><span style="COLOR: rgb(128,0,128)">0</span><span style="COLOR: rgb(0,0,0)"> Last_Error: Skip_Counter: </span><span style="COLOR: rgb(128,0,128)">0</span><span style="COLOR: rgb(0,0,0)">Exec_Master_Log_Pos: </span><span style="COLOR: rgb(128,0,128)">756</span><span style="COLOR: rgb(0,0,0)">Relay_Log_Space: </span><span style="COLOR: rgb(128,0,128)">830</span><span style="COLOR: rgb(0,0,0)">Until_Condition: None Until_Log_File:Until_Log_Pos: </span><span style="COLOR: rgb(128,0,128)">0</span><span style="COLOR: rgb(0,0,0)"> Master_SSL_Allowed: Yes Master_SSL_CA_File: </span>/etc/slave/<span style="COLOR: rgb(0,0,0)">cacert.pem Master_SSL_CA_Path:Master_SSL_Cert: </span>/etc/slave/<span style="COLOR: rgb(0,0,0)">mysql.crtMaster_SSL_Cipher: Master_SSL_Key: </span>/etc/slave/<span style="COLOR: rgb(0,0,0)">mysql.keySeconds_Behind_Master: </span><span style="COLOR: rgb(128,0,128)">0</span><span style="COLOR: rgb(0,0,0)">Master_SSL_Verify_Server_Cert: NoLast_IO_Errno: </span><span style="COLOR: rgb(128,0,128)">0</span><span style="COLOR: rgb(0,0,0)">Last_IO_Error: Last_SQL_Errno: </span><span style="COLOR: rgb(128,0,128)">0</span><span style="COLOR: rgb(0,0,0)"> Last_SQL_Error:Replicate_Ignore_Server_Ids: Master_Server_Id: </span><span style="COLOR: rgb(128,0,128)">1</span><span style="COLOR: rgb(0,0,0)"> Master_SSL_Crl: </span>/etc/slave/<span style="COLOR: rgb(0,0,0)">cacert.pem Master_SSL_Crlpath: Using_Gtid: NoGtid_IO_Pos:</span><span style="COLOR: rgb(128,0,128)">1</span> row <span style="COLOR: rgb(0,0,255)">in</span> set (<span style="COLOR: rgb(128,0,128)">0.00</span> sec)<span style="FONT-SIZE: 14px; FONT-FAMILY: verdana, Arial, Helvetica, sans-serif; LINE-HEIGHT: 1.5; BACKGROUND-COLOR: rgb(255,255,255)"> </span>
Copy after login

DB1连接DB2

MariaDB [(none)]> change master to master_host=<span style="COLOR: rgb(128,0,0)">'</span><span style="COLOR: rgb(128,0,0)">192.168.1.112</span><span style="COLOR: rgb(128,0,0)">'</span>,master_user=<span style="COLOR: rgb(128,0,0)">'</span><span style="COLOR: rgb(128,0,0)">repluser</span><span style="COLOR: rgb(128,0,0)">'</span>,master_password=<span style="COLOR: rgb(128,0,0)">'</span><span style="COLOR: rgb(128,0,0)">replpass</span><span style="COLOR: rgb(128,0,0)">'</span>,master_log_file=<span style="COLOR: rgb(128,0,0)">'</span><span style="COLOR: rgb(128,0,0)">mysql-bin.000007</span><span style="COLOR: rgb(128,0,0)">'</span>,master_log_pos=<span style="COLOR: rgb(128,0,128)">548</span><span style="COLOR: rgb(0,0,0)">;Query OK, </span><span style="COLOR: rgb(128,0,128)">0</span> rows affected (<span style="COLOR: rgb(128,0,128)">0.03</span><span style="COLOR: rgb(0,0,0)"> sec)MariaDB [(none)]</span>><span style="COLOR: rgb(0,0,0)"> start slave;Query OK, </span><span style="COLOR: rgb(128,0,128)">0</span> rows affected (<span style="COLOR: rgb(128,0,128)">0.00</span><span style="COLOR: rgb(0,0,0)"> sec)MariaDB [(none)]</span>><span style="COLOR: rgb(0,0,0)"> show slave status/G</span>*************************** <span style="COLOR: rgb(128,0,128)">1</span>. row ***************************<span style="COLOR: rgb(0,0,0)"> Slave_IO_State: Waiting </span><span style="COLOR: rgb(0,0,255)">for</span><span style="COLOR: rgb(0,0,0)"> master to send eventMaster_Host: </span><span style="COLOR: rgb(128,0,128)">192.168</span>.<span style="COLOR: rgb(128,0,128)">1.112</span><span style="COLOR: rgb(0,0,0)">Master_User: repluserMaster_Port: </span><span style="COLOR: rgb(128,0,128)">3306</span><span style="COLOR: rgb(0,0,0)">Connect_Retry: </span><span style="COLOR: rgb(128,0,128)">60</span><span style="COLOR: rgb(0,0,0)">Master_Log_File: mysql</span>-bin.<span style="COLOR: rgb(128,0,128)">000007</span><span style="COLOR: rgb(0,0,0)">Read_Master_Log_Pos: </span><span style="COLOR: rgb(128,0,128)">548</span><span style="COLOR: rgb(0,0,0)"> Relay_Log_File: essun</span>-relay-bin.<span style="COLOR: rgb(128,0,128)">000002</span><span style="COLOR: rgb(0,0,0)">Relay_Log_Pos: </span><span style="COLOR: rgb(128,0,128)">535</span><span style="COLOR: rgb(0,0,0)">Relay_Master_Log_File: mysql</span>-bin.<span style="COLOR: rgb(128,0,128)">000007</span><span style="COLOR: rgb(0,0,0)"> Slave_IO_Running: YesSlave_SQL_Running: YesReplicate_Do_DB:Replicate_Ignore_DB: Replicate_Do_Table: Replicate_Ignore_Table:Replicate_Wild_Do_Table:Replicate_Wild_Ignore_Table: Last_Errno: </span><span style="COLOR: rgb(128,0,128)">0</span><span style="COLOR: rgb(0,0,0)"> Last_Error: Skip_Counter: </span><span style="COLOR: rgb(128,0,128)">0</span><span style="COLOR: rgb(0,0,0)">Exec_Master_Log_Pos: </span><span style="COLOR: rgb(128,0,128)">548</span><span style="COLOR: rgb(0,0,0)">Relay_Log_Space: </span><span style="COLOR: rgb(128,0,128)">832</span><span style="COLOR: rgb(0,0,0)">Until_Condition: None Until_Log_File:Until_Log_Pos: </span><span style="COLOR: rgb(128,0,128)">0</span><span style="COLOR: rgb(0,0,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: </span><span style="COLOR: rgb(128,0,128)">0</span><span style="COLOR: rgb(0,0,0)">Master_SSL_Verify_Server_Cert: NoLast_IO_Errno: </span><span style="COLOR: rgb(128,0,128)">0</span><span style="COLOR: rgb(0,0,0)">Last_IO_Error: Last_SQL_Errno: </span><span style="COLOR: rgb(128,0,128)">0</span><span style="COLOR: rgb(0,0,0)"> Last_SQL_Error:Replicate_Ignore_Server_Ids: Master_Server_Id: </span><span style="COLOR: rgb(128,0,128)">2</span><span style="COLOR: rgb(0,0,0)"> Master_SSL_Crl: Master_SSL_Crlpath: Using_Gtid: NoGtid_IO_Pos:</span><span style="COLOR: rgb(128,0,128)">1</span> row <span style="COLOR: rgb(0,0,255)">in</span> set (<span style="COLOR: rgb(128,0,128)">0.00</span> sec)
Copy after login
Copy after login

4、测试

在DB2中建立一个数据库testdb

MariaDB [(none)]> change master to master_host=<span style="COLOR: rgb(128,0,0)">'</span><span style="COLOR: rgb(128,0,0)">192.168.1.112</span><span style="COLOR: rgb(128,0,0)">'</span>,master_user=<span style="COLOR: rgb(128,0,0)">'</span><span style="COLOR: rgb(128,0,0)">repluser</span><span style="COLOR: rgb(128,0,0)">'</span>,master_password=<span style="COLOR: rgb(128,0,0)">'</span><span style="COLOR: rgb(128,0,0)">replpass</span><span style="COLOR: rgb(128,0,0)">'</span>,master_log_file=<span style="COLOR: rgb(128,0,0)">'</span><span style="COLOR: rgb(128,0,0)">mysql-bin.000007</span><span style="COLOR: rgb(128,0,0)">'</span>,master_log_pos=<span style="COLOR: rgb(128,0,128)">548</span><span style="COLOR: rgb(0,0,0)">;Query OK, </span><span style="COLOR: rgb(128,0,128)">0</span> rows affected (<span style="COLOR: rgb(128,0,128)">0.03</span><span style="COLOR: rgb(0,0,0)"> sec)MariaDB [(none)]</span>><span style="COLOR: rgb(0,0,0)"> start slave;Query OK, </span><span style="COLOR: rgb(128,0,128)">0</span> rows affected (<span style="COLOR: rgb(128,0,128)">0.00</span><span style="COLOR: rgb(0,0,0)"> sec)MariaDB [(none)]</span>><span style="COLOR: rgb(0,0,0)"> show slave status/G</span>*************************** <span style="COLOR: rgb(128,0,128)">1</span>. row ***************************<span style="COLOR: rgb(0,0,0)"> Slave_IO_State: Waiting </span><span style="COLOR: rgb(0,0,255)">for</span><span style="COLOR: rgb(0,0,0)"> master to send eventMaster_Host: </span><span style="COLOR: rgb(128,0,128)">192.168</span>.<span style="COLOR: rgb(128,0,128)">1.112</span><span style="COLOR: rgb(0,0,0)">Master_User: repluserMaster_Port: </span><span style="COLOR: rgb(128,0,128)">3306</span><span style="COLOR: rgb(0,0,0)">Connect_Retry: </span><span style="COLOR: rgb(128,0,128)">60</span><span style="COLOR: rgb(0,0,0)">Master_Log_File: mysql</span>-bin.<span style="COLOR: rgb(128,0,128)">000007</span><span style="COLOR: rgb(0,0,0)">Read_Master_Log_Pos: </span><span style="COLOR: rgb(128,0,128)">548</span><span style="COLOR: rgb(0,0,0)"> Relay_Log_File: essun</span>-relay-bin.<span style="COLOR: rgb(128,0,128)">000002</span><span style="COLOR: rgb(0,0,0)">Relay_Log_Pos: </span><span style="COLOR: rgb(128,0,128)">535</span><span style="COLOR: rgb(0,0,0)">Relay_Master_Log_File: mysql</span>-bin.<span style="COLOR: rgb(128,0,128)">000007</span><span style="COLOR: rgb(0,0,0)"> Slave_IO_Running: YesSlave_SQL_Running: YesReplicate_Do_DB:Replicate_Ignore_DB: Replicate_Do_Table: Replicate_Ignore_Table:Replicate_Wild_Do_Table:Replicate_Wild_Ignore_Table: Last_Errno: </span><span style="COLOR: rgb(128,0,128)">0</span><span style="COLOR: rgb(0,0,0)"> Last_Error: Skip_Counter: </span><span style="COLOR: rgb(128,0,128)">0</span><span style="COLOR: rgb(0,0,0)">Exec_Master_Log_Pos: </span><span style="COLOR: rgb(128,0,128)">548</span><span style="COLOR: rgb(0,0,0)">Relay_Log_Space: </span><span style="COLOR: rgb(128,0,128)">832</span><span style="COLOR: rgb(0,0,0)">Until_Condition: None Until_Log_File:Until_Log_Pos: </span><span style="COLOR: rgb(128,0,128)">0</span><span style="COLOR: rgb(0,0,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: </span><span style="COLOR: rgb(128,0,128)">0</span><span style="COLOR: rgb(0,0,0)">Master_SSL_Verify_Server_Cert: NoLast_IO_Errno: </span><span style="COLOR: rgb(128,0,128)">0</span><span style="COLOR: rgb(0,0,0)">Last_IO_Error: Last_SQL_Errno: </span><span style="COLOR: rgb(128,0,128)">0</span><span style="COLOR: rgb(0,0,0)"> Last_SQL_Error:Replicate_Ignore_Server_Ids: Master_Server_Id: </span><span style="COLOR: rgb(128,0,128)">2</span><span style="COLOR: rgb(0,0,0)"> Master_SSL_Crl: Master_SSL_Crlpath: Using_Gtid: NoGtid_IO_Pos:</span><span style="COLOR: rgb(128,0,128)">1</span> row <span style="COLOR: rgb(0,0,255)">in</span> set (<span style="COLOR: rgb(128,0,128)">0.00</span> sec)
Copy after login
Copy after login

在DB1中对testdb,插入一条数据

MariaDB [testdb]> insert t1 values (<span style="COLOR: rgb(128,0,0)">'</span><span style="COLOR: rgb(128,0,0)">tom</span><span style="COLOR: rgb(128,0,0)">'</span>,<span style="COLOR: rgb(128,0,128)">24</span><span style="COLOR: rgb(0,0,0)">);Query OK, </span><span style="COLOR: rgb(128,0,128)">1</span> row affected (<span style="COLOR: rgb(128,0,128)">0.01</span> sec)
Copy after login

 在DB3中查看结果

MariaDB [(none)]> <span style="COLOR: rgb(0,0,255)">select</span> *<span style="COLOR: rgb(0,0,0)"> from testdb.t1;</span>+------+-----+| name | age |+------+-----+| tom|<span style="COLOR: rgb(128,0,128)">24</span> || king |<span style="COLOR: rgb(128,0,128)">24</span> |+------+-----+<span style="COLOR: rgb(128,0,128)">2</span> rows <span style="COLOR: rgb(0,0,255)">in</span> set (<span style="COLOR: rgb(128,0,128)">0.00</span><span style="COLOR: rgb(0,0,0)"> sec)MariaDB [(none)]</span>>
Copy after login

OK!三台DB的主从配置正常工作

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

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

下载对应的版本就可以的。

#rpm -ivh epel-release-<span style="COLOR: rgb(128,0,128)">6</span>-<span style="COLOR: rgb(128,0,128)">8</span><span style="COLOR: rgb(0,0,0)">.noarch.rpm</span><span style="COLOR: rgb(0,0,255)">yum</span> -y <span style="COLOR: rgb(0,0,255)">install</span> mysql-mmm-agent
Copy after login

每一个节点都要安装

在每一个节点上要给Monitor授权用户

MariaDB [(none)]> GRANT SUPER, REPLICATION CLIENT, PROCESS ON *.* TO <span style="COLOR: rgb(128,0,0)">'</span><span style="COLOR: rgb(128,0,0)">mmm_agent</span><span style="COLOR: rgb(128,0,0)">'</span>@<span style="COLOR: rgb(128,0,0)">'</span><span style="COLOR: rgb(128,0,0)">192.168.1.116</span><span style="COLOR: rgb(128,0,0)">'</span> IDENTIFIED BY <span style="COLOR: rgb(128,0,0)">'</span><span style="COLOR: rgb(128,0,0)">123456</span><span style="COLOR: rgb(128,0,0)">'</span><span style="COLOR: rgb(0,0,0)">;Query OK, </span><span style="COLOR: rgb(128,0,128)">0</span> rows affected (<span style="COLOR: rgb(128,0,128)">0.01</span><span style="COLOR: rgb(0,0,0)"> sec)MariaDB [(none)]</span>> GRANT REPLICATION CLIENT ON *.* TO <span style="COLOR: rgb(128,0,0)">'</span><span style="COLOR: rgb(128,0,0)">mmm_monitor</span><span style="COLOR: rgb(128,0,0)">'</span>@<span style="COLOR: rgb(128,0,0)">'</span><span style="COLOR: rgb(128,0,0)">192.168.1.116</span><span style="COLOR: rgb(128,0,0)">'</span> IDENTIFIED BY <span style="COLOR: rgb(128,0,0)">'</span><span style="COLOR: rgb(128,0,0)">123456</span><span style="COLOR: rgb(128,0,0)">'</span><span style="COLOR: rgb(0,0,0)">;Query OK, </span><span style="COLOR: rgb(128,0,128)">0</span> rows affected (<span style="COLOR: rgb(128,0,128)">0.01</span><span style="COLOR: rgb(0,0,0)"> sec)MariaDB [(none)]</span>> GRANT REPLICATION CLIENT ON *.* TO <span style="COLOR: rgb(128,0,0)">'</span><span style="COLOR: rgb(128,0,0)">repluser</span><span style="COLOR: rgb(128,0,0)">'</span>@<span style="COLOR: rgb(128,0,0)">'</span><span style="COLOR: rgb(128,0,0)">192.168.1.116</span><span style="COLOR: rgb(128,0,0)">'</span> IDENTIFIED BY <span style="COLOR: rgb(128,0,0)">'</span><span style="COLOR: rgb(128,0,0)">replpass</span><span style="COLOR: rgb(128,0,0)">'</span><span style="COLOR: rgb(0,0,0)">;Query OK, </span><span style="COLOR: rgb(128,0,128)">0</span> rows affected (<span style="COLOR: rgb(128,0,128)">0.01</span> sec)
Copy after login

6、在Monitor节点上要安装

#<span style="COLOR: rgb(0,0,255)">yum</span> -y <span style="COLOR: rgb(0,0,255)">install</span> mysql-mmm*
Copy after login

此包同样也在epel源中

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

<span style="COLOR: rgb(0,0,0)">active_master_role	writer</span><host default><span style="COLOR: rgb(0,0,0)">	cluster_interface	 eth0	pid_path				</span>/var/run/mysql-mmm/<span style="COLOR: rgb(0,0,0)">mmm_agentd.pid	bin_path				</span>/usr/libexec/mysql-mmm/<span style="COLOR: rgb(0,0,0)">	replication_user		repluser #复制用户	replication_password	replpass #复制密码	agent_user			mmm_agent #代理用户	agent_password		</span><span style="COLOR: rgb(128,0,128)">123456</span><span style="COLOR: rgb(0,0,0)"> #代理用户的密码</span></host><host db1><span style="COLOR: rgb(0,0,0)">	ip	</span><span style="COLOR: rgb(128,0,128)">192.168</span>.<span style="COLOR: rgb(128,0,128)">1.109</span><span style="COLOR: rgb(0,0,0)">	mode	master	peer	db2</span></host><host db2><span style="COLOR: rgb(0,0,0)">	ip	</span><span style="COLOR: rgb(128,0,128)">192.168</span>.<span style="COLOR: rgb(128,0,128)">1.112</span><span style="COLOR: rgb(0,0,0)">	mode	master	peer	db1</span></host>																															 <host db3><span style="COLOR: rgb(0,0,0)">	ip	</span><span style="COLOR: rgb(128,0,128)">192.168</span>.<span style="COLOR: rgb(128,0,128)">1.113</span><span style="COLOR: rgb(0,0,0)">	mode	slave</span></host><role writer><span style="COLOR: rgb(0,0,0)">	hosts db1, db2	ips	 </span><span style="COLOR: rgb(128,0,128)">192.168</span>.<span style="COLOR: rgb(128,0,128)">1.24</span><span style="COLOR: rgb(0,0,0)">	mode	exclusive #排它</span></role>	<role reader><span style="COLOR: rgb(0,0,0)">	hosts	db2, db3	ips	</span><span style="COLOR: rgb(128,0,128)">192.168</span>.<span style="COLOR: rgb(128,0,128)">1.22</span>, <span style="COLOR: rgb(128,0,128)">192.168</span>.<span style="COLOR: rgb(128,0,128)">1.23</span><span style="COLOR: rgb(0,0,0)">	mode	balanced #均衡</span></role>
Copy after login

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

8、每一个DB中都会有mmm_agent的配置文件,编辑mmm_agent.conf

在数据库服务器上,还有一个mmm_agent.conf需要修改,其内容是:

<span style="COLOR: rgb(0,0,0)">include mmm_common.conf# The </span><span style="COLOR: rgb(128,0,0)">'</span><span style="COLOR: rgb(128,0,0)">this</span><span style="COLOR: rgb(128,0,0)">'</span><span style="COLOR: rgb(0,0,0)"> variable refers to this server.Proper operation requires# that </span><span style="COLOR: rgb(128,0,0)">'</span><span style="COLOR: rgb(128,0,0)">this</span><span style="COLOR: rgb(128,0,0)">'</span><span style="COLOR: rgb(0,0,0)"> server (db1 by default), as well as all other servers, have the# proper IP addresses set </span><span style="COLOR: rgb(0,0,255)">in</span><span style="COLOR: rgb(0,0,0)"> mmm_common.conf.this db2</span>
Copy after login

第一行表示:将之前Monitor中的mmm_common.conf文件载入到此文件中,供此文件中的参数设用。

最后一行标记此主机的角色(引用mmm_common.conf中的host段)在不同的数据库服务器上要分别改为db1和db3否则代理就会无法启动。

9、编辑mmm_mon.confg

在Monitor上,修改mmm_mon.conf文件,修改后内容为:

<span style="COLOR: rgb(0,0,0)">include mmm_common.conf</span><monitor><span style="COLOR: rgb(0,0,0)">	ip				</span><span style="COLOR: rgb(128,0,128)">192.168</span>.<span style="COLOR: rgb(128,0,128)">1.116</span><span style="COLOR: rgb(0,0,0)"> #当前monitor主机地址	pid_path			</span>/var/run/mysql-mmm/<span style="COLOR: rgb(0,0,0)">mmm_mond.pid	bin_path			</span>/usr/libexec/mysql-<span style="COLOR: rgb(0,0,0)">mmm	status_path		 </span>/var/lib/mysql-mmm/<span style="COLOR: rgb(0,0,0)">mmm_mond.status	ping_ips			</span><span style="COLOR: rgb(128,0,128)">192.168</span>.<span style="COLOR: rgb(128,0,128)">1.109</span>, <span style="COLOR: rgb(128,0,128)">192.168</span>.<span style="COLOR: rgb(128,0,128)">1.112</span><span style="COLOR: rgb(0,0,0)"> #真实DB地址	auto_set_online	 </span><span style="COLOR: rgb(128,0,128)">10</span><span style="COLOR: rgb(0,0,0)">	# The kill_host_bin does not exist by default, though the monitor will	# throw a warning about it missing.See the section </span><span style="COLOR: rgb(128,0,128)">5.10</span> <span style="COLOR: rgb(128,0,0)">"</span><span style="COLOR: rgb(128,0,0)">Kill Host</span>	# Functionality<span style="COLOR: rgb(128,0,0)">"</span><span style="COLOR: rgb(128,0,0)"> in the PDF documentation.</span><span style="COLOR: rgb(0,0,0)">	#	# kill_host_bin	 </span>/usr/libexec/mysql-mmm/monitor/<span style="COLOR: rgb(0,0,0)">kill_host	#</span></monitor><host default><span style="COLOR: rgb(0,0,0)">	monitor_user		mmm_monitor #监控DB的用户名	monitor_password	</span><span style="COLOR: rgb(128,0,128)">123456</span><span style="COLOR: rgb(0,0,0)"> #密码</span></host><span style="COLOR: rgb(0,0,0)">debug </span><span style="COLOR: rgb(128,0,128)">0</span> #关闭debug功能,如果程序无法监控得到,可以使用debug 1查错
Copy after login

10、启动MMM

在各DB端启动mmm-agent

#cd /etc/init.d/<span style="COLOR: rgb(0,0,0)"># chkconfig mysql</span>-mmm-<span style="COLOR: rgb(0,0,0)">monitor on# service mysql</span>-mmm-monitor start
Copy after login
Copy after login

在Monitor端启动监控程序

#cd /etc/init.d/<span style="COLOR: rgb(0,0,0)"># chkconfig mysql</span>-mmm-<span style="COLOR: rgb(0,0,0)">monitor on# service mysql</span>-mmm-monitor start
Copy after login
Copy after login

过几秒钟,就可以使用mmm_control show查看在线监控端(DB)了

[root@essun ~]# service mysql-mmm-<span style="COLOR: rgb(0,0,0)">monitor statusmmm_mond (pid</span><span style="COLOR: rgb(128,0,128)">5395</span><span style="COLOR: rgb(0,0,0)">) is running...[root@essun </span>~<span style="COLOR: rgb(0,0,0)">]# mmm_control showdb1(</span><span style="COLOR: rgb(128,0,128)">192.168</span>.<span style="COLOR: rgb(128,0,128)">1.109</span>) master/<span style="COLOR: rgb(0,0,0)">ONLINE. Roles:db2(</span><span style="COLOR: rgb(128,0,128)">192.168</span>.<span style="COLOR: rgb(128,0,128)">1.112</span>) master/ONLINE. Roles: reader(<span style="COLOR: rgb(128,0,128)">192.168</span>.<span style="COLOR: rgb(128,0,128)">1.22</span>), writer(<span style="COLOR: rgb(128,0,128)">192.168</span>.<span style="COLOR: rgb(128,0,128)">1.24</span><span style="COLOR: rgb(0,0,0)">)db3(</span><span style="COLOR: rgb(128,0,128)">192.168</span>.<span style="COLOR: rgb(128,0,128)">1.113</span>) slave/ONLINE. Roles: reader(<span style="COLOR: rgb(128,0,128)">192.168</span>.<span style="COLOR: rgb(128,0,128)">1.23</span>)
Copy after login

注:可以使用

[root@essun ~]# mmm_control --<span style="COLOR: rgb(0,0,0)">helpInvalid command </span><span style="COLOR: rgb(128,0,0)">'</span><span style="COLOR: rgb(128,0,0)">--help</span><span style="COLOR: rgb(128,0,0)">'</span><span style="COLOR: rgb(0,0,0)">Valid commands are:	help							</span>-<span style="COLOR: rgb(0,0,0)"> show this message	</span><span style="COLOR: rgb(0,0,255)">ping</span>							- <span style="COLOR: rgb(0,0,255)">ping</span><span style="COLOR: rgb(0,0,0)"> monitor	show							</span>-<span style="COLOR: rgb(0,0,0)"> show status	checks [</span><host>|all [<check>|all]] -<span style="COLOR: rgb(0,0,0)"> show checks status	set_online </span><host>				 - set host <host><span style="COLOR: rgb(0,0,0)"> online	set_offline </span><host>				- set host <host><span style="COLOR: rgb(0,0,0)"> offline	mode							</span>-<span style="COLOR: rgb(0,0,0)"> print current mode.	set_active						</span>-<span style="COLOR: rgb(0,0,0)"> switch into active mode.	set_manual						</span>-<span style="COLOR: rgb(0,0,0)"> switch into manual mode.	set_passive					 </span>-<span style="COLOR: rgb(0,0,0)"> switch into passive mode.	move_role [</span>--force] <role> <host> - move exclusive role <role> to host <host><span style="COLOR: rgb(0,0,0)">										(Only use </span>--force <span style="COLOR: rgb(0,0,255)">if</span> you know what you are doing!<span style="COLOR: rgb(0,0,0)">)	set_ip </span><ip> <host>				- set role with ip <ip> to host <host></host></ip></host></ip></host></role></host></role></host></host></host></host></check></host>
Copy after login

查看mmm_control的可用参数

11、模拟DB2下线

Monitor当前状态

wKioL1NbCqeRJcXLAANcNReiKZU310.jpg

让DB2下线,当前可写主机是db1,db3

wKiom1NbC0rDNCvUAAIuI8SpVSI111.jpg

db2没有下线之前还可以读写,当下线之后,可写的切换到DB1上了,所有读的都到了db3上了

当DB2重新上线后的情况如下

wKiom1NbDvXRoookAAIUNUXGfXg419.jpg

注:DB1、DB同时只能一有个写,一个读!

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

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How to copy lyrics from QQ Music How to copy lyrics How to copy lyrics from QQ Music How to copy lyrics Mar 12, 2024 pm 08:22 PM

We users should be able to understand the diversity of some functions when using this platform. We know that the lyrics of some songs are very well written. Sometimes we even listen to it several times and feel that the meaning is very profound. So if we want to understand the meaning of it, we want to copy it directly and use it as copywriting. However, if we want to use it, we still need to You just need to learn how to copy lyrics. I believe that everyone is familiar with these operations, but it is indeed a bit difficult to operate on a mobile phone. So in order to give you a better understanding, today the editor is here to help you. A good explanation of some of the above operating experiences. If you also like it, come and take a look with the editor. Don’t miss it.​

How to implement dual WeChat login on Huawei mobile phones? How to implement dual WeChat login on Huawei mobile phones? Mar 24, 2024 am 11:27 AM

How to implement dual WeChat login on Huawei mobile phones? With the rise of social media, WeChat has become one of the indispensable communication tools in people's daily lives. However, many people may encounter a problem: logging into multiple WeChat accounts at the same time on the same mobile phone. For Huawei mobile phone users, it is not difficult to achieve dual WeChat login. This article will introduce how to achieve dual WeChat login on Huawei mobile phones. First of all, the EMUI system that comes with Huawei mobile phones provides a very convenient function - dual application opening. Through the application dual opening function, users can simultaneously

How to implement the WeChat clone function on Huawei mobile phones How to implement the WeChat clone function on Huawei mobile phones Mar 24, 2024 pm 06:03 PM

How to implement the WeChat clone function on Huawei mobile phones With the popularity of social software and people's increasing emphasis on privacy and security, the WeChat clone function has gradually become the focus of people's attention. The WeChat clone function can help users log in to multiple WeChat accounts on the same mobile phone at the same time, making it easier to manage and use. It is not difficult to implement the WeChat clone function on Huawei mobile phones. You only need to follow the following steps. Step 1: Make sure that the mobile phone system version and WeChat version meet the requirements. First, make sure that your Huawei mobile phone system version has been updated to the latest version, as well as the WeChat App.

PHP Programming Guide: Methods to Implement Fibonacci Sequence PHP Programming Guide: Methods to Implement Fibonacci Sequence Mar 20, 2024 pm 04:54 PM

The programming language PHP is a powerful tool for web development, capable of supporting a variety of different programming logics and algorithms. Among them, implementing the Fibonacci sequence is a common and classic programming problem. In this article, we will introduce how to use the PHP programming language to implement the Fibonacci sequence, and attach specific code examples. The Fibonacci sequence is a mathematical sequence defined as follows: the first and second elements of the sequence are 1, and starting from the third element, the value of each element is equal to the sum of the previous two elements. The first few elements of the sequence

Master how Golang enables game development possibilities Master how Golang enables game development possibilities Mar 16, 2024 pm 12:57 PM

In today's software development field, Golang (Go language), as an efficient, concise and highly concurrency programming language, is increasingly favored by developers. Its rich standard library and efficient concurrency features make it a high-profile choice in the field of game development. This article will explore how to use Golang for game development and demonstrate its powerful possibilities through specific code examples. 1. Golang’s advantages in game development. As a statically typed language, Golang is used in building large-scale game systems.

PHP Game Requirements Implementation Guide PHP Game Requirements Implementation Guide Mar 11, 2024 am 08:45 AM

PHP Game Requirements Implementation Guide With the popularity and development of the Internet, the web game market is becoming more and more popular. Many developers hope to use the PHP language to develop their own web games, and implementing game requirements is a key step. This article will introduce how to use PHP language to implement common game requirements and provide specific code examples. 1. Create game characters In web games, game characters are a very important element. We need to define the attributes of the game character, such as name, level, experience value, etc., and provide methods to operate these

PS copy layer shortcut key PS copy layer shortcut key Feb 23, 2024 pm 02:34 PM

In the PS copy layer shortcut keys, we can know that if you want to copy a layer when using PS, you can use the shortcut key [Ctrl+J] for quick copying. This introduction to the shortcut keys for copying layers can tell you the specific operation method. The following is the detailed content, so take a look. PS copy layer shortcut key answer: [Ctrl+J] Specific method: 1. Open the image in PS and select the layer that needs to be copied. 2. Press [Ctrl+J] on the keyboard at the same time to complete the copy of the layer. Other copying methods: 1. After opening the image, press and hold the layer and move the [New Layer] icon downwards. 2. After moving to the icon, let go. 3. The layer copy is completed.

What is the difference between cut and copy What is the difference between cut and copy Mar 22, 2024 pm 03:58 PM

1. Cutting refers to the operation of moving selected content from one position to another. 2. The copy operation creates a copy of the selected content and saves the copy in the system clipboard, and the original content will remain in its original location. 3. If the user wants to move the content to another location and delete the content in the original location, they can use the cut operation. 4. If the user needs to create a copy of the content and paste it elsewhere without affecting the original content, then use the copy operation.

See all articles