MySql 多服务器数据库同步

1). 配置语句说明(基本知识):
#服务器ID, 每台服务器的ID不能设为相同的数.
server-id=1

#启用从库日志,这样可以进行链式复制
log-slave-updates

#从库是否只读,0表示可读写,1表示只读
read-only=1

#只复制某个表
replicate-do-table=tablename

#只复制某些表(可用匹配符)
replicate-wild-do-table=tablename%

#只复制某个库
replicate-do-db=dbname

#只复制某些库
replicte-wild-do-db=dbname%

#不复制某个表
replicate-ignore-table=tablename

#不复制某些表
replicate-wild-ignore-table=tablename%

#不复制某个库
replicate-ignore-db=dbname

#出现错误后忽略,如果不加这个,出现任何错误,同步进程会终止
slave-skip-errors=all

sync_binlog =1

#复制完的sql语句是否立即从中继日志中清除,1表示立即清除
relay-log-purge=1

#从服务器主机,用于show slave hosts生成从库清单
report-host=hostname

#主服务器主机地址与端口, 用于从服务器连接到主服务器
master-host=hostname
master-port=3306

#主服务器数据库用户名与密码(请赋予复制的权限(slave))
master-user=dbuser
master-password=password

#预设重试间隔时间
master-connect-retry=60

#开启数据库日志
log-bin=mysql-bin.log

#要同步的数据库的名字
binlog-do-db=dbname

#不要同步的数据库的名字
binlog-ignore-db=dbname

2).实例配置

a). A机(master)  ip: 210.21.104.237
b). B机(slave)     ip: 58.177.114.3

(所有配置都添加在my.cnf  [mysqld] 配置段下)
Master : A机配置
server-id=1
log-bin=mysql-bin.log
binlog-do-db=test
binlog-ignore-db=mysql

重启mysql
登陆mysql
增加同步帐号,最好指定登陆IP:

#赋予全部权限
mysql > GRANT all on *.* to 'test'@'58.177.114.3' identified by 'test';
#只赋予replication权限
mysql > GRANT replication slave on *.* to 'test'@'58.177.114.3' identified by 'test';
(注意: 以上赋予权限命令都必须用on *.* 所有库, 否则会报错)
#刷新
mysql > FLUSH PRIVILEGES;

#显示master状态
mysql > SHOW MASTER STATUS;

-----------------------------------------------------------------------------
| File                       | Position | Binlog_Do_DB   | Binlog_Ignore_DB |
+--------------------+----------+------------------+--------------------+
| mysql-bin.000001 |          98 | test                   |     mysql              |
+--------------------+----------+------------------+--------------------+
如果显示以上信息表示master配置成功, 记下File 和 Position 两栏的数据用于配置slave

Slave : B机配置
server-id=2
master-host=210.21.104.237
master-port=3306
master-user=test
master-password=test
master-connect-retry=60
replicate-do-db=test
replicate-ignore-db=mysql

重启mysql
进入数据库

#停止slave
mysql > slave stop;

#配置同步文件的参数
mysql > CHANGE MASTER TO MASTER_HOST='124.172.241.12',MASTER_USER='3glab_authen',MASTER_PASSWORD='3glab_authen',MASTER_LOG_FILE='mysql-bin.000001',MASTER_LOG_POS=98;

CHANGE MASTER TO MASTER_HOST='58.177.114.3',MASTER_USER='3glab_authen',MASTER_PASSWORD='3glab_authen',MASTER_LOG_FILE='mysql-bin.000009',MASTER_LOG_POS=98;

#启动slave
mysql > START SLAVE;

mysql> show processlist;
+----+-------------+-----------+------+------------+-------+---------------------------------------------------------------------------+-------------------+
| Id | User                | Host         | db     | Command | Time   | State                                                                                                         | Info                     |
+----+-------------+-----------+------+------------+-------+---------------------------------------------------------------------------+-------------------+
|  6 | system user    |                  | NULL | Connect    | 57236 | Waiting for master to send event                                                           | NULL                   |
|  7 | system user    |                  | NULL | Connect    | 31988 | Has read all relay log; waiting for the slave I/O thread to update it    | NULL                   |
| 58 | root               | localhost  | NULL | Query        |     0     | NULL                                                                                                         | show processlist |
+----+-------------+-----------+------+------------+-------+---------------------------------------------------------------------------+-------------------+
3 rows in set (0.00 sec)
如果显示以上信息就表示同步成功

posted @ 2010-07-29 16:52  Zhou Hr  阅读(660)  评论(0编辑  收藏  举报