这篇文章将为大家详细讲解有关如何理解基于keepalived的MySQL主主复制,文章内容质量较高,因此小编分享给大家做个参考,希望大家阅读完这篇文章后对相关知识有一定的了解。
系统环境:Linux red hat 6.3
mysql版本:mysql 5.6.34
mater1节点ip:192.168.140.222 主机名:red1
mater2节点ip:192.168.140.224 主机名:red2
一、分别在两个节点安装mysql数据库。
二、1节点修改/etc/my.cnf参数
server_id =1 //标志,节点之间不能重复
log-bin=/var/lib/mysql/xbinlog/binlog //开启binlog,并保存在位置/var/lib/mysql/xbinlog/下
binlog-format=ROW //binlog的模式是row模式
log_slave_updates=1 //开启log-slave-updates参数后,从库从主库复制的数据会写入log-bin日志文件里
sync_binlog=1 //当每进行1次事务提交之后,MySQL将进行一次fsync之类的磁盘同步指令来将binlog_cache中的数据强制写入磁盘。
innodb_flush_log_at_trx_commit=1 //代表每1次事务提交或事务外的指令都需要把日志写入(flush)硬盘
slave-parallel-workers=2 //设定从服务器的SQL线程数位2个;0表示关闭多线程复制功能
replicate-do-db=replicate //同步的数据库是replicate
并且给/var/lib/mysql/xbinlog/目录赋予mysql:mysql权限,否则会起不来 chown mysql:mysql /var/lib/mysql/xbinlog/
重启mysql服务
2节点修改/etc/my.cnf参数
server_id =2 //这个参数要保证2个节点不一致
log_bin=/opt/mysql/data/ybinlog/binlog
binlog-format=ROW
log_slave_updates=1
sync_binlog=1
innodb_flush_log_at_trx_commit=1
slave-parallel-workers=2
replicate-do-db=replicate
并且给/var/lib/mysql/ybinlog/目录赋予mysql:mysql权限,否则会起不来 chown mysql:mysql /var/lib/mysql/ybinlog/
重启mysql服务
三、在1节点:
mysql> grant replication slave on *.* to 'replicate'@'192.168.140.224' identified by 'replicate';
Query OK, 0 rows affected (0.00 sec)
mysql> flush tables with read lock;
Query OK, 0 rows affected (0.00 sec)
mysql> show master status;
+---------------+----------+--------------+------------------+-------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+---------------+----------+--------------+------------------+-------------------+
| binlog.000002 | 337 | | | |
+---------------+----------+--------------+------------------+-------------------+
1 row in set (0.00 sec)
mysql> unlock tables;
Query OK, 0 rows affected (0.00 sec)
在2节点
mysql> change master to master_host='192.168.140.222',master_user='replicate',master_password='replicate',master_log_file='binlog.000002',master_log_pos=337;
Query OK, 0 rows affected, 2 warnings (0.00 sec)
mysql> start slave;
Query OK, 0 rows affected, 1 warning (0.01 sec)
mysql> show slave status\G //查看Slave_IO_Running 和 Slave_SQL_Running两项是不是yes状态
mysql> show master status;
+---------------+----------+--------------+------------------+-------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+---------------+----------+--------------+------------------+-------------------+
| binlog.000001 | 120 | | | |
+---------------+----------+--------------+------------------+-------------------+
在1节点
mysql> change master to master_host='192.168.140.224',master_user='replicate', master_password='replicate',master_log_file='binlog.000001',master_log_pos=120;
Query OK, 0 rows affected, 2 warnings (0.01 sec)
mysql> start slave;
Query OK, 0 rows affected, 1 warning (0.00 sec)
mysql> show slave status\G //查看Slave_IO_Running 和 Slave_SQL_Running两项是不是yes状态
四、测试,在2个节点都插入数据,然后select验证一下
五、下载keepalived
http://www.keepalived.org/software/keepalived-1.2.13.tar.gz
解压:tar xvf keepalived-1.2.13.tar.gz
cd keepalived-1.2.13
初始化: ./configure --disable-fwmark
编译安装:make && make install
keepalived的配置前提
1、本机的主机名与hosts中定义的主机保持一致。
2、各节点要能互相解析主机名,也就是说hosts文件中要有本机的和对方的ip和对应的主机名
3、各个节点的时间必须同步
关闭selinux并清空防火墙规则
[root@red1 ~]# setenforce 0
[root@red1 ~]# iptables -F
配置keepalived
cp /usr/local/etc/sysconfig/keepalived /etc/sysconfig/
cp /usr/local/sbin/keepalived /usr/bin/
cp /software/keepalived/keepalived-1.2.13/keepalived/etc/init.d/keepalived.init /etc/init.d
mkdir -p /etc/keepalived
cp /software/keepalived/keepalived-1.2.13/keepalived/etc/keepalived/keepalived.conf /etc/keepalived/
节点1上配置
vim /etc/keepalived/keepalived.conf
! Configuration File for keepalived
global_defs { //全局配置段
notification_email { //定义故障通知邮箱
root@localhost //邮箱地址,可以多个
}
notification_email_from root@localhost //发件人地址
smtp_server localhost //邮件服务器地址
smtp_connect_timeout 30 //联系邮件服务器的超时时长
router_id red1 //当前物理设备在网络中的唯一ID标识
}
vrrp_instance VI_1 { //实例名在配置文件中不能重复
state MASTER //MASTER或BACKUP,定义当前节点默认为主或从节点
interface eth0
virtual_router_id 51 //当前虚拟路由的标识符,必须全局唯一
priority 100 //定义当前实例的优先级,备机要小于主机
advert_int 1 //状态通告周期(秒)---用于主节点向BACKUP节点通告心跳信息
authentication { /消息认证配置
auth_type PASS //认证方式(MD5或PASS简单字符认证)
auth_pass 1111 //认证密码,所有同一虚拟实例节点的必须保持一致
}
virtual_ipaddress { //自动配置在interface上的(vip)虚拟IP地址,可以是多个
192.168.140.66 //虚拟ip
}
}
virtual_server 192.168.140.66 { //虚拟ip
delay_loop 6 //RealServer的健康状态探查的时间间隔
lb_algo rr //负载均衡的调度算法(Scheduler)
lb_kind DR //LVS类型。支持NAT/DR/TUN
persistence_timeout 50 //持久连接超时时间
protocol TCP //只能适用TCP,若适用UDP则使用ops参数
real_server 192.168.140.222 { //1节点ip
weight 1 权重
TCP_CHECK {
connect_timeout 3 健康状态监测超时时间
nb_get_retry 3 number of get retry
delay_before_retry 3 每次重试之前延迟3秒
# connect_port 3306
}
}
}
节点2上配置
! Configuration File for keepalived
global_defs {
notification_email {
root@localhost
}
notification_email_from root@localhost
smtp_server localhost
smtp_connect_timeout 30
router_id red2
}
vrrp_instance VI_1 {
state BACKUP
interface eth0
virtual_router_id 51
priority 90
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
192.168.140.66
}
}
virtual_server 192.168.140.66 {
delay_loop 6
lb_algo rr
lb_kind DR
persistence_timeout 50
protocol TCP
real_server 192.168.140.224{
weight 1
TCP_CHECK {
connect_timeout 3
nb_get_retry 3
delay_before_retry 3
#connect_port 3306
}
}
}
开启服务 service keepalived start
查看状态 service keepalived status
查看VIP是否在本节点 ip add //VIP:192.168.140.66会在节点1上,因为1节点的优先级高。
六、测试keepalived
此时在1节点和2节点上执行ip add,会发现VIP 192.168.140.66会飘在1节点上。
然后在1节点上执行service keepalived stop,然后在2节点上执行ip add,会发现vip飘到了2节点。
然后在1节点service keepalived start,会发现vip又回到了1节点。
到此,测试成功
关于如何理解基于keepalived的MySQL主主复制就分享到这里了,希望以上内容可以对大家有一定的帮助,可以学到更多知识。如果觉得文章不错,可以把它分享出去让更多的人看到。
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。