欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

MySQL-MMM安装指南(Multi-Master Replication Manager for MySQL)

程序员文章站 2024-03-31 14:50:58
最基本的mmm安装必须至少需要2个数据库服务器和一个监控服务器下面要配置的mysql cluster环境包含四台数据库服务器和一台监控服务器,如下: fu...

最基本的mmm安装必须至少需要2个数据库服务器和一个监控服务器下面要配置的mysql cluster环境包含四台数据库服务器和一台监控服务器,如下:

function ip hostname server id
monitoring host 192.168.0.10 mon -
master 1 192.168.0.11 db1 1
master 2 192.168.0.12 db2 2
slave 1 192.168.0.13 db3 3
slave 2 192.168.0.14 db4 4

如果是个人学习安装,一下子找5台机器不太容易,可以虚拟机就可以完成。

 配置完成后,使用下面的虚拟ip访问mysql cluster,他们通过mmm分配到不同的服务器。

ip role description
192.168.0.100 writer 应用程序应该连接到这个ip进行写操作
192.168.0.101 reader 应用程序应该链接到这些ip中的一个进行读操作
192.168.0.102 reader
192.168.0.103 reader
192.168.0.104 reader

结构图如下:

MySQL-MMM安装指南(Multi-Master Replication Manager for MySQL)

2. basic configuration of master 1

first we install mysql on all hosts:
aptitude install mysql-serverthen we edit the configuration file /etc/mysql/my.cnf and add the following lines - be sure to use different server ids for all hosts:

复制代码 代码如下:

server_id = 1
log_bin = /var/log/mysql/mysql-bin.log
log_bin_index = /var/log/mysql/mysql-bin.log.index
relay_log = /var/log/mysql/mysql-relay-bin
relay_log_index = /var/log/mysql/mysql-relay-bin.index
expire_logs_days = 10
max_binlog_size = 100m
log_slave_updates = 1


then remove the following entry:
bind-address = 127.0.0.1set to number of masters:
auto_increment_increment = 2set to a unique, incremented number, less than auto_increment_increment, on each server

auto_increment_offset = 1do not bind of any specific ip, use 0.0.0.0 instead:

bind-address = 0.0.0.0afterwards we need to restart mysql for our changes to take effect:

/etc/init.d/mysql restart

3. create usersnow we can create the required users. we'll need 3 different users:

function description privileges
monitor user used by the mmm monitor to check the health of the mysql servers replication client
agent user used by the mmm agent to change read-only mode, replication master, etc. super, replication client, process
relication user used for replication replication slave

复制代码 代码如下:

grant replication client                 on *.* to 'mmm_monitor'@'192.168.0.%' identified by 'monitor_password';
grant super, replication client, process on *.* to 'mmm_agent'@'192.168.0.%'   identified by 'agent_password';
grant replication slave                  on *.* to 'replication'@'192.168.0.%' identified by 'replication_password';

note: we could be more restrictive here regarding the hosts from which the users are allowed to connect: mmm_monitor is used from 192.168.0.10. mmm_agent and replication are used from 192.168.0.11 - 192.168.0.14.
note: don't use a replication_password longer than 32 characters

4. synchronisation of data between both databases

i'll assume that db1 contains the correct data. if you have an empty database, you still have to syncronize the accounts we have just created.
first make sure that no one is altering the data while we create a backup.

复制代码 代码如下:

(db1) mysql> flush tables with read lock;

then get the current position in the binary-log. we will need this values when we setup the replication on db2, db3 and db4.

复制代码 代码如下:

(db1) mysql> show master status;
+------------------+----------+--------------+------------------+
| file             | position | binlog_do_db | binlog_ignore_db |
+------------------+----------+--------------+------------------+
| mysql-bin.000002 |      374 |              |                  |
+------------------+----------+--------------+------------------+
1 row in set (0.00 sec)

don't close this mysql-shell. if you close it, the database lock will be removed. open a second console and type:

