1.實驗概述
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 |
2.Mysql主從複製配置
分別在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
3.安裝配置mmm
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)
4.測試
通過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