主主架构介绍
在企业系统中,数据库高可用是非常重要的环节。MySQL数据库常见的企业部署方式是一主多从,而它最大的风险在于主库的单点故障,从库切换成主库需要作变更。双主或多主模式可以有效的提升MySQL数据库的高可用性。双主与多主最大的问题在于需要考虑自增ID问题,这是需要在参数文件进行特别设置,最终实现双主或多主自增ID互相不冲突。
MySQL双主同步架构方案思路是:
- 两台服务器都可以实施读写操作,但默认只运用一台负责读写操作,而另一台处于备机或从库、备份的节点。
- 两台服务器通过Keepalive方案实现高可用架构,对外提供VIP动态地址。
主主架构部署
MySQL部署安装
# Repo源下载与安装
wget https://dev.mysql.com/get/mysql57-community-release-el7-11.noarch.rpm
yum localinstall mysql57-community-release-el7-11.noarch.rpm
# MySQL数据库安装
yum install mysql-community-server
# MySQL
mysqld -V
MySQL配置文件
如下配置是MEM内存资源为8GB,仅供参考。
[Master A] 配置文件
# For advice on how to change settings please see
# http://dev.mysql.com/doc/refman/5.7/en/server-configuration-defaults.html
# Configuration name server
[mysql]
# CLIENT #
port = 3306
default-character-set = utf8mb4
[client]
socket = /home/mysql/mysql.sock
[mysqld]
#
# Remove leading # and set to the amount of RAM for the most important data
# cache in MySQL. Start at 70% of total RAM for dedicated server, else 10%.
# innodb_buffer_pool_size = 128M
#
# Remove leading # to turn on a very important data integrity option: logging
# changes to the binary log between backups.
# log_bin
#
# Remove leading # to set options mainly useful for reporting servers.
# The server defaults are faster for transactions and fast SELECTs.
# Adjust sizes as needed, experiment to find the optimal values.
# join_buffer_size = 128M
# sort_buffer_size = 2M
# read_rnd_buffer_size = 2M
[mysqld]
# GENERAL #
user = mysql
default-storage-engine = InnoDB
socket = /home/mysql/mysql.sock
pid-file = /home/mysql/mysql.pid
character-set-server = utf8mb4
lower-case-table-names = 1
performance_schema = ON
sql_mode = ''
server-id = 1
# MyISAM #
key-buffer-size = 32M
myisam_recover_options = FORCE,BACKUP
# SAFETY #
max-allowed-packet = 16M
max-connect-errors = 1000000
# DATA STORAGE #
datadir = /home/mysql/
# BINARY LOGGING #
log-bin = /home/mysql/mysql-bin-master
binlog_format = mixed
expire-logs-days = 14
sync-binlog = 1
log-bin-trust-function-creators= 1
# MASTER DB #
#binlog-do-db = mdm,androidpnserver
binlog-ignore-db = mysql,information_schema,performance_schema
auto-increment-increment = 2
auto-increment-offset = 1
# SLAVE DB #
#replicate-do-db = mdm,androidpnserver
replicate-ignore-db = mysql,information_schema,performance_schema
relay_log = /home/mysql/relay-bin
log-slave-updates = ON
# CACHES AND LIMITS #
tmp-table-size = 32M
max-heap-table-size = 32M
query-cache-type = 0
query-cache-size = 0
max-connections = 500
thread-cache-size = 50
open-files-limit = 65535
table-definition-cache = 1024
table-open-cache = 2048
# INNODB #
innodb-flush-method = O_DIRECT
innodb-log-files-in-group = 2
innodb-log-file-size = 256M
innodb-flush-log-at-trx-commit = 1
innodb-file-per-table = 1
innodb-buffer-pool-size = 4G
# LOGGING #
log-error = /home/mysql/mysql-error.log
log-queries-not-using-indexes = 1
slow-query-log = 1
slow-query-log-file = /home/mysql/mysql-slow.log
# Disabling symbolic-links is recommended to prevent assorted security risks
symbolic-links=0
[Master B] 配置文件
# For advice on how to change settings please see
# http://dev.mysql.com/doc/refman/5.7/en/server-configuration-defaults.html
# Configuration name server
[mysql]
# CLIENT #
port = 3306
default-character-set = utf8mb4
[client]
socket = /home/mysql/mysql.sock
[mysqld]
#
# Remove leading # and set to the amount of RAM for the most important data
# cache in MySQL. Start at 70% of total RAM for dedicated server, else 10%.
# innodb_buffer_pool_size = 128M
#
# Remove leading # to turn on a very important data integrity option: logging
# changes to the binary log between backups.
# log_bin
#
# Remove leading # to set options mainly useful for reporting servers.
# The server defaults are faster for transactions and fast SELECTs.
# Adjust sizes as needed, experiment to find the optimal values.
# join_buffer_size = 128M
# sort_buffer_size = 2M
# read_rnd_buffer_size = 2M
[mysqld]
# GENERAL #
user = mysql
default-storage-engine = InnoDB
socket = /home/mysql/mysql.sock
pid-file = /home/mysql/mysql.pid
character-set-server = utf8mb4
lower-case-table-names = 1
performance_schema = ON
sql_mode = ''
server-id = 2
# MyISAM #
key-buffer-size = 32M
myisam_recover_options = FORCE,BACKUP
# SAFETY #
max-allowed-packet = 16M
max-connect-errors = 1000000
# DATA STORAGE #
datadir = /home/mysql/
# BINARY LOGGING #
log-bin = /home/mysql/mysql-bin-slave
binlog_format = mixed
expire-logs-days = 14
sync-binlog = 1
log-bin-trust-function-creators= 1
# MASTER DB #
#binlog-do-db = mdm,androidpnserver
binlog-ignore-db = mysql,information_schema,performance_schema
auto-increment-increment = 2
auto-increment-offset = 2
# SLAVE DB #
#replicate-do-db = mdm,androidpnserver
replicate-ignore-db = mysql,information_schema,performance_schema
relay_log = /home/mysql/relay-bin
log-slave-updates = ON
# CACHES AND LIMITS #
tmp-table-size = 32M
max-heap-table-size = 32M
query-cache-type = 0
query-cache-size = 0
max-connections = 500
thread-cache-size = 50
open-files-limit = 65535
table-definition-cache = 1024
table-open-cache = 2048
# INNODB #
innodb-flush-method = O_DIRECT
innodb-log-files-in-group = 2
innodb-log-file-size = 256M
innodb-flush-log-at-trx-commit = 1
innodb-file-per-table = 1
innodb-buffer-pool-size = 4G
# LOGGING #
log-error = /home/mysql/mysql-error.log
log-queries-not-using-indexes = 1
slow-query-log = 1
slow-query-log-file = /home/mysql/mysql-slow.log
# Disabling symbolic-links is recommended to prevent assorted security risks
symbolic-links=0
补充信息:
只有utf8mb4才是真正的UTF-8字符集,默认UTF-8并不是。
MySQL初始化
服务配置
# MySQL服务启动
systemctl start mysqld.service
# MySQL服务自动启动
systemctl enable mysqld.service
# MySQL服务状态检查
systemctl status mysqld.service
密码更新
默认密码更新
grep 'temporary password' /home/mysql/mysql-error.log
mysql -uroot -p
ALTER USER 'root'@'localhost' IDENTIFIED BY 'P@ssw0rd';
MySQL主主配置
同步账号创建
[Master A] 账号
GRANT REPLICATION SLAVE ON *.* TO replication@'192.168.0.%' IDENTIFIED BY 'P@ssw0rd';
flush privileges;
[Master B] 账号
GRANT REPLICATION SLAVE ON *.* TO replication@'192.168.0.%' IDENTIFIED BY 'P@ssw0rd';
flush privileges;
Slave角色配置
[Master A] 状态
flush tables with read lock; #防止进入新的数据
show master status\G;
*************************** 1. row ***************************
File: mysql-bin.000003
Position: 1114
Binlog_Do_DB:
Binlog_Ignore_DB: mysql,information_schema,performance_schema
Executed_Gtid_Set:
1 row in set (0.00 sec)
[Master B] 状态
flush tables with read lock; #防止进入新的数据
show master status\G;
*************************** 1. row ***************************
File: mysql-bin.000002
Position: 865
Binlog_Do_DB:
Binlog_Ignore_DB: mysql,information_schema,performance_schema
Executed_Gtid_Set:
1 row in set (0.00 sec)
[Master A] 配置Slave
CHANGE MASTER TO MASTER_HOST='192.168.0.82',MASTER_USER='replication',MASTER_PASSWORD='P@ssw0rd',MASTER_LOG_FILE='mysql-bin.000002',MASTER_LOG_POS=865;
start slave;
# 状态检查
show slave status\G;
# 退出锁定
UNLOCK TABLES;
[Master B] 配置Slave
CHANGE MASTER TO MASTER_HOST='192.168.0.81',MASTER_USER='replication',MASTER_PASSWORD='P@ssw0rd',MASTER_LOG_FILE='mysql-bin.000003',MASTER_LOG_POS=1114;
start slave;
# 状态检查
show slave status\G;
# 退出锁定
UNLOCK TABLES;
MySQL主主验证
[Master A] 验证
show databases;
create database test01;
show databases;
drop database test02;
show databases;
[Master B] 验证
show databases;
create database test01;
show databases;
drop database test02;
show databases;
Keepalive部署安装
Keepalive安装
yum -y install keepalived psmisc
Keepalive配置
[Master A] 的Keepalive配置
vi /etc/keepalived/keepalived.conf
global_defs {
router_id LVS_DEVEL
vrrp_skip_check_adv_addr
vrrp_garp_interval 0
vrrp_gna_interval 0
}
vrrp_script chk_mysql {
script "/usr/bin/killall -0 mysqld"
}
vrrp_instance VI_1 {
state MASTER
interface ens192
virtual_router_id 51
priority 100
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
192.168.0.83
}
track_script {
chk_mysql
}
}
[Master B] 的Keepalive配置
vi /etc/keepalived/keepalived.conf
global_defs {
router_id LVS_DEVEL
vrrp_skip_check_adv_addr
vrrp_garp_interval 0
vrrp_gna_interval 0
}
vrrp_script chk_mysql {
script "/usr/bin/killall -0 mysqld"
}
vrrp_instance VI_1 {
state BACKUP
interface ens192
virtual_router_id 51
priority 90
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
192.168.0.83
}
track_script {
chk_mysql
}
}
Keepalive启动
systemctl start keepalived.service
systemctl enable keepalived.service
systemctl status keepalived.service
Keepalive检查
ip addr
Keepalive 检验
[Master A] 暂停MySQL服务
systemctl stop mysqld.service
VIP动态地址将会自动切换到[Master B]且前端业务会收到一次链接失败