溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點(diǎn)擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

mysql-mmm如何實(shí)現(xiàn)mysql互為主從復(fù)制HA功能

發(fā)布時(shí)間:2021-12-01 09:58:06 來源:億速云 閱讀:125 作者:柒染 欄目:數(shù)據(jù)庫

mysql-mmm如何實(shí)現(xiàn)mysql互為主從復(fù)制HA功能,很多新手對此不是很清楚,為了幫助大家解決這個(gè)難題,下面小編將為大家詳細(xì)講解,有這方面需求的人可以來學(xué)習(xí)下,希望你能有所收獲。 

每個(gè)mysql服務(wù)器節(jié)點(diǎn)都需要運(yùn)行mmmd_agent,同時(shí)在另外一臺(tái)機(jī)器【可以是獨(dú)立的一臺(tái)服務(wù)器也可以是和AppServer共同享一個(gè)服務(wù)器】上運(yùn)行mmmd_mon
mmm利用了虛擬IP技術(shù),1個(gè)網(wǎng)卡可以使用多個(gè)IP
所以使用mmm時(shí),需要2*n+1個(gè)IP,n為mysql節(jié)點(diǎn)數(shù),包括slave和master
當(dāng)數(shù)據(jù)庫節(jié)點(diǎn)fail時(shí),mmmd_mon檢測不到mmmd_agent的心跳或者對應(yīng)的mysql服務(wù)器的狀態(tài)時(shí)
mmmd_mon將作出決定并下令給某個(gè)正常的數(shù)據(jù)庫節(jié)點(diǎn)的mmmd_agent,使得該mmmd_agent“篡位”
【即 使用剛才fail的那個(gè)結(jié)點(diǎn)的虛擬IP,使得虛擬IP從fail結(jié)點(diǎn)指向此時(shí)的正常機(jī)器】

 

MMMM需要5個(gè)IP,兩個(gè)節(jié)點(diǎn)使用固定IP,兩個(gè)程式讀IP(只讀),1個(gè) 程式寫IP(用來更新)
后面這三個(gè)虛擬IP是根據(jù)節(jié)點(diǎn)的可用性在節(jié)點(diǎn)之間實(shí)現(xiàn)跳轉(zhuǎn)的

 

一。IP分配
IP分配如下:
A :mysql master 246
固定IP:211.100.97.246
程式讀IP:211.100.97.244  (虛擬)

B:mysql master 250
固定IP:211.100.97.250
程式讀IP:211.100.97.243  (虛擬)

monitor 245
程式寫IP:211.100.97.248  (虛擬)


給246添加虛擬IP 211.100.97.244
 ifconfig eth2:1 211.100.97.244 netmask 255.255.255.224 up
[root@XKWB5510 software]# ifconfig -a|grep "inet addr"|head -3|tail -2|awk -F "[ :]+" '{print $4"/"$NF}'
211.100.97.246/255.255.255.224
211.100.97.244/255.255.255.224

給250添加虛擬IP 211.100.97.243
 ifconfig eth0:1 211.100.97.243 netmask 255.255.255.224 up
 [root@XKWB5705 software]# ifconfig -a|grep "inet addr"|head -2|awk -F "[ :]+" '{print $4"/"$NF}'
211.100.97.250/255.255.255.224
211.100.97.243/255.255.255.224

給245添加虛擬IP:211.100.97.248
 ifconfig eth2:1 211.100.97.248 netmask 255.255.255.224 up
[root@CentOS mysql-5.1.56]# ifconfig -a|grep "inet addr"|head -3|tail -2|awk -F "[ :]+" '{print $4"/"$NF}'
211.100.97.245/255.255.255.224
211.100.97.248/255.255.255.224

二 授權(quán)
在AB機(jī)器添加代理賬號(hào) useradd rep_agent
在monitor機(jī)器上添加監(jiān)控賬號(hào) useradd rep_monitor

A上授權(quán)
mysql> grant all privileges on *.* to  identified by '123456';
mysql> grant all privileges on *.* to  identified by '123456';

查看授權(quán)情況
mysql> select host,user from mysql.user where user like 'rep%';
+----------------+-------------+
| host           | user        |
+----------------+-------------+
| %              | rep_agent   |
| %              | rep_monitor |
| 211.100.97.250 | replication |
| localhost      | replication |
+----------------+-------------+
4 rows in set (0.01 sec)


B上授權(quán)
mysql> grant all privileges on *.* to  identified by '123456';
mysql> grant all privileges on *.* to  identified by '123456';

mysql> select host,user from mysql.user where user like 'rep%';
+----------------+-------------+
| host           | user        |
+----------------+-------------+
| %              | rep_agent   |
| %              | rep_monitor |
| 211.100.97.246 | replication |
| localhost      | replication |
+----------------+-------------+
4 rows in set (0.00 sec)


