主な主なアーキテクチャの紹介
エンタープライズシステムでは、データベースの高い可用性は非常に重要なリンクです。MySQLデータベースの一般的なエンタープライズ展開方法は1つのマスターと複数のスレーブであり、その最大のリスクはマスターデータベースの単一障害点にあり、データベースからマスターデータベースへの切り替えには変更が必要です。デュアルマスターモードまたはマルチマスターモードは、MySQLデータベースの高可用性を効果的に向上させることができます。デュアルマスターとマルチマスターの最大の問題は、自動インクリメントIDの問題を考慮する必要があることです。これには、パラメーターファイルに特別な設定が必要であり、最終的に、デュアルマスターまたはマルチマスターの自動インクリメントIDが互いに競合しません。
MySQLデュアルマスター同期アーキテクチャのアイデアは次のとおりです:
- 両方のサーバーは読み取りおよび書き込み操作を実行できますが、デフォルトでは、一方のみが読み取りおよび書き込み操作を担当し、もう一方はスタンバイまたはスレーブデータベースまたはバックアップノードにあります。
- 2つのサーバーは、キープアライブソリューションを通じて高可用性アーキテクチャを実装し、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;
キープアライブの展開とインストール
キープアライブインストール
yum -y install keepalived psmisc
キープアライブ構成
[マスターA]キープアライブ構成
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]キープアライブ構成
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
}
}
キープアライブ開始
systemctl start keepalived.service
systemctl enable keepalived.service
systemctl status keepalived.service
キープアライブチェック
ip addr
キープアライブテスト
[マスターA] MySQLサービスを一時停止します
systemctl stop mysqld.service
VIP動的アドレスは自動的に[マスターB]に切り替わり、フロントエンドビジネスはリンク障害を受け取ります