Chinaunix首页 | 论坛 | 博客
  • 博客访问: 116459
  • 博文数量: 8
  • 博客积分: 241
  • 博客等级: 入伍新兵
  • 技术积分: 118
  • 用 户 组: 普通用户
  • 注册时间: 2011-11-14 10:16
文章分类
文章存档

2013年(1)

2012年(3)

2011年(4)

分类: Mysql/postgreSQL

2011-11-17 16:53:25


Mysql5.5.13版本主从同步配置方法

测试环境

主服务器IP192.168.0.202(MYSQL:5.5.13OS:Redhat as 6.0)

从服务器IP192.168.0.210(MYSQL:5.5.13OS:Redhat as 6.0)

:此文档只描述主à从 的同步,而不是互备。要保证主从服务器的配置文件一致(仅仅server-id不一样,从服务器定义不要repl的数据库名称)

如需要达到互备效果,而在操作完整个文档内容后,执行下面两步即可:

1.  在从机上执行:mysql> grant replication slave on *.* to rep2@192.168.0.202 identified by 'password';

2.  在主机上执行:start slave

开始配置主从服务器:

l  主服务器配置:

主服务器上的配置和低版本的配置方式一样,只需要在配置文件中开启二进制日志文件和设置server-id=1

编辑mysql配置文件/etc/my.cnf,在[mysqld]部分加入:

server-id=1

log-bin=/data/binlog/binlog.binbinlog存放的地方

在主服务器上建立一个从服务器进行复制使用的账户(用户名:rep;密码:*********)

mysql> grant replication slave on *.* to rep@192.168.0.210 identified by '********';

mysql> flush privileges;

重启mysql服务

# /usr/loca/mysql/bin/mysqladmin shutdown

#/usr/local/mysql/bin/mysqld_safe &

l  从服务器配置:

Mysql5.1.7版本以后已经不支持把master配置属性写在my.cnf文件中了,只能把需要同步的数据库和需要忽略的数据库加入。

编辑从服务器的mysql配置文件/etc/my.cnf,在[mysqld]部分加入:

server-id=2

log-bin=/logdata/binlog/binlog.bin

replicate-do-db=db_name     #这两条最好加在结尾[需要同步的数据库]

replicate-ignore-db=mysql       #这两条最好加在结尾[不需要同步的库]

replicate-ignore-db=test    #一般将mysqltest这两个库取消不要同步,保证业务使用的数据库即可

重启mysql服务。

进入mysql命令行,停止从服务器线程并执行mysql命令,然后再启动从服务器线程:

mysql> stop slave;

Query OK, 0 rows affected (0.01 sec)

 

mysql> change master to

    -> master_host='192.168.0.202',

    -> master_user='root',

    -> master_password='password';

Query OK, 0 rows affected (0.01 sec)

mysql> start slave;

从服务器上检查复制进程是否正确:

mysql> show slave status \G

*************************** 1. row ***************************

               Slave_IO_State: Waiting for master to send event

                  Master_Host: 192.168.0.202

                  Master_User: rep

                  Master_Port: 3306

                Connect_Retry: 60

              Master_Log_File: binlog.000022

          Read_Master_Log_Pos: 1206606

               Relay_Log_File: test-file01-relay-bin.000008

                Relay_Log_Pos: 666782613

        Relay_Master_Log_File: binlog.000007

             Slave_IO_Running: Yes

            Slave_SQL_Running: Yes

              Replicate_Do_DB:

          Replicate_Ignore_DB: mysql,test

           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: 666782470

              Relay_Log_Space: 7793804982

              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: 6055915

Master_SSL_Verify_Server_Cert: No

                Last_IO_Errno: 0

                Last_IO_Error:

               Last_SQL_Errno: 0

               Last_SQL_Error:

  Replicate_Ignore_Server_Ids:

             Master_Server_Id: 1

1 row in set (0.00 sec)

上面出现: Slave_IO_Running: Yes Slave_SQL_Running: Yes 表示复制正常,如果有一个显示是NO,请检查以上的主从设置步骤是否正确。如果出现复制错误,从服务器的错误日志中也会出现错误消息。

主从复制异常处理

l  Slave_IO异常
通过show slave status \G;

看到slave_IO出现异常;

Slave_IO_Running:NO
Slave_SQL_Running:Yes
Seconds_Behind_Master: NULL
这个时候在从数据库的日志文件中应该有相应的日志信息,可以通过检查日志看到导致失败的原因;大致可能会有这样两种情况:
1.
在配置slave同步时因为slave访问master没有权限导致;
2. master
上的mysql-bin.xxxxxx文件异常(比如主库异常,丢失binlog文件等);
对于第一种情况,仔细检查数据库访问权限即可解决