三 mmm安裝
CentOS軟件倉庫默認(rèn)是不含這些軟件的,必須要有epel這個(gè)包的支持。故我們必須先安裝epel:
wget  
rpm -Uvh epel-release-5-4.noarch.rpm

源碼包安裝mysql-mmm
wget 
tar zxf mysql-master-master-1.2.3.tar.gz
cd mysql-master-master-1.2.3
 ./install.pl 
 
 
 
另外安裝mmm之前需要安裝以下幾個(gè)必須的perl模塊
Data::Dumper
POSIX
Cwd
threads
threads::shared
Thread::Queue
Thread::Semaphore
IO::Socket
Proc::Daemon
Time::HiRes
DBI
DBD::mysql
Algorithm::Diff

否則在安裝mmm執(zhí)行install.pl命令的時(shí)候,會(huì)出現(xiàn)如下報(bào)錯(cuò):
1)
Checking required module 'Proc::Daemon'...Error!
 Can't locate Proc/Daemon.pm in @INC (@INC contains: /usr/lib64/perl5/site_perl/5.8.8/x86_64-linux-thread-multi /usr/lib/perl5/site_perl/5.8.8 /usr/lib/perl5/site_perl /usr/lib64/perl5/vendor_perl/5.8.8/x86_64-linux-thread-multi /usr/lib/perl5/vendor_perl/5.8.8 /usr/lib/perl5/vendor_perl /usr/lib64/perl5/5.8.8/x86_64-linux-thread-multi /usr/lib/perl5/5.8.8 .) at (eval 13) line 2.
BEGIN failed--compilation aborted at (eval 13) line 2.

------------------------------------------------------------
Required module 'Proc::Daemon' is not found on this system!
Install it (e.g. run command 'cpan Proc::Daemon') and try again.

根據(jù)以上報(bào)錯(cuò)提示運(yùn)行
cpan Proc::Daemon    基本上都是回車

2)
Checking required module 'DBI'...Error!
 Can't locate DBI.pm in @INC (@INC contains: /usr/lib64/perl5/site_perl/5.8.8/x86_64-linux-thread-multi /usr/lib/perl5/site_perl/5.8.8 /usr/lib/perl5/site_perl /usr/lib64/perl5/vendor_perl/5.8.8/x86_64-linux-thread-multi /usr/lib/perl5/vendor_perl/5.8.8 /usr/lib/perl5/vendor_perl /usr/lib64/perl5/5.8.8/x86_64-linux-thread-multi /usr/lib/perl5/5.8.8 .) at (eval 16) line 2.
BEGIN failed--compilation aborted at (eval 16) line 2.

------------------------------------------------------------
Required module 'DBI' is not found on this system!
Install it (e.g. run command 'cpan DBI') and try again.
++++++++++++++++++++++++++++
根據(jù)報(bào)錯(cuò)提示運(yùn)行命令
cpan DBI

3)
Checking required module 'DBD::mysql'...Error!
 Can't locate DBD/mysql.pm in @INC (@INC contains: /usr/lib64/perl5/site_perl/5.8.8/x86_64-linux-thread-multi /usr/lib/perl5/site_perl/5.8.8 /usr/lib/perl5/site_perl /usr/lib64/perl5/vendor_perl/5.8.8/x86_64-linux-thread-multi /usr/lib/perl5/vendor_perl/5.8.8 /usr/lib/perl5/vendor_perl /usr/lib64/perl5/5.8.8/x86_64-linux-thread-multi /usr/lib/perl5/5.8.8 .) at (eval 19) line 2.
BEGIN failed--compilation aborted at (eval 19) line 2.

------------------------------------------------------------
Required module 'DBD::mysql' is not found on this system!
Install it (e.g. run command 'cpan DBD::mysql') and try again.


運(yùn)行這個(gè)命令cpan DBD::mysql
報(bào)錯(cuò)如下:
CPAN.pm: Going to build C/CA/CAPTTOFU/DBD-mysql-4.020.tar.gz

Can't exec "mysql_config": No such file or directory at Makefile.PL line 83.

Cannot find the file 'mysql_config'! Your execution PATH doesn't seem 
not contain the path to mysql_config. Resorting to guessed values!
Can't exec "mysql_config": No such file or directory at Makefile.PL line 478.
Can't find mysql_config. Use --mysql_config option to specify where mysql_config is located
Can't exec "mysql_config": No such file or directory at Makefile.PL line 478.
Can't find mysql_config. Use --mysql_config option to specify where mysql_config is located
Can't exec "mysql_config": No such file or directory at Makefile.PL line 478.
Can't find mysql_config. Use --mysql_config option to specify where mysql_config is located


PLEASE NOTE:

