环境lnmp架构。web服务器上lnmp+memcached.192.168.4.1 1.增加4台mysql数据库双主双从 其中192.168.4.10、192.168.4.11作为MySQL双主服务器192.168.4.12、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.10、192.168.4.11作为MySQL双主服务器192.168.4.12、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台服务器master1、master2、slave1、slave2、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
本文出自 “蚂蚱小子” 博客,请务必保留此出处http://zxlwz.blog.51cto.com/6952946/1852594
原文地址:http://zxlwz.blog.51cto.com/6952946/1852594