2、设置Slave,需要修改my.cnf,增加如下几行:
server-id = 2
master-host = rep1 #主服务器名
master-user = rep #同步账户名,默认是test
master-password = rep #同步帐户密码,默认是空
master-port = 3306 #主服务器的 TCP/IP 端口号,默认是3306
set-variable=replicate-ignore-db=mysql #略过同步的数据库名,如果有多个,请设置多次
set-variable=replicate-do-db=yejr #想要同步的数据库名,如果有多个,请设置多次
接下来在Slave上检验一下是否能正确连接到Master上,并且具备相应的权限。
root$mysql -hrep1 -urep -prep
mysql>SHOW GRANTS;
+------------------------------------------------------------------------------+
| Grants for rep@rep2 |
+------------------------------------------------------------------------------+
| GRANT SELECT, FILE, REPLICATION SLAVE ON *.* TO 'rep'@'rep2' IDENTIFIED BY
PASSWORD '*9FF2C222F44C7BBA5CC7E3BE8573AA4E1776278C' |
+------------------------------------------------------------------------------+
现在,可以启动Slave了。启动成功后,登录Slave,查看一下同步状态:
mysql -hlocalhost -uroot
mysql>SHOW SLAVE STATUS\G
*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: rep1
Master_User: rep
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: binlog.000001
Read_Master_Log_Pos: 98
Relay_Log_File: relay.000003
Relay_Log_Pos: 232
Relay_Master_Log_File: binlog.000001
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
Replicate_Do_DB:
Replicate_Ignore_DB:
Replicate_Do_Table:
Replicate_Ignore_Table:
Replicate_Wild_Do_Table:
Replicate_Wild_Ignore_Table:
Last_Errno: 0
Last_Error:
Skip_Counter: 0
Exec_Master_Log_Pos: 98
Relay_Log_Space: 232
Until_Condition: None
Until_Log_File:
Until_Log_Pos: 0
Master_SSL_Allowed: No
Master_SSL_CA_File:
Master_SSL_CA_Path:
Master_SSL_Cert:
Master_SSL_Cipher:
Master_SSL_Key:
Seconds_Behind_Master: 0
1 row in set (0.00 sec)
可以看到,Slave_IO_Running 和 Slave_SQL_Running 两列的值都为 "Yes",这表明 Slave 的 I/O 和 SQL 线程都在正常运行。
至此,mysql同步设置成功。 |