For 'make test' to run properly, you must ensure that the 
database user 'root' can connect to your MySQL server 
and has the proper privileges that these tests require such 
as 'drop table', 'create table', 'drop procedure', 'create procedure'
as well as others.

mysql> grant all privileges on test.* to  identified by 's3kr1t';

You can also optionally set the user to run 'make test' with:

perl Makefile.PL --testuser=username

Can't exec "mysql_config": No such file or directory at Makefile.PL line 478.
Can't find mysql_config. Use --mysql_config option to specify where mysql_config is located
Can't exec "mysql_config": No such file or directory at Makefile.PL line 478.
Can't find mysql_config. Use --mysql_config option to specify where mysql_config is located
Can't exec "mysql_config": No such file or directory at Makefile.PL line 478.
Can't find mysql_config. Use --mysql_config option to specify where mysql_config is located
Failed to determine directory of mysql.h. Use

  perl Makefile.PL --cflags=-I<dir>

to set this directory. For details see the INSTALL.html file,
section "C Compiler flags" or type

  perl Makefile.PL --help
Running make test
  Make had some problems, maybe interrupted? Won't test
Running make install
  Make had some problems, maybe interrupted? Won't install


根據(jù)以上腦挫提示查看是否有mysql_config
[root@XKWB5510 mysql-master-master-1.2.3]# find /usr/local/mysql/  -name "mysql_config*"
/usr/local/mysql/bin/mysql_config
/usr/local/mysql/share/man/man1/mysql_config.1

看一下mysql_config的權(quán)限
[root@XKWB5510 mysql-master-master-1.2.3]# ls -l /usr/local/mysql/bin/mysql_config
-rwxr-xr-x 1 root root 6105 Sep 21 22:43 /usr/local/mysql/bin/mysql_config

解決辦法
URL:
DBD-mysql-4.020.tar.gz 
tar zxf DBD-mysql-4.020.tar.gz
cd DBD-mysql-4.020
perl Makefile.PL --mysql_config=/usr/local/mysql/bin/mysql_config
make
make install


4)
Checking required module 'Algorithm::Diff'...Error!
 Can't locate Algorithm/Diff.pm in @INC (@INC contains: /usr/lib64/perl5/site_perl/5.8.8/x86_64-linux-thread-multi /usr/lib/perl5/site_perl/5.8.8 /usr/lib/perl5/site_perl /usr/lib64/perl5/vendor_perl/5.8.8/x86_64-linux-thread-multi /usr/lib/perl5/vendor_perl/5.8.8 /usr/lib/perl5/vendor_perl /usr/lib64/perl5/5.8.8/x86_64-linux-thread-multi /usr/lib/perl5/5.8.8 .) at (eval 20) line 2.
BEGIN failed--compilation aborted at (eval 20) line 2.

------------------------------------------------------------
Required module 'Algorithm::Diff' is not found on this system!
Install it (e.g. run command 'cpan Algorithm::Diff') and try again.
+++++++++++++++++
運(yùn)行
cpan Algorithm::Diff

一直跟著錯(cuò)誤提示走就行,直到?jīng)]有Error

5)
最后安裝成功的提示是這樣的:
[root@XKWB5705 mysql-master-master-1.2.3]# ./install.pl 
Checking platform support... linux Ok!
Checking required module 'Data::Dumper'...Ok!
Checking required module 'POSIX'...Ok!
Checking required module 'Cwd'...Ok!
Checking required module 'threads'...Ok!
Checking required module 'threads::shared'...Ok!
Checking required module 'Thread::Queue'...Ok!
Checking required module 'Thread::Semaphore'...Ok!
Checking required module 'IO::Socket'...Ok!
Checking required module 'Proc::Daemon'...Ok!
Checking required module 'Time::HiRes'...Ok!
Checking required module 'DBI'...Ok!
Checking required module 'DBD::mysql'...Ok!
Checking required module 'Algorithm::Diff'...Ok!
Checking iproute installation...Ok!
Installing mmm files...
Confgiuration:
  - installation directory: '/usr/local/mmm'
  - create symlinks: on
  - symlinks directory: '/usr/local/sbin'

Copying files to '/usr/local/mmm' directory...Ok!

