在MySQL中設置主從數據庫的步驟如下:
CREATE USER 'repl'@'slave_ip' IDENTIFIED BY 'password';
GRANT REPLICATION SLAVE ON *.* TO 'repl'@'slave_ip';
log-bin = /var/log/mysql/mysql-bin.log
server-id = 1
FLUSH TABLES WITH READ LOCK;
SHOW MASTER STATUS;
記錄下File和Position的值,備用。
CHANGE MASTER TO
MASTER_HOST = 'master_ip',
MASTER_USER = 'repl',
MASTER_PASSWORD = 'password',
MASTER_LOG_FILE = 'mysql-bin.000001', -- 替換為主服務器上的File值
MASTER_LOG_POS = 12345; -- 替換為主服務器上的Position值
START SLAVE;
UNLOCK TABLES;
SHOW SLAVE STATUS\G;
確保Slave_IO_Running和Slave_SQL_Running都為Yes。
通過以上步驟,可以成功設置主從數據庫,實現主從復制功能。