1 主服务器上进行的操作
mysql> GRANT REPLICATION SLAVE ON *.* to ‘rep1’@’192.168.0.101’ identified by ‘password’;
查询主数据库状态
Mysql>show master status;+——————+———-+————–+——————+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB |
+——————+———-+————–+——————+
| mysql-bin.000003 | 2612 | | |
+——————+———-+————–+——————+
记录下 FILE 及 Position 的值,在后面进行从服务器操作的时候需要用到。
2 配置从服务器
修改从服务器的配置文件mysql/etc/my.cnf
将 server-id = 1修改为 server-id = 10,并确保这个ID没有被别的MySQL服务所使用。
重新启动mysql服务
登录管理MySQL服务器
执行同步命令:
mysql>change master to-> master_host=’192.168.0.101’,-> master_user=’rep1’,-> master_password=’password’,-> master_log_file=’mysql-bin.000003’,-> master_log_pos=2612;
正确执行后启动Slave同步进程
mysql>start slave;
mysql>show slave status\G==============================================
**************** 1. row *******************Slave_IO_State:
Master_Host:192.168.10.101Master_User: rep1
Master_Port:3306Connect_Retry:60Master_Log_File: mysql-bin.000005Read_Master_Log_Pos:415Relay_Log_File: localhost-relay-bin.000008Relay_Log_Pos:561Relay_Master_Log_File: mysql-bin.000005Slave_IO_Running: YES
Slave_SQL_Running: YES
Replicate_Do_DB:
……………省略若干……………
Master_Server_Id: 1
1 row in set (0.01 sec)
==============================================
其中Slave_IO_Running 与 Slave_SQL_Running 的值都必须为YES,才表明状态正常。
如果主服务器已经存在应用数据,则在进行主从复制时,需要做以下处理:
(1)主数据库进行锁表操作,不让数据再进行写入动作
mysql> FLUSH TABLES WITH READ LOCK;
(2)查看主数据库状态
mysql> show master status;
(3)复制数据文件
将主服务器的数据文件(整个/mysql/data目录)复制到从服务器,建议通过tar归档压缩后再传到从服务器解压。
(4)取消主数据库锁定
mysql> UNLOCK TABLES;