db1$ mysqldump -u root -p --all-databases > /tmp/database-backup.sql

now we can remove the database-lock. go to the first shell:

(db1) mysql> unlock tables;copy the database backup to db2, db3 and db4.

复制代码 代码如下:

db1$ scp /tmp/database-backup.sql <user>@192.168.0.12:/tmp
db1$ scp /tmp/database-backup.sql <user>@192.168.0.13:/tmp
db1$ scp /tmp/database-backup.sql <user>@192.168.0.14:/tmp

then import this into db2, db3 and db4:

复制代码 代码如下:

db2$ mysql -u root -p < /tmp/database-backup.sql
db3$ mysql -u root -p < /tmp/database-backup.sql
db4$ mysql -u root -p < /tmp/database-backup.sql

then flush the privileges on db2, db3 and db4. we have altered the user-table and mysql has to reread this table.

复制代码 代码如下:

(db2) mysql> flush privileges;
(db3) mysql> flush privileges;
(db4) mysql> flush privileges;

on debian and ubuntu, copy the passwords in /etc/mysql/debian.cnf from db1 to db2, db3 and db4. this password is used for starting and stopping mysql.
both databases now contain the same data. we now can setup replication to keep it that way.
note: import just only add records from dump file. you should drop all databases before import dump file.

5. setup replication

configure replication on db2, db3 and db4 with the following commands:

复制代码 代码如下:

(db2) mysql> change master to master_host='192.168.0.11', master_port=3306, master_user='replication',
              master_password='replication_password', master_log_file='<file>', master_log_pos=<position>;
(db3) mysql> change master to master_host='192.168.0.11', master_port=3306, master_user='replication',
              master_password='replication_password', master_log_file='<file>', master_log_pos=<position>;
(db4) mysql> change master to master_host='192.168.0.11', master_port=3306, master_user='replication',
              master_password='replication_password', master_log_file='<file>', master_log_pos=<position>;

please insert the values return by “show master status” on db1 at the <file> and <position> tags.
start the slave-process on all 3 hosts:

复制代码 代码如下:

(db2) mysql> start slave;
(db3) mysql> start slave;
(db4) mysql> start slave;

now check if the replication is running correctly on all hosts:

复制代码 代码如下:

(db2) mysql> show slave status\g
*************************** 1. row ***************************
             slave_io_state: waiting for master to send event
                master_host: 192.168.0.11
                master_user: replication
                master_port: 3306
              connect_retry: 60

(db3) mysql> show slave status\g
*************************** 1. row ***************************
             slave_io_state: waiting for master to send event
                master_host: 192.168.0.11
                master_user: replication
                master_port: 3306
              connect_retry: 60

(db4) mysql> show slave status\g
*************************** 1. row ***************************
             slave_io_state: waiting for master to send event
                master_host: 192.168.0.11
                master_user: replication
                master_port: 3306
              connect_retry: 60

now we have to make db1 replicate from db2. first we have to determine the values for master_log_file and master_log_pos:

复制代码 代码如下:

(db2) mysql> show master status;
+------------------+----------+--------------+------------------+
| file             | position | binlog_do_db | binlog_ignore_db |
+------------------+----------+--------------+------------------+
| mysql-bin.000001 |       98 |              |                  |
+------------------+----------+--------------+------------------+
1 row in set (0.00 sec)

now we configure replication on db1 with the following command:

复制代码 代码如下:

(db1) mysql> change master to master_host = '192.168.0.12', master_port=3306, master_user='replication',
              master_password='replication_password', master_log_file='<file>', master_log_pos=<position>;

now insert the values return by “show master status” on db2 at the <file> and <position> tags.

start the slave-process:

(db1) mysql> start slave;now check if the replication is running correctly on db1:

复制代码 代码如下:

