1 [root@localhost ~]# vi /etc/sysconfig/network 2 NETWORKING=yes 3 HOSTNAME=Master01 4 [root@Master01 ~]# vi /etc/hosts 5 Master01 172.24.8.10 6 Master02 172.24.8.11 7 Slave01 172.24.8.12 8 Slave02 172.24.8.13 9 Monitor 172.24.8.20
1 [root@Master01 ~]# vi /etc/my.cnf 2 [mysqld] 3 …… 4 server-id=1 #設置主服務器master的id 5 log-bin=mysql-bin #配置二進制變動日誌命名格式 6 replicate-wild-ignore-table=mysql.% 7 replicate-wild-ignore-table=test.% 8 replicate-wild-ignore-table=information_schema.% 9 sync-binlog=1 #每條自動更新,安全性高,默認是0 10 auto_increment_increment=2 #字段一次遞增多少 11 auto_increment_offset=1 #自增字段的起始值 12 log_slave_updates #當一個主故障,另外一個當即接管 13 read_only=1
1 # 不一樣步哪些數據庫 2 binlog-ignore-db = mysql 3 binlog-ignore-db = test 4 binlog-ignore-db = information_schema 5 # 只同步哪些數據庫,除此以外,其餘不一樣步 6 binlog-do-db = mysqltest
1 replicate-wild-ignore-table=mysql.% #從庫配置不一樣步表 2 replicate-wild-do-table=test.% #從庫配置須要同步的表
提示:不要在主庫使用binlog-ignore-db和binlog-do-db,也不要在從庫使用replicate-wild-ignore和replicate-wild-do-table。html
1 [root@Master01 ~]# service mysqld start 2 [root@Master01 ~]# mysql -uroot -px120952576 3 mysql> grant replication slave on *.* to 'repl_user'@'172.24.8.%' identified by 'x12345678';
#建立用於複製的帳號
4 mysql> grant replication client on *.* to 'mmm_monitor'@'172.24.8.%' identified by 'x12345678';
#建立用於監控MySQL服務健康的帳號
5 mysql> grant super,replication client,process on *.* to 'mmm_agent'@'172.24.8.%' identified by 'x12345678';
#建立用於切換隻讀模式、同步Master信息的帳號 6 mysql> grant select,insert,update,delete,create,drop on *.* to 'mmm_test'@'172.24.8.%' identified by 'x120952576' with grant option; #建立以後用於讀寫分離測試的帳號 7 mysql> flush privileges; 8 [root@Master01 ~]# service mysqld restart 9 [root@Master01 ~]# mysql -uroot -px120952576 10 mysql> show master status;
1 [root@Master01 ~]# service mysqld restart 2 [root@Master01 ~]# mysql -uroot -px120952576 3 mysql> show master status;
1 [root@Master01 ~]# mysql -uroot -px120952576 2 Enter password: 3 mysql> change master to master_host='172.24.8.11', 4 -> master_user='repl_user', 5 -> master_password='x12345678', 6 -> master_log_file='mysql-bin.000001', 7 -> master_port=3306, 8 -> master_log_pos=120; 9 mysql> start slave; 10 mysql> show slave status\G #查看slave狀態
1 [root@Master02 ~]# mysql -uroot -px120952576 2 mysql> change master to master_host='172.24.8.10', 3 -> master_user='repl_user', 4 -> master_password='x12345678', 5 -> master_log_file='mysql-bin.000001', 6 -> master_log_pos=120; 7 mysql> start slave; 8 mysql> show slave status\G #查看slave狀態
1 [root@Master01 ~]# scp /etc/my.cnf root@172.24.8.12:/etc/ 2 [root@Master01 ~]# scp /etc/my.cnf root@172.24.8.13:/etc/ 3 [root@Slave01 ~]# vi /etc/my.cnf 4 [mysqld] 5 …… 6 server-id=3 #設置主服務器Slave01的id 7 [root@Slave02 ~]# vi /etc/my.cnf 8 [mysqld] 9 …… 10 server-id=4 #設置主服務器Slave02的id
1 [root@Slave01 ~]# service mysqld start 2 [root@Slave01 ~]# mysql -uroot -px120952576 3 mysql> grant replication slave on *.* to 'repl_user'@'172.24.8.%' identified by 'x12345678'; #建立用於複製的帳號 4 mysql> grant replication client on *.* to 'mmm_monitor'@'172.24.8.%' identified by 'x12345678'; #建立用於監控MySQL服務健康的帳號 5 mysql> grant super,replication client,process on *.* to 'mmm_agent'@'172.24.8.%' identified by 'x12345678';
#建立用於切換隻讀模式、同步Master信息的帳號 6 mysql> grant select,insert,update,delete,create,drop on *.* to 'mmm_test'@'172.24.8.%' identified by 'x120952576' with grant option; #建立以後用於讀寫分離測試的帳號 7 mysql> flush privileges;
1 [root@Slave01 ~]# service mysqld restart 2 [root@Slave01 ~]# mysql -uroot -px120952576 3 mysql> change master to master_host='172.24.8.10', 4 -> master_user='repl_user', 5 -> master_password='x12345678', 6 -> master_log_file='mysql-bin.000001', 7 -> master_log_pos=120; 8 mysql> start slave; 9 mysql> show slave status\G #查看slave狀態
1 [root@Monitor tmp]# wget http://mirrors.ustc.edu.cn/epel/epel-release-latest-6.noarch.rpm 2 [root@Monitor tmp]# rpm -ivh epel-release-latest-6.noarch.rpm 3 [root@Monitor ~]# yum -y install mysql-mmm*
1 [root@Master01 ~]# wget http://mirrors.ustc.edu.cn/epel/epel-release-latest-6.noarch.rpm 2 [root@Master01 ~]# rpm -ivh epel-release-latest-6.noarch.rpm 3 [root@Master01 ~]# yum -y install mysql-mmm-agent
提示:若yum安裝出現如下報錯,建議在全部節點改用編譯安裝,操做見附1-附2:
mysql
1 [root@Master01 ~]# cat install.sh 2 #!/bin/bash 3 wget http://xrl.us/cpanm --no-check-certificate 4 mv cpanm /usr/bin 5 chmod 755 /usr/bin/cpanm 6 cat > /root/list << EOF 7 install Algorithm::Diff 8 install Class::Singleton 9 install DBI 10 install DBD::mysql 11 install File::Basename 12 install File::stat 13 install File::Temp 14 install Log::Dispatch 15 install Log::Log4perl 16 install Mail::Send 17 install Net::ARP 18 install Net::Ping 19 install Proc::Daemon 20 install Thread::Queue 21 install Time::HiRes 22 EOF 23 for package in `cat /root/list` 24 do 25 cpanm $package 26 done
1 [root@Master01 tmp]# wget http://mysql-mmm.org/_media/:mmm2:mysql-mmm-2.2.1.tar.gz 2 [root@Master01 tmp]# mv \:mmm2\:mysql-mmm-2.2.1.tar.gz mysql-mmm-2.2.1.tar.gz 3 [root@Master01 tmp]# tar -zxvf mysql-mmm-2.2.1.tar.gz 4 [root@Master01 tmp]# cd mysql-mmm-2.2.1/ 5 [root@Master01 mysql-mmm-2.2.1]# make install
1 [root@Master01 ~]# vi /etc/mysql-mmm/mmm_agent.conf 2 include mmm_common.conf 3 this db1
1 [root@Master01 ~]# vi /etc/mysql-mmm/mmm_common.conf 2 active_master_role writer 3 #當設置爲writer的時候,須要在全部MySQL節點設置「read_only=1」,則MMM會本劇每一個節點的角色進行動態判斷,當MMM的角色設置<role writer>時候,則會自動添加「set global read_only=0」操做,即打開寫權限。 4 <host default> 5 cluster_interface eth0 #設置網絡接口,不能爲子接口。 6 pid_path /var/run/mysql-mmm/mmm_agentd.pid #設定PID文件路徑 7 bin_path /usr/libexec/mysql-mmm/ #設置MMM可執行文件路徑 8 replication_user repl_user #設置複製的用戶名 9 replication_password x12345678 #設置複製的用戶名的密碼 10 agent_user mmm_agent #設置更改只讀操做的用戶 11 agent_password x12345678 #設置更改只讀操做用戶的密碼 12 </host> 13 14 <host db1> #設置db1的配置信息,此db1名字可自定義,但必須和mmm_agent.conf中一致。 16 ip 172.24.8.10 #設置db1的物理IP 17 mode master #設置db1角色爲Master 18 peer db2 #設置db1對等的主機名,即db2和db1均爲Master 19 </host> 20 21 <host db2> #相似db1的設置 22 ip 172.24.8.11 23 mode master 24 peer db1 25 </host> 26 27 <host db3> #相似db1的設置 28 ip 172.24.8.12 29 mode slave #設置db3的角色爲Slave 30 </host> 31 32 <host db4> #相似db1的設置 33 ip 172.24.8.13 34 mode slave #設置db4的角色爲Slave 35 </host> 36 37 <role writer> #設置可寫角色 38 hosts db1, db2 #設置可執行寫操做的主機 39 ips 172.24.8.100 #設置可寫的虛擬IP 40 mode exclusive #設置角色的模式爲互斥,互斥角色只有一個IP,而且同一時間只能分配一個主機。 41 </role> 42 43 <role reader> #設置只讀角色 44 hosts db1, db2, db3, db4 #設置只讀角色的主機 45 ips 172.24.8.101, 172.24.8.102, 172.24.8.103, 172.24.8.104 46 #設置可讀的虛擬IP,能夠有多個。 47 mode balanced
#設置角色的模式爲負載均衡,在負載均衡角色中,能夠有多個IP,這些IP被均衡地、動態地分配給多個MySQL主機。
49 </role>
1 [root@Master01 ~]# vi /etc/default/mysql-mmm-agent 2 ENABLED=1
1 [root@Master01 ~]# scp /etc/mysql-mmm/mmm_common.conf root@172.24.8.11:/etc/mysql-mmm/ 2 [root@Master01 ~]# scp /etc/mysql-mmm/mmm_common.conf root@172.24.8.12:/etc/mysql-mmm/ 3 [root@Master01 ~]# scp /etc/mysql-mmm/mmm_common.conf root@172.24.8.13:/etc/mysql-mmm/ 4 [root@Master01 ~]# scp /etc/default/mysql-mmm-agent root@172.24.8.11://etc/default/ 5 [root@Master01 ~]# scp /etc/default/mysql-mmm-agent root@172.24.8.12://etc/default/ 6 [root@Master01 ~]# scp /etc/default/mysql-mmm-agent root@172.24.8.13://etc/default/
1 [root@Master02 ~]# vi /etc/mysql-mmm/mmm_agent.conf 2 include mmm_common.conf 3 this db2 4 [root@Slave01 ~]## vi /etc/mysql-mmm/mmm_agent.conf 5 include mmm_common.conf 6 this db3 7 [root@Slave02 ~]## vi /etc/mysql-mmm/mmm_agent.conf 8 include mmm_common.conf 9 this db4
1 [root@Monitor ~]# vi /etc/mysql-mmm/mmm_mon.conf 2 include mmm_common.conf 3 <monitor> 4 ip 127.0.0.1 #爲了安全,只設置本機監聽,mmm_mond默認監聽的端口爲9988 6 pid_path /var/run/mysql-mmm/mmm_mond.pid #設置mmm_mond進程pid文件位置 8 bin_path /usr/libexec/mysql-mmm #mmm可執行文件路徑 10 status_path /var/lib/mysql-mmm/mmm_mond.status #mmm集羣的狀態文件 12 ping_ips 172.24.8.10, 172.24.8.11, 172.24.8.12, 172.24.8.13 13 #用於測試的IP,只要其中有一個地址能ping通,表明網絡正常,不要寫入本機(即Monitor)IP。 14 auto_set_online 60 #設置是否自動上線,若大於0,抖動的主機在抖動時間範圍事後,自動上線。 16 …… 17 </monitor> 18 19 <host default> 20 monitor_user mmm_monitor #設置mmm_monitor帳號。 22 monitor_password x12345678 #設置mmm_monitor密碼。 24 </host> 25 debug 0 #mmm管理器的運行模式,1爲debug模式,0爲正常模式。
1 [root@Monitor ~]# /etc/init.d/mysql-mmm-monitor start 2 [root@Monitor ~]# chkconfig mysql-mmm-monitor on
1 [root@Master01 ~]# /etc/init.d/mysql-mmm-agent start 2 [root@Master01 ~]# chkconfig mysql-mmm-agent on 3 [root@Master02 ~]# /etc/init.d/mysql-mmm-agent start 4 [root@Master02 ~]# chkconfig mysql-mmm-agent on 5 [root@Slave01 ~]# /etc/init.d/mysql-mmm-agent start 6 [root@Slave01 ~]# chkconfig mysql-mmm-agent on 7 [root@Slave02 ~]# /etc/init.d/mysql-mmm-agent start 8 [root@Slave02 ~]# chkconfig mysql-mmm-agent on
1 [root@Monitor ~]# mmm_control help 2 Valid commands are: 3 help - show this message #顯示幫助信息 4 ping - ping monitor #測試網絡運行狀態 5 show - show status #顯示MMM集羣節點狀態 6 checks [<host>|all [<check>|all]] - show checks status #顯示MMM集羣中指定(全部)節點的詳細狀態 8 set_online <host> - set host <host> online #將MMM集羣中的節點設置爲online狀態 10 set_offline <host> - set host <host> offline #將MMM集羣中的節點設置爲offline狀態 12 mode - print current mode. #顯示MMM集羣當前的運行模式,有active(默認)、manual和passive三種模式。 14 set_active - switch into active mode. 15 set_manual - switch into manual mode. 16 set_passive - switch into passive mode. #切換MMM集羣到以上三種模式。 18 move_role [--force] <role> <host> - move exclusive role <role> to host <host> 19 #在互斥模式下切換角色 20 (Only use --force if you know what you are doing!) 21 set_ip <ip> <host> - set role with ip <ip> to host <host> 22 #用來在被動模式下操縱角色
1 [root@Monitor ~]# mmm_control show
1 [root@Monitor ~]# mmm_control mode
1 [root@Monitor ~]# mmm_control checks all
1 [root@Client ~]# mysql -uroot -px120952576 -h172.24.8.100 2 mysql> show variables like "%hostname%"; 3 mysql> show variables like "%server_id%";
1 mysql> create database mysqltest; 2 mysql> use mysqltest; 3 mysql> create table user(id int(5),name char(10)); 4 mysql> insert into user values (00001,'zhangsan');
1 [root@Master02 ~]# mysql -uroot -px120952576 2 mysql> show databases; 3 mysql> select * from mysqltest.user;
1 [root@Client ~]# mysql -ummm_test -px120952576 -h172.24.8.101 2 mysql> use mysqltest; 3 mysql> create table test_mmm(id int,email varchar(100));
1 [root@Monitor ~]# mmm_control show #查看現有集羣狀況
1 [root@Master01 ~]# service mysqld stop #中止Master01節點 2 [root@Monitor ~]# mmm_control show #再次查看集羣節點狀況
1 [root@Master01 ~]# service mysqld start #再次啓動Master01節點 2 [root@Monitor ~]# mmm_control show
1 [root@Monitor ~]# mmm_control move_role writer db1 2 [root@Monitor ~]# mmm_control show
1 [root@Slave01 ~]# service mysqld stop 2 [root@Monitor ~]# mmm_control show
1 [root@Slave01 ~]# service mysqld start 2 [root@Monitor ~]# mmm_control show