MySQL -MMM 学习整理
2024-09-07 18:05:27
一. 规划
1.主机规划
服务器 |
IP |
作用 |
monitor |
10.0.0.10 |
监控服务器 |
master-01 |
10.0.0.5 |
读写主机01 |
master-02 |
10.0.0.6 |
读写主机02 |
slave-01 |
10.0.0.8 |
只读主机01 |
slave-02 |
10.0.0.9 |
只读主机02 |
2.虚拟IP分配
服务器 |
虚拟IP |
mysql读写状态 |
monitor |
10.0.0.20 |
writer |
master-01 |
10.0.0.25 |
reader |
master-02 |
10.0.0.26 |
reader |
salve-01 |
10.0.0.28 |
reader |
二. 安装操作系统
Centos-7.2 最小化安装(64位)
三.配置操作系统
1. 关闭 selinux
cat /etc/selinux/config SELINUX=disabled
2. 关闭防火墙
systemctl stop firewalld systemctl disable firewalld
3. 配置NTP ,同步时间
yum -y install ntp vi /etc/ntp.conf 增加3个时间服务器 server s2d.time.edu.cn iburst #西南地区网络中心 server s2e.time.edu.cn iburst #西北地区网络中心 server s2f.time.edu.cn iburst #东北地区网络中心
4. 启动ntp服务
systemctl start ntpd systemctl enable ntpd
5. 调整为上海时区
timedatectl set-timezone Asia/Shanghai # 设置系统时区为上海
查看状态
ntpq –p
四.安装MySQL 5.6 (不是MariaDB)
1.安裝 MySQL Repository
rpm -Uvh http://dev.mysql.com/get/mysql-community-release-el7-5.noarch.rpm
2.安裝 MySQL Server, MySQL client
yum install mysql-community-server
3.开机自动启动 MySQL
systemctl enable mysqld
4.启动MySQL
systemctl start mysqld
5.MySQL 预设为空密码, 執行以下指令修改
mysql_secure_installation mysql -u root -p
#进入mysql 控制台,增加root用户远程登录的权限
GRANT ALL PRIVILEGES ON *.* TO root@"%" IDENTIFIED BY 'GDkyUDJM6gr2Dx' WITH GRANT OPTION; flush privileges;
6.修改mysql的存储目录
a. 停止mysql systemctl stop mysql b. 迁移数据库存储目录 mkdir /opt/mysql chown mysql:mysql /opt/mysql mv /var/lib/mysql /opt/mysql cd /opt/mysql mv mysql data c. 修改配置文件 vim /etc/my.cnf #修改datadir和socket指向 datadir=/opt/mysql/data socket=/opt/mysql/mysql.sock #同时增加下面的代码,这个是为了让mysql client能够连上mysql,避免Can't connect to local MySQL server through socket '/var/lib/mysql/mysql.sock错误! [mysql] socket=/opt/mysql/mysql.sock d. 启动mysql systemctl start mysql
7. 配置MySQL Relication
a. 在/opt/mysql下增加log目录
主要内容如下: [mysqld] datadir=/opt/mysql/data socket=/opt/mysql/mysql.sock user=mysql #下面为新添加的内容 read_only= default-storage-engine=innodb replicate-ignore-db=mysql,information_schema #不同步的数据库,多个写多行 replicate-do-db=mysqltest #同步的数据库,多个写多行 binlog-ignore-db=mysql,information_schema #不需要记录二进制日志的数据库,多个用逗号隔开 binlog-do-db=mysqltest #需要记录二进制日志的数据库,多个用逗号隔开 # 以下这三个参数一定要同时在my.cnf中配置。否则在mysql.err中会出现报错 gtid_mode= on log_slave_updates= #当一个主故障,另一个立即接管 enforce_gtid_consistency= # 三个参数设置结束 sync-binlog= #每条自动更新,安全性高,默认是0 server-id = #server-id在每台服务器上的值都是不一样,在这里依次为1、、、。 #这里的日志文件命名也每台机器不一样,比如(mysql-master--bin.log,mysql-master--bin.log,mysql-slave--bin.log,mysql-slave--bin.log) g-bin = /opt/mysql/log/mysql-master--bin.log log_bin_index = /opt/mysql/log/mysql-master--bin.log.index relay_log = /opt/mysql/log/mysql-master--bin.relay relay_log_index = /opt/mysql/log/mysql-master--bin.relay.index # 日志文件指定结束 expire_logs_days = max_binlog_size = 100M log_slave_updates =
更加详细的用于生产环境的配置 见这篇文章
生产环境的mysql 5.6 和 5.7 的配置文件my.cnf
b. 在/opt/mysql下增加log目录 cd /opt/mysql
mkdir log
chown mysql:mysql log
c. 重新启动mysql systemctl restart mysql
e. 检查配置是否成功
)登录mysql,执行show master status,看是否有如下输出
+——————+———-+————–+——————+——————-+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+——————+———-+————–+——————+——————-+
| mysql-master--bin. | | | mysql | |
+——————+———-+————–+——————+——————-+
)到/opt/log/mysql目录下,看是否产生了类似mysql-master--bin.000001和mysql-master--bin.log.index的文件。 f. 在4个mysql的服务器上都修改一下。注意日志命名每台机器都不一样
五.新建同步数据库需要的用户
使用mysql-mmm时一共需要三个用户: replication、mmm_agentmmm_monitor(管理服务器上用来监控cluster状态的用户,所以可以限定只能从管理服务器登录)。使用下面三条命令新建这三个用户并分配相应的权限
所有的mysql 服务器都运行一遍 GRANT REPLICATION CLIENT ON *.* TO 'mmm_monitor'@'10.0.0.%' IDENTIFIED BY 'monitor';GRANT SUPER, REPLICATION CLIENT, PROCESS ON *.* TO 'mmm_agent'@'10.0.0.%' IDENTIFIED BY 'agent'; GRANT REPLICATION SLAVE ON *.* TO 'replication'@'10.0.0.%' IDENTIFIED BY 'replication'; flush privileges;
六.设置复制机制
(从master-001复制到master-002,从master-002复制到slave-001,slave-002)
.配置master-001作为主,复制到master-
a.配置master- 由于这里是配置master-001做为源数据,所以不需要设置change master 和 启动slave进程 b.配置master- (指定 change master 为 master-,表示从master- 复制到 master-) #ssh到 master- 服务器 mysql -u root -p #在mysql控制台录入 #这个是在my.cnf中没有使用GTID(MySQL5.6新特性)下的命令 #CHANGE MASTER TO MASTER_HOST='10.0.0.5',MASTER_USER='replication',MASTER_PASSWORD='replication',MASTER_PORT=,MASTER_LOG_FILE='mysql-master-02-bin.000001', MASTER_LOG_POS=,MASTER_CONNECT_RETRY=; # 这个是在my.cnf中使用GTID(MySQL5.6新特性)下的命令 CHANGE MASTER TO MASTER_HOST='10.0.0.5',MASTER_USER='replication',MASTER_PASSWORD='replication',MASTER_PORT=,MASTER_AUTO_POSITION = ; #重置reset reset slave; #启动slave start slave; #查看slave状态 show slave status\G #结果如下 mysql> show slave status\G *************************** . row *************************** Slave_IO_State: Waiting for master to send event
Master_Host: 192.168.1.212
Master_User: replication
Master_Port:
Connect_Retry:
Master_Log_File: mysql-master--bin.
Read_Master_Log_Pos:
Relay_Log_File: mysql-master--bin.
Relay_Log_Pos:
Relay_Master_Log_File: mysql-master--bin.
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
Replicate_Do_DB:
Replicate_Ignore_DB: mysql
Replicate_Do_Table:
Replicate_Ignore_Table:
Replicate_Wild_Do_Table:
Replicate_Wild_Ignore_Table:
Last_Errno:
Last_Error:
Skip_Counter:
Exec_Master_Log_Pos:
Relay_Log_Space:
Until_Condition: None
Until_Log_File:
Until_Log_Pos:
Master_SSL_Allowed: No
Master_SSL_CA_File:
Master_SSL_CA_Path:
Master_SSL_Cert:
Master_SSL_Cipher:
Master_SSL_Key:
Seconds_Behind_Master:
Master_SSL_Verify_Server_Cert: No
Last_IO_Errno:
Last_IO_Error:
Last_SQL_Errno:
Last_SQL_Error:
Replicate_Ignore_Server_Ids:
Master_Server_Id:
Master_UUID: 3d3b9f4f-f74f-11e5-9a30-005056b324c4
Master_Info_File: /opt/mysql/data/master.info
SQL_Delay:
SQL_Remaining_Delay: NULL
Slave_SQL_Running_State: Slave has read all relay log; waiting for the slave I/O thread to update it
Master_Retry_Count:
Master_Bind:
Last_IO_Error_Timestamp:
Last_SQL_Error_Timestamp:
Master_SSL_Crl:
Master_SSL_Crlpath:
Retrieved_Gtid_Set:
Executed_Gtid_Set:
Auto_Position:
row in set (0.00 sec) 注意:
Slave_IO_Running: Yes
Slave_SQL_Running: Yes c.配置slave-: (指定 change master 为 master-,表示从master- 复制到 slave-) #ssh到 slave- 服务器 mysql -u root -p #在mysql控制台录入 #这个是在my.cnf中没有使用GTID(MySQL5.6新特性)下的命令
CHANGE MASTER TO MASTER_HOST='10.0.0.6',MASTER_USER='replication',MASTER_PASSWORD='replication',MASTER_PORT=,MASTER_LOG_FILE='mysql-slave-001-bin.000001', MASTER_LOG_POS=,MASTER_CONNECT_RETRY=; # 这个是在my.cnf中使用GTID(MySQL5.6新特性)下的命令 CHANGE MASTER TO MASTER_HOST='10.0.0.6',MASTER_USER='replication',MASTER_PASSWORD='replication',MASTER_PORT=,MASTER_AUTO_POSITION = ; #重置reset reset slave; #启动slave start slave; #查看slave状态 show slave status\G #结果如下 mysql> show slave status\G d.配置slave- (指定 change master 为 master-,表示从master- 复制到 slave-) #ssh到 slave- 服务器 mysql -u root -p #在mysql控制台录入 #这个是在my.cnf中没有使用GTID(MySQL5.6新特性)下的命令
CHANGE MASTER TO MASTER_HOST='10.0.0.6',MASTER_USER='replication',MASTER_PASSWORD='replication',MASTER_PORT=,MASTER_LOG_FILE='mysql-slave-002-bin.000001', MASTER_LOG_POS=,MASTER_CONNECT_RETRY=; # 这个是在my.cnf中使用GTID(MySQL5.6新特性)下的命令 CHANGE MASTER TO MASTER_HOST='10.0.0.6',MASTER_USER='replication',MASTER_PASSWORD='replication',MASTER_PORT=,MASTER_AUTO_POSITION = ; #重置reset reset slave; #启动slave start slave; #查看slave状态 show slave status\G #结果如下 mysql> show slave status\G
这样就完成了master- 和 master-002的相互复制,并且从master-002复制到slave- 和 slave-.
a.在master-001中建立database ,确认master-,slave- 和 slave-002中都同步复制。
b.在master-002中建立database ,确认master-,slave- 和 slave-002中都同步复制。
2.测试复制机制是否成功。
3.MySQL5.6 GTID新特性实践
http://cenalulu.github.io/mysql/mysql-5-6-gtid-basic/
4.mysql主从复制-CHANGE MASTER TO 语法详解
http://blog.csdn.net/jesseyoung/article/details/41942809
七.安装MMM(http://mysql-mmm.org/)
1.安装epel扩展包
CentOS软件仓库默认是不含这些软件的,必须要有epel这个包的支持。故我们必须先安装epel
wget http://dl.fedoraproject.org/pub/epel/7/x86_64/Packages/e/epel-release-7-11.noarch.rpm
rpm -ivh epel-release--.noarch.rpm
yum install mysql-mmm-monitor
yum install mysql-mmm-agent #用yum安装的 mysql-mmm组件启动的时候会出错。
所以用下面的tar.gz文件来安装
yum erase mysql-mmm-monitor
yum erase mysql-mmm-agent
2.在monitor(10.0.0.20) 上安装监控程序
· cd /tmp
wget http://pkgs.fedoraproject.org/repo/pkgs/mysql-mmm/mysql-mmm-2.2.1.tar.gz/f5f8b48bdf89251d3183328f0249461e/mysql-mmm-2.2.1.tar.gz
tar -xzvf mysql-mmm-2.2..tar.gz
cd mysql-mmm-2.2..tar.gz
make install
3.在数据库服务器(192.168.1.211-214)上安装代理
cd /tmp
wget http://pkgs.fedoraproject.org/repo/pkgs/mysql-mmm/mysql-mmm-2.2.1.tar.gz/f5f8b48bdf89251d3183328f0249461e/mysql-mmm-2.2.1.tar.gz
tar -xzvf mysql-mmm-2.2..tar.gz
cd mysql-mmm-2.2..tar.gz
make install
4.配置MMM
a.编辑/etc/mysql-mmm/mmm_common.conf 完成安装后,所有的配置文件都放到了/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/ #这里要确认是否下面有agent,monitor,tools的目录。否则无法生成vip
replication_user replication
replication_password replication
agent_user mmm_agent
agent_password agent
</host>
<host db1>
ip 10.0.0.25
mode master
peer db2
</host>
<host db2>
ip 10.0.0.26
mode master
peer db1
</host>
<host db3>
ip 10.0.0.28
mode slave
</host>
<host db4>
ip 10.0.0.29
mode slave
</host>
<role writer>
hosts db1, db2
ips 10.0.0.20
mode exclusive
</role>
<role reader>
hosts db2, db3, db4
ips 10.0.0.25, 10.0.0.26, 10.0.0.28
mode balanced
</role>
通过scp命令分别复制到monitor-、master-、master-、slave-001和slave-002共五台机器上。 b. 编辑 4台mysql节点机上的/etc/mysql-mmm/mmm_agent.conf
在数据库服务器上,还有一个mmm_agent.conf需要修改,其内容是: include mmm_common.conf
# The 'this' variable refers to this server. Proper operation requires
# that 'this' server (db1 by default), as well as all other servers, have the # proper IP addresses set in mmm_common.conf.
this db1
最后一行的db1,在不同的数据库服务器上要分别改为db2、db3和db4,否则代理就会无法启动。 c. 编辑 monitor主机上的/etc/mysql-mmm/mmm_mon.conf
在monitor-001上配置mmm_mon.conf include mmm_common.conf
<monitor>
ip 127.0.0.1 #为了安全性,设置只在本机监听,mmm_mond 默认监听9988
pid_path /var/run/mmm_mond.pid
bin_path /usr/lib/mysql-mmm/
status_path /var/lib/misc/mmm_mond.status
ping_ips 10.0.0.5, 10.0.0.6, 10.0.0.8, 10.0.0.9 #用于测试网络可用性 IP 地址列表,只要其中有一个地址 ping 通,就代表网络正常,这里不要写入本机地址
#flap_duration #抖动的时间范围,单位秒,这两个参数考虑情况添加
# flap_count #在抖动的时间范围内,最大的抖动次数
auto_set_online #是否设置自动上线,如果该值大于0,抖动的主机在抖动的时间范围过后,则设置自动上线
# The kill_host_bin does not exist by default, though the monitor will
# throw a warning about it missing. See the section 5.10 "Kill Host
</monitor>
<check default>
check_period
trap_period
timeout
#restart_after
max_backlog
</check>
<host default>
monitor_user mmm_monitor
monitor_password monitor
</host>
5.自动启动agent 和 monitor
a. 在monitor上启动monitor systemctl enable mysql-mmm-monitor
systemctl start mysql-mmm-monitor
b. 在4个数据库服务器上启动agent systemctl enable mysql-mmm-agent
systemctl start mysql-mmm-agent
如果出现Starting MMM Monitor daemon: Can't locate Proc/Daemon.pm in @INC (@INC contains: /usr/local/lib64/perl5 /usr/local/share/perl5 /usr/lib64/perl5/vendor_perl /usr/share/perl5/vendor_perl /usr/lib64/perl5 /usr/share/perl5 .) at /usr/sbin/mmm_mond line 11 [root@monitor ~]# yum install cpan [root@monitor ~]# cpan Proc::Daemon [root@monitor ~]# cpan Log::Log4perl yum install –y perl-* yum install -y libart_lgpl.x86_64 yum install -y mysql-mmm.noarch fail yum install -y rrdtool.x86_64 yum install -y rrdtool-perl.x86_64 [root@monitor mysql-mmm-2.2.]# /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 如果出现Configuration file /etc/mysql-mmm/mmm_common.conf is world readable!这种错误,需要查看/etc/mysql-mmm/mmm_common.conf文件的权限,应该是 chmod /etc/mysql-mmm/mmm_common.conf
集群中所有配置文件的权限最好都设置为640,否则启动 MMM 服务的时候可能出错
八. 测试集群
MMM启动顺序:先启动monitor,再启动 agent
mmm_control show # Warning: agent on host db1 is not reachable db1(10.0.0.25) master/AWAITING_RECOVERY. Roles: db2(10.0.0.26) master/ONLINE. Roles: reader(10.0.0.26), writer(10.0.0.20) db3(10.0.0.28) slave/ONLINE. Roles: reader(10.0.0.28) db4(10.0.0.29) slave/ONLINE. Roles: reader(10.0.0.25)
九. 问题和解决办法
1.无法分配vip
在monitor主机上使用 systemctl start mysql-mmm-monitor 无法分配vip。而通过/etc/init.d/mysql-mmm-monitor start则可以分配vip 解决办法
http://blog.csdn.net/remote_roamer/article/details/49869395
由于缺少环境变量,导致无法激活vip。
那么就在 /etc/rc.d/init.d/mysql-mmm-monitor的脚本文件里面最上面,加入
source /root/.bash_profile
2.vip漂移后,无法ping通10.0.0.20
问题描述:
当master-001发生故障,导致DB不可用时,VIP会自动漂移到master-002上,以实现高可用。但出现了一个问题,由于ARP老化时间过长,导致漂移过去的VIP不可用,也无法ping通。也就是说,MySQL-MMM没有考虑到ARP老化时间过长的情况而采取强刷ARP的方式。 a.解决办法1 同时修改master- 和 master-002上的代码(这个没测试成功)
修改文件
/usr/share/perl5/vendor_perl/MMM/Agent/Helpers/Actions.pm vim /usr/share/perl5/vendor_perl/MMM/Agent/Helpers/Actions.pm 在sub configure_ip($$)代码段里面的_exit_ok();前面加入以下代码 #这里是解决vip偏移后,无法ping通。原因是arp 老化时间过长,这里强制刷新arpmy $getway = `/sbin/route | awk 'default/ (print $2)'`
`/sbin/arping -I $if -c -s $ip $getway `;
#配置结束 b.解决办法2
在vip漂移到的主机上手工重置arping arping -I eno16777984 -c -s 0.0.0.20 10.0.0.1 c.解决办法3
在master- 和 master- 上使用shell脚本来刷新(这个方法正常运行) vim /root/refresh_vip.sh
#增加如下内容:
#!/bin/sh
PATH=/sbin:/bin:/usr/sbin:/usr/bin:/usr/local/bin:/usr/local/sbin
MMM_COMM_CONF="/etc/mysql-mmm/mmm_common.conf"
ETH_NAME=`awk '/cluster_interface/{print $2}' ${MMM_COMM_CONF}`
VIP_ADDR=`grep -A '<role writer>' ${MMM_COMM_CONF} | awk '/ips/{print $2}'`
GETWAY_ADDR=`/sbin/route | awk '/default/ {print $2}'` if [[ -n `/sbin/ip addr show ${ETH_NAME} | grep ${VIP_ADDR}` ]]; then
/sbin/arping -I ${ETH_NAME} -c -s ${VIP_ADDR} ${GETWAY_ADDR} >/dev/null >&
fi
十.放入crontab中运行
crontab -e
* * * * * sleep ; /root/refresh_vip.sh >/dev/null >&
* * * * * sleep ; /root/refresh_vip.sh >/dev/null >&
* * * * * sleep ; /root/refresh_vip.sh >/dev/null >&
* * * * * sleep ; /root/refresh_vip.sh >/dev/null >&
* * * * * sleep ; /root/refresh_vip.sh >/dev/null >&
最新文章
- python字符串基础知识
- AngularJs $interval 和 $timeout
- [Js]无缝滚动
- crm 4 IFRAME 元素隐藏
- .NET 认识
- EXTJS 4.2 资料 控件之combo 联动
- [转] Android LocalService与RemoteService理解
- Http静态资源的缓存
- 带参数的存储过程和标量Function
- 【原创】构建高性能ASP.NET站点 第五章—性能调优综述(后篇)
- docker中管理数据
- oracle11g 体系结构详解
- datatable拆分多个
- rabbitmq-cluster搭建
- webpack的安装及使用
- xml嵌套防止解析
- android广播(内部类)使用
- Jmeter笔记:响应断言详解
- 案例:通过shell脚本实现mysql数据备份与清理
- SQL 行列转置