通过配置主从步骤,从新设置下grant权限(针对不同用户)
对于第二种情况,详细介绍一下:
mysql> show slave status \G

*************************** 1. row ***************************

               Slave_IO_State: Waiting for master to send event

                  Master_Host: 192.168.0.202

                  Master_User: rep

                  Master_Port: 3306

                Connect_Retry: 60

              Master_Log_File: binlog.000022

          Read_Master_Log_Pos: 1206606

               Relay_Log_File: test-file01-relay-bin.000008

                Relay_Log_Pos: 666782613

        Relay_Master_Log_File: binlog.000007

             Slave_IO_Running: Yes

            Slave_SQL_Running: Yes

              Replicate_Do_DB:

          Replicate_Ignore_DB: mysql,test

           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: 666782470

              Relay_Log_Space: 7793804982

              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: 6055915

Master_SSL_Verify_Server_Cert: No

                Last_IO_Errno: 0

                Last_IO_Error:

               Last_SQL_Errno: 0

               Last_SQL_Error:

  Replicate_Ignore_Server_Ids:

             Master_Server_Id: 1

1 row in set (0.00 sec)
[root@test-file01 data]# tail -f test-file01.err
111117 15:15:23 [Note] Slave: received end packet from server, apparent master shutdown:

111117 15:15:23 [Note] Slave I/O thread: Failed reading log event, reconnecting to retry, log 'binlog.000022' at position 1760927

111117 15:15:23 [ERROR] Slave I/O: error reconnecting to master 'rep@192.168.0.202:3306' - retry-time: 60  retries: 86400, Error_code: 2003

111117 15:24:23 [Note] Slave: connected to master 'rep@192.168.0.202:3306',replication resumed in log 'binlog.000022' at position 1760927

111117 15:35:55 [Note] Slave: received end packet from server, apparent master shutdown:

111117 15:35:55 [Note] Slave I/O thread: Failed reading log event, reconnecting to retry, log 'binlog.000023' at position 377015

111117 15:35:55 [ERROR] Slave I/O: error reconnecting to master 'rep@192.168.0.202:3306' - retry-time: 60  retries: 86400, Error_code: 2003

111117 15:37:55 [Note] Slave: connected to master 'rep@192.168.0.202:3306',replication resumed in log 'binlog.000023' at position 377015

111117 15:37:55 [ERROR] Error reading packet from server: Client requested master to start replication from impossible position ( server_errno=1236)

111117 15:37:55 [ERROR] Slave I/O: Got fatal error 1236 from master when reading data from binary log: 'Client requested master to start replication from impossible position', Error_code: 1236

111117 15:37:55 [Note] Slave I/O thread exiting, read up to log 'binlog.000023', position 377015

解决步骤:
重启master库:service mysqld restart
mysql> show master status;

+---------------+----------+--------------+------------------+

| File          | Position | Binlog_Do_DB | Binlog_Ignore_DB |

+---------------+----------+--------------+------------------+

| binlog.000023 |  4157716 |              |                  |

+---------------+----------+--------------+------------------+

1 row in set (0.01 sec)mysql> slave stop;
mysql> change master to Master_Log_File='binlog.000023',Master_Log_Pos=3436946;

msql> slave start;
mysql> show slave status \G;

*************************** 1. row ***************************

               Slave_IO_State: Waiting for master to send event

                  Master_Host: 192.168.0.202

                  Master_User: rep

                  Master_Port: 3306

                Connect_Retry: 60

              Master_Log_File: binlog.000023

          Read_Master_Log_Pos: 4157716

               Relay_Log_File: test-file01-relay-bin.000002

                Relay_Log_Pos: 721020

        Relay_Master_Log_File: binlog.000023

             Slave_IO_Running: Yes

            Slave_SQL_Running: Yes

              Replicate_Do_DB:

          Replicate_Ignore_DB: mysql,test

           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: 4157716

              Relay_Log_Space: 721182

              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

Master_SSL_Verify_Server_Cert: No

                Last_IO_Errno: 0

                Last_IO_Error:

               Last_SQL_Errno: 0

               Last_SQL_Error:

  Replicate_Ignore_Server_Ids:

             Master_Server_Id: 1

1 row in set (0.00 sec)

 

ERROR:

No query specified

阅读(5618) | 评论(1) | 转发(1) |
给主人留下些什么吧!~~

☆彼岸★花开2011-11-19 19:43:59

( ^_^ )不错嘛~学习了