mysql5.6数据库同步,单向双向同步问题

windowsMySQL5.6实现主从数据库同步数据

  mysql5.6数据库同步,单向双向同步问题

.单向同步

主数据库(mysql5.6)192.168.1.104

从数据库(mysql5.6)192.168.1.105

 

略去创建库的步骤,这里认为你同步的数据库已经存在,而且主从数据库的库和表结构均相同

 

1.在主数据库上创建用户

insert into mysql.user(host,user,password)

values('localhost','admin',password('123456'));

flush privileges;

 

2.主数据库提供用户,赋值访问权限

仅仅192.168.1.105这个机器使用admin/123456同步

grant replication slave,reload,super on *.* to 'admin'@'192.168.1.105' identified by '123456' with grant option;

 

所有人都只用admin/123456同步

grant replication slave,reload,super on *.* to 'admin'@'%' identified by '123456' with grant option;

 

 

3.修改104主数据库的my.ini

在[mysqld]节点下配置一下代码

#设置服务器id,为1表示主服务器,注意:如果原来的配置文件中已经有这一行,就不用再添加了。

server_id=1

log_bin=mysql-bin  #启动MySQ二进制日志系统,注意:如果原来的配置文件中已经有这一行,就不用再添加了。

binlog_do_db=test  #需要同步的数据库名,如果有多个数据库,可重复此参数,每个数据库一行

binlog_ignore_db=mysql  #不同步mysql系统数据库

binlog_ignore_db=information_schema  #不同步information_schema系统数据库

 

然后保存my.ini配置文件

管理员打开cmd

先停止mysql服务,net stop mysql

然后重启mysql服务,net start mysql

服务启动成功后,登陆mysql

mysql -u root -p123456  注意,-p和123456之间不用空格

 

在查看主数据库的状态,show master status\G;

+------------------+----------+--------------+------------------+
| File                        | Position  | Binlog_Do_DB | Binlog_Ignore_DB |
+------------------+----------+--------------+------------------+
| mysql-bin.000001 |    120      | test            | mysql                  |
+------------------+----------+--------------+------------------+
1 row in set (0.00 sec)

 

注意:这里记住File的值:mysql-bin.000001和Position的值:120,后面会用到。

 

4.修改105从数据库的my.ini文件

server_id=2

log_bin=mysql-bin

replicate_do_db=test

replicate_ignore_db=mysql

replicate_ignore_db=information_schema

 

保存my.ini

然后停止mysql服务,net stop mysql

在启动mysql服务,net start mysql

然后进入mysql -u root -p123456

先停止从数据库同步,stop slave,

再输入:change master to master_host='192.168.1.104',master_user='admin',master_password='123456',master_log_file='mysql-bin.000001',master_log_pos=120; #执行同步语句

 

开启同步,start slave

注:经测试,stop slave ,start slave 和 slave start,slave stop一样都可以,

最后查看一下状态:show slave status;

mysql> show slave status\G;

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

               Slave_IO_State: Waiting for master to send event

                  Master_Host: 192.168.1.104

                  Master_User: test01

                  Master_Port: 3306

                Connect_Retry: 60

              Master_Log_File: mysql-bin.000001

          Read_Master_Log_Pos: 120

               Relay_Log_File: mysql-bin.000001

                Relay_Log_Pos: 283

        Relay_Master_Log_File: mysql-bin.000001

             Slave_IO_Running: Yes

            Slave_SQL_Running: Yes

              Replicate_Do_DB:

          Replicate_Ignore_DB: mysql,information_schema

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

              Relay_Log_Space: 465

              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

                  Master_UUID: f44dc946-f462-11e4-81cc-00ffb613a054

             Master_Info_File: D:\GreenProgramFiles\MySQL Server 5.6\data\master

.info

                    SQL_Delay: 0

          SQL_Remaining_Delay: NULL

      Slave_SQL_Running_State: Slave has read all relay log; waiting for the sla

ve I/O thread to update it

           Master_Retry_Count: 86400

                  Master_Bind:

      Last_IO_Error_Timestamp:

     Last_SQL_Error_Timestamp:

               Master_SSL_Crl:

           Master_SSL_Crlpath:

           Retrieved_Gtid_Set:

            Executed_Gtid_Set:

                Auto_Position: 0

1 row in set (0.00 sec)

 

注:Slave_IO_Running: Yes

            Slave_SQL_Running: Yes

这两个都是yes的时候同步才会成功发生,否则不能实现同步

 

5.最后在主数据库上执行插入,更改,删除语句,在从数据库上查看,数据保持一致了,

 

 

 

 

 

.双向同步

在上述配置保持不变的情况下,各自增加主从配置项

