MySQL 5.5.9版本主从同步配置方法

2014-11-24 18:53:19 · 作者: · 浏览: 0

主服务器IP:1.1.1.21


从服务器IP:1.1.1.22


注:此文档只描述主à从 的同步,而不是互备。如需要达到互备效果,而在操作完整个文档内容后,执行下面两步即可:


1. 在从机上执行:mysql> grant replication slave on *.*to root@1.1.1.21 identified by 'password';


2. 在主机上执行:start slave


主服务器配置:


主服务器上的配置和低版本的配置方式一样,只需要在配置文件中开启二进制日志文件和设置server-id=1。


编辑mysql配置文件/etc/my.cnf,在[mysqld]部分加入:


server-id=1


log-bin=mysql-bin


在主服务器上建立一个从服务器进行复制使用的账户(用户名:root;密码:longmaster):


mysql> grant replication slave on *.* toroot@1.1.1.22 identified by 'password';


mysql> flush privileges;


重启mysql服务


Service mysqld restart


从服务器配置:


Mysql5.1.7版本以后已经不支持把master配置属性写在my.cnf文件中了,只能把需要同步的数据库和需要忽略的数据库加入。


编辑从服务器的mysql配置文件/etc/my.cnf,在[mysqld]部分加入:


server-id=2


log-bin=mysql-bin


replicate-do-db=db_name #这两条最好加在结尾[需要同步的数据库]


replicate-ignore-db=db_name #这两条最好加在结尾[不需要同步的库]


重启mysql服务。


进入mysql命令行,停止从服务器线程并执行mysql命令,然后再启动从服务器线程:


mysql> stop slave;


Query OK, 0 rowsaffected (0.01 sec)



mysql> changemaster to


-> master_host='1.1.1.21',


-> master_user='root',


-> master_password='password';


Query OK, 0 rowsaffected (0.01 sec)


mysql> start slave;


从服务器上检查复制进程是否正确:


mysql> show slave status\G;


***************************1. row ***************************


Slave_IO_State: Waiting formaster to send event


Master_Host: 1.1.1.21


Master_User: you


Master_Port: 3306


Connect_Retry: 60


Master_Log_File: master-bin.000001


Read_Master_Log_Pos: 107


Relay_Log_File:localhost-relay-bin.000002


Relay_Log_Pos: 254


Relay_Master_Log_File:master-bin.000001


Slave_IO_Running: Yes


Slave_SQL_Running: Yes


Replicate_Do_DB: y1


Replicate_Ignore_DB: y2


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: 107


Relay_Log_Space: 414


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


Master_SSL_Verify_Server_Cert:No


Last_IO_Errno: 0


Last_IO_Error:


Last_SQL_Errno: 0


Last_SQL_Error:


Replicate_Ignore_Server_Ids:


Master_Server_Id: 1


1 row in set (0.00 sec)



ERROR:


No query specified


上面出现: Slave_IO_Running: Yes 和 Slave_SQL_Running: Yes 表示复制正常,如果有一个显示是NO,请检查以上的主从设置步骤是否正确。如果出现复制错误,从服务器的错误日志中也会出现错误消息。