1. 程式人生 > >mysql 集群 監控

mysql 集群 監控

命令 地址 sla server-id make 主機 mode monit 主機名

部署mysql高可用集群(mysql-mmm+主從同步)
4臺數據庫服務器:
四個數據庫之保留初始的四個庫,其他庫全部刪除
主機158
主機137
主機99
主機67
主機102(可與其他四臺ping通即可)
5臺主機的公共配置:
下載mysql-mmm.zip
配置yum源
service iptables stop
setenforce 0
yum -y install perl perl-*
主機158與主機137配置為主主結構:
主機158:
mysql> grant replication slave on *.* to [email protected]"%" identified by "Anonymous";
[[email protected] ~]# vim /etc/my.cnf
[mysqld]
log-bin=master158
server_id=158
[[email protected] ~]# /etc/init.d/mysql restart
mysql> show master status\G;(在192.168.4.137上查看節點)
mysql> change master to master_host="192.168.4.137",master_user="hydra",master_password="Anonymous",master_log_file="master137.000001",master_log_pos=120;
mysql> start slave;
主機137:
mysql> grant replication slave on *.* to [email protected]"%" identified by "Anonymous";
[[email protected] ~]# vim /etc/my.cnf
[mysqld]
log-bin=master137
server_id=137
log-slave-updates
[[email protected] ~]# /etc/init.d/mysql restart
mysql> show master status\G;(在192.168.4.158上查看節點)
mysql> change master to master_host="192.168.4.158",master_user="hydra",master_password="Anonymous",master_log_file="master158.000001",master_log_pos=120;
mysql> start slave;

主機99和主機67同時配置為主機137的從數據庫,並能夠讓主機99和主機67能同步主機158上的數據:
主機99:
[[email protected] ~]# vim /etc/my.cnf
[mysqld]
server-id=99
[[email protected] ~]# /etc/init.d/mysql restart
mysql> show master status\G;(在192.168.4.137上查看節點)
mysql> change master to master_host="192.168.4.137",master_user="hydra",master_password="Anonymous",master_log_file="master137.000001",master_log_pos=120;
mysql> start slave;

主機67:
[[email protected] ~]# vim /etc/my.cnf
[mysqld]
server_id=67
[[email protected] ~]# /etc/init.d/mysql restart
mysql> show master status\G;(在192.168.4.137上查看節點)
mysql> change master to master_host="192.168.4.137",master_user="hydra",master_password="Anonymous",master_log_file="master137.000001",master_log_pos=120;
mysql> start slave;

測試主從同步配置:
使用158的數據庫做用戶授權,在其他三臺數據庫服務器能夠查看到授權用戶
mysql> grant all on bbs.* to [email protected]"%" identified by "123";
mysql> select user,host from mysql.user;(在其他三臺數據庫服務器測試)
+-------+-----------+
| user | host |
+-------+-----------+
| hydra | % |
| stu | % |
| root | 127.0.0.1 |
| root | 137 |
| root | ::1 |
| root | localhost |
+-------+-----------+
在客戶端測試授權用戶:
[[email protected] ~]# mysql -h192.168.4.158 -ustu -p123(測試可以連接)
mysql> create database bbs;
mysql> create table bbs.t1(id int);
mysql> insert into bbs.t1 values(911);
在其他數據庫服務器查看:
mysql> select * from bbs.t1;
+------+
| id |
+------+
| 911 |
+------+

配置mysql-mmm
mysql主主復制管理器,監控,故障轉移和管理的一套腳本套件
用perl語言編寫,能對居於標準的主從配置的任意數量的從服務器進行讀負載均衡
和實現數據備份,mysql本身沒有提供故障轉移的方案,從而實現數據庫服務器的高可用

mysql-mmm架構
mmm-monitor:管理節點,運行在監控服務器上,負責所有的監控工作的監控
mmm-agent:數據庫節點,運行在數據庫服務器上,提供簡單的遠程服務集,提供給監控節點