Creating symlink: '/usr/local/mmm/sbin/mmm_control' -> '/usr/local/sbin/mmm_control'...Ok!
Creating symlink: '/usr/local/mmm/sbin/mmmd_agent' -> '/usr/local/sbin/mmmd_agent'...Ok!
Creating symlink: '/usr/local/mmm/sbin/mmm_restore' -> '/usr/local/sbin/mmm_restore'...Ok!
Creating symlink: '/usr/local/mmm/sbin/mmmd_angel' -> '/usr/local/sbin/mmmd_angel'...Ok!
Creating symlink: '/usr/local/mmm/sbin/mmm_get_dump' -> '/usr/local/sbin/mmm_get_dump'...Ok!
Creating symlink: '/usr/local/mmm/sbin/mmm_backup' -> '/usr/local/sbin/mmm_backup'...Ok!
Creating symlink: '/usr/local/mmm/sbin/mmm_clone' -> '/usr/local/sbin/mmm_clone'...Ok!
Creating symlink: '/usr/local/mmm/sbin/mmmd_mon' -> '/usr/local/sbin/mmmd_mon'...Ok!
Creating symlink: '/usr/local/mmm/man/man1/mmmd_mon.1' -> '/usr/local/man/man1/mmmd_mon.1'...Ok!
Creating symlink: '/usr/local/mmm/man/man1/mmm_restore.1' -> '/usr/local/man/man1/mmm_restore.1'...Ok!
Creating symlink: '/usr/local/mmm/man/man1/mmmd_agent.1' -> '/usr/local/man/man1/mmmd_agent.1'...Ok!
Creating symlink: '/usr/local/mmm/man/man1/mmm_clone.1' -> '/usr/local/man/man1/mmm_clone.1'...Ok!
Creating symlink: '/usr/local/mmm/man/man1/mmm_get_dump.1' -> '/usr/local/man/man1/mmm_get_dump.1'...Ok!
Creating symlink: '/usr/local/mmm/man/man1/mmm_control.1' -> '/usr/local/man/man1/mmm_control.1'...Ok!
Creating symlink: '/usr/local/mmm/man/man1/mmmd_angel.1' -> '/usr/local/man/man1/mmmd_angel.1'...Ok!
Creating symlink: '/usr/local/mmm/man/man1/mmm_backup.1' -> '/usr/local/man/man1/mmm_backup.1'...Ok!

Installation is done!

++++++++++++++++++++++++++++++++++++++

四  配置

 

 cp /home/sysadmin/zhaoyj/software/mysql-master-master-1.2.3/etc/examples/mmm_agent.conf.example  /usr/local/mmm/etc/mmm_agent.conf
--------------------------------
這個(gè)配置選項(xiàng)是必須的嗎?【不是】
# Cluster interface 
cluster_interface eth0 
----------------------
db1的配置文件 /usr/local/mmm/etc/mmm_agent.conf
#
# Master-Master Manager config (agent)
#

include mmm_common.conf

# Paths
pid_path /usr/local/mmm/var/mmmd_agent.pid
bin_path /usr/local/mmm/bin

# MMMD command socket tcp-port and ip
bind_port 9989

# Logging setup 
log mydebug 
    file /usr/local/mmm/var/mmm-debug.log 
    level debug 
log mytraps 
    file /usr/local/mmm/var/mmm-traps.log 
    level trap

# Define current server id
this db1
mode master

# For masters 
peer db2

# Cluster hosts addresses and access params
host db1
    ip 211.100.97.246
    port 3306
    user rep_agent
    password 123456

host db2
    ip 211.100.97.250
    port 3306
    user rep_agent
    password 123456

------------------------
db2的配置文件  /usr/local/mmm/etc/mmm_agent.conf
# Master-Master Manager config (agent)
#

include mmm_common.conf

# Paths
pid_path /usr/local/mmm/var/mmmd_agent.pid
bin_path /usr/local/mmm/bin

# MMMD command socket tcp-port and ip
bind_port 9989

# Logging setup 
log mydebug 
    file /usr/local/mmm/var/mmm-debug.log 
    level debug 
log mytraps 
    file /usr/local/mmm/var/mmm-traps.log 
    level trap


# Define current server id
this db2
mode master

# For masters 
peer db1


# Cluster hosts addresses and access params
host db1
    ip 211.100.97.246
    port 3306
    user rep_agent
    password 123456

host db2
    ip 211.100.97.250
    port 3306
    user rep_agent
    password 123456
 
 
--------------------------
 db1 db2以及monitor共同的配置文件/usr/local/mmm/etc/mmm_common.conf  
# Cluster interface
#cluster_interface eth0

# Debug mode
debug no

# Paths
bin_path /usr/local/mmm/bin
pid_path /usr/local/mmm/var/mmmd.pid 
status_path /usr/local/mmm/var/mmmd.status

# Choose the default failover method [manual|wait|auto] 
failover_method auto

# How many seconds to wait for both masters to become ONLINE 
# before switching from WAIT to AUTO failover method, 0 = wait indefinitely 
wait_for_other_master 2

# How many seconds to wait before switching node status from AWAITING_RECOVERY to ONLINE
# 0 = disabled 
auto_set_online 1


# Logging setup
log mydebug 
    file /usr/local/mmm/var/mmm-debug.log
    level debug

