This commit is contained in:
2022-04-18 11:21:20 +08:00
commit 45a7af638f
210 changed files with 8997 additions and 0 deletions

View File

@@ -0,0 +1,49 @@
# 部署 MySQL 两节点互为主从+高可用
- 在每台服务器上执行如下操作
- 按实际环境修改
- docker-compose.yml
- keepalived/conf/keepalived.conf
- mysql/my.cnf
- 创建目录
```
grep '\<source:' docker-compose.yml | cut -d: -f2 | xargs mkdir -p
```
- 启动
```
docker-compose up -d
```
- 查看日志,直至出现 "MySQL is ready for connections."
```
docker-compose logs mysql
```
- 配置 mysql 互相同步
```
# 进入mysql终端
docker exec -ti mysql mysql
# 设置同步源
CHANGE REPLICATION SOURCE TO
SOURCE_HOST = '对方 host',
SOURCE_PORT = 3306,
SOURCE_USER = 'replicator',
SOURCE_PASSWORD = 'China_19$(10)!',
SOURCE_AUTO_POSITION = 1;
# 启动同步
START REPLICA;
# 查看同步状态
SHOW REPLICA STATUS\G
# 如果输出Replica_IO_Running: Yes和Replica_SQL_Running: Yes则同步正常
# 退出 mysql
quit
```
- 在其中一台 mysql 服务器上导入初始数据

View File

@@ -0,0 +1,39 @@
version: "3.7"
services:
keepalived:
image: harbor.colben.cn/general/keepalived
container_name: keepalived
restart: "on-failure"
stop_grace_period: 1m
privileged: true
network_mode: host
volumes:
- type: bind
source: ./keepalived/conf
target: /etc/keepalived
- type: bind
source: ./keepalived/log
target: /var/log/keepalived
mysql:
image: harbor.colben.cn/general/mysql:8
container_name: mysql
restart: "on-failure"
stop_grace_period: 5m
privileged: true
network_mode: host
volumes:
- type: bind
source: ./mysql/my.cnf
target: /etc/my.cnf
- type: bind
source: ./mysql/binlog
target: /var/lib/mysql-bin
- type: bind
source: ./mysql/db
target: /var/lib/mysql
- type: bind
source: ./mysql/log
target: /var/log/mysql

View File

@@ -0,0 +1,33 @@
global_defs {
router_id mysql1 # 在另一台服务器中,这里配置 mysql2
script_user root
enable_script_security
}
vrrp_script chk_mysql {
script "/sbin/ss -lnt | grep -q ':3306\>'"
interval 10
weight 0
fall 2
rise 2
}
vrrp_instance VI_1 {
state BACKUP
virtual_router_id 13
priority 150 # 在另一台服务器中这里配置100
advert_int 2
nopreempt
interface eth0 # 这里的 eth0 是服务器的网卡名
track_script {
chk_mysql
}
authentication {
auth_type PASS
auth_pass El_en_mysql_1234
}
virtual_ipaddress {
虚拟IP/掩码 dev eth0 # 这里的eth0是服务器的网卡名
}
}

View File

@@ -0,0 +1,4 @@
CREATE USER replicator@'%' IDENTIFIED BY 'China_19$(10)!';
GRANT REPLICATION SLAVE ON *.* TO replicator@'%';
FLUSH PRIVILEGES;

View File

@@ -0,0 +1,30 @@
[mysqld]
mysqlx = OFF
skip-name-resolve = 1
max-user-connections = 600
## this should be less than 80% of total memory
innodb-buffer-pool-size = xxxxG
## this should be half of "innodb-buffer-pool-size"
innodb-buffer-pool-instances = xxxx
# binlog
## this should be an unsigned tinyint and different with the other mysql server.
server-id = xxxx
log-bin = /var/lib/mysql-bin/master
binlog-format = ROW
binlog-expire-logs-seconds = 172800
gtid-mode = ON
enforce-gtid-consistency = TRUE
# replication
replicate-wild-ignore-table = information_schema.%
replicate-wild-ignore-table = mysql.%
replicate-wild-ignore-table = performance_schema.%
replicate-wild-ignore-table = sys.%
slave-parallel-workers = 2
log-slave-updates = FALSE
relay-log = /var/lib/mysql-bin/slave
relay-log-recovery = TRUE