在所有的服務器上安裝mysql-mmm
[[email protected] ~]# unzip mysql-mmm.zip
[[email protected] mysql-mmm]# ls
Algorithm-Diff-1.1902.tar.gz mysql-mmm-P.txt
install.sh Net-ARP-1.0.8.tgz
mysql-mmm-2.2.1.tar.gz perl-Log-Log4perl-1.26-1.el6.rf.noarch.rpm
mysql-mmm-L.docx Proc-Daemon-0.03.tar.gz
[[email protected] mysql-mmm]# tar -xf mysql-mmm-2.2.1.tar.gz
[[email protected] mysql-mmm-2.2.1]# ls
bin COPYING etc INSTALL lib Makefile README sbin UPGRADE VERSION
[[email protected] mysql-mmm-2.2.1]# make install(安裝)
[[email protected] ~]# ls /etc/mysql-mmm/*.conf(配置文件)
/etc/mysql-mmm/mmm_agent.conf /etc/mysql-mmm/mmm_mon.conf
/etc/mysql-mmm/mmm_common.conf /etc/mysql-mmm/mmm_tools.conf
配置文件說明:
mmm_agent.conf(是mmm_agent服務的主配置文件)
mmm_mon.conf(是mmm_monitor的主配置文件)
mmm_common.conf(公共配置文件,所有的服務器上都要)

配置mysql-mmm的ip規劃:
寫ip地址 192.168.4.119 (在主機158和主機137之間浮動)

讀ip地址 192.168.4.200 (在主機99和主機67之間浮動)
192.168.4.201
修改mmm_common.conf配置文件之前,在所有數據庫服務器上做如下授權:
mysql> grant replication client,process,super on *.* to [email protected]"%" identified by "Anonymous";

更改mmm_common.conf配置文件:
[[email protected] mysql-mmm]# sed -i ‘/^$/d‘ mmm_common.conf(刪除空行)
[[email protected] mysql-mmm]# vim mmm_common.conf
<host default>(本機配置)
cluster_interface eth0(本機網卡)
pid_path /var/run/mmm_agentd.pid(進程pid號存放處)
bin_path /usr/lib/mysql-mmm/(可執行命令安裝路徑)
replication_user hydra(拷貝數據權限的用戶名)
replication_password Anonymous(被授權用戶密碼)
agent_user agent(有agent權限的用戶名)
agent_password Anonymous(密碼)
</host>
<host master158>(主機名)
ip 192.168.4.158(主機的ip地址)
mode master(主機的類型)
peer master137(同類型)
</host>
<host master137>(主機名)
ip 192.168.4.137(主機的ip地址)
mode master(主機的類型)
peer master158(同類型)
</host>
<host slave99>(從主機名)
ip 192.168.4.99(主機的ip地址)
mode slave(主機的類型)
</host>
<host slave67>(從主機名)
ip 192.168.4.67(主機的ip地址)
mode slave(主機的類型)
</host>
<role writer>(寫類型)
hosts master158,master137(寫類型的主機名)
ips 192.168.4.119(浮動ip)
mode exclusive(獨占模式)
</role>
<role reader>(讀類型)
hosts slave99,slave67(讀類型的主機名)
ips 192.168.4.200,192.168.4.201(浮動ip)
mode balanced(共享模式)
</role>
[[email protected] mysql-mmm]# scp mmm_common.conf [email protected]:/etc/mysql-mmm/(保存後把此配置文件發送到所有的服務器上)

更改mmm_agent.conf配置文件
修改mmm_agent.con文件
[[email protected] mysql-mmm]# vim mmm_agent.conf
include mmm_common.conf(先加載此配置文件)
this master158(本機在mmm_common.conf文件裏的主機名)
修改mmm_agent.con文件
[[email protected] mysql-mmm]# vim mmm_agent.conf
include mmm_common.conf
this master137(本機在mmm_common.conf文件裏的主機名)
修改mmm_agent.con文件
[[email protected] mysql-mmm]# vim mmm_agent.conf
include mmm_common.conf
this slave99(本機在mmm_common.conf文件裏的主機名)
修改mmm_agent.con文件
[[email protected] mysql-mmm]# vim mmm_agent.conf
include mmm_common.conf
this slave67(本機在mmm_common.conf文件裏的主機名)


