環境lnmp架構。web服務器上lnmp+memcached.192.168.4.1 1.增長4臺mysql數據庫雙主雙從 其中192.168.4.十、192.168.4.11做爲MySQL雙主服務器192.168.4.十二、192.168.4.13做爲主服務器的從服務器。 192.168.4.10 master1 192.168.4.11 master2 192.168.4.12 slave1 192.168.4.13 slave2 每臺步驟都設置以下 1.[root@master1 ~]# vim /etc/hosts 192.168.4.10 master1 192.168.4.11 master2 192.168.4.12 slave1 192.168.4.13 slave2 2.初始化配置數據庫 1)把LNMP上的mysql庫導出,密碼假如是123456。 [root@lnmp ~]#mysqladmin -uroot -p123456 --all-databases >all.sql [root@lnmp ~]#for i in 10 11 12 13 ;do scp all.sql root@192.168.4.$i:/root ;done 2)刪除庫並導入all.sql [root@master1 ~]#mysql -uroot -p123456 mysql>drop database all; #刪除全部庫。 [root@master1 ~]#mysql -uroot -p123456 grant replication slave on *.* to slaveuser@"192.168.4.%" identified by "123456"; //主從同步受權只須要在一臺設備上操做便可 Query OK, 0 rows affected (0.00 sec) 2)修改全部my.cnf [root@master1 ~]# cat /etc/my.cnf server_id=10 log-bin [root@master2 ~]# cat /etc/my.cnf server_id=11 log-bin [root@slave1 ~]# cat /etc/my.cnf server_id=12 log-bin [root@slave2 ~]# cat /etc/my.cnf server_id=13 log-bin [root@master1 ~]mysql> show master status\G; *************************** 1. row *************************** File: master1-bin.000001 Position: 120 Binlog_Do_DB: Binlog_Ignore_DB: Executed_Gtid_Set: 1 row in set (0.00 sec) /etc/init.d/mysqld restart 3)在mater2上 此步驟slave1,和slave2上和這裏如出一轍 [root@master2 ~]# mysql -uroot -p123456 mysql> change master to //設置主服務器信息 -> master_host="192.168.4.10", //設置主服務器IP地址 -> master_user="slaveuser", //設置主從同步用戶 -> master_password="123456", //設置主從同步密碼 -> master_log_file="master1-bin.000001", //設置主庫binlog日誌名稱 -> master_log_pos=120; //設置主從binlog日誌使用節點 Query OK, 0 rows affected, 2 warnings (0.06 sec) mysql> start slave; //啓動同步進程 Query OK, 0 rows affected (0.00 sec) mysql> show slave status\G //查看主從是否成功 Slave_IO_Running: Yes //IO節點正常 Slave_SQL_Running: Yes //SQL節點正常 4設置master2的從 [root@master2 ~]#mysql -uroot -p123456 mysql> show master status\G; *************************** 1. row *************************** File: master2-bin.000001 Position: 120 Binlog_Do_DB: Binlog_Ignore_DB: Executed_Gtid_Set: 1 row in set (0.00 sec) 5在mster1上 [root@master1 ~]# mysql -uroot -p123456 mysql> change master to //設置主服務器信息 -> master_host="192.168.4.11", //設置主服務器IP地址 -> master_user="slaveuser", //設置主從同步用戶 -> master_password="123456", //設置主從同步密碼 -> master_log_file="master2-bin.000001", //設置主庫binlog日誌名稱 -> master_log_pos=120; //設置主從binlog日誌使用節點 Query OK, 0 rows affected, 2 warnings (0.06 sec) mysql> start slave; //啓動同步進程 Query OK, 0 rows affected (0.00 sec) mysql> show slave status\G //查看主從是否成功 Slave_IO_Running: Yes //IO節點正常 Slave_SQL_Running: Yes //SQL節點正常 6雙主雙從完成測試,查看測試結果 [root@master1 ~]# mysql -uroot -p123456 mysql>create database thistest; 4.上面還只是環境下面纔是正題。 部署mysql-mmm 前面環境192.168.4.十、192.168.4.11做爲MySQL雙主服務器192.168.4.十二、192.168.4.13做爲主服務器的 從服務器現添加一臺192.168.4.100做爲MySQL-MMM架構中管理監控服務器實現監控MySQL主從服務器的工做狀態及決定故障節點的移除或恢復 工做因此添加192.168.4.100 monitor到hosts裏 1.mysql-mmm依賴安裝mysql-mmm是perl寫的因此安裝perl相關庫文件 [root@master1 ~]# yum -y install gcc* perl* 這裏苦逼5臺機器上都要這樣。依賴各類煩 提供下面安裝包的地址https://pan.baidu.com/s/1qYGQBwS 安裝Net-ARP等依賴可能裝了沒有就要裝上這幾個Net-ARP,Proc-Daemon,Algorithm-Diff,Log-Log4perl 安裝安裝Log-Log4perl 類 [root@mysql-master1 ~]# rpm -ivh perl-Log-Log4perl-1.26-1.el6.rf.noarch.rpm warning: perl-Log-Log4perl-1.26-1.el6.rf.noarch.rpm: Header V3 DSA/SHA1 Signature, key ID 6b8d79e6: NOKEY Preparing... ######################### [100%] 1:perl-Log-Log4perl ######################## [100%] 安裝Algorithm-Diff類 [root@mysql-master1 ~]# tar -zxvf Algorithm-Diff-1.1902.tar.gz //解壓安裝包 [root@mysql-master1 ~]# cd Algorithm-Diff-1.1902 //切換到安裝目錄 [root@mysql-master1 Algorithm-Diff-1.1902]# perl Makefile.PL //生成makefile文件 Checking if your kit is complete... Looks good Writing Makefile for Algorithm::Diff [root@mysql-master1 Algorithm-Diff-1.1902]# cd //切換到軟件包目錄 [root@mysql-master1 ~]# 安裝Proc-Daemon類 [root@mysql-master1 ~]# tar -zxvf Proc-Daemon-0.03.tar.gz //解壓安裝包 [root@mysql-master1 ~]# cd Proc-Daemon-0.03 //切換到安裝目錄 [root@mysql-master1 Proc-Daemon-0.03]# perl Makefile.PL //生成makefile文件 Checking if your kit is complete... Looks good Writing Makefile for Proc::Daemon [root@mysql-master1 Proc-Daemon-0.03]# make && make install //編譯編譯安裝 [root@mysql-master1 Proc-Daemon-0.03]# cd //切換到軟件包目錄 [root@mysql-master1 ~]# 安裝Net-ARP虛擬IP分配工具 [root@mysql-master1 ~]# gunzip Net-ARP-1.0.8.tgz //使用gunzip解壓tgz格式的安裝包 [root@mysql-master1 ~]# tar xvf Net-ARP-1.0.8.tar //解壓tar安裝包 [root@mysql-master1 ~]# cd Net-ARP-1.0.8 //切換到安裝目錄 [root@mysql-master1 Net-ARP-1.0.8]# perl Makefile.PL //生成makefile文件 Module Net::Pcap is required for make test! Checking if your kit is complete... Looks good Writing Makefile for Net::ARP [root@mysql-master1 Net-ARP-1.0.8]# make && make install //編譯編譯安裝 [root@mysql-master1 Net-ARP-1.0.8]# cd //切換到軟件包目錄 [root@mysql-master1 ~]# 安裝Mysql-MMM軟件包 [root@mysql-master1 ~]# tar xvf mysql-mmm-2.2.1.tar.gz //解壓安裝包 [root@mysql-master1 ~]# cd mysql-mmm-2.2.1 //切換到安裝目錄 [root@mysql-master1 mysql-mmm-2.2.1]# make && make install //編譯編譯安裝 修改公共配置文件 MySQL集羣的5臺服務器master一、master二、slave一、slave二、monitor都須要配置能夠先配好一臺後使用scp複製。 [root@master1 ~]# vim /etc/mysql-mmm/mmm_common.conf active_master_role writer cluster_interface eth0 //設置主從同步的網卡 pid_path /var/run/mmm_agentd.pid bin_path /usr/lib/mysql-mmm/ replication_user slaveuser //設置主從同步的用戶 replication_password 123456 //設置主從同步用戶密碼 agent_user agent //mmm-agent控制數據庫用戶 agent_password 123456 //mmm-agent控制數據庫用戶密碼 //設置第一個主服務器 ip 192.168.4.10 //master1 IP 地址 mode master peer master2 //指定另一臺主服務器 //指定另一臺主服務器 ip 192.168.4.11 mode master peer master1 //設置第一臺從服務器 ip 192.168.4.12 //slave1 IP 地址 mode slave //本段落配置的是slave服務器 ip 192.168.4.13 mode slave //設置寫入服務器工做模式 hosts master1,master2 //提供寫的主服務器 ips 192.168.4.200 //設置VIP地址 mode exclusive //排他模式 //設置讀取服務器工做模式 hosts slave1,slave2 //提供讀的服務器信息 ips 192.168.4.201,192.168.4.202 //多個虛擬IP mode balanced //均衡模式 [root@master1 ~]# 修改管理主機配置文件monitor主機配置 [root@monitor ~]# vim /etc/mysql-mmm/mmm_mon.conf include mmm_common.conf ip 192.168.4.100 //設置管理主機IP地址 pid_path /var/run/mmm_mond.pid bin_path /usr/lib/mysql-mmm/ status_path /var/lib/misc/mmm_mond.status ping_ips 192.168.4.10,192.168.4.11,192.168.4.12,192.168.4.13 //設置被監控數據庫 monitor_user monitor //監控數據庫MySQL用戶 monitor_password 123456 //監控數據庫MySQL用戶密碼 debug 0 [root@monitor ~]# 關鍵地方 受權agent和monitor。 否則啥都別搭。作了主從了隨便哪臺機器 mysql> grant replication client on *.* to monitor@"%" identified by "123456"; //MMM所需架構用戶受權 Query OK, 0 rows affected (0.06 sec) mysql> grant replication client,process,super on *.* to agent@"%" identified by "123456"; //MMM所需架構用戶受權 Query OK, 0 rows affected (0.00 sec) mysql> grant all on *.* to root@"%" identified by "123456"; //測試用戶受權給本身一個客戶機開個後門測試用。 Query OK, 0 rows affected (0.00 sec) 修改客戶端配置文件 master1配置 [root@master1 ~]# cat /etc/mysql-mmm/mmm_agent.conf include mmm_common.conf this master1 master2配置 [root@master2 ~]# cat /etc/mysql-mmm/mmm_agent.conf include mmm_common.conf this master2 slave1配置 [root@slave1 ~]# cat /etc/mysql-mmm/mmm_agent.conf include mmm_common.conf this slave1 slave2配置 [root@slave2 ~]# cat /etc/mysql-mmm/mmm_agent.conf include mmm_common.conf this slave2 這裏配置到此結束 MySQL-MMM架構使用 1啓動mmm-agent進程 master1,master2,slave1,slave2啓動 在這裏仍是提醒一下前面依賴沒裝會啓動失敗的運氣好的起來了也是有各類問題的起不了能夠 查看log日誌,大概在這裏吧 /var/log/mysql-mmm/***.log還有就是改vim /etc/mysql-mmm/mmm_mon.conf 最後一行改稱debug 1重起mysql-monitor 會有不錯的排錯效果。 master1操做 [root@master1 ~]# /etc/init.d/mysql-mmm-agent start Daemon bin: '/usr/sbin/mmm_agentd'Daemon pid: '/var/run/mmm_agentd.pid' Starting MMM Agent daemon... Ok 2啓動mmm-monitor進程 monitor主機操做 [root@monitor ~]# /etc/init.d/mysql-mmm-monitor start Daemon bin: '/usr/sbin/mmm_mond' Daemon pid: '/var/run/mmm_mond.pid' Starting MMM Monitor daemon: Ok 設置集羣中服務器爲online狀態 [root@monitor ~]# mmm_control show master1(192.168.4.10) master/AWAITING_RECOVERY. Roles: master2(192.168.4.11) master/AWAITING_RECOVERY. Roles: slave1(192.168.4.12) slave/AWAITING_RECOVERY. Roles: slave2(192.168.4.13) slave/AWAITING_RECOVERY. Roles: [root@monitor ~]# mmm_control set_online master1 OK: State of 'master1' changed to ONLINE. Now you can wait some time and check its new roles! [root@monitor ~]# mmm_control set_online master2 OK: State of 'master2' changed to ONLINE. Now you can wait some time and check its new roles! [root@monitor ~]# mmm_control set_online slave1 OK: State of 'slave1' changed to ONLINE. Now you can wait some time and check its new roles! [root@monitor ~]# mmm_control set_online slave2 OK: State of 'slave2' changed to ONLINE. Now you can wait some time and check its new roles! [root@monitor ~]# mmm_control show master1(192.168.4.10) master/ONLINE. Roles: writer(192.168.4.200) #測試關閉master1是否自動切到master2上而且主從也跟着變了 master2(192.168.4.11) master/ONLINE. Roles: slave1(192.168.4.12) slave/ONLINE. Roles: reader(192.168.4.201) slave2(192.168.4.13) slave/ONLINE. Roles: reader(192.168.4.202) 查看vip [root@master1 ~]#ip add show [root@slave1 ~]#ip add show [root@slave2 ~]#ip add show [root@client ~]# mysql -h192.168.4.200 -uroot -p123456 -e "show databases" //訪問虛擬IP測試 原創親測 出現問題,解決方案: 雖然mmm_control show 能看到writer(192.168.4.200)的ip,可是在master1上,ip add show 看不到 現象:vim /var/log/mysql-mmm/mmm_agentd.log 08:30:37 FATAL Couldn't allow writes: ERROR: Can't connect to MySQL (host = 192.168.4.10:3306, user = agent)! Access denied for user 'agent'@'master1' (using password: YES) mysql> select user,host,password from mysql.user; +-----------+-------------+-------------------------------------------+ | user | host | password | +-----------+-------------+-------------------------------------------+ | root | localhost | *6BB4837EB74329105EE4568DDA7DC67ED2CA2AD9 | | root | master1 | | | root | 127.0.0.1 | | | root | ::1 | | | | localhost | | | | master1 | | | slaveuser | 192.168.4.% | *6BB4837EB74329105EE4568DDA7DC67ED2CA2AD9 | | monitor | % | *6BB4837EB74329105EE4568DDA7DC67ED2CA2AD9 | | agent | % | *6BB4837EB74329105EE4568DDA7DC67ED2CA2AD9 | | root | % | *6BB4837EB74329105EE4568DDA7DC67ED2CA2AD9 | +-----------+-------------+-------------------------------------------+ 10 rows in set (0.00 sec) mysql> delete from mysql.user where user=""; Query OK, 2 rows affected (0.00 sec) [root@master1 ~]/etc/init.d/mysqld restart [root@master1 ~]ip add show eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000 link/ether 54:52:04:04:01:01 brd ff:ff:ff:ff:ff:ff inet 192.168.4.10/24 brd 192.168.4.255 scope global eth0 inet 192.168.4.200/32 scope global eth0 inet6 fe80::5652:4ff:fe04:101/64 scope link