MySQL主从复制

   实验环境:

  master and the slave server machine have the same configuration as followings:

  [root@master1 ~]# uname -a

  Linux master1 2.6.18-8.el5 #1 SMP Fri Jan 26 14:15:21 EST 2007 i686 i686 i386 GNU/Linux

  mysql> select @@version;

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

  | @@version |

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

  | 5.6.19-enterprise-commercial-advanced |

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

  1 row in set (0.00 sec)

  master IP: 192.168.92.11

  slave IP: 192.168.92.111

  二,主从服务器分别作的事情

  Master sever:

  changes data

  keeps log of changes

  slave server:

  ask master for events

  executes events

  三,复制的类型(同步|异步|半同步)

  Synchronouse replication

  1,data is replicated and appllied then committed

  2,provides consistency ,but slower

  3,provided by MySQL Cluster

  Asynchronous replication

  1,transactions committed immediately and replicated

  2,no consistency,but faster

  3,provided by MySQL Server

  SemiSyncReplication

  1,provided by Google

  四,复制所需要的日志

  Binary log的作用:

  1,log every change (select 是不记录的,只记录改变的)

  2,split into transaction groups

  两个复制相关的二进制文件:

  File: master_bin.NNNNNN

  1,The actual contents of the binlog

  File: master_bin.index

  1,an index file over the files above

  五,复制所用到的线程

  Master: I/O thread

  Slave: I/O thread and SQL Thread

  master.info contains:

  1,Read coordinates: which contains master log name and master log position

  2,Connection information: which contains the following two information:

  a,host,user,password ,port

  b,SSL keys and certificates

  relay-log.info contains:

  1,Group master coordinates: which contains master log name and master log position

  2,Group relay log coordinates: which contains relay log name and relay log position

  六,具体的实施步骤:

  The following 8 Steps are need to be taken to configure the master slave replication:

  1,Fix my.cnf file for master and slave

  2,add user and grants on master

  3,make sure the related configuration

  4,on the master,use the SHOW MASTER STATUS statement to determine the current binary log file name and position:

  5,load backup dump file into master

  6,configure slave

  7,start slave

  8,check slave status show slave statusG

  1,Fix my.cnf file for master and slave

  Master configuration --required(必选择)

  log_bin = master_bin

  server_id =11

  配置好了后,重新启动mysql服务

  [root@master1 ~]# cd /etc/rc.d/init.d/

  [root@master1 init.d]# service mysql restart

  Shutting down MySQL.. [ OK ]

  Starting MySQL......... [ OK ]

  slave configuration --required(必选择)

  server_id=111 The master and slave must have the different server_id

  配置好了后,重新启动mysql服务

  [root@slave1 init.d]# service mysql restart

  Shutting down MySQL.. [ OK ]

  Starting MySQL......... [ OK ]

  2,add user and grants on master

  mysql> CREATE USER 'repl'@'192.168.92.111' IDENTIFIED BY 'slavepass';

  mysql> GRANT REPLICATION SLAVE ON *.* TO 'repl'@'192.168.92.111';

  3,make sure the related configuration

  show variables like 'server%';

  show variables like 'log%';

  show grants for 'repl'@'192.168.92.111';

  mysql> show variables like 'server%';

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

  | Variable_name | Value |

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

  | server_id | 11 |

  | server_id_bits | 32 |

  | server_uuid | 303c6931-0d5e-11e4-9f5c-000c29f09a2c |

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

  3 rows in set (0.00 sec)

  show variables like 'log%'; 看log_bin是否开启用

  mysql> show variables like 'log%';

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

  | Variable_name | Value |

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

  | log_bin | ON |

  | log_bin_basename | /var/lib/mysql/master_bin |

  | log_bin_index | /var/lib/mysql/master_bin.index |

  | log_bin_trust_function_creators | OFF |

  | log_bin_use_v1_row_events | OFF |

  | log_error | /var/lib/mysql/master1.err |

  | log_output | FILE |

  | log_queries_not_using_indexes | OFF |

  | log_slave_updates | OFF |

  | log_slow_admin_statements | OFF |

  | log_slow_slave_statements | OFF |

  | log_throttle_queries_not_using_indexes | 0 |

  | log_warnings | 1 |

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

  13 rows in set (0.00 sec)

  mysql> show grants for 'repl'@'192.168.92.111';

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

  | Grants for repl@192.168.92.111 |

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

  | GRANT REPLICATION SLAVE ON *.* TO 'repl'@'192.168.92.111' IDENTIFIED BY PASSWORD '*809534247D21AC735802078139D8A854F45C31F3' |

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

  1 row in set (0.00 sec)

  4,on the master,use the SHOW MASTER STATUS statement to determine the current binary log file name and position:

  mysql> show master statusG

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

  File: master_bin.000001

  Position: 589

  Binlog_Do_DB:

  Binlog_Ignore_DB:

  Executed_Gtid_Set:

  1 row in set (0.00 sec)

  5,load backup dump file into master

  一定要先创建一个数据库

  mysql> create database cddl;

  Query OK, 1 row affected (0.02 sec)

  还原数据库到master上:

  mysql -h 192.168.92.11 -uroot -ppassword cddl< /mysql_installer/cddl20140702.sql

  6,configure slave

  CHANGE MASTER TO

  MASTER_HOST='192.168.92.11',

  MASTER_USER='repl',

  MASTER_PASSWORD='slavepass',

  MASTER_PORT=3306,

  MASTER_LOG_FILE='master_bin.000001',

  MASTER_LOG_POS=589,

  MASTER_CONNECT_RETRY=10;

  7,start slave

  mysql> start slave;

  Query OK, 0 rows affected (0.25 sec)

  8,check slave status

  mysql> show slave statusG;

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

  Slave_IO_State: Waiting for master to send event

  Master_Host: 192.168.92.11

  Master_User: repl

  Master_Port: 3306

  Connect_Retry: 10

  Master_Log_File: master_bin.000002

  Read_Master_Log_Pos: 120

  Relay_Log_File: slave1-relay-bin.000002

  Relay_Log_Pos: 43341241

  Relay_Master_Log_File: master_bin.000001

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

  Relay_Log_Space: 46042813

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

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

  Master_UUID: 24f8486c-0d8c-11e4-a088-000c29f09a2c

  Master_Info_File: /var/lib/mysql/master.info

  SQL_Delay: 0

  SQL_Remaining_Delay: NULL

  Slave_SQL_Running_State: creating table

  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.01 sec)

  ERROR:

  No query specified

  从上面可以看出备库正在做复制。

  mysql> show slave statusG;

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

  Slave_IO_State: Waiting for master to send event

  Master_Host: 192.168.92.11

  Master_User: repl

  Master_Port: 3306

  Connect_Retry: 10

  Master_Log_File: master_bin.000002

  Read_Master_Log_Pos: 120

  Relay_Log_File: slave1-relay-bin.000003

  Relay_Log_Pos: 284

  Relay_Master_Log_File: master_bin.000002

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

  Relay_Log_Space: 46042639

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

  Master_UUID: 24f8486c-0d8c-11e4-a088-000c29f09a2c

  Master_Info_File: /var/lib/mysql/master.info

  SQL_Delay: 0

  SQL_Remaining_Delay: NULL

  Slave_SQL_Running_State: Slave has read all relay log; waiting for the slave 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)

  ERROR:

  No query specified

  从上面可以看出replicate完成,至此最简单的 master - slave配置成功。

  下面测试一下主从复制:

  master:

  mysql> create table TT(id int ,name varchar(30));

  Query OK, 0 rows affected (0.10 sec)

  mysql> insert into TT VALUES (1,'FAFAFAFAFA');

  Query OK, 1 row affected (0.09 sec)

  mysql> insert into TT VALUES (1,'FAFAFAFAFA2');

  Query OK, 1 row affected (0.04 sec)

  mysql> insert into TT VALUES (3,'FAFAFAFAFA3');

  Query OK, 1 row affected (0.00 sec)

  mysql> COMMIT;

  Query OK, 0 rows affected (0.00 sec)

  在slave上查询:

  mysql> select * from TT;

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

  | id | name |

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

  | 1 | FAFAFAFAFA |

  | 1 | FAFAFAFAFA2 |

  | 3 | FAFAFAFAFA3 |

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

  3 rows in set (0.02 sec)

  可以看出顺利的传到slave 库上来了。

