本文記錄在 CentOS 5.6 環境下,使用 RPM 包安裝 MySQL 5.6.13 並配置主從復制的標準化流程,適用於生產環境基準部署參考。
| 角色 | IP | Server ID | 備註 |
|---|---|---|---|
| Master | 10.1.1.231 | 1025 | 讀寫節點 |
| Slave | 10.1.1.234 | 1052 | 只讀備份節點 |
所需 RPM 包(官方 Generic Linux 版):
# 安裝依賴
yum -y install libaio libaio-devel perl-Time-HiRes curl curl-devel zlib-devel \
openssl-devel perl cpio expat-devel gettext-devel perl-ExtUtils-MakeMaker perl-DBD-MySQL
# 依序安裝 RPM(shared-compat 需最先安裝以替換系統自帶 mysql-libs)
rpm -ivh MySQL-shared-compat-5.6.13-1.linux_glibc2.5.x86_64.rpm
rpm -ivh MySQL-shared-5.6.13-1.linux_glibc2.5.x86_64.rpm
rpm -ivh MySQL-server-5.6.13-1.linux_glibc2.5.x86_64.rpm
rpm -ivh MySQL-devel-5.6.13-1.linux_glibc2.5.x86_64.rpm
rpm -ivh MySQL-client-5.6.13-1.linux_glibc2.5.x86_64.rpm
rpm -ivh MySQL-test-5.6.13-1.linux_glibc2.5.x86_64.rpm
/etc/my.cnf)[mysqld]
server-id=1025
log-bin=mysql-master-bin
binlog_format=mixed
expire_logs_days=15
max_connections=1000
innodb_flush_log_at_trx_commit=1
sync_binlog=1
binlog-do-db=denovo_ng
binlog-ignore-db=mysql,test,information_schema
innodb_buffer_pool_size=40960M
skip-name-resolve
datadir=/state/partition1/mysql/data
port=3306
socket=/state/partition1/mysql/mysql.sock
sql_mode=NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES
[mysqld]
server-id=1052
log-bin=mysql-slave-bin
max_binlog_size=1000M
binlog_format=mixed
expire_logs_days=7
innodb_flush_log_at_trx_commit=1
sync_binlog=1
read_only=1
binlog-do-db=denovo_ng
binlog-ignore-db=mysql,test,information_schema
relay_log_purge=0
skip-name-resolve
max_connections=1000
sql_mode=NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES
關鍵差異點:Slave 啟用 read_only=1、relay_log_purge=0,並同樣開啟 Binlog 以支援級聯復制或未來提升為 Master。
GRANT REPLICATION SLAVE ON *.* TO 'jpsync'@'10.1.1.234' IDENTIFIED BY 'jppasswd';
FLUSH PRIVILEGES;
SHOW MASTER STATUS; -- 記錄 File / Position
STOP SLAVE;
CHANGE MASTER TO
MASTER_HOST='10.1.1.231',
MASTER_PORT=3306,
MASTER_USER='jpsync',
MASTER_PASSWORD='jppasswd',
MASTER_LOG_FILE='mysql-master-bin.000003',
MASTER_LOG_POS=408;
START SLAVE;
SHOW SLAVE STATUS\G;
# 確認 Slave_IO_Running: Yes / Slave_SQL_Running: Yes
雙 Yes 即代表主從復制建立成功,可進行業務驗證。