Reference

Setting Up Binary Log File Position Based Replication

配置步骤

NOTE Certain steps within the setup process require the SUPER privilege. If you do not have this privilege, it might not be possible to enable replication.

There are some generic tasks that are common to all setups:

On the master, you must enable binary logging and configure a unique server ID. This might require a server restart. See Section 16.1.2.1, “Setting the Replication Master Configuration”.

为了使用 replication,必须开启 log-bin & server-id,配置完成需要重启MySQL

配置 master

[mysqld]
log-bin=mysql-bin
server-id=1

NOTE

  • 如果没有 server-id,master 将拒绝slave连接

  • 为了最大的满足 持久性 和 一致性 ,应该在master里配置 innodb_flush_log_at_trx_commit=1sync_binlog=1

  • 确保 skip-networking 未启用。 如果网络被禁用,复制将会失败

    • replication 应该是采用 TCP/IP 的方式进行传输

    • 具体可以看看MySQL连接方式:TCP/IP, SOCKET

    • 连接不指定ip或使用localhost,应该走 socket

    • 其他走 TCP/IP

On each slave that you want to connect to the master, you must configure a unique server ID. This might require a server restart. See Section 16.1.2.5.1, “Setting the Replication Slave Configuration”.

每一个 slave 必须配置一个唯一 server-id(互不相同哦),且不能为 0,然后重启

配置 slaves

[mysqld]
server-id=2

在主从里,slave 没有必要开启 log-bin,如果开启了,你还可以使其作为更复杂的 replication topology(复制拓扑) 的一部分

使 slave 能连接到 master

NOTE 本节建议先读完再配置,因为涉及到数据备份记录master状态的细节

为了能够使 slave 连接到 master,需要制定 master 的连接信息,在 slave 执行:

mysql> CHANGE MASTER TO->     MASTER_HOST='master_host_name',
    ->     MASTER_USER='replication_user_name',
    ->     MASTER_PASSWORD='replication_password',
    ->     MASTER_LOG_FILE='recorded_log_file_name',
    ->     MASTER_LOG_POS=recorded_log_position;

这里特别说明一下,MASTER_LOG_FILE, MASTER_LOG_POS 是 slave 要读取的 binlog 的文件名及文件位置,在 master 上执行sql show master status 查看,但在配置之前需要禁止所有写操作,这里建议查看下面两篇文档:

  • 13.4.2.1 CHANGE MASTER TO Syntax

  • 16.1.2.3, “Obtaining the Replication Master Binary Log Coordinates”

在一个新环境下,即新的mysql,没有数据,可以不指定 MASTER_LOG_FILE, MASTER_LOG_POS, 但大多数情况,我们在配置主从使,已经有大量的数据了,这个时候分两种情况:

  • 已配置 log-bin
  • 未配置 log-bin

这两种情况只有一个地方不一样:

diff-whether-binlog-exists
  1. 禁止所有写入

    mysql> FLUSH TABLES WITH READ LOCK;
  2. 假如有binlog,记录 master status : show master status
  3. 手动将已有数据导入 slave,数据备份的方法有两种:mysqldump 和 打包datadir下面的数据
  4. 启动 slave ,最好指定参数 --skip-slave-start,以便不会启动复制,然后导入数据
  5. 最后执行 change master to ..
    • 之前有 binlog,这里需要指定之前记录的 master status: MASTER_LOG_FILE 及 MASTER_LOG_POS
    • 无 binlog,这里不需要配置 MASTER_LOG_FILE 及 MASTER_LOG_POS,或默认值 MASTER_LOG_FILE=”, MASTER_LOG_POS=4

具体步骤在下面会说到:#启动复制前的准备

NOTE

主从不能使用 socket 连接,必须能够使用 TCP/IP 连接到 master

其他

  • 涉及到 中继日志 的概念,参考:Section 16.2.4, “Replication Relay and Status Logs”

  • 建立数据快照:16.1.2.4 Choosing a Method for Data Snapshots

创建用于 主从复制 的用户

嗯,这些都不重要了

Optionally, create a separate user for your slaves to use during authentication with the master when reading the binary log for replication. See Section 16.1.2.2, “Creating a User for Replication”.

mysql> CREATE USER 'repl'@'%.mydomain.com' IDENTIFIED BY 'slavepass';
mysql> GRANT REPLICATION SLAVE ON *.* TO 'repl'@'%.mydomain.com';

更多账号管理,见:Section 13.7.1, “Account Management Statements”

启动复制前的准备

Before creating a data snapshot or starting the replication process, on the master you should record the current position in the binary log. You need this information when configuring the slave so that the slave knows where within the binary log to start executing events. See Section 16.1.2.3, “Obtaining the Replication Master Binary Log Coordinates”.

If you already have data on the master and want to use it to synchronize the slave, you need to create a data snapshot to copy the data to the slave. The storage engine you are using has an impact on how you create the snapshot. When you are using MyISAM, you must stop processing statements on the master to obtain a read-lock, then obtain its current binary log coordinates and dump its data, before permitting the master to continue executing statements. If you do not stop the execution of statements, the data dump and the master status information will not match, resulting in inconsistent or corrupted databases on the slaves. For more information on replicating a MyISAM master, see Section 16.1.2.3, “Obtaining the Replication Master Binary Log Coordinates”. If you are using InnoDB, you do not need a read-lock and a transaction that is long enough to transfer the data snapshot is sufficient. For more information, see Section 14.19, “InnoDB and MySQL Replication”.

