一.设主服务器IP:10.35.51.1
从服务器IP:10.35.51.2
二. 主服务器端
1.设置用户(目的是在从服务器上用此用户登录能够访问主服务器上要同步的那个数据库):
GRANT FILE ON *.* TO 'yeshb'@'%' IDENTIFIED BY ‘123456’;//创建用户:yeshb;密码:123456
GRANT REPLICATION slave ON *.* TO ‘yeshb’@'%' IDENTIFIED BY ‘123456’;//设此用户在其它机子登录都能够被同步,设置这个用户有对此主机同步的权限
(grant all privileges on *.* to ‘yeshb@'%' identified by '123456'; FLUSH PRIVILEGES )//应该跟这个没有关,可不用
2.在主服务器的/etc/my.cnf配置文件上设置
log-bin #设置日志文件的目录
server-id = 1
binlog-do-db=passport #指定需要日志的数据库,没有passport 这个数据库要先创建
3.重启mysql
在主服务器下,输入show master status \G显示如下信息,就表示成功:
***************** 1. row ***************************
File: localhost-bin.000009
Position: 80 //记录主服务器数据库passport的日志位置
Binlog_Do_DB: passport
Binlog_Ignore_DB:
三.从服务器端
1. 从服务器/etc/my.cnf配置文件上设置
server-id=2
master-host=10.35.51.1
master-user=yeshb #同步用户帐号
master-password=123456
master-port=3306
master-connect-retry=60 #预设发起连接重试的次数
replicate-do-db=passport #告诉slave只做passport数据库的更新,此服务器上要先创建passport这个数据库,数据结构要也主服务器上的passport一致,也可具体到更新某一些表的同步
2.重启mysql
在从服务器下,输入show slave status \G显示如下,就表示配置成功:
***************** 1. row ***************************
Slave_IO_State: Waiting for master to send event//只有这个状态为"才表示同步启动成功了,如果为"Connecting to master "表示正在连接,如果为空就表示启动失败。(失败可能原因是 a.用这个用户和密码连接不到主服务器上.b.可以连接得上,但没有同步的权限.c.Read_Master_Log_Pos:4这个日志点在主服务器上不存在,重新设置一个确切存在的日志点位置,应该就可以了,可能)
Master_Host: 10.35.60.175
Master_User: yeshb
Master_Port: 3306
Connect_Retry: 60
Master_Log_File:
Read_Master_Log_Pos: 4 //表示从4这个日志位置开始跟主服务器保持一致
Relay_Log_File: localhost-relay-bin.000001
Relay_Log_Pos: 4
Relay_Master_Log_File:
Slave_IO_Running: Yes //要都为yes
Slave_SQL_Running: Yes //
Replicate_Do_DB: passport
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: 0
Relay_Log_Space: 4
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: NULL
3.如果要修改设置开始同步的日志位置
stop slave;//先停止同步
change master to
master_log_pos=80,//关键的一步,设置从这个日志位置开始与主服务器开始同步,这个日志点一定是存在的(小等于当前主服务器上的show master status显示中的Position值 ),
( 其它也可以设置
master_host='10.35.51.1',
master_port=3306,
master_user='yeshb',
master_password='123456';
)//本测试没有去用到这个
start slave;//启动同步
四. 测试:
在主服务器端进行增删改,在从服务器上也能够跟着起变化(是从Read_Master_Log_Pos上主服务器所标记的位置开始随便着主服务器的变化而变化),从服务器自己修改了,即使同步了,主服务器也没有办法去修改覆盖从服务器上的内容.
注:如果从服务器上添加了一条记录ID为10,主服务器也添加了一条记录ID也为10,那么同步时会提示发生冲突了,主服务器就不能覆盖了从服务器上的那条记录,但其它的没有冲突的,照常能够保持一致,也就是说只要有冲突,还是以从服务器为准.
分享到:
2009-06-09 14:51
浏览 1047
评论