keepalived+HAProxy+MySQL双主实验

keepalived+HAProxy+MySQL双主实验

  • 环境准备
shell 复制代码
node1(HAProxy1):192.168.184.10
node2(HAProxy2):192.168.184.20
node3(MySQL1):192.168.184.30
node4(MySQL2):192.168.184.40
虚拟IP vip:192.168.184.100
  • MySQL部署
shell 复制代码
在node3执行以下脚本:
#!/bin/bash
systemctl stop firewalld
setenforce 0
yum install mariadb-server -y
sed -i '/^\[mysqld\]$/a\binlog-ignore = information_schema' /etc/my.cnf.d/server.cnf
sed -i '/^\[mysqld\]$/a\binlog-ignore = mysql' /etc/my.cnf.d/server.cnf
sed -i '/^\[mysqld\]$/a\skip-name-resolve' /etc/my.cnf.d/server.cnf
sed -i '/^\[mysqld\]$/a\auto-increment-increment = 1' /etc/my.cnf.d/server.cnf # 注意node4节点上必须不同
sed -i '/^\[mysqld\]$/a\log-bin = mysql-bin' /etc/my.cnf.d/server.cnf
sed -i '/^\[mysqld\]$/a\auto_increment_offset = 1' /etc/my.cnf.d/server.cnf # 注意node4节点上必须不同
sed -i '/^\[mysqld\]$/a\server-id = 1' /etc/my.cnf.d/server.cnf # 注意node4节点上必须不同
systemctl restart mariadb
mysql -uroot -e "grant replication slave on *.* to repuser@'192.168.184.30' identified by '000000';"
shell 复制代码
在node4执行以下脚本:
#!/bin/bash
systemctl stop firewalld
setenforce 0
yum install mariadb-server -y
sed -i '/^\[mysqld\]$/a\binlog-ignore = information_schema' /etc/my.cnf.d/server.cnf
sed -i '/^\[mysqld\]$/a\binlog-ignore = mysql' /etc/my.cnf.d/server.cnf
sed -i '/^\[mysqld\]$/a\skip-name-resolve' /etc/my.cnf.d/server.cnf
sed -i '/^\[mysqld\]$/a\auto-increment-increment = 2' /etc/my.cnf.d/server.cnf
sed -i '/^\[mysqld\]$/a\log-bin = mysql-bin' /etc/my.cnf.d/server.cnf
sed -i '/^\[mysqld\]$/a\auto_increment_offset = 2' /etc/my.cnf.d/server.cnf 
sed -i '/^\[mysqld\]$/a\server-id = 2' /etc/my.cnf.d/server.cnf 
systemctl restart mariadb
mysql -uroot -e "grant replication slave on *.* to repuser@'192.168.184.30' identified by '000000';"
shell 复制代码
查询node3节点master状态:
MariaDB [(none)]> show master status;
+------------------+----------+--------------+--------------------------+
|       File       | Position | Binlog_Do_DB |      Binlog_Ignore_DB    |
+------------------+----------+--------------+--------------------------+
| mysql-bin.000001 |   401    |              | mysql,information_schema |
+------------------+----------+--------------+--------------------------+
shell 复制代码
查询node4节点master状态
MariaDB [(none)]> show master status;
+------------------+----------+--------------+--------------------------+
|       File       | Position | Binlog_Do_DB |     Binlog_Ignore_DB     |
+------------------+----------+--------------+--------------------------+
| mysql-bin.000001 |    245   |              | mysql,information_schema |
+------------------+----------+--------------+--------------------------+
shell 复制代码
在node3节点执行连接命令:
MariaDB [(none)]> change master to master_host="192.168.184.40",master_port=3306,master_user="repuser",master_password="000000",master_log_file="mysql-bin.000001",master_log_pos=245;
MariaDB [mysql]> start slave;
shell 复制代码
在node4节点执行连接命令:
MariaDB [(none)]> change master to master_host="192.168.184.30",master_port=3306,master_user="repuser",master_password="000000",master_log_file="mysql-bin.000001",master_log_pos=401;
MariaDB [mysql]> start slave;
shell 复制代码
查看从节点状态: show slave status \G; 观察IO和SQL线程是否为YES
MariaDB [(none)]> show slave status \G;
    Slave_IO_Running: Yes
    Slave_SQL_Running: Yes