时间: 2024-10-29 07:49:14

MySQL主从复制的相关文章

Centos 6.3下mysql主从复制笔记

MySQL主从复制结构是基于mysql bin-log日志基础上从库通过打开IO进程收到主库的bin-log日志增量信息,并保存到本地relay log,而后再通过打开MYSQL进程从relay log上获取的增量信息并翻译成SQL语句后写到从数据库. 主从复制结构实际上可以实现两个功能 1.从库充当主库的数据库备份实例 2.读写分离主库负责正常读写数据从库只负责读数据 实际生产环境因为很多应用实际读数据库的次数远大于写数据库的次数所以在项目开发初期编写程序时做一个判断对所有读的操作全部推到从库

MySQL主从复制的延迟监测

主从复制延迟的监测,我以前的做法是通过比较show slave status\G中的两个变量的差值(Read_Master_Log_Pos,Exec_Master_Log_Pos),将差值设置为一个自己认为合理的范围,Seconds_Behind_Master 没有适用过,今天做一次解析: Seconds_Behind_Master 是通过比较 SQL THREAD 接受 events事件的时间戳(timestamp) 与IO THREAD  执行事件 events时间戳的差值--秒数来确定sl

mysql dba系统学习(18)mysql主从复制的实现

mysql主从复制的实现 1.MySQL复制的实现原理 MySQL支持单向.双向复制.异步复制,复制过程中一个服务器充当主服务器,而一个或多个其它服务器充当从服务器.主服务器将更新写入一个二进制日志文件中,并创建一个索引文件以跟踪日志循环.这些日志可以记录发送到从服务器的更新.当一个从服务器连接主服务器时,日志文件会通知主服务器,从服务器在日志中读取的最后一次成功更新的位置.接着,从服务器在上次成功更新的位置处开始进入更新操作.更新完成后从服务器开始进入等待状态,等待主服务器后续的更新.需要注意

