萬盛學電腦網

您现在的位置: 萬盛學電腦網 >> 程式設計 >  >> 資料庫 >> MySQL 教學

MySQL 5.6 主從環境安裝配置完整步驟詳解

CentOS 5.6 下 MySQL 5.6 主從架構部署指南

本文記錄在 CentOS 5.6 環境下,使用 RPM 包安裝 MySQL 5.6.13 並配置主從復制的標準化流程,適用於生產環境基準部署參考。

一、伺服器規劃與軟體準備

角色IPServer ID備註
Master10.1.1.2311025讀寫節點
Slave10.1.1.2341052只讀備份節點

所需 RPM 包(官方 Generic Linux 版):

  • MySQL-server-5.6.13-1.linux_glibc2.5.x86_64.rpm
  • MySQL-client-5.6.13-1.linux_glibc2.5.x86_64.rpm
  • MySQL-devel-5.6.13-1.linux_glibc2.5.x86_64.rpm
  • MySQL-shared-5.6.13-1.linux_glibc2.5.x86_64.rpm
  • MySQL-shared-compat-5.6.13-1.linux_glibc2.5.x86_64.rpm
  • MySQL-test-5.6.13-1.linux_glibc2.5.x86_64.rpm

二、安裝 MySQL 核心套件

# 安裝依賴
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)

Master 關鍵配置

[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

Slave 關鍵配置

[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。

四、建立復制關係

  1. Master 建立復制帳號
    GRANT REPLICATION SLAVE ON *.* TO 'jpsync'@'10.1.1.234' IDENTIFIED BY 'jppasswd';
    FLUSH PRIVILEGES;
    SHOW MASTER STATUS;  -- 記錄 File / Position
    
  2. Slave 配置同步起點並啟動
    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 即代表主從復制建立成功,可進行業務驗證。

copyright © 萬盛學電腦網 all rights reserved