log mytraps 
    file /usr/local/mmm/var/mmm-traps.log
    level trap
    email 
    email 

# Email notification settings
email notify
    from_address 
    from_name MMM Control

# Define roles
active_master_role reader

# MMMD command socket tcp-port
agent_port 9989
monitor_ip 127.0.0.1


# Cluster hosts addresses and access params

host db1
    ip 211.100.97.246
    port 3306
    user rep_agent
    password 123456 
    mode master
    pear db2

host db2
    ip 211.100.97.250
    port 3306
    user rep_agent
    password 123456
    mode master
    pear db1


# Define roles that are assigned to the above hosts
# Mysql Reader role
role reader
    mode balanced
    servers db1, db2
    ip 211.100.97.243,211.100.97.244

# Mysql Writer role
role writer
    mode exclusive
    servers db1, db2
    ip 211.100.97.248

# Replication credentials used by slaves to connect to the master
replication_user replication
replication_password 123456

# Checks parameters

# Ping checker
check ping
    check_period 1
    trap_period 5
    timeout 2

# Mysql checker 
# (restarts after 10000 checks to prevent memory leaks)
check mysql
    check_period 1
    trap_period  2
    timeout 2
    restart_after 10000

# Mysql replication backlog checker 
# (restarts after 10000 checks to prevent memory leaks)
check rep_backlog
    check_period 5
    trap_period 10
    max_backlog 60
    timeout 2
    restart_after 10000

# Mysql replication threads checker 
# (restarts after 10000 checks to prevent memory leaks)
check rep_threads
    check_period 1
    trap_period 5
    timeout 2
    restart_after 10000
 
----------------------------
 
monit機(jī)上 mmm_mon.conf 的配置文件

# Cluster interface
#cluster_interface eth0

# Debug mode
debug no

# Paths
bin_path /usr/local/mmm/bin
pid_path /usr/local/mmm/var/mmmd.pid 
status_path /usr/local/mmm/var/mmmd.status

# Choose the default failover method [manual|wait|auto] 
failover_method auto

# How many seconds to wait for both masters to become ONLINE 
# before switching from WAIT to AUTO failover method, 0 = wait indefinitely 
wait_for_other_master 2

# How many seconds to wait before switching node status from AWAITING_RECOVERY to ONLINE
# 0 = disabled 
auto_set_online 1


# Logging setup
log mydebug 
    file /usr/local/mmm/var/mmm-debug.log
    level debug

log mytraps 
    file /usr/local/mmm/var/mmm-traps.log
    level trap
    email 
    email 

# Email notification settings
email notify
    from_address 
    from_name MMM Control

# Define roles
active_master_role writer

# MMMD command socket tcp-port
agent_port 9989
monitor_ip 127.0.0.1


# Cluster hosts addresses and access params

host db1
    ip 211.100.97.246
    port 3306
    user rep_agent
    password 123456 
    mode master
    pear db2

host db2
    ip 211.100.97.250
    port 3306
    user rep_agent
    password 123456
    mode master
    pear db1


# Define roles that are assigned to the above hosts
# Mysql Reader role
role reader
    mode balanced
    servers db1, db2
    ip 211.100.97.243,211.100.97.244

# Mysql Writer role
role writer
    mode exclusive
    servers db1, db2
    ip 211.100.97.248

# Replication credentials used by slaves to connect to the master
replication_user replication
replication_password 123456

# Checks parameters

# Ping checker
check ping
    check_period 1
    trap_period 5
    timeout 2

# Mysql checker 
# (restarts after 10000 checks to prevent memory leaks)
check mysql
    check_period 1
    trap_period  2
    timeout 2
    restart_after 10000

# Mysql replication backlog checker 
# (restarts after 10000 checks to prevent memory leaks)
check rep_backlog
    check_period 5
    trap_period 10
    max_backlog 60
    timeout 2
    restart_after 10000

# Mysql replication threads checker 
# (restarts after 10000 checks to prevent memory leaks)
check rep_threads
    check_period 1
    trap_period 5
    timeout 2
    restart_after 10000
 --------------------------


五 啟動(dòng)進(jìn)程
 
在db1和db2上配置完mmm_agent.conf和mmm_common.conf之后才能啟動(dòng)agent進(jìn)程
啟動(dòng) mmmd_agent進(jìn)程
[root@XKWB5705 etc]# /usr/local/mmm/scripts/init.d/mmm_agent start
Starting MMM Agent daemon: MySQL Multi-Master Replication Manager
Version: 1.2.3
Ok