修改mmm_mon.conf配置文件之前,在所有數據庫服務器上做如下授權:
mysql> grant replication client on *.* to [email protected]"%" identified by "Anonymous";
更改mmm_mon.conf配置文件:(在監控端修改即可,被監控端不用改)
[[email protected] mysql-mmm]# vim mmm_mon.conf
include mmm_common.conf
<monitor>
ip 192.168.4.102(監控端ip)
pid_path /var/run/mmm_mond.pid(pid存放處)
bin_path /usr/lib/mysql-mmm/
status_path /var/lib/misc/mmm_mond.status
ping_ips 192.168.4.158, 192.168.4.137, 192.168.4.99, 192.168.4.67(被監控的服務器ip地址)
</monitor>
<host default>
monitor_user monitor(授權用戶)
monitor_password Anonymous(密碼)
</host>
debug 0(1開啟,輸出啟動過程信息)

啟動數據庫服務器上的mmm_agent服務:
啟動服務:/etc/init.d/mysql-mmm-agent start
日誌文件:/var/log/mysql-mmm/mmm_agentd.log
端口:netstat -utnalp | grep :9989
安裝服務運行需要的軟件包
Algorithm-Diff-1.1902.tar.gz
Proc-Daemon-0.03.tar.gz
perl-Log-Log4perl-1.26-1.el6.rf.noarch.rpm
把安裝步驟寫成腳本
進入在每臺數據庫服務器上包所在的目錄上執行
進入每臺數據庫服務器執行腳本
[r[email protected] ~]# vim xx.sh
#/bin/bash
tar -xf Proc-Daemon-0.03.tar.gz
cd Proc-Daemon-0.03
perl Makefile.PL
make
make install
cd..
rpm -ivh perl-Log-Log4perl-1.26-1.el6.rf.noarch.rpm
tar -xf Algorithm-Diff-1.1902.tar.gz
cd Algorithm-Diff-1.1902
perl Makefile.PL
make
make install
cd ..
[[email protected] ~]# chmod +x xx.sh
[[email protected] ~]# scp xx.sh 192.168.4.137:/root/(把腳本上傳到其他服務器上)

啟動mmm_monitor服務
依賴包和mmm_agent的一樣,執行腳本即可
啟動服務:/etc/init.d/mysql-mmm-monitor start
日誌文件:/var/log/mysql-mmm/mmm_monitor.log
端口:netstat -utnalp | grep :9988

在所有數據庫服務器上安裝獲取虛擬ip的程序
Net-ARP-1.0.8.tgz

[[email protected] ~]# vim xxx.sh
#/bin/bash
gunzip Net-ARP-1.0.8.tgz
tar -xf Net-ARP-1.0.8.tar
cd Net-ARP-1.0.8
perl Makefile.PL
make
make install
[[email protected] Net-ARP-1.0.8]# chmod +x xxx.sh (把執行腳本上傳到所有數據庫服務器上)

測試配置
在102上查看配置
mmm_control help(幫助)
mmm_control show(查看狀態)
mmm_control set_online 主機名(在線狀態)
[[email protected] ~]# mmm_control set_online master158(設置主機158在線模式)
[[email protected] ~]# mmm_control show
master137(192.168.4.137) master/ONLINE. Roles:
master158(192.168.4.158) master/ONLINE. Roles: writer(192.168.4.119)
slave67(192.168.4.67) slave/ONLINE. Roles: reader(192.168.4.200)
slave99(192.168.4.99) slave/ONLINE. Roles: reader(192.168.4.201)

客戶端測試虛擬ip訪問:
[[email protected] ~]# ping192.168.4.119
[[email protected] ~]# mysql -h192.168.4.119 -uhydra -pAnonymous

mysql 集群 監控