这里说的是,之前是否已经开启 binlog 及 之前是否有数据,前面已经说明,见:diff-whether-binlog-exists

Note

一些操作需要 SUPER 权限,如果没有此权限,可能无法启动 replication

其他场景

全新的主从数据库, 参考:Section 16.1.2.5.3, “Setting Up Replication between a New Master and Slaves”.

现有的数据库建立 replica:参考:Section 16.1.2.5.4, “Setting Up Replication with Existing Data”.

在已有的 replica 环境里添加新的 slaves, 参考:Section 16.1.2.6, “Adding Slaves to a Replication Environment”.

其他问题

性能优化·并行复制外建约束问题
并行复制外键约束问题二

MySQL—Replication相关推荐

  1. 用HAproxy+keepalived+mysql Replication 构建基于企业级负载均衡

    最近用HAproxy+keepalived+mysql复制测试高可用性Linux系统集群.    HAProxy是一款免费的提供高可用性.负载均衡以及基于TCP(第四层)和HTTP(第七层)应用的代理 ...

  2. 认识MySQL Replication

    MySQL Replication 是 MySQL 非常有特色的一个功能,他能够将一个 MySQL Server 的 Instance 中的数据完整的复制到另外一个 MySQL Server 的 In ...

  3. MySQL Replication 主从复制全方位解决方案

    MySQL Replication 主从复制全方位解决方案 参考文章: (1)MySQL Replication 主从复制全方位解决方案 (2)https://www.cnblogs.com/clsn ...

  4. mysql流行的四种高可用架构之:Keepalived+Mysql Replication

    关于MySQL-HA,目前有很多种解决方案,目前互联网上用的较多的是Keepalived+Mysql Replication组合.MMM+Mysql Replication组合.Heartbeat+D ...

  5. mysql replication 协议_深入解析MySQL replication协议

    WHY 最开始的时候,go-mysql只是简单的抽象mixer的代码,提供一个基本的mysql driver以及proxy framework,但做到后面,笔者突然觉得,既然研究了这么久mysql c ...

  6. Mysql Replication 机制

    mysql replication 实现原理 一.replication 线程 Mysql 的Replication是一个异步的复制过程,从一个 mysql Instance(master)复制到另一 ...

  7. mysql replication principle--转

    原文地址:http://www.codeweblog.com/mysql-replication-principle/ 1, the replication process  Mysql replic ...

  8. mysql replication health is not ok_MySQL的高可用——MHA

    在之前的博客中,介绍了mysql的主从模型以及深层次的mysql的读写分离插件--ProxySQL,让我们可以很大程度上提升数据库服务器的性能和优化用户的体验,但是,我们对于数据库的可靠性似乎缺了一点 ...

  9. mysql双节点安装_快速安装及配置MySQL Replication双主节点集群--及改变数据保存目录...

    192.168.1.101  master/slave 192.168.1.102  slave 192.168.1.103  slave 操作系统均为centos6.5 原理图: 1. 分别安装my ...

  10. drbd heartbeat mysql_Heartbeat+DRBD+MySQL Replication故障处理

    不久前的一次机房网络故障,再一次对我们在Heartbeat+DRBD+MySQL数据库架构运维水平的一个考验,之前不止一次的测试与线上部署,还有之后大言不惭的关于该架构组件的所谓深入理解,在这一次不经 ...

最新文章

  1. 方向:AI研究方向历史性转变!从机器学习到人类研究
  2. 为什么wait和notify只能在synchronized中?
  3. 课程第七天内容《基础交换七》
  4. list修改元素的值_第115天:Python 到底是值传递还是引用传递
  5. KaleidoscopeGame
  6. Golang与C#之switch区别
  7. Copy List with Random Pointer
  8. python脚本语言采用声音作为手段_LKJ自动化测试脚本定义及生成技术研究
  9. 不同的写法 其中 1 2 (试了下 没有效果 ,先记载这里把)
  10. 作为现代计算机理论的基础的,作为现代计算机理论基础的冯·诺依曼原理和思想是()。...
  11. 从技术上解读大数据的应用现状和开源未来! | 技术头条
  12. matlab 创建批量文件夹_学会这12个批量操作,从此告别加班!
  13. 8086考试主要考的最小模式
  14. hdu1247 Hat’s Words 字符串模拟
  15. 18. Element firstChild 属性
  16. Day-22 基础模块3 正则表达式_re模块
  17. 使用Quartz.Net定时删除Log
  18. 一次HBase问题的解决过程(Status: INCONSISTENT)
  19. 穿越回二年级讲CPU工作原理。
  20. TextView的属性

热门文章

  1. 点量FLV视频加密系统
  2. Vivado从此开始(进阶篇)读书笔记——跨时钟处理
  3. 前端JavaScript——打砖块小游戏
  4. mosquitto mysql_转mosquitto auth plugin 编译配置
  5. Android Dalvik虚拟机内存分配问题
  6. 关于在Fragment下onActivityResult回调无结果的问题
  7. 初识操作系统(OS)
  8. OpenGL - Draw Triangle
  9. echarts dataZoom组件
  10. LCU 模式选择过程分析(CTU的深度选择及CU的分割)