104:主数据库已经配置完毕,需要配置从数据库

log_bin=mysql-bin

replicate_do_db=test

replicate_ignore_db=mysql

replicate_ignore_db=information_schema

重启mysql,登陆后输入:

stop slave;

change master to master_host='192.168.1.105',master_user='admin',master_password='123456',master_log_file='mysql-bin.0000001',master_log_pos=120;

# master_log_file='mysql-bin.0000001',master_log_pos=120;

其中这两个参数是根据master主数据库的二进制日志文件和为止固定的

start slave;

show slave status\G;

 

 

 

105:从数据库已经配置完毕,需要配置主数据库

log_bin=mysql-bin

binlog_do_db=test

binlog_ignore_db=mysql

binlog_ignore_db=information_schema

 

重启mysql,登陆输入:

flush tables with read lock;

show master status\G;得到主数据库的信息

根据这这个命令的结果在跟新104主从数据库的同步日志和位置

mysql>change master to master_log_file='mysql-bin.00002',master_log_pos=120;

 

同时要为用户开放权限

仅仅192.168.1.105这个机器使用admin/123456同步

grant replication slave,reload,super on *.* to 'admin'@'192.168.1.105' identified by '123456' with grant option;

 

所有人都只用admin/123456同步

grant replication slave,reload,super on *.* to 'admin'@'%' identified by '123456' with grant option;

 

 

GRANT REPLICATION SLAVE ON *.* TO 'admin'@'%' IDENTIFIED BY '123456'; 

 

GRANT FILE,SELECT,REPLICATION SLAVE ON *.* TO 'admin'@'%' IDENTIFIED BY '123456';

 

 

最后配置完毕的结果

104: 主从数据库

#主数据库配置项

server_id=1

#auto_increment_increment=2

#auto_increment_offset=1

log_bin=mysql-bin

binlog_do_db=test

binlog_ignore_db=mysql

binlog_ignore_db=information_schema

 

#从数据库配置项

replicate_do_db= test #同步的数据库

replicate_ignore_db=mysql

replicate_ignore_db=information_schema

 

 

105:主从数据库

#从数据库配置项

server_id=2

#auto_increment_increment=2

#auto_increment_offset=1

log_bin=mysql-bin

replicate_do_db= test #同步的数据库

replicate_ignore_db=mysql

replicate_ignore_db=information_schema

 

#主数据库配置项

binlog_do_db=test

binlog_ignore_db=mysql

binlog_ignore_db=information_schema

 

 

 

遇到的问题总结:

1.经测试,主mysql5.6,从mysql5.5,不能实现同步,

Slave_IO_Running: No

Slave_SQL_Running: Yes

错误提示:Last_IO_Error: Got fatal error 1236 from master when  reading data from binary log:

问题:一般版本不一致

 

2.

Slave_IO_Running: Yes

Slave_SQL_Running: No

 

 

3. Last_IO_Error: Fatal error: The slave I/O thread stops because master and slave have equal MySQL server UUIDs; these UUIDs must be different for

 replication to work.

mysql>show variables like ‘server_id';

+—————+——-+
| Variable_name | Value |
+—————+——-+
| server_id | 3 |
+—————+——-+

主从并不一样,排除该问题。

找到原因在于,拷贝整个data目录,把auto.cnf文件也拷贝过来了,里面记录了数据库的uuid,每个库的uuid应该是不一样的。

[auto]
server-uuid=6dcee5be-8cdb-11e2-9408-90e2ba2e2ea6

解决办法,按照这个16进制格式,随便改下,重启mysql即可。

 

4. 111010 17:35:49 [ERROR] Error reading packet from server: Client requested master

 to start replication from impossible position ( server_errno=1236)

111010 17:35:49 [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

111010 17:35:49 [Note] Slave I/O thread exiting, read up to log 'mysql-bin.000288',

position 627806304

 

解决方法:

mysql> stop slave;

mysql> change master to master_log_file='mysql-bin.000288',master_log_pos=627625751;

mysql> start slave;

 

5.在一台主机上配置一台从机,启动的时候报 ERROR 1872 (HY000): Slave failed to initialize relay log info structure from the

 repository(双向配置后,启动从机报错)

解决方法:配置relay_log=relay-,

然后再重新停止net stop mysql,

启动net start mysql,

登陆mysql -u root -p123456

运行reset slave

然后启动从机start slave

 

 

 

posted @ 2017-01-12 00:49  ching126  阅读(322)  评论(0编辑  收藏  举报
一个小小的平凡的事情坚持10年,回头看看,你会收获惊喜;坚持20年,回头看看,你的命运已经发生质的蜕变;坚持30年甚至更多年,回头看看,你的人生已经发生惊天动地的变化。