在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。
通过以上步骤,可以成功设置主从数据库,实现主从复制功能。