机器简介:
monitor 192.168.146.131 监控服务器
master-001 192.168.146.129 读写主机001
master-002 192.168.146.130 读写主机002
slave-001 192.168.146.131 只读主机001
虚拟IP分配
master-001 192.168.146.135 writer
master-002 192.168.146.136 reader
slave-001 192.168.146.137 reader
三台相同操作:
关闭防火墙
一,安装mysql
二,mysql 权限赋值
GRANT REPLICATION SLAVE ON *.* TO ‘myrepl‘@‘192.168.146.%‘ IDENTIFIED BY ‘123456‘;
GRANT REPLICATION CLIENT ON *.* TO ‘mmm_monitor‘@‘192.168.146.%‘ IDENTIFIED BY ‘monitor‘;
GRANT SUPER, REPLICATION CLIENT, PROCESS ON *.* TO ‘mmm_agent‘@‘192.168.146.%‘ IDENTIFIED BY ‘agent‘;
二,安装MMM
安装mysql-MMM
安装依赖包:
yum install epel-release
yum install perl-Log-Log4perl-Appender-Socket-UNIX
rpm 安装
rpm -ivh mysql-mmm-2.2.1-2.el6.noarch.rpm
安装mysql-agent
安装依赖包:
yum install -y perl-Algorithm-Diff perl-Class-Singleton perl-Proc-Daemon perl-Net-ARP perl-DBD-mysql
rpm 安装
rpm -ivh mysql-mmm-agent-2.2.1-2.el6.noarch.rpm
chkconfig mysql-mmm-agent on
(131上)monitor 机器上安装monitor包
rpm -ivh mysql-mmm-monitor-2.2.1-2.el6.noarch.rpm
三.配置MMM
修改my.cnf
db1
server-id= 1
gtid_mode= on
log_slave_updates=1 #当一个主故障,另一个立即接管
enforce_gtid_consistency= 1
auto-increment-increment = 2
auto-increment-offset = 1
replicate-ignore-db=mysql,information_schema #不同步的数据库,多个写多行
db2
server-id= 2
gtid_mode= on
log_slave_updates=1 #当一个主故障,另一个立即接管
enforce_gtid_consistency= 1
auto-increment-increment = 2
auto-increment-offset = 2
replicate-ignore-db=mysql,information_schema #不同步的数据库,多个写多行
db3
server-id= 3
gtid_mode= on
log_slave_updates=1 #当一个主故障,另一个立即接管
enforce_gtid_consistency= 1
replicate-ignore-db=mysql,information_schema #不同步的数据库,多个写多行
read_only=1
配置主从
130 和131 上操作
CHANGE MASTER TO MASTER_HOST=‘192.168.146.129‘,MASTER_USER=‘myrepl‘,MASTER_PASSWORD=‘123456‘,MASTER_PORT=3306,MASTER_LOG_FILE=‘mysql-bin.000022‘, MASTER_CONNECT_RETRY=311;
start slave;
129上操作
130 和131 上操作
CHANGE MASTER TO MASTER_HOST=‘192.168.146.130‘,MASTER_USER=‘myrepl‘,MASTER_PASSWORD=‘123456‘,MASTER_PORT=3306,MASTER_LOG_FILE=‘mysql-bin.000032‘, MASTER_CONNECT_RETRY=234;
start slave;
配置/etc/mysql-mmm/mmm_common.conf #三机器统一
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 myrepl
replication_password 123456
agent_user mmm_agent
agent_password agent
</host>
<host db1>
ip 192.168.146.129
mode master
peer db2
</host>
<host db2>
ip 192.168.146.130
mode master
peer db1
</host>
<host db3>
ip 192.168.146.131
mode slave
</host>
<role writer>
hosts db1, db2
ips 192.168.146.135
mode exclusive
</role>
<role reader>
hosts db1, db2, db3
ips 192.168.146.136, 192.168.146.137, 192.168.146.138
mode balanced
</role>
配置/etc/mysql-mmm/mmm_agent.conf
this db1 #是谁就写谁
管理机上配置 mmm_mon.conf
include mmm_common.conf
<monitor>
ip 127.0.0.1
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.146.129,192.168.146.130 #其他机器IP
auto_set_online 60
# 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
monitor_password monitor
</host>
debug 0
加入自启动:
chkconfig mysql-mmm-monitor on
chkconfig mysql-mmm-agent on
开启服务:
启动 mmm_agent
service mysql-mmm-agent start
启动 mmm_monitor
service mysql-mmm-monitor start
管理命令
[[email protected] mysql-mmm]# 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>
[[email protected] mysql-mmm]# mmm_control show
db1(192.168.146.129) master/ONLINE. Roles: reader(192.168.146.138), writer(192.168.146.135)
db2(192.168.146.130) master/ONLINE. Roles: reader(192.168.146.137)
db3(192.168.146.131) slave/ONLINE. Roles: reader(192.168.146.136
[[email protected] mysql-mmm]# mmm_control checks all
db2 ping [last change: 2016/11/10 19:18:54] OK
db2 mysql [last change: 2016/11/10 19:18:54] OK
db2 rep_threads [last change: 2016/11/10 19:18:54] OK
db2 rep_backlog [last change: 2016/11/10 19:18:54] OK: Backlog is null
db3 ping [last change: 2016/11/10 19:18:54] OK
db3 mysql [last change: 2016/11/10 19:19:17] OK
db3 rep_threads [last change: 2016/11/10 19:19:17] OK
db3 rep_backlog [last change: 2016/11/10 19:19:17] OK: Backlog is null
db1 ping [last change: 2016/11/10 19:18:54] OK
db1 mysql [last change: 2016/11/10 19:18:54] OK
db1 rep_threads [last change: 2016/11/10 19:18:54] OK
db1 rep_backlog [last change: 2016/11/10 19:18:54] OK: Backlog is null
测试转换:
DB1上操作:
[[email protected] mysql-mmm]# mysqladmin -uroot -p -S /tmp/mysql.sock shutdown
Enter password:
管理机器上查看:
Bye
[[email protected] mysql-mmm]# mmm_control checks all
db2 ping [last change: 2016/11/10 19:18:54] OK
db2 mysql [last change: 2016/11/10 19:18:54] OK
db2 rep_threads [last change: 2016/11/10 19:18:54] OK
db2 rep_backlog [last change: 2016/11/10 19:18:54] OK: Backlog is null
db3 ping [last change: 2016/11/10 19:18:54] OK
db3 mysql [last change: 2016/11/10 19:39:20] OK
db3 rep_threads [last change: 2016/11/10 19:39:20] OK
db3 rep_backlog [last change: 2016/11/10 19:19:17] OK: Backlog is null
db1 ping [last change: 2016/11/10 19:18:54] OK
db1 mysql [last change: 2016/11/10 19:31:34] ERROR: Connect error (host = 192.168.146.129:3306, user = mmm_monitor)! Lost connection to MySQL server at ‘reading initial communication packet‘, system error: 111
db1 rep_threads [last change: 2016/11/10 19:18:54] OK
db1 rep_backlog [last change: 2016/11/10 19:18:54] OK: Backlog is null
[[email protected] mysql-mmm]# mmm_control show
db1(192.168.146.129) master/HARD_OFFLINE. Roles:
db2(192.168.146.130) master/ONLINE. Roles: reader(192.168.146.137), writer(192.168.146.135)
db3(192.168.146.131) slave/ONLINE. Roles: reader(192.168.146.136), reader(192.168.146.138)
启动DB1 等待60s再看:
[[email protected] mysql-mmm]# mmm_control show
db1(192.168.146.129) master/ONLINE. Roles: reader(192.168.146.136)
db2(192.168.146.130) master/ONLINE. Roles: reader(192.168.146.137), writer(192.168.146.135)
db3(192.168.146.131) slave/ONLINE. Roles: reader(192.168.146.138)
我们想把writer角色切回到db1上:
[[email protected] mysql-mmm]# mmm_control move_role writer db1
OK: Role ‘writer‘ has been moved from ‘db2‘ to ‘db1‘. Now you can wait some time and check new roles info!
[[email protected] mysql-mmm]# mmm_control show
db1(192.168.146.129) master/ONLINE. Roles: reader(192.168.146.136), writer(192.168.146.135)
db2(192.168.146.130) master/ONLINE. Roles: reader(192.168.146.137)
db3(192.168.146.131) slave/ONLINE. Roles: reader(192.168.146.138)
遇到两个问题
1. 监控程序服务器无法启动
在管理服务器上,一切都完成后,通过mmm_control查看状态,得到下面的错误信息:
ERROR: Cant connect to monitor daemon! 通过编辑/etc/mysql-mmm/mmm_mon.conf文件将debug 0改为debug 1,打开监控程序的debug状态。重新启动监控程序(service mysql-mmm-monitor restart),就会看到详细的错误信息,找不到Perl Time HiRes库。执行yum -y install perl-Time-HiRes*就可以解决。
2. 防火墙问题导致Warning: agent on host db1 is not reachable.
控制台程序正确启动后,再次执行mmm_control show,却看到下面的输出:
# Warning: agent on host db1 is not reachable
# Warning: agent on host db2 is not reachable
db1(192.168.146.129) master/ONLINE. Roles:
db2(192.168.146.130) master/ONLINE. Roles:
db3(192.168.146.131) slave/ONLINE. Roles: reader(192.168.146.138)
再次打开debug,查看错误信息
通过telnet 192.168.146.130 9989下面检查网络连接,得到了No route to host的错误信息。登录db1,关闭iptables(这不是一个好主意)。同样,关闭db2、db3上的防火墙,再次重启监控程序,一切回到正常状态!