配置MySQL主從復制需要進行以下步驟:
server-id = 1
log_bin = /var/log/mysql/mysql-bin.log
binlog_do_db = your_database_name
CREATE USER 'replication_user'@'slave_ip_address' IDENTIFIED BY 'password';
GRANT REPLICATION SLAVE ON *.* TO 'replication_user'@'slave_ip_address';
在主數據庫上執行SHOW MASTER STATUS;
命令,記錄下File和Position的值,將其用于配置從數據庫。
在從數據庫上編輯MySQL配置文件,添加如下配置:
server-id = 2
relay-log = /var/log/mysql/mysql-relay-bin.log
log_slave_updates = 1
CHANGE MASTER TO MASTER_HOST='master_ip_address', MASTER_USER='replication_user', MASTER_PASSWORD='password', MASTER_LOG_FILE='master_log_file', MASTER_LOG_POS=master_log_position;
其中,master_ip_address為主數據庫地址,replication_user為創建的復制用戶,password為密碼,master_log_file和master_log_position為主數據庫上SHOW MASTER STATUS;返回的File和Position的值。
START SLAVE;
SHOW SLAVE STATUS\G
命令來查看從數據庫的復制狀態。配置完成后,主數據庫上的數據變化會自動同步到從數據庫上。