在monit上配置完mmm_mon.conf之后啟動(dòng)mon進(jìn)程
[root@CentOS etc]# /usr/local/mmm/scripts/init.d/mmm_mon start
Daemon bin: '/usr/local/mmm/sbin/mmmd_mon'
Daemon pid: '/usr/local/mmm/var/mmmd.pid'
Starting MMM Monitor daemon: MySQL Multi-Master Replication Manager
Version: 1.2.3
Reading config file: 'mmm_mon.conf'
$VAR1 = {};
Ok

------------------------
db1和db2查看進(jìn)程
[root@XKWB5510 etc]# ps aux |grep mmm
root     13702  0.4  0.4 107260  8444 ?        S    15:21   0:03 /usr/bin/perl /usr/local/mmm/sbin/mmmd_agent

monit上查看進(jìn)程
[root@CentOS etc]# ps aux |grep mmm
root     24608  0.6  1.9 258556 39352 ?        Sl   15:17   0:04 perl /usr/local/mmm/sbin/mmmd_mon
root     24611  0.0  0.4 107392  8280 ?        S    15:17   0:00 perl /usr/local/mmm/bin/check/checker rep_backlog
root     24613  0.1  0.4 107392  8252 ?        S    15:17   0:00 perl /usr/local/mmm/bin/check/checker mysql
root     24615  0.2  0.2  91668  5368 ?        S    15:17   0:01 perl /usr/local/mmm/bin/check/checker ping
root     24617  0.2  0.4 107392  8280 ?        S    15:17   0:01 perl /usr/local/mmm/bin/check/checker rep_threads

連續(xù)觀察了幾次monit上的進(jìn)程變化情況
從變化情況可以看出monitor用fping檢測兩個(gè)節(jié)點(diǎn)的存活狀況
[root@CentOS etc]# ps aux |grep mmm
root     24608  0.6  1.9 258556 39356 ?        Sl   15:17   0:07 perl /usr/local/mmm/sbin/mmmd_mon
root     24611  0.0  0.4 107392  8288 ?        S    15:17   0:00 perl /usr/local/mmm/bin/check/checker rep_backlog
root     24613  0.1  0.4 107392  8264 ?        S    15:17   0:01 perl /usr/local/mmm/bin/check/checker mysql
root     24615  0.2  0.2  91668  5368 ?        S    15:17   0:02 perl /usr/local/mmm/bin/check/checker ping
root     24617  0.1  0.4 107392  8280 ?        S    15:17   0:02 perl /usr/local/mmm/bin/check/checker rep_threads

[root@CentOS etc]# ps aux |grep mmm
root     24608  0.6  1.9 258556 39356 ?        Sl   15:17   0:07 perl /usr/local/mmm/sbin/mmmd_mon
root     24611  0.0  0.4 107392  8288 ?        S    15:17   0:00 perl /usr/local/mmm/bin/check/checker rep_backlog
root     24613  0.1  0.4 107392  8264 ?        S    15:17   0:01 perl /usr/local/mmm/bin/check/checker mysql
root     24615  0.2  0.2  91668  5368 ?        S    15:17   0:02 perl /usr/local/mmm/bin/check/checker ping
root     24617  0.1  0.4 107392  8280 ?        S    15:17   0:02 perl /usr/local/mmm/bin/check/checker rep_threads
root     25887  0.0  0.0   1804   504 ?        S    15:35   0:00 /usr/local/mmm/bin/sys/fping -q -u -t 500 -C 1 211.100.97.246

[root@CentOS etc]# ps aux |grep mmm
root     24608  0.6  1.9 258556 39356 ?        Sl   15:17   0:07 perl /usr/local/mmm/sbin/mmmd_mon
root     24611  0.0  0.4 107392  8288 ?        S    15:17   0:00 perl /usr/local/mmm/bin/check/checker rep_backlog
root     24613  0.1  0.4 107392  8264 ?        S    15:17   0:01 perl /usr/local/mmm/bin/check/checker mysql
root     24615  0.2  0.2  91668  5368 ?        S    15:17   0:02 perl /usr/local/mmm/bin/check/checker ping
root     24617  0.1  0.4 107392  8280 ?        S    15:17   0:02 perl /usr/local/mmm/bin/check/checker rep_threads
root     25890  0.0  0.0   1804   504 ?        S    15:35   0:00 /usr/local/mmm/bin/sys/fping -q -u -t 500 -C 1 211.100.97.250

連續(xù)觀察db2上進(jìn)程的變化情況
可以看到db2不斷檢測db1的讀進(jìn)程以及monit的寫進(jìn)程
[root@XKWB5705 etc]# ps aux |grep mmm
root      1613  0.3  0.2 107272  8440 ?        S    15:18   0:04 /usr/bin/perl /usr/local/mmm/sbin/mmmd_agent
root     12026  0.0  0.1  99564  7056 ?        S    15:38   0:00 perl /usr/local/mmm/bin/agent/check_role writer(211.100.97.248;)
root     12027  0.0  0.1 102200  7572 ?        R    15:38   0:00 perl /usr/local/mmm/bin/mysql_allow_write


