注:5.6 之后建议使用 GTIDs 方式同步
主数据库相关操作
主数据库配置文件
1 2 3 4 5
| [mysqld] log-bin=mysql-bin binlog_format=row server-id = 1
|
创建一个用户用于同步
1 2 3 4 5
| create user 'username'@'host' identified by 'password'
grant replication salve on *.* to 'username'@'host'
|
获取同步点
1 2 3 4 5 6 7
| FLUSH TABLES WITH READ LOCK;
SHOW MASTER STATUS;
unlock tables;
|
只读模式开启和关闭
1 2 3 4 5 6 7 8 9
| show global variables like "%read_only%"; flush tables with read lock; set global read_only=1; show global variables like "%read_only%";
unlock tables; set global read_only=0;
|
导入数据
从数据库相关操作
创建
1 2 3 4 5 6 7
| mysql> CHANGE MASTER TO -> MASTER_HOST='master_host_name', -> MASTER_USER='replication_user_name', -> MASTER_PORT=16300 -> MASTER_PASSWORD='replication_password', -> MASTER_LOG_FILE='recorded_log_file_name', -> MASTER_LOG_POS=recorded_log_position;
|
状态查看
1 2 3 4 5 6
| START SLAVE;
stop slave
show slave status\g;
|
参考
[主从说明]https://dev.mysql.com/doc/refman/5.7/en/replication-howto.html