shell 复制代码
测试:
1.在node3上创建db1数据库,在node4上查看是否有db1
2.在node4上创建db2数据库,在node3上查看是否有db2
3.最终要实现node3和node4上保持数据同步
  • HAProxy部署
shell 复制代码
在node1和node2上执行以下脚本:
#!/bin/bash
yum install haproxy ‐y
mv /etc/haproxy/haproxy.cfg{,.bak}
cat > /etc/haproxy/haproxy.cfg << EOF
global
	log 127.0.0.1 local2
	chroot /var/lib/haproxy
	pidfile /var/run/haproxy.pid
	maxconn 4000
	user haproxy
	group haproxy
	daemon
	stats socket /var/lib/haproxy/stats
listen mysql_proxy
	bind 0.0.0.0:3306
	mode tcp
	balance source
	server mysqldb1 192.168.184.30:3306 weight 1 check
	server mysqldb2 192.168.184.40:3306 weight 2 check
listen stats
	mode http
	bind 0.0.0.0:8080
	stats enable
	stats uri /dbs
	stats realm haproxy\ statistics
	stats auth admin:admin
EOF
systemctl start haproxy
  • keepalived部署
shell 复制代码
node1上执行以下脚本:
#!/bin/bash
yum install keepalived ‐y
mv /etc/keepalived/keepalived.conf{,.bak}
cat > /etc/keepalived/keepalived.conf << EOF
! Configuration File for keepalived
global_defs {
	router_id node1
}
vrrp_script chk_http_port {
	script "/usr/local/src/check_proxy_pid.sh"
	interval 1
	weight ‐2
}
vrrp_instance VI_1 {
	state MASTER
	interface ens33
	virtual_router_id 10
	priority 100
	advert_int 1
	authentication {
	auth_type PASS
	auth_pass 1111
	}
	track_script {
		chk_http_port
	}
	virtual_ipaddress {
		192.168.184.100
	}
}
EOF
systemctl start keepalived
node2上执行以下脚本:
#!/bin/bash
yum install keepalived ‐y
mv /etc/keepalived/keepalived.conf{,.bak}
cat > /etc/keepalived/keepalived.conf << EOF
! Configuration File for keepalived
global_defs {
	router_id node2
}
vrrp_instance VI_1 {
	state MASTER
	interface ens33
	virtual_router_id 10
	priority 99
	advert_int 1
	authentication {
		auth_type PASS
		auth_pass 1111
	}
	virtual_ipaddress {
		192.168.10.100
	}
}
EOF
systemctl start keepalived
[root@node1 src]# cat check_proxy_pid.sh
#!/bin/bash
A=`ps -C haproxy --no-header | wc -l`
if [ $A -eq 0 ];then
exit 1
else
exit 0
fi
相关推荐
云贝贝贝1 小时前
MySQL 运维高频 6 坑:字符集、超时、SQL_MODE、隐式转换、Online DDL、主从延迟
运维·sql·mysql
jyOverQ16 小时前
MySQL 联合索引怎么用?最左匹配原则到底是什么意思?
数据库·mysql
没文化的阿浩16 小时前
【MySQL】用户管理
android·mysql·adb
ShineWinsu18 小时前
对于MySQL:数据库的操作的解析
linux·数据库·c++·mysql·面试·笔试·库的操作
‎ദ്ദിᵔ.˛.ᵔ₎18 小时前
MySQL 数据类型
数据库·mysql
这个DBA有点耶18 小时前
索引合并不是万能药:MySQL同时用两个索引,为什么比不用还慢?
数据库·mysql·dba
Michaelwubo19 小时前
mysql8 主从 HA切换
android·adb
大牧师20 小时前
MySQL 学习教程
数据库·sql·mysql·docker·node·全栈·后端数据
数据库小学妹20 小时前
MySQL redo刷盘实测:innodb_flush_log_at_trx_commit与故障矩阵
运维·数据库·mysql
知码研习20 小时前
Windows MySQL8.0.44 保姆级超详细安装配置教程(含 ZIP 免安装、MSI 图形安装、完整卸载流程)
mysql