更多博文请关注:没有伞的孩子必须努力奔跑 (www.xuchanggang.cn)
一、mysql-mmm简述:
[root@client100 ~]# yum -y install mysql mysql-server [root@client101 ~]# yum -y install mysql mysql-server [root@client102 ~]# yum -y install mysql mysql-server [root@client103 ~]# yum -y install mysql mysql-server # 设置1台DB server(192.168.1.100)的登陆密码[其他机器使用备份恢复即可] [root@client100 ~]# mysqladmin -uroot password 'kongzhong' # 修改192.168.1.100数据库配置文件 [root@client100 ~]# vim /etc/my.cnf # 添加或修改如下参数 max_connections=1400 binlog-format='row' server_id= 100 log_bin= /var/lib/mysql/mysql-100-bin.log log_bin_index= /var/lib/mysql/mysql-bin.log.index relay_log= /var/lib/mysql/mysql-relay-bin relay_log_index= /var/lib/mysql/mysql-relay-bin.index expire_logs_days= 10 max_binlog_size= 100M log_slave_updates= 1 sync-binlog=1 auto_increment_increment=2 auto_increment_offset=1 # skip-name-resolve 这个参数可以选配,如果不使用,则host配置文件必须有所有dbserver对应解析关系 skip-name-resolve # 修改192.168.1.101数据库配置文件 [root@client101 ~]# vim /etc/my.cnf max_connections=1400 binlog-format='row' server_id= 101 log_bin= /var/lib/mysql/mysql-101-bin.log log_bin_index= /var/lib/mysql/mysql-bin.log.index relay_log= /var/lib/mysql/mysql-relay-bin relay_log_index= /var/lib/mysql/mysql-relay-bin.index expire_logs_days= 10 max_binlog_size= 100M log_slave_updates= 1 sync-binlog=1 auto_increment_increment=2 # 这个参数值和上面的值不一样 auto_increment_offset=2 skip-name-resolve # 修改192.168.1.102数据库配置文件 [root@client102 ~]# vim /etc/my.cnf max_connections=1400 server_id= 102 log_bin= /var/lib/mysql/mysql-102-bin.log log_bin_index= /var/lib/mysql/mysql-bin.log.index relay_log= /var/lib/mysql/mysql-relay-bin relay_log_index= /var/lib/mysql/mysql-relay-bin.index log_slave_updates= 1 # 修改192.168.1.103数据库配置文件 [root@client103 ~]# vim /etc/my.cnf max_connections=1400 server_id= 103 log_bin= /var/lib/mysql/mysql-103-bin.log log_bin_index= /var/lib/mysql/mysql-bin.log.index relay_log= /var/lib/mysql/mysql-relay-bin relay_log_index= /var/lib/mysql/mysql-relay-bin.index log_slave_updates= 1 # 启动所有DB server上的mysql数据库 [root@client100 ~]# /etc/init.d/mysqld start [root@client101 ~]# /etc/init.d/mysqld start [root@client102 ~]# /etc/init.d/mysqld start [root@client103 ~]# /etc/init.d/mysqld start # 登陆192.168.1.100上的数据库,做授权操作(授权用户包括复制的用户,mmm需要的监控,代理用户等) mysql> grant replication slave on *.* to 'slave'@'192.168.1.%' identified by 'kongzhong'; mysql> grant replication client on *.* to 'mmm_monitor'@'192.168.1.%' identified by 'kongzhong'; mysql> grant replication client,process,super on *.* to 'mmm_agent'@'192.168.1.%' identified by 'kongzhong'; mysql> flush privileges; # 备份192.168.1.100的数据,并传送到其他几台DB server上 [root@client100 ~]# mysqldump -uroot -p --all-databases --lock-all-tables --flush-logs >/tmp/all.sql Enter password: [root@client100 ~]# scp /tmp/all.sql 192.168.1.101:/tmp/ [email protected]'s password: all.sql 100% 495KB 495.3KB/s 00:00 [root@client100 ~]# scp /tmp/all.sql 192.168.1.102:/tmp/ [email protected]'s password: all.sql 100% 495KB 495.3KB/s 00:00 [root@client100 ~]# scp /tmp/all.sql 192.168.1.103:/tmp/ [email protected]'s password: all.sql 100% 495KB 495.3KB/s 00:00
mysql> show master status; +----------------------+----------+--------------+------------------+ | File | Position | Binlog_Do_DB | Binlog_Ignore_DB | +----------------------+----------+--------------+------------------+ | mysql-100-bin.000003 | 106 | | | +----------------------+----------+--------------+------------------+ 1 row in set (0.00 sec)
mysql> CHANGE MASTER TO master_host='192.168.1.100', -> master_port=3306, -> master_user='slave', -> master_password='kongzhong', -> master_log_file='mysql-100-bin.000003', -> master_log_pos=106; mysql> start slave; mysql> show slave start\G; # 配置完主从,查看下面两个参数的值是否为yes,根据提示排错 Slave_IO_Running: Yes Slave_SQL_Running: Yes
# 在192.168.1.101上查看当前日志文件的位置 mysql> show master status; +----------------------+----------+--------------+------------------+ | File | Position | Binlog_Do_DB | Binlog_Ignore_DB | +----------------------+----------+--------------+------------------+ | mysql-101-bin.000004 | 996936 | | | +----------------------+----------+--------------+------------------+ 1 row in set (0.00 sec) # 在192.168.1.100上,执行如下语句 mysql>CHANGE MASTER TO master_host='192.168.1.101', -> master_port=3306, -> master_user='slave', -> master_password='kongzhong', -> master_log_file='mysql-101-bin.000004', -> master_log_pos=996936; mysql> start slave; mysql> show slave start\G; # 配置完主从,查看下面两个参数的值是否为yes,根据提示排错 Slave_IO_Running: Yes Slave_SQL_Running: Yes # 以上配置完之后,大家可以在主DB server创建库来验证主从的有效性(这里不演示了)
# 所有机器(包括dbserver和监控机器)都需安装[这里指演示在一台机器上安装] [root@client104 ~]# yum -y install libart_lgpl perl-Date-Manip perl-XML-DOM perl-XML-DOM-XPath perl-XML-Parser perl-XML-RegExp rrdtool perl-Class-Singleton perl perl-DBD-MySQL perl-Params-Validate perl-MailTools perl-Time-HiRes # 以下几个软件,本地光盘上是没有的,需要使用网络yum源[epel],我这里下载好了 # 下载地址如下:http://www.kuaipan.cn/file/id_119710994921422892.htm [root@client104 ~]# yum -y install perl-Algorithm-Diff-1.1902-9.el6.noarch.rpm perl-IPC-Shareable-0.60-2.el6.rf.noarch.rpm perl-Log-Dispatch-2.26-1.el6.rf.noarch.rpm perl-Log-Log4perl-1.26-1.el6.rf.noarch.rpm perl-Net-ARP-1.0.6-2.1.el6.x86_64.rpm perl-Proc-Daemon-0.06-1.el6.noarch.rpm perl-Proc-ProcessTable-0.44-4.el6.x86_64.rpm rrdtool-perl-1.3.8-6.el6.x86_64.rpm # 数据库端需要安装 [[email protected] ~]#yum -y install mysql-connector-odbc
# 安装mysql-mmm,可以使用epel提供的rpm包安装 # 这里不使用rpm安装,软件都已共享,自行下载,在每台机器上安装mysql-mmm[这里仅演示在1台机器上mysql-mmm] [root@client104 ~]# tar -xf mysql-mmm-2.2.1.tar.gz [root@client104 ~]# cd mysql-mmm-2.2.1 [root@client104 mysql-mmm-2.2.1]# make install # 配置mmm_common.conf,并传送到每台机器上对应目录[包括监控机] [root@client104 mysql-mmm-2.2.1]# vim /etc/mysql-mmm/mmm_common.conf active_master_role writer <host default> cluster_interface eth0 pid_path /var/run/mmm_agentd.pid bin_path /usr/lib/mysql-mmm/ # 用于主从复制的账户和密码 replication_user slave replication_password kongzhong # 代理端的账户和密码[这些账户,上面都已做过授权,不清楚,可以回头看看] agent_user mmm_agent agent_password kongzhong </host> # 配置第一台DB server,这里的ip地址写真实数据库IP地址,模式为主dbserver <host db1> ip 192.168.1.100 mode master peer db1 </host> # 配置第二台DB server,模式为主dbserver <host db2> ip 192.168.1.101 mode master peer db2 </host> # 配置第三台DB server,模式为从dbserver <host db3> ip 192.168.1.102 mode slave </host> # 配置第四台DB server,模式为从dbserver <host db4> ip 192.168.1.103 mode slave </host> # 配置可写的DB server(即master dbserver),这里的ip为虚拟ip地址 <role writer> hosts db1, db2 ips 192.168.1.12 mode exclusive </role> # 配置可读的DB server(可以是所有,也可以是部分),这里的ip为虚拟ip地址,模式为轮询, <role reader> hosts db1, db2, db3, db4 ips 192.168.1.13, 192.168.1.14, 192.168.1.15,192.168.1.16 mode balanced </role> # 上面的配置文件传送到各个主机 [root@client104 mysql-mmm-2.2.1]# scp /etc/mysql-mmm/mmm_common.conf 192.168.1.101:/etc/mysql-mmm/mmm_common.conf [root@client104 mysql-mmm-2.2.1]# scp /etc/mysql-mmm/mmm_common.conf 192.168.1.102:/etc/mysql-mmm/mmm_common.conf [root@client104 mysql-mmm-2.2.1]# scp /etc/mysql-mmm/mmm_common.conf 192.168.1.103:/etc/mysql-mmm/mmm_common.conf [root@client104 mysql-mmm-2.2.1]# scp /etc/mysql-mmm/mmm_common.conf 192.168.1.100:/etc/mysql-mmm/mmm_common.conf
[root@client100 mysql-mmm-2.2.1]# vim /etc/mysql-mmm/mmm_agent.conf include mmm_common.conf this db1 [root@client101 mysql-mmm-2.2.1]# vim /etc/mysql-mmm/mmm_agent.conf include mmm_common.conf this db2 [root@client102 mysql-mmm-2.2.1]# vim /etc/mysql-mmm/mmm_agent.conf include mmm_common.conf this db3 [root@client103 mysql-mmm-2.2.1]# vim /etc/mysql-mmm/mmm_agent.conf include mmm_common.conf this db4
[root@client104 mysql-mmm-2.2.1]# vim /etc/mysql-mmm/mmm_mon.conf include mmm_common.conf # 以下IP均为真实IP地址 <monitor> ip 192.168.1.104 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.1.100, 192.168.1.101, 192.168.1.102, 192.168.1.103 </monitor> # 监管的账号密码 <host default> monitor_user mmm_monitor monitor_password kongzhong </host> debug 0
[root@client100 ~]# /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 [root@client101 ~]# /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 [root@client102 ~]# /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 [root@client103 ~]# /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 # 监管端启动 [root@client104 mysql-mmm-2.2.1]# /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 # 查看状态[第一次需要激活dbserver] [root@client104 ~]# mmm_control show db1(192.168.1.100) master/AWAITING_RECOVERY. Roles: db2(192.168.1.101) master/AWAITING_RECOVERY. Roles: db3(192.168.1.102) slave/AWAITING_RECOVERY. Roles: db4(192.168.1.103) slave/AWAITING_RECOVERY. Roles: # 激活所有dbserver [root@client104 ~]# mmm_control set_online db1 [root@client104 ~]# mmm_control set_online db2 [root@client104 ~]# mmm_control set_online db3 [root@client104 ~]# mmm_control set_online db4 # 再次查看 [root@client104 ~]# mmm_control show db1(192.168.1.100) master/ONLINE. Roles: reader(192.168.1.14) db2(192.168.1.101) master/ONLINE. Roles: reader(192.168.1.13), writer(192.168.1.12) db3(192.168.1.102) slave/ONLINE. Roles: reader(192.168.1.16) db4(192.168.1.103) slave/ONLINE. Roles: reader(192.168.1.15) # 现在mmm就配置好,大家可以自行测试 # 我的测试方法为:停掉192.168.1.101的dbserver 查看虚拟ip变化,其他几台dbserver复制的变化 # 启动192.168.1.101的dbserver,停掉192.168.1.100,查看虚拟ip的变化,特别看两台slave上复制主服务器的指向