高可用架构-- MySQL主从复制的配置

环境 操作系统:CentOS-6.6-x86_64-bin-DVD1.iso MySQL版本:mysql-5.6.26.tar.gz 主节点IP:192.168.1.205     主机名:edu-mysql-01 从节点IP:192.168.1.206     主机名:edu-mysql-02 主机配置:4核CPU.4G内存   依赖课程 <高可用架构篇--第13节--MySQL源码编译安装(CentOS-6.6+MySQL-5.6)>   MySQL主从复制官方文档 http://dev.

MySQL主从复制报错slave have equal MySQL server UUIDs

最近在部署MySQL主从复制架构的时候,碰到了"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." 这个错误提示.即主从架构中使用了相同的UUID.检查server_id系统变量,已经是不同的设置,那原因是

数据库-关于mysql主从复制的问题

问题描述 关于mysql主从复制的问题 最近在学习mysql主从复制的东西,配置什么基本都完成了,在最后第二部复制数据库文件的时候出现了问题.我停止了主从服务器的服务,将data文件夹整个复制到了从服务器中,但是主从服务器的引擎不同,主服务器my.ini中default-storage-engine=INNODB,从服务器的为MyISAM.进行复制后开启服务,但是从服务器只有数据库,库内不存在任何表.使用front查看时连mysql数据库下也是空.求教应该怎么处理. ![图片说明](http:/

关于mysql主从复制自增长列

问题描述 关于mysql主从复制自增长列 现有两台mysql服务器A和B A:auto_increment_offset = 2 auto_increment_increment = 2 binlog_format="STATEMENT" B:auto_increment_offset = 1 auto_increment_increment = 2 A和B都有如下表,建表语句如下: test | CREATE TABLE test (id int(11) NOT NULL AUTO_

MySQL主从复制中常见的3个错误及填坑方案

一.问题描述    主从复制错误一直是MySQL DBA一直填不完的坑,如鲠在喉,也有人说mysql主从复制不稳定云云,其实MySQL复制比我们想象中要坚强得多,而绝大部分DBA却认为只要跳过错误继续复制就好啦,接下来不发生错误就好了,其实跳过错误就会有数据不一致的风险,数据不一致可能还会越来越严重,而我就复制错误中反复出现的1045.1032和1062错误引起的数据库主从不一致的的现象进行深入分析及给出一套完善的解决方案.   (1) [ERROR]1452:无法在外键的表插入参考主键没有的数

MySQL 主从复制详解(详细)

目录: MySQL 主从原理 MySQL 主从配置 MySQL 主从一致性检查 MySQL 主从错误处理 参考链接 一.mysql主从原理 1. 基本介绍 MySQL 内建的复制功能是构建大型,高性能应用程序的基础.将 MySQL 的 数亿分布到到多个系统上去,这种分步的机制,是通过将 MySQL 的某一台主机的数据复制到其它主机( Slave )上,并重新执行一遍来实现的.复制过程中一个服务器充当服务器,而一个或多个其它服务器充当从服务器.主服务器将更新写入二进制日志,并维护文件的一个索引以跟

关于mysql主从复制的概述与分类(转)

一.概述: 按照MySQL的同步复制特点,大体上可以分为三种类别: 1.异步复制: 2.半同步复制: 3.完全同步的复制: -------------------------------------------------------------------------------------------------- (一).异步复制: 1.概念 mysql的异步复制在业界中的叫法有很多,比如:AB复制.主从同步.mysql replication等等.说白了就是master和slave结构.