Mysql之双向主从加keepalived高可用
最近在做MySQL数据库的双向主从,了解到keepalived能够自动判断并切换到可用数据库,自己试了一下,整理出文档来。
先声明一下环境
iptables开启3306端口或者关掉,关闭selinux
MySQL-01:192.168.204.138
MySQL-02:192.168.204.139
VIP:192.168.204.200 #web服务器连接的ip,自己可以使用工具连接试一下。
MySQL的安装过程就略过了,根据个人情况自己安装即可。
1.修改数据库的配置文件/etc/my.cnf:
1.1修改MySQL-01的数据库文件,在[mysql]下添加如下内容
server_id = 1 log_bin = mysql-bin
1.2修改MySQL-02的数据库文件,在[mysql]下添加如下内容
server_id = 2 log_bin = mysql-bin
2.搭建单向主从
2.1在MySQL-01上
2.1.1操作授权
mysql -u root -p #输入密码 mysql> grant replication slave on *.* to ‘slave‘@‘192.168.204.139‘ identified by ‘123456‘; mysql> flush privileges;
2.1.2数据传递给MySQL-02
mysql -u root -p #输入密码 flush tables with read lock; #锁表操作 mysql> show master status; +------------------+----------+--------------+------------------+-------------------+ | File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set | +------------------+----------+--------------+------------------+-------------------+ | mysql-bin.000012 | 120 | | | | +------------------+----------+--------------+------------------+-------------------+ 1 row in set (0.00 sec) mysqldump -u root -p --all-databases > mysqldump.sql mysql -u root -p mysql> unlock tables; mysql> quit scp myqsldump.sql 192.168.204.139:/root/
2.2在MySQL-02上操作
2.2.1恢复数据库数据
mysql -u root -p < mysqldump.sql
2.2.2建立主从同步
mysql -u root -p mysql> change master to master_host=‘192.168.204.138‘,master_user=‘slave‘,master_password=‘123456‘,master_log_file=‘mysql-bin.000012‘,master_log_pos=120,master_port=3306; start slave;
检查是否成功
show slave status\G;
Slave_IO_Running: Yes Slave_SQL_Running: Yes
都为Yes表示同步成功。
注:master_port=3306,默认时不需要加,但是修改过端口的则需要该选项
3.搭建互为主从
3.1在MySQl-01上操作
3.1.1用户授权
mysql -u root -p123456 mysql> grant replication slave on *.* to ‘slave‘@‘192.168.204.138‘ identified by ‘123456‘; //建立权限 mysql> flush privileges;
注:因为做的是所有库的主从,所以在MySQL-01上的操作会同步到MySQL-02上
3.2在MySQL-02上的操作
mysql -u root -p mysql> show master status\G; +------------------+----------+--------------+------------------+-------------------+ | File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set | +------------------+----------+--------------+------------------+-------------------+ | mysql-bin.000009 | 120 | | | | +------------------+----------+--------------+------------------+-------------------+ 1 row in set (0.00 sec)
3.1.2在MySQL-01上操作授权
mysql> change master to master_host=‘192.168.204.138‘,master_user=‘slave‘,master_password=‘123456‘,master_log_file=‘mysql-bin.000009‘,master_log_pos=120; mysql> start slave;
检查是否成功
show slave status\G;
Slave_IO_Running: Yes Slave_SQL_Running: Yes
表示成功。
此,mysql的双向主从同步已完成
4.keepalived的搭建
4.1在两台服务器依次做以下操作
a.安装keepalived
wget http://www.keepalived.org/software/keepalived-1.2.15.tar.gz tar zxvf keepalived-1.2.15.tar.gz cd keepalived-1.2.15 ./configure --prefix=/usr/local/keepalived make && make install
假如编译过程出现错误,请安装 gcc,openssl,openssl-devel
b.复制相关文件
cp /usr/local/keepalived/etc/rc.d/init.d/keepalived /etc/init.d/ cp /usr/local/keepalived/etc/sysconfig/keepalived /etc/sysconfig/ mkdir /etc/keepalived/ cp /usr/local/keepalived/etc/keepalived/keepalived.conf /etc/keepalived/ cp /usr/local/keepalived/sbin/keepalived /usr/sbin/
4.2修改配置文件
请先将/etc/keepalived/keepalived.conf文件清空
4.2.1在MySQL-01上操作
将一下内容复制进去
! Configuration File for keepalived global_defs { notification_email { [email protected] } notification_email_from [email protected] smtp_server 127.0.0.1 smtp_connect_timeout 30 router_id MySQL-ha } vrrp_instance VI_1 { state master interface eth0 virtual_router_id 51 priority 100 advert_int 1 nopreempt authentication { auth_type PASS auth_pass 1111 } virtual_ipaddress { 192.168.204.222 } } virtual_server 192.168.204.222 3306 { delay_loop 6 lb_algo wrr lb_kind DR persistence_timeout 50 protocol TCP real_server 192.168.204.138 3306 { weight 3 notify_down /var/lib/mysql/killkeepalived.sh TCP_CHECK { connect_timeout 10 nb_get_retry 3 delay_before_retry 3 connect_port 3306 } } }
4.2.2在MySQL-02上操作
vim /etc/keepalived/keepalived.conf
! Configuration File for keepalived global_defs { notification_email { [email protected] } notification_email_from [email protected] smtp_server 127.0.0.1 smtp_connect_timeout 30 router_id MySQL-ha } vrrp_instance VI_1 { state backup interface eth0 virtual_router_id 51 priority 100 advert_int 1 nopreempt authentication { auth_type PASS auth_pass 1111 } virtual_ipaddress { 192.168.204.222 } } virtual_server 192.168.204.222 3306 { delay_loop 6 lb_algo wrr lb_kind DR persistence_timeout 50 protocol TCP real_server 192.168.204.139 3306 { weight 3 notify_down /var/lib/mysql/killkeepalived.sh TCP_CHECK { connect_timeout 10 nb_get_retry 3 delay_before_retry 3 connect_port 3306 } } }
4.3在两台服务器做以下操作
vim /var/lib/mysql/killkeepalived.sh #!/bin/sh pkill keepalived chmod +x /var/lib/mysql/killkeepalived.sh
4.4启动keepalived服务
service keepalived restart
5.测试是否可用
创建一个新的用户
mysql> grant all privileges on *.* to ‘test‘@‘%‘ identified by ‘123456‘; mysql> flush privileges;
使用工具测试一下,选择navacat ,ip处填写VIP地址,连接可用
至此,配置完成
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。