Mysql数据库双主部署方案

mysql运行在Dcoker容器中

docker bridge 路由互联

Docker容器之间使用直接路由的方式互联

VPN为直接路由提供虚拟子网

(悄悄说一句,不知道什么时候的文章,随便就发出来了)

VPN wireguard

1
2
3
add-apt-repository ppa:wireguard/wireguard
apt upgrade
apt install wireguard resolvconf -y

server

1
2
3
cd /etc/wireguard
wg genkey | tee server_privatekey | wg pubkey > server_publickey
wg genkey | tee client_privatekey | wg pubkey > client_publickey
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
───────┬────────────────────────────────────────────────────────────────────────
│ File: client_privatekey
───────┼────────────────────────────────────────────────────────────────────────
1 │ 8DuJ+ZtSBZBt4q5hXShQHQ4v9OK2IMe/hZKhnf9OAXk=
───────┴────────────────────────────────────────────────────────────────────────
───────┬────────────────────────────────────────────────────────────────────────
│ File: client_publickeyserver_publickeyet
───────┼────────────────────────────────────────────────────────────────────────
1 │ WLntiEvzdX3JYIgKd5Hto4USki/LHmbhmC4XVbIEZ0U=
───────┴────────────────────────────────────────────────────────────────────────
───────┬────────────────────────────────────────────────────────────────────────
│ File: server_privatekey
───────┼────────────────────────────────────────────────────────────────────────
1 │ SAoDsXx4bfgxk/zpbaWyO0NNWILDqF0BSwsC6cx6jUI=
───────┴────────────────────────────────────────────────────────────────────────
───────┬────────────────────────────────────────────────────────────────────────
│ File: server_publickeyet
───────┼────────────────────────────────────────────────────────────────────────
1 │ mnCRmMFjves7n+s35QPOgB2yEz4xPWAN+tbdOKcZbQg=
───────┴────────────────────────────────────────────────────────────────────────

wg0.conf

1
2
3
4
5
6
7
8
9
10
11
12
[Interface]
PrivateKey = $(cat server_privatekey)
Address = 10.0.0.1/24
PostUp = iptables -A FORWARD -i wg0 -j ACCEPT; iptables -A FORWARD -o wg0 -j ACCEPT; iptables -t nat -A POSTROUTING -o eth0 -j MASQUERADE
PostDown = iptables -D FORWARD -i wg0 -j ACCEPT; iptables -D FORWARD -o wg0 -j ACCEPT; iptables -t nat -D POSTROUTING -o eth0 -j MASQUERADE
ListenPort = 443
DNS = 8.8.8.8
MTU = 1420

[Peer]
PublicKey = $(cat client_publickey)
AllowedIPs = 10.0.0.2/32

client.conf

1
2
3
4
5
6
7
8
9
10
11
[Interface]
PrivateKey = $(cat client_privatekey)
Address = 10.0.0.2/24
DNS = 8.8.8.8
MTU = 1420

[Peer]
PublicKey = $(cat server_publickey)
Endpoint = 128.199.152.999:443
AllowedIPs = 0.0.0.0/0, ::0/0
PersistentKeepalive = 25

systemctl enable wg-quick@wg0

创建Docker Bridge

1
2
3
docker network create --driver=bridge --subnet=172.28.1.0/24 -o com.docker.network.bridge.name=gbr0 gbr0

docker network create --driver=bridge --subnet=172.28.2.0/24 -o com.docker.network.bridge.name=gbr0 gbr0

永久化路由

需要直接连接 需要VPN - wireguard

/etc/network/interfaces.d/gbr0-init.cfg

1
2
3
up ip route add 172.28.2.0/24 via 对方IP || true

up ip route add 172.28.1.0/24 via 对方IP || true

永久化iptabel

添加路由表

1
2
3
iptables -t nat -A POSTROUTING -s 172.17.1.0/24 ! -d 172.17.0.0/16 -j MASQUERADE

iptables -t nat -A POSTROUTING -s 172.17.2.0/24 ! -d 172.17.0.0/16 -j MASQUERADE

保存路由表

1
sudo sh -c "iptables-save > /etc/iptables.conf"

开机自动应用

/etc/rc.local

1
2
# Load iptables rules from this file
iptables-restore < /etc/iptables.conf

开启SNAT

/etc/sysctl.conf << net.ipv4.ip_forward = 1

sysctl -p

mysql 双M结构

1
2
3
4
5
6
7
8
9
10
11
[mysqld]
server-id = 1
log_bin = /var/log/mysql/mariadb-bin
log_bin_index = /var/log/mysql/mariadb-bin.index
binlog-do-db = tudou1 #需要同步的数据库,这里同步tudou1和tudou2两个数据库
binlog-do-db = tudou2
binlog-ignore-db = mysql #忽略同步的数据库
log_slave_updates #把从库的写操作记录到binlog中 (缺少之后,双主创建失败)
expire_logs_days = 365 #日志文件过期天数,默认是 0,表示不过期
auto_increment_increment= 2 #设定为主服务器的数量,防止auto_increment字段重复
auto_increment_offset = 1 #自增长字段的初始值,在多台master环境下,不会出现自增长ID重复
1
2
3
4
5
6
7
8
9
10
11
[mysqld]
server-id = 2
log_bin = /var/log/mysql/mariadb-bin
log_bin_index = /var/log/mysql/mariadb-bin.index
binlog-do-db = tudou1 #需要同步的数据库,这里同步tudou1和tudou2两个数据库
binlog-do-db = tudou2
binlog-ignore-db = mysql #忽略同步的数据库
log_slave_updates #把从库的写操作记录到binlog中 (缺少之后,双主创建失败)
expire_logs_days = 365 #日志文件过期天数,默认是 0,表示不过期
auto_increment_increment= 2 #设定为主服务器的数量,防止auto_increment字段重复
auto_increment_offset = 2 #自增长字段的初始值,在多台master环境下,不会出现自增长ID重复

创建同步账户

1
2
3
4
5
6
7
8
// 服务器 1

GRANT REPLICATION SLAVE ON *.* TO 'repuser'@'server-2' IDENTIFIED BY 'repuser';
FLUSH PRIVILEGES;

// 服务器 2
GRANT REPLICATION SLAVE ON *.* TO 'repuser'@'server-1' IDENTIFIED BY 'repuser';
FLUSH PRIVILEGES;

设置同步

1
2
3
4
5
6
7
8
9
10
11
12
13
14
// 服务器 2

MariaDB [mysql]> CHANGE MASTER TO MASTER_HOST='server-1',MASTER_PORT=3306,MASTER_USER='repuser',MASTER_PASSWORD='repuser',MASTER_LOG_FILE='mariadb-bin.000514',MASTER_LOG_POS=639;

MariaDB [mysql]> START SLAVE;

// 服务器 1

MariaDB [mysql]> CHANGE MASTER TO MASTER_HOST='server-2',MASTER_PORT=3306,MASTER_USER='repuser',MASTER_PASSWORD='repuser',MASTER_LOG_FILE='mariadb-bin.000006',MASTER_LOG_POS=1057;

MariaDB [mysql]> START SLAVE;

// 完毕之后,分别执行
MariaDB [mysql]> SHOW SLAVE STATUS\G