Chinaunix首页 | 论坛 | 博客
  • 博客访问: 7388755
  • 博文数量: 1755
  • 博客积分: 18684
  • 博客等级: 上将
  • 技术积分: 16227
  • 用 户 组: 普通用户
  • 注册时间: 2010-06-02 10:28
个人简介

啥也没写

文章分类

全部博文(1755)

文章存档

2024年(1)

2023年(44)

2022年(39)

2021年(46)

2020年(43)

2019年(27)

2018年(44)

2017年(50)

2016年(47)

2015年(15)

2014年(21)

2013年(43)

2012年(143)

2011年(228)

2010年(263)

2009年(384)

2008年(246)

2007年(30)

2006年(38)

2005年(2)

2004年(1)

分类: LINUX

2008-12-23 09:55:40

一。前期准备
机器A:ip地址 192.168.1.210 (maste1)
机器B:ip地址  192.168.1.211  (master2)
机器A同时充当Slave角色,为便于区分,名称设为 Slave2
机器B同时充当Slave角色,为便于区分,名称设为 Slave1
假设两台机器都已经安装好mysql,安装路径假设/usr/local/mysql/下,并假设
mysql配置文件my.cnf存放在/etc/下
二。修改机器A中mysql配置文件/etc/my.cnf
在[mysqld]配置段添加如下字段

binlog-do-db= wywdb       # 指明欲同步数据库
binlog-ignore-db=mysql    #  指明不需要同步数据库
binlog-ignore-db=test      # 指明不需要同步数据库
并假设次server id 为1,即
server-id=1
在机器A,即master1上为机器B,即slave1添加一同步帐号

grant replication slave on *.* to identified by ’slave’;

然后重启A机器mysql
三。修改B机器中mysql配置文件
同样在[mysqld]字段下添加如下内容

server-id=2
master-host = 192.168.1.210
master-user = replication
master-password = slave
master-port = 3306

然后重启B机器mysql
四。在B中进入mysql

mysql> start slave;
mysql> show slave status\G;
*************************** 1. row ***************************
             Slave_IO_State: Waiting for master to send event
                Master_Host: 192.168.1.210
                Master_User: replication
                Master_Port: 3306
              Connect_Retry: 60
            Master_Log_File: mysql-bin.000003
        Read_Master_Log_Pos: 98
             Relay_Log_File: webserver-relay-bin.000004
              Relay_Log_Pos: 235
      Relay_Master_Log_File: mysql-bin.000003
           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: 235
            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)
ERROR:
No query specified

将显示以上同步信息,其中Slave_IO_Running和Slave_SQL_Running对应字段内容都为yes,
并且Slave_IO_State显示“Waiting for master to send event”,则表示mysql的Master和Slave
模式配置成功,否则表示不成功,可查看相应日志,查看什么地方出现了问题。
此时还可可进去A机器中查看master1的mysql同步信息,并可与B机器mysql对比。
在A机器中进入mysql:

mysql> show master status;
+——————+———-+————–+——————+
| File             | Position | Binlog_Do_DB | Binlog_Ignore_DB |
+——————+———-+————–+——————+
| mysql-bin.000003 |       98 | wywdb        | mysql,test       |
+——————+———-+————–+——————+
1 row in set (0.00 sec)

五。在机器B中为其增加一同步复制帐号
mysql> grant replication slave on *.* to identified by ’slave2′;
并修改A中mysql的配置文件
在[mysqld]增加如下内容
master-host = 192.168.1.211
master-user = replication
master-password = slave2
master-port = 3306
六。依次重启A和B中mysql
  进A中mysql
mysql> start slave;
然后可以查看同步情况
进入A中:

mysql> show slave status\G;
*************************** 1. row ***************************
             Slave_IO_State: Waiting for master to send event
                Master_Host: 192.168.1.211
                Master_User: replication
                Master_Port: 3306
              Connect_Retry: 60
            Master_Log_File: mysql-bin.000004
        Read_Master_Log_Pos: 98
             Relay_Log_File: centos-relay-bin.000005
              Relay_Log_Pos: 235
      Relay_Master_Log_File: mysql-bin.000004
           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: 235
            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)
ERROR:
No query specified
mysql> show master status;
+——————+———-+————–+——————+
| File             | Position | Binlog_Do_DB | Binlog_Ignore_DB |
+——————+———-+————–+——————+
| mysql-bin.000004 |       98 | wywdb        | mysql,test       |
+——————+———-+————–+——————+
1 row in set (0.00 sec)
在B中:
mysql> show slave status\G;
*************************** 1. row ***************************
             Slave_IO_State: Waiting for master to send event
                Master_Host: 192.168.1.210
                Master_User: replication
                Master_Port: 3306
              Connect_Retry: 60
            Master_Log_File: mysql-bin.000004
        Read_Master_Log_Pos: 98
             Relay_Log_File: webserver-relay-bin.000007
              Relay_Log_Pos: 235
      Relay_Master_Log_File: mysql-bin.000004
           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: 235
            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)
ERROR:
No query specified
 

mysql> show master status;
+——————+———-+————–+——————+
| File             | Position | Binlog_Do_DB | Binlog_Ignore_DB |
+——————+———-+————–+——————+
| mysql-bin.000004 |       98 | wywdb        |                  |
+——————+———-+————–+——————+
1 row in set (0.00 sec)
看到如上信息证明mysql的master和master模式同步配置成功。
 
七。简单同步验证测试
在A中
mysql> use wywdb
Database changed
mysql> create table test(id int,name varchar(20));
Query OK, 0 rows affected (0.03 sec)
mysql> insert into test values(1,’wanzhihua’);
Query OK, 1 row affected (0.00 sec)
mysql> select * from test;
+——+———–+
| id   | name      |
+——+———–+
|    1 | wanzhihua |
+——+———–+
1 row in set (0.01 sec)

然后进入B中
mysql> use wywdb
Database changed
mysql> select * from test;
+——+———–+
| id   | name      |
+——+———–+
|    1 | wanzhihua |
+——+———–+
1 row in set (0.00 sec)

证明A--->B同步成功
接着
 
mysql> insert into test values (2, ’shenyanfei’);
Query OK, 1 row affected (0.00 sec)
mysql> select * from test;
+——+————+
| id   | name       |
+——+————+
|    1 | wanzhihua  |
|    2 | shenyanfei |
+——+————+
2 rows in set (0.01 sec)
然后进去A中

mysql> select * from test;
+——+————+
| id   | name       |
+——+————+
|    1 | wanzhihua  |
|    2 | shenyanfei |
+——+————+
2 rows in set (0.00 sec)
证明AB互为同步成功。
后继:本文只是安装过程,对自增长,存储引擎,索引等没有详加阐述
,如需要,则需根据自身实际情况在次基础上作些修改。
 
阅读(2784) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~