mysql 数据同步配置

一、环境
        主机:
                  主控机操作系统:ubuntu linux server
                  IP192.168.5.7
                  MySQL版本:5.0.27
                  
从属机操作系统:windows xp              
IP192.168.5.146
                  MySQL版本:5.0.42
                                                                                                                                                                                                   
二、主控机的my.cnf文件与复制相关的配置
[mysqld]
#server-id              = 1
log_bin                 = /var/log/mysql/mysql-bin.log(系统默认得未改)
server-id               =1
#sql-bin-update-same
binlog-do-db=metro
 
三、从属机的my.cnf文件与复制相关的配置
[mysqld]
server-id           = 2
master-user         =username
master-password     =password
master-connect-retry = 60
read-only           = 1
relay-log-purge     = 1
replicate-do-db     = metro
report-host         =192.168.5.7
slave-skip-errors   = all
replicate-wild-ignore-table=metro.b
在主控服务器上进行权限设置:
GRANT FILE ON *.* TO us@192.168.5.146 IDENTIFIED BY 'password';(192.168.5.146可以用'%'代替,表示可以从任何ip登陆)
先在主控服务器上执行:
 
然后进入master数据库的Mysql控制台执行
//全局锁定
FLUSH TABLES WITH READ LOCK;
 
然后进入另外一个窗口,拷贝db1下所有文件到slave数据库的var目录下
 
//读取相关的二进制文件和偏移量
SHOW MASTER STATUS;
+---------------+----------+--------------+------------------+
| File            | Position | Binlog_Do_DB | Binlog_Ignore_DB |
+---------------+----------+--------------+------------------+
| mysql-bin.003 | 73         | db1            | mysql              |
+---------------+----------+--------------+------------------+
 
记录下Position 和 File,然后执行
 
//解锁
UNLOCK TABLES;
 
然后配置slave数据库的my.cnf中有如下段
[mysqld]
server-id           = 2
master-user         =username
master-password     =password
master-connect-retry = 60
read-only           = 1
relay-log-purge     = 1
replicate-do-db     = metro
report-host         =192.168.5.7
slave-skip-errors   = all
replicate-wild-ignore-table=metro.b
//不同步的表,例如discuz中的cdb_sessions表
然后启动slave数据库,进入mysql控制台,执行
 
slave stop;
 
CHANGE MASTER TO

  -> MASTER_HOST='192.168.5.7', //主服务器的IP地址

  -> MASTER_USER='username', //同步数据库的用户

  -> MASTER_PASSWORD='password', //同步数据库的密码

  -> MASTER_LOG_FILE='mysql-bin.003', //主服务器二进制日志的文件名,前面要求记住的参

  -> MASTER_LOG_POS=73; //日志文件的开始位置(前面要求记住的参数)
 
 
mysql> slave start;
ERROR 1200 (HY000): The server is not configured as slave; fix in config file or with CHANGE MASTER TO
出现错误,然后再执行Change master语句就可以解决:
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                
在从属服务器上执行:
mysql> change master to master_host='192.168.5.7',master_user='username',master_password='passwor',master_log_file='ccxy_db.000008' ,master_log_pos=98;
Query OK, 0 rows affected (0.01 sec)
 
接着执行:
mysql> slave start;
Query OK, 0 rows affected (0.01 sec)
就可以解决.
 
在从属服务器上也可以设置某张或多张表的数据不进行复制,只要在my.cnf文件中添加:
replicate-wild-ignore-table=dbname.tablename
也可以限制不复制某个数据库:
replicate- ignore-db =dbname
注意:
        在主服务器上设置了binlog-ignore-db就会把这些被列出来的数据库任何更改都不会写入二进制日志文件中的,若需要保留这些更改日志,请不要在主服务器上设置这个选项。而是在从属服务器的my.cnf文件中设置需要复制的数据库与不复制的数据库。
        另外:使用复制功能应该根据用途不同与实际情况,考虑服务器之间的物理位置关系(指是否在同一域或同网段内等),因为网络传送会影响到你复制的速度,从而可能抵消一部分你为了提供系统响应的速度。

  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 线程都在正常运行。

  至此,同步设定成功。

另外mysql6.0作为slave时, my.ini只要增加server-id           = 3就可以了,其他得属性可以在运行 mysql>change master to master_host='192.168.5.7',master_user='username',master_password='passwor',master_log_file='ccxy_db.000008' ,master_log_pos=98;

配置,如果多写了导致服务无法启动,可以删去,6.0做slave得我也是在windows xp下使用得


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值