(db1) mysql> show slave status\g
*************************** 1. row ***************************
             slave_io_state: waiting for master to send event
                master_host: 192.168.0.12
                master_user: <replication>
                master_port: 3306
              connect_retry: 60

replication between the nodes should now be complete. try it by inserting some data into both db1 and db2 and check that the data will appear on all other nodes.

6. install mmm

create user
optional: create user that will be the owner of the mmm scripts and configuration files. this will provide an easier method to securely manage the monitor scripts.

useradd --comment "mmm script owner" --shell /sbin/nologin mmmdmonitoring host
first install dependencies:

复制代码 代码如下:

aptitude install liblog-log4perl-perl libmailtools-perl liblog-dispatch-perl libclass-singleton-perl libproc-daemon-perl libalgorithm-diff-perl libdbi-perl libdbd-mysql-perl

then fetch the latest mysql-mmm-common*.deb and mysql-mmm-monitor*.deb and install it:

dpkg -i mysql-mmm-common_*.deb mysql-mmm-monitor*.deb

database hosts
on ubuntu first install dependencies:

aptitude install liblog-log4perl-perl libmailtools-perl liblog-dispatch-perl iproute libnet-arp-perl libproc-daemon-perl libalgorithm-diff-perl libdbi-perl libdbd-mysql-perlthen fetch the latest mysql-mmm-common*.deb and mysql-mmm-agent*.deb and install it:

dpkg -i mysql-mmm-common_*.deb mysql-mmm-agent_*.debon redhat

yum install -y mysql-mmm-agentthis will take care of all the dependencies, which may include:

installed:

mysql-mmm-agent.noarch 0:2.2.1-1.el5

dependency installed:

复制代码 代码如下:

libart_lgpl.x86_64 0:2.3.17-4                                                
mysql-mmm.noarch 0:2.2.1-1.el5                                               
perl-algorithm-diff.noarch 0:1.1902-2.el5                                    
perl-dbd-mysql.x86_64 0:4.008-1.rf                                           
perl-datemanip.noarch 0:5.44-1.2.1                                           
perl-ipc-shareable.noarch 0:0.60-3.el5                                       
perl-log-dispatch.noarch 0:2.20-1.el5                                        
perl-log-dispatch-filerotate.noarch 0:1.16-1.el5                             
perl-log-log4perl.noarch 0:1.13-2.el5                                        
perl-mime-lite.noarch 0:3.01-5.el5                                           
perl-mail-sender.noarch 0:0.8.13-2.el5.1                                     
perl-mail-sendmail.noarch 0:0.79-9.el5.1                                     
perl-mailtools.noarch 0:1.77-1.el5                                           
perl-net-arp.x86_64 0:1.0.6-2.1.el5                                          
perl-params-validate.x86_64 0:0.88-3.el5                                     
perl-proc-daemon.noarch 0:0.03-1.el5                                         
perl-timedate.noarch 1:1.16-5.el5                                            
perl-xml-dom.noarch 0:1.44-2.el5                                             
perl-xml-parser.x86_64 0:2.34-6.1.2.2.1                                      
perl-xml-regexp.noarch 0:0.03-2.el5                                          
rrdtool.x86_64 0:1.2.27-3.el5                                                
rrdtool-perl.x86_64 0:1.2.27-3.el5

configure mmm

all generic configuration-options are grouped in a separate file called /etc/mysql-mmm/mmm_common.conf. this file will be the same on all hosts in the system:

复制代码 代码如下:

active_master_role          writer

<host default>
    cluster_interface       eth0
    pid_path                /var/run/mmmd_agent.pid
    bin_path                /usr/lib/mysql-mmm/
    replication_user        replication
    replication_password    replication_password
    agent_user              mmm_agent
    agent_password          agent_password
</host>
<host db1>
    ip                      192.168.0.11
    mode                    master
    peer                    db2
</host>
<host db2>
    ip                      192.168.0.12
    mode                    master
    peer                    db1
