1.主机状态
hostname |
function |
ip |
serverid |
lab1.stu21.com |
monitoring host |
172.16.21.101 |
- |
lab2.stu21.com |
master 1 |
172.16.21.102 |
2 |
lab3.stu21.com |
master 2 |
172.16.21.103 |
3 |
lab4.stu21.com |
slave 1 |
172.16.21.104 |
4 |
2.虚拟IP:
172.16.21.200 |
Writer |
172.16.21.201 |
Reader |
172.16.21.202 |
Reader |
分别在lab2-4安装mysql-server和mysql
# yum -y install mysql-server mysql
my.cnf配置文件
[mysql] socket=/var/lib/mysql/mysql.sock [mysqld] datadir=/data/mydata socket=/var/lib/mysql/mysql.sock user=mysql skip_name_resolv # Disabling symbolic-links is recommended to prevent assorted security risks symbolic-links=0 # Server ID server_id = 2 #此处server_id对应第一张表中的serverid # LogFile log_bin = /data/binlog/mysql-bin relay_log = /data/relaylog/mysql-relay # Increment auto_increment_increment = 2 auto_increment_offset = 1 #lab3上为2
#分别在lab2-4上进行授权
#通过MMM监视器用来检查MySQL服务器的健康 GRANT REPLICATION CLIENT ON *.* TO 'mmm_monitor'@'172.16.21.%' IDENTIFIED BY '123456'; #用MMM改变只读模式,复制,等等。 GRANT SUPER, REPLICATION CLIENT, PROCESS ON *.* TO 'mmm_agent'@'172.16.21.%' IDENTIFIED BY '123456'; #用于复制 GRANT REPLICATION SLAVE ON *.* TO 'replication'@'172.16.21.%' IDENTIFIED BY '123456';
将数据最完整数据路上的数据进行备份,并在其他两台主机上进行恢复
#记录文件及Postion mysql> show master status; +------------------+----------+--------------+------------------+ | File | Position | Binlog_Do_DB | Binlog_Ignore_DB | +------------------+----------+--------------+------------------+ | mysql-bin.000006 | 420 | | | +------------------+----------+--------------+------------------+ 1 row in set (0.00 sec) mysql> FLUSH TABLES WITH READ LOCK; #新开一个终端 [root@lab2 ~]# mysqldump -u root -p --all-databases > /tmp/database-backup.sql [root@lab2 ~]# scp /tmp/database-backup.sql 172.16.21.103:/tmp database-backup.sql 100% 513KB 512.9KB/s 00:00 [root@lab2 ~]# scp /tmp/database-backup.sql 172.16.21.104:/tmp database-backup.sql 100% 513KB 512.9K #恢复 [root@lab3 ~]# mysql </tmp/database-backup.sql [root@lab4 ~]# mysql </tmp/database-backup.sql #分别在lab3和lab4将lab2设置为主服务器 mysql> CHANGE MASTER TO master_host = '172.16.21.102', master_port=3306, master_user='replication', master_password='123456', master_log_file='mysql-bin.000006', master_log_pos=420; mysql> start slave; #在lab2上,将lab3设置为主服务器 mysql> CHANGE MASTER TO master_host = '172.16.21.103', master_port=3306, master_user='replication', master_password='123456', master_log_file='mysql-bin.000005', master_log_pos=106;
#在lab4的配置文件中,添加只读选项并重启
read_only =ON
1.在monitor(lab1)安装mysql-mmm
yum install -y mysql-mmm-*
2.在lab2-alb4安扎un个mysql-mmm-agent
yum install -y mysql-mmm-agent
3.编辑通用配置文件/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 replicantion replication_password 123456 agent_user mmm_agent agent_password 123456 </host> <host lab2> ip 172.16.21.102 mode master peer lab3 </host> <host lab3> ip 172.16.21.103 mode master peer lab2 </host> <host lab4> ip 172.16.21.104 mode slave </host> <role writer> hosts lab2,lab3 ips 172.16.21.200 mode exclusive </role> <role reader> hosts lab3,lab4 ips 172.16.21.201,172.16.21.202 mode balanced </role>
#分别在lab2-lab4上编辑vim /etc/mysql-mmm/mmm_agent.conf
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 lab3 #此处根据mmm_common.conf设置为对应hostname
#在lab2编辑监控端配置/etc/mysql-mmm/mmm_mon.conf
include mmm_common.conf <monitor> ip 172.16.21.101 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 172.16.21.102,172.16.21.103,172.16.21.104 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 123456 </host> debug 0
此时我们已经可以启动服务
#在lab1上启动监控端程序 [root@lab1 /]# service mysql-mmm-monitor start #在lab2-lab4上分别启动代理程序 [root@lab2 /]# service mysql-mmm-agent start #在lab1上查看后端状态,并设置为上线状态 [root@lab1 /]# mmm_control show [root@lab1 /]# mmm_control set_online lab2 [root@lab1 /]# mmm_control set_online lab3 [root@lab1 /]# mmm_control set_online lab4 #再次查看,已经处于在线状态,且vip已经分配到各主机上 [root@lab1 /]# mmm_control show lab2(172.16.21.102) master/ONLINE. Roles: writer(172.16.21.200) lab3(172.16.21.103) master/ONLINE. Roles: reader(172.16.21.201) lab4(172.16.21.104) slave/ONLINE. Roles: reader(172.16.21.202)
通过vip访问mysql服务器,已经可以正常访问
[root@lab1 /]# mysql -h172.16.21.200 -u root -p Enter password: Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 12360 .......
手动将lab2下线,测试是否仍然可以访问
[root@lab2 mysql-mmm]# service mysqld stop Stopping mysqld: [ OK ] [root@lab1 /]# mysql -h172.16.21.200 -u root -p Enter password: Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 7322 Server version: 5.1.71-log Source distribution