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=1
和sync_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
查看,但在配置之前需要禁止所有写操作,这里建议查看下面两篇文档:
在一个新环境下,即新的mysql,没有数据,可以不指定 MASTER_LOG_FILE, MASTER_LOG_POS
, 但大多数情况,我们在配置主从使,已经有大量的数据了,这个时候分两种情况:
- 已配置 log-bin
- 未配置 log-bin
这两种情况只有一个地方不一样:
diff-whether-binlog-exists
- 禁止所有写入
mysql> FLUSH TABLES WITH READ LOCK;
- 假如有binlog,记录 master status :
show master status
- 手动将已有数据导入 slave,数据备份的方法有两种:mysqldump 和 打包datadir下面的数据
- 启动 slave ,最好指定参数
--skip-slave-start
,以便不会启动复制,然后导入数据 - 最后执行
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”
创建用于 主从复制 的用户
嗯,这些都不重要了
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”.