</host>
<host db3>
    ip                      192.168.0.13
    mode                    slave
</host>
<host db4>
    ip                      192.168.0.14
    mode                    slave
</host>

<role writer>
    hosts                   db1, db2
    ips                     192.168.0.100
    mode                    exclusive
</role>
<role reader>
    hosts                   db1, db2, db3, db4
    ips                     192.168.0.101, 192.168.0.102, 192.168.0.103, 192.168.0.104
    mode                    balanced
</role>

don't forget to copy this file to all other hosts (including the monitoring host).

on the database hosts we need to edit /etc/mysql-mmm/mmm_agent.conf. change “db1” accordingly on the other hosts:

复制代码 代码如下:

include mmm_common.conf
this db1

on the monitor host we need to edit /etc/mysql-mmm/mmm_mon.conf:

复制代码 代码如下:

include mmm_common.conf

<monitor>
    ip                      127.0.0.1
    pid_path                /var/run/mmmd_mon.pid
    bin_path                /usr/lib/mysql-mmm/
    status_path             /var/lib/misc/mmmd_mon.status
    ping_ips                192.168.0.1, 192.168.0.11, 192.168.0.12, 192.168.0.13, 192.168.0.14
</monitor>

<host default>
    monitor_user            mmm_monitor
    monitor_password        monitor_password
</host>

debug 0

ping_ips are some ips that are pinged to determine whether the network connection of the monitor is ok. i used my switch (192.168.0.1) and the four database server.


7. start mmm

 

start the agents
(on the database hosts)

debian/ubuntu
edit /etc/default/mysql-mmm-agent to enable the agent:

enabled=1red hat
rhel/fedora does not enable packages to start at boot time per default policy, so you might have to turn it on manually so the agents will start automatically when server is rebooted:

chkconfig mysql-mmm-agent onthen start it:

/etc/init.d/mysql-mmm-agent startstart the monitor
(on the monitoring host) edit /etc/default/mysql-mmm-monitor to enable the monitor:

enabled=1then start it:

/etc/init.d/mysql-mmm-monitor start

wait some seconds for mmmd_mon to start up. after a few seconds you can use mmm_control to check the status of the cluster:

复制代码 代码如下:

mon$ mmm_control show
  db1(192.168.0.11) master/awaiting_recovery. roles:
  db2(192.168.0.12) master/awaiting_recovery. roles:
  db3(192.168.0.13) slave/awaiting_recovery. roles:
  db4(192.168.0.14) slave/awaiting_recovery. roles:

because its the first startup the monitor does not know our hosts, so it sets all hosts to state awaiting_recovery and logs a warning message:

复制代码 代码如下:

mon$ tail /var/log/mysql-mmm/mmm_mon.warn

2009/10/28 23:15:28  warn detected new host 'db1': setting its initial state to 'awaiting_recovery'. use 'mmm_control set_online db1' to switch it online.
2009/10/28 23:15:28  warn detected new host 'db2': setting its initial state to 'awaiting_recovery'. use 'mmm_control set_online db2' to switch it online.
2009/10/28 23:15:28  warn detected new host 'db3': setting its initial state to 'awaiting_recovery'. use 'mmm_control set_online db3' to switch it online.
2009/10/28 23:15:28  warn detected new host 'db4': setting its initial state to 'awaiting_recovery'. use 'mmm_control set_online db4' to switch it online.

now we set or hosts online (db1 first, because the slaves replicate from this host):

复制代码 代码如下:

mon$ mmm_control set_online db1
ok: state of 'db1' changed to online. now you can wait some time and check its new roles!
mon$ mmm_control set_online db2
ok: state of 'db2' changed to online. now you can wait some time and check its new roles!
mon$ mmm_control set_online db3
ok: state of 'db3' changed to online. now you can wait some time and check its new roles!
mon$ mmm_control set_online db4
ok: state of 'db4' changed to online. now you can wait some time and check its new roles!

参考: