mySQL数据同步设置

使用的mysql版本:mysql4.0.X

master: 192.168.1.102
slave:    192.168.1.109

如果是win机子修改c:/winnt/my.ini 如果是linux的机子请修改 /etc/my.cnf

一、master操作
1、修改my.ini
##############
server-id=1
log-bin=c:/mysql/logs/mysql_binary_log
binlog-do-db=test
binlog-ignore-db=mysql
##############

说明
binlog-do-db=test                  允许同步的数据库
binlog-ignore-db=mysql      不允许同步的数据库

2、然后重启mysql
c:/mysql/bin/mysql restart  或 /etc/init.d/mysql restart

3、在master上增加一个同步的用户名
mysql> GRANT REPLICATION SLAVE ON *.* TO 'backup'@'%' IDENTIFIED BY '123456';
注:
如果mysql版本在4.0.2以前的版本请用
mysql> GRANT FILE ON *.* TO 'backup'@'%' IDENTIFIED BY '123456';

4、接下来操作要master上要同步的数据库
mysql> USE test;
mysql> FLUSH TABLES WITH READ LOCK;       #锁定要同步的test表,然后导出数据结构


执行如下命令查看master的状态
mysql> SHOW MASTER STATUS;

得到如下结果
Code:
+----------------------+----------+--------------+------------------+
| File                 | Position | Binlog_do_db | Binlog_ignore_db |
+----------------------+----------+--------------+------------------+
| mysql_binary_log.025 | 796947     | test            |  mysql              |
+----------------------+----------+--------------+------------------+
1 row in set (0.00 sec)


接下来备份要同步数据库(为导入slave作准备)
$ mysqldump --opt test > test.sql


mysql> UNLOCK TABLES;                   #已做好同步数据库结构导出后,解锁这个表

二、slave操作
1、把master里导出的同步数据库结构再导入slave的mysql里
mysql tes < test.sql

2、修改slave的my.ini
####################
log-bin=c:/mysql/logs/mysql_binary_log
server-id=2
master-host=192.168.1.102
master-user=backup
master-password=123456
master-connect-retry=60
replicate-do-db=test
####################


说明:
master-host=192.168.1.102                #master的IP
master-user=backup                            #master上作为同步用的用户名
master-password=123456                 #同步用户名的密码
master-connect-retry=60                     #设置同步的时间
replicate-do-db=test                             #需要同步的数据库

3、重新启用mysql
c:/mysql/bin/mysql restart  或 /etc/init.d/mysql restart

4、进入slave的mysql,对mysql进行操作
mysql> stop slave;                  #停止slave服务器

mysql> CHANGE MASTER TO
    ->     MASTER_HOST='192.168.1.102',
    ->     MASTER_USER='backup',
    ->     MASTER_PASSWORD='123456',
    ->     MASTER_LOG_FILE='mysql_binary_log.025',
    ->     MASTER_LOG_POS=796947;

mysql> START SLAVE;         #开启slave服务器就可以同步了



注:
  MASTER_LOG_FILE='mysql_binary_log.025',
  MASTER_LOG_POS=796947;
上面这两条是一开始从master上进入mysql,运行 SHOW MASTER STATUS; 查看到的,在实际操作中也可以不加的。

 

 


##############################################
在master的mysql里运行
mysql > show processlist;
看到两个 system user 就是正常的
+----+-------------+----------------+------+-------------+------+---------------
--------------------------------------------------------+------------------+
| Id | User        | Host           | db   | Command     | Time | State
                                                        | Info             |
+----+-------------+----------------+------+-------------+------+---------------
--------------------------------------------------------+------------------+
|  1 | system user |                | NULL | Connect     | 4499 | Waiting for ma
ster to send event                                      | NULL             |
|  2 | system user |                | NULL | Connect     | 4499 | Has read all r
elay log; waiting for the I/O slave thread to update it | NULL             |
|  7 | root        | localhost:1309 | test | Query       | 0    | NULL
                                                        | show processlist |
| 40 | backup      | ETE-KF2:1354   | NULL | Binlog Dump | 513  | Has sent all b
inlog to slave; waiting for binlog to be updated        | NULL             |
+----+-------------+----------------+------+-------------+------+---------------
--------------------------------------------------------+------------------+
4 rows in set (0.00 sec)

mysql> show master status;
出现如下:
+----------------------+----------+--------------+------------------+
| File                 | Position | Binlog_do_db | Binlog_ignore_db |
+----------------------+----------+--------------+------------------+
| mysql_binary_log.001 | 1011     | test         | mysql            |
+----------------------+----------+--------------+------------------+



在slave的mysql里运行
mysql> show processlist;
出现如下:
+----+-------------+----------------+------+---------+------+-------------------
----------------------------------------------------+------------------+
| Id | User        | Host           | db   | Command | Time | State
                                                    | Info             |
+----+-------------+----------------+------+---------+------+-------------------
----------------------------------------------------+------------------+
|  3 | root        | localhost:1353 | test | Query   | 0    | NULL
                                                    | show processlist |
|  4 | system user |                | NULL | Connect | 740  | Waiting for master
 to send event                                      | NULL             |
|  5 | system user |                | NULL | Connect | 730  | Has read all relay
 log; waiting for the I/O slave thread to update it | NULL             |
+----+-------------+----------------+------+---------+------+-------------------
----------------------------------------------------+------------------+
3 rows in set (0.00 sec)


mysql> show slave status;
+---------------+-------------+-------------+---------------+-------------------
---+---------------------+-----------------------+---------------+--------------
---------+------------------+-------------------+-----------------+-------------
--------+------------+------------+--------------+---------------------+--------
---------+
| Master_Host   | Master_User | Master_Port | Connect_retry | Master_Log_File
   | Read_Master_Log_Pos | Relay_Log_File        | Relay_Log_Pos | Relay_Master_
Log_File | Slave_IO_Running | Slave_SQL_Running | Replicate_do_db | Replicate_ig
nore_db | Last_errno | Last_error | Skip_counter | Exec_master_log_pos | Relay_l
og_space |
+---------------+-------------+-------------+---------------+-------------------
---+---------------------+-----------------------+---------------+--------------
---------+------------------+-------------------+-----------------+-------------
--------+------------+------------+--------------+---------------------+--------
---------+
| 192.168.1.102 | backup      | 3306        | 10            | mysql_binary_log.0
01 | 1011                | ete-kf2-relay-bin.002 | 227           | mysql_binary_
log.001  | Yes              | Yes               | test            |
        | 0          |            | 0            | 1011                | 223
         |
+---------------+-------------+-------------+---------------+-------------------
---+---------------------+-----------------------+---------------+--------------
---------+------------------+-------------------+-----------------+-------------
--------+------------+------------+--------------+---------------------+--------
---------+
1 row in set (0.00 sec)

 

参考文档:
http://forums.gentoo.org/viewtopic.php?t=241123
http://www.howtoforge.com/mysql_database_replication_p2
http://dev.mysql.com/doc/refman/4.1/en/replication-howto.html
http://bbs.chinaunix.net/viewthread.php?tid=692359&highlight=mysql

上一篇:交换个连接吧


下一篇:mySQL5.0.21安装注意的问题