MySQL 데이터베이스 기술의 이중 마스터 동기화 아키텍처 구성

주요 주요 아키텍처 소개

엔터프라이즈 시스템에서 높은 데이터베이스 가용성은 매우 중요한 링크입니다. MySQL 데이터베이스의 일반적인 엔터프라이즈 배포 방법은 하나의 마스터와 여러 개의 슬레이브이며 가장 큰 위험은 마스터 데이터베이스의 단일 장애 지점에 있으며 데이터베이스에서 마스터 데이터베이스로 전환하려면 변경이 필요합니다. 듀얼 마스터 또는 멀티 마스터 모드는 MySQL 데이터베이스의 고 가용성을 효과적으로 향상시킬 수 있습니다. 이중 마스터 및 다중 마스터의 가장 큰 문제점은 매개 변수 파일에 특별한 설정이 필요한 자동 증가 ID 문제를 고려해야한다는 점이며, 마지막으로 이중 마스터 또는 다중 마스터 자동 증가 ID가 서로 충돌하지 않습니다.

여기에 사진 설명 삽입
MySQL 이중 마스터 동기화 아키텍처의 아이디어는 다음과 같습니다.

  1. 두 서버 모두 읽기 및 쓰기 작업을 수행 할 수 있지만 기본적으로 하나만 읽기 및 쓰기 작업을 담당하고 다른 하나는 대기 또는 슬레이브 데이터베이스 또는 백업 노드에 있습니다.
  2. 두 서버는 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이며 참조 용입니다.

[마스터 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

[마스터 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 마스터 마스터 구성

계정 생성 동기화

[마스터 A] 계정

GRANT REPLICATION SLAVE ON *.* TO replication@'192.168.0.%' IDENTIFIED BY 'P@ssw0rd';
flush privileges;

[마스터 B] 계정

GRANT REPLICATION SLAVE ON *.* TO replication@'192.168.0.%' IDENTIFIED BY 'P@ssw0rd';
flush privileges;

슬레이브 역할 구성

[마스터 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)

[마스터 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)

[마스터 A] 슬레이브 구성

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;

[마스터 B] 슬레이브 구성

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 마스터 마스터 확인

[마스터 A] 검증

show databases;
create database test01;
show databases;
drop database test02;
show databases;

[마스터 B] 검증

show databases;
create database test01;
show databases;
drop database test02;
show databases;

Keepalive 배포 및 설치

Keepalive 설치

yum -y install keepalived psmisc

Keepalive 구성

[마스터 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
    }
}

[마스터 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 테스트

[마스터 A] MySQL 서비스 일시 중지

systemctl stop mysqld.service

VIP 동적 주소는 자동으로 [마스터 B]로 전환되고 프런트 엔드 비즈니스는 링크 실패를 받게됩니다.
여기에 사진 설명 삽입
여기에 사진 설명 삽입

추천

출처blog.csdn.net/weixin_38623994/article/details/110184601