[root@XKWB5705 etc]# ps aux |grep mmm
root      1613  0.3  0.2 107272  8440 ?        S    15:18   0:04 /usr/bin/perl /usr/local/mmm/sbin/mmmd_agent
root     12121  0.0  0.1  92552  6176 ?        R    15:38   0:00 perl /usr/local/mmm/bin/agent/check_role writer(211.100.97.248;)

root      1613  0.3  0.2 107272  8440 ?        S    15:18   0:04 /usr/bin/perl /usr/local/mmm/sbin/mmmd_agent
root     12202  0.0  0.1  90072  5744 ?        R    15:38   0:00 perl /usr/local/mmm/bin/agent/check_role reader(211.100.97.244;)


[root@XKWB5705 etc]# ps aux |grep mmm
root      1613  0.3  0.2 107272  8440 ?        S    15:18   0:04 /usr/bin/perl /usr/local/mmm/sbin/mmmd_agent

連續(xù)觀察db1上進(jìn)程的變化情況
可以看到db1不斷檢測db2的讀進(jìn)程
[root@XKWB5510 etc]# ps aux |grep mmm |grep -v grep
root     13702  0.4  0.4 107260  8444 ?        S    15:21   0:06 /usr/bin/perl /usr/local/mmm/sbin/mmmd_agent
root     26820  0.0  0.3  97212  6712 ?        R    15:44   0:00 perl /usr/local/mmm/bin/agent/check_role reader(211.100.97.243;)
[root@XKWB5510 etc]# ps aux |grep mmm |grep -v grep
root     13702  0.4  0.4 107260  8444 ?        S    15:21   0:06 /usr/bin/perl /usr/local/mmm/sbin/mmmd_agent
----------------------------

六 添加開機(jī)自動(dòng)啟動(dòng)

db1, db2 開機(jī)自啟動(dòng)
cp -r /usr/local/mmm/scripts/init.d/mmm_agent /etc/init.d/mmmd
chkconfig --add mmmd
chkconfig --level 345 mmmd on
查看一下添加結(jié)果:
[root@XKWB5705 etc]# chkconfig --list mmmd
mmmd            0:off 1:off 2:off 3:on 4:on 5:on 6:off

Mon 開機(jī)自啟動(dòng)
[root@CentOS etc]# cp -r /usr/local/mmm/scripts/init.d/mmm_agent /etc/init.d/mmmd
[root@CentOS etc]# chkconfig --add mmmd
[root@CentOS etc]# chkconfig --level 345 mmmd on
[root@CentOS etc]# chkconfig --list mmmd
mmmd            0:off 1:off 2:off 3:on 4:on 5:on 6:off
----------------------------------

七 測試

