- 创建用户,添加权限
mysql> create user 'slave'@'192.168.137.3' identified by '123'; Query OK, 0 rows affected (0.03 sec) mysql> grant replication slave on *.* to 'slave'@'192.168.137.3'; Query OK, 0 rows affected (0.00 sec)
- 备份主服务器
--flush-logs, -F
开始导出之前刷新日志。
--lock-tables, -l
开始导出前,锁定所有表。用READ LOCAL锁定表以允许MyISAM表并行插入。对于支持事务的表例如InnoDB和BDB,--single-transaction是一个更好的选择,因为它根本不需要锁定表。
sudo mysqldump -uroot -p test --single-transaction -F > /tmp/test.sql
- 传递到从服务器
scp /tmp/test.sql 192.168.1.111:/tmp/
- 修改配置文件,主从的server-id不一致
log-bin = mysql-bin server-id = 1
- 从服务器配置,server-id = 2
mysql> change master to master_host='106.15.35.91',master_port=3306, master_user='rpl',master_password='123',master_log_file='mysql-bin.000001',master_log_pos=154; mysql> start slave; mysql> show slave statusG;
*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: 1.1.1.1
Master_User: rpl
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: mysql-bin.000001
Read_Master_Log_Pos: 913
Relay_Log_File: u18-relay-bin.000005
Relay_Log_Pos: 320
Relay_Master_Log_File: mysql-bin.000001
Slave_IO_Running: Yes
Slave_SQL_Running: Yes