环境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.[[email protected] ~]# 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。 [[email protected] ~]#mysqladmin -uroot -p123456 --all-databases >all.sql [[email protected] ~]#for i in 10 11 12 13 ;do scp all.sql [email protected]$i:/root ;done 2)删除库并导入all.sql [[email protected] ~]#mysql -uroot -p123456 mysql>drop database all; #删除所有库。 [[email protected] ~]#mysql -uroot -p123456 grant replication slave on *.* to [email protected]"192.168.4.%" identified by "123456"; //主从同步授权只需要在一台设备上操作即可 Query OK, 0 rows affected (0.00 sec) 2)修改所有my.cnf [[email protected] ~]# cat /etc/my.cnf server_id=10 log-bin [[email protected] ~]# cat /etc/my.cnf server_id=11 log-bin [[email protected] ~]# cat /etc/my.cnf server_id=12 log-bin [[email protected] ~]# cat /etc/my.cnf server_id=13 log-bin [[email protected] ~]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上和这里一模一样 [[email protected] ~]# 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的从 [[email protected] ~]#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上 [[email protected] ~]# 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双主双从完成测试,查看测试结果 [[email protected] ~]# 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相关库文件 [[email protected] ~]# yum -y install gcc* perl* 这里苦逼5台机器上都要这样。依赖各种烦 提供下面安装包的地址https://pan.baidu.com/s/1qYGQBwS 安装Net-ARP等依赖可能装了没有就要装上这几个Net-ARP,Proc-Daemon,Algorithm-Diff,Log-Log4perl 安装安装Log-Log4perl 类 [[email protected] ~]# 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类 [[email protected] ~]# tar -zxvf Algorithm-Diff-1.1902.tar.gz //解压安装包 [[email protected] ~]# cd Algorithm-Diff-1.1902 //切换到安装目录 [[email protected] Algorithm-Diff-1.1902]# perl Makefile.PL //生成makefile文件 Checking if your kit is complete... Looks good Writing Makefile for Algorithm::Diff [[email protected] Algorithm-Diff-1.1902]# cd //切换到软件包目录 [[email protected] ~]# 安装Proc-Daemon类 [[email protected] ~]# tar -zxvf Proc-Daemon-0.03.tar.gz //解压安装包 [[email protected] ~]# cd Proc-Daemon-0.03 //切换到安装目录 [[email protected] Proc-Daemon-0.03]# perl Makefile.PL //生成makefile文件 Checking if your kit is complete... Looks good Writing Makefile for Proc::Daemon [[email protected] Proc-Daemon-0.03]# make && make install //编译编译安装 [[email protected] Proc-Daemon-0.03]# cd //切换到软件包目录 [[email protected] ~]# 安装Net-ARP虚拟IP分配工具 [[email protected] ~]# gunzip Net-ARP-1.0.8.tgz //使用gunzip解压tgz格式的安装包 [[email protected] ~]# tar xvf Net-ARP-1.0.8.tar //解压tar安装包 [[email protected] ~]# cd Net-ARP-1.0.8 //切换到安装目录 [[email protected] 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 [[email protected] Net-ARP-1.0.8]# make && make install //编译编译安装 [[email protected] Net-ARP-1.0.8]# cd //切换到软件包目录 [[email protected] ~]# 安装Mysql-MMM软件包 [[email protected] ~]# tar xvf mysql-mmm-2.2.1.tar.gz //解压安装包 [[email protected] ~]# cd mysql-mmm-2.2.1 //切换到安装目录 [[email protected] mysql-mmm-2.2.1]# make && make install //编译编译安装 修改公共配置文件 MySQL集群的5台服务器master1、master2、slave1、slave2、monitor都需要配置可以先配好一台后使用scp复制。 [[email protected] ~]# 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 //均衡模式 [[email protected] ~]# 修改管理主机配置文件monitor主机配置 [[email protected] ~]# 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 [[email protected] ~]# 关键地方 授权agent和monitor。 不然啥都别搭。做了主从了随便哪台机器 mysql> grant replication client on *.* to [email protected]"%" identified by "123456"; //MMM所需架构用户授权 Query OK, 0 rows affected (0.06 sec) mysql> grant replication client,process,super on *.* to [email protected]"%" identified by "123456"; //MMM所需架构用户授权 Query OK, 0 rows affected (0.00 sec) mysql> grant all on *.* to [email protected]"%" identified by "123456"; //测试用户授权给自己一个客户机开个后门测试用。 Query OK, 0 rows affected (0.00 sec) 修改客户端配置文件 master1配置 [[email protected] ~]# cat /etc/mysql-mmm/mmm_agent.conf include mmm_common.conf this master1 master2配置 [[email protected] ~]# cat /etc/mysql-mmm/mmm_agent.conf include mmm_common.conf this master2 slave1配置 [[email protected] ~]# cat /etc/mysql-mmm/mmm_agent.conf include mmm_common.conf this slave1 slave2配置 [[email protected] ~]# 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操作 [[email protected] ~]# /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主机操作 [[email protected] ~]# /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状态 [[email protected] ~]# 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: [[email protected] ~]# mmm_control set_online master1 OK: State of ‘master1‘ changed to ONLINE. Now you can wait some time and check its new roles! [[email protected] ~]# mmm_control set_online master2 OK: State of ‘master2‘ changed to ONLINE. Now you can wait some time and check its new roles! [[email protected] ~]# mmm_control set_online slave1 OK: State of ‘slave1‘ changed to ONLINE. Now you can wait some time and check its new roles! [[email protected] ~]# mmm_control set_online slave2 OK: State of ‘slave2‘ changed to ONLINE. Now you can wait some time and check its new roles! [[email protected] ~]# 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 [[email protected] ~]#ip add show [[email protected] ~]#ip add show [[email protected] ~]#ip add show [[email protected] ~]# 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) [[email protected] ~]/etc/init.d/mysqld restart [[email protected] ~]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
时间: 2024-09-28 20:12:25