先介紹一下幾種狀態(tài):
online  正常運(yùn)行
admin_offline  主機(jī)被手動(dòng)設(shè)置成offline
hard_offline   主機(jī)處于offline狀態(tài),可能是檢測ping或者mysql失敗
awaiting_recovery  主機(jī)正在等待恢復(fù)
replication_delay   replication backlog太大了(檢測rep_backlog線程失?。?br/> replication_fail    replication線程沒有運(yùn)行(檢測rep_threads線程失?。?/span>


最初始狀態(tài):

mmm_control set_online db1    讓db1上線

mmm_control set_online db2    讓db2上線

[root@CentOS etc]# mmm_control show  
Servers status:
  db1(211.100.97.246): master/ONLINE. Roles: reader(211.100.97.243;), writer(211.100.97.248;)
  db2(211.100.97.250): master/ONLINE. Roles: reader(211.100.97.244;)

  從以上可以看到db1是主寫服務(wù)器
-------------------------------------


當(dāng)我停止db1【246】的mysql進(jìn)程時(shí),日志信息
[2011-09-28 16:00:00]: 24608: Check: CHECK_FAIL('db2', 'rep_threads')  Returned message: ERROR: Replication is broken
[2011-09-28 16:00:07]: 24608: Check: CHECK_OK('db1', 'mysql')
[2011-09-28 16:00:08]: 24608: Daemon: State change(db1): HARD_OFFLINE -> AWAITING_RECOVERY
[2011-09-28 16:00:10]: 24608: Daemon: State change(db1): AWAITING_RECOVERY -> ONLINE. Uptime diff = 12.109999999986 seconds; Status change diff = 1317196810

[root@CentOS var]# mmm_control show
Servers status:
  db1(211.100.97.246): master/HARD_OFFLINE. Roles: None
  db2(211.100.97.250): master/ONLINE. Roles: reader(211.100.97.243;), reader(211.100.97.244;), writer(211.100.97.248;)
從以上可以看到主寫服務(wù)器已經(jīng)從db1切換到db2,而且db1是offline狀態(tài)


當(dāng)我重新啟動(dòng)db1【246】的mysql進(jìn)程時(shí)
[2011-09-28 16:01:54]: 24608: Check: CHECK_OK('db1', 'mysql')
[2011-09-28 16:01:55]: 24608: Daemon: State change(db1): HARD_OFFLINE -> AWAITING_RECOVERY
[2011-09-28 16:01:56]: 24608: Check: CHECK_OK('db2', 'rep_threads')
[2011-09-28 16:01:56]: 24608: Daemon: State change(db1): AWAITING_RECOVERY -> ONLINE. Uptime diff = 2.94999999995343 seconds; Status change diff = 1317196916
-------------------------------

當(dāng)我停止db2【250】的mysql進(jìn)程時(shí)
[2011-09-28 16:29:22]: 24608: Check: CHECK_FAIL('db2', 'mysql')  Returned message: ERROR: Connect error (host = 211.100.97.250:3306, user = rep_agent, pass = 'xxxxxx')! Lost connection to MySQL server at 'reading initial communication packet', system error: 111
[2011-09-28 16:29:23]: 24608: Daemon: State change(db2): ONLINE -> HARD_OFFLINE
[2011-09-28 16:29:26]: 24608: Check: CHECK_FAIL('db1', 'rep_threads')  Returned message: ERROR: Replication is broken

[root@CentOS var]# mmm_control show
Servers status:
  db1(211.100.97.246): master/ONLINE. Roles: reader(211.100.97.243;), reader(211.100.97.244;), writer(211.100.97.248;)
  db2(211.100.97.250): master/HARD_OFFLINE. Roles: None
從以上可以看到db2處于offline狀態(tài)

當(dāng)我重新啟動(dòng)db2【250】的mysql進(jìn)程時(shí),日志里面的狀態(tài)提示已經(jīng)發(fā)生了變化,變成了online狀態(tài)
[2011-09-28 16:34:26]: 24608: Check: CHECK_OK('db2', 'mysql')
[2011-09-28 16:34:28]: 24608: Daemon: State change(db2): HARD_OFFLINE -> AWAITING_RECOVERY
[2011-09-28 16:34:29]: 24608: Daemon: State change(db2): AWAITING_RECOVERY -> ONLINE. Uptime diff = 306.320000000065 seconds; Status change diff = 1317198869

[root@CentOS var]# mmm_control show
Servers status:
  db1(211.100.97.246): master/ONLINE. Roles: reader(211.100.97.244;), writer(211.100.97.248;)
  db2(211.100.97.250): master/ONLINE. Roles: reader(211.100.97.243;)

通過以上測試證明整個(gè)搭建成功,已經(jīng)實(shí)現(xiàn)了高可用,實(shí)現(xiàn)失敗節(jié)點(diǎn)的自動(dòng)切換


八  附---MMM簡介
 
MMM即Master-Master Replication Manager for MySQL(mysql主主復(fù)制管理器)關(guān)于mysql主主復(fù)制配置的監(jiān)控、故障轉(zhuǎn)移和管理的一套可伸縮的腳本套件(在任何時(shí)候只有一個(gè)節(jié)點(diǎn)可以被寫入),這個(gè)套件也能對居于標(biāo)準(zhǔn)的主從配置的任意數(shù)量的從服務(wù)器進(jìn)行讀負(fù)載均衡,所以你可以用它來在一組居于復(fù)制的服務(wù)器啟動(dòng)虛擬ip,除此之外,它還有實(shí)現(xiàn)數(shù)據(jù)備份、節(jié)點(diǎn)之間重新同步功能的腳本。
 
MySQL本身沒有提供replication failover的解決方案,通過MMM方案能實(shí)現(xiàn)服務(wù)器的故障轉(zhuǎn)移,從而實(shí)現(xiàn)mysql的高可用。
 
官方網(wǎng)站為:
 
Mmm主要功能由下面三個(gè)腳本提供
 
l         mmm_mond  負(fù)責(zé)所有的監(jiān)控工作的監(jiān)控守護(hù)進(jìn)程,決定節(jié)點(diǎn)的移除等等
 
l         mmm_agentd  運(yùn)行在服務(wù)器上的代理守護(hù)進(jìn)程,通過簡單遠(yuǎn)程服務(wù)集提供給監(jiān)控節(jié)點(diǎn)
 
l         mmm_control  通過命令行管理mmm_mond進(jìn)程

看完上述內(nèi)容是否對您有幫助呢?如果還想對相關(guān)知識(shí)有進(jìn)一步的了解或閱讀更多相關(guān)文章,請關(guān)注億速云行業(yè)資訊頻道,感謝您對億速云的支持。

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI