- 在主从服务器分别配置
#vi /etc/my.cnf
[mysqld]
log-bin=mysql-bin //[必须]启用二进制日志
server-id=222 //[必须]服务器唯一ID,默认是1,一般取IP最后一段
- 在主服务器上创建一个授权账户
GRANT REPLICATION SLAVE ON *.* to 'user'@'127.0.0.1' identified by 'password';
- 锁定主服务器数据库
flush tables with read lock;
- 记录master的状态
show master status;
+------------------+----------+--------------+------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB |
+------------------+----------+--------------+------------------+
| mysql-bin.000006 | 112625 | | |
+------------------+----------+--------------+------------------+
- 导出主服务器的数据库
mysqldump -u user -p password --all-databases > backup.sql
- 解锁
unlock tables;
- 将导出的数据导入从服务器
mysql -u user -p password < backup.sql
- 设置从机
change master to master_host = '127.0.0.1',master_port = 3306,master_user = 'user',master_password='password',master_log_file='mysql-bin.000006',master_log_pos=112625 ;
- 在从机启动同步
start slave;
网友评论