本节来讲讲Mysql的复制
参考官方文档
主要方式包括:
- 传统方式:基于主机的二进制日志(master’s binary log)
- 新的方式:基于全局事务标识(Global Transaction Identifiers)
常用Mysql的主从复制:基于Master的二进制日志(binarylog)
参考官方文档
在进行复制配置之前,我们可能遇到两种可能:
+ 我们Master可能是全新的
+ 我们的Master可能旧的,已存在旧数据
在这两种情况下有这个共同的步骤是:
- 在Master上,必须启用binary logging、配置一个唯一的server-id
- 在每一个希望连接到Master的Slave上配置唯一service-id
- 可选的,在Master上创建一个用户专用于复制
- 如果你的Master已经存在数据,需要先将数据copy到Slaves
- 得到binary log的文件名和位置
- 配置Slaves设置master的 host name, login credentials, and binary log file name and position
常见的场景:
- 配置一个新的的Master和Slaves
- 配置一个已经存在数据的复制
- 像一个复制环境中添加一个Slave
配置一个新的的Master和Slaves
1.Master配置my.conf
- log-bin:开启二进制日志
- server-id:唯一的id,范围1至2^32-1
- 可选参数binlog_format:ROW/STATEMENT/MIXED
[mysqld]
log-bin=mysql-bin
server-id=1
2.Slave配置my.conf
server-id=2
3.创建一个专用于复制的用户
在Master机器,mysql-client上执行如下指令
mysql> CREATE USER 'repl'@'%.mydomain.com' IDENTIFIED BY 'slavepass';
mysql> GRANT REPLICATION SLAVE ON *.* TO 'repl'@'%.mydomain.com';
4.查看Master的status信息
mysql>SHOW MASTER STATUS;
得到:文件名和位置
+------------------+----------+--------------+------------------+-------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+------------------+----------+--------------+------------------+-------------------+
| mysql-bin.000001 | 595 | | | |
+------------------+----------+--------------+------------------+-------------------+
1 row in set (0.00 sec)
5.在Slave上设置Master参数
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;
启动slave
mysql> start slave;
6.检查配置是否成功
mysql> show slave status\G
展示如下:
*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: hadoop01
Master_User: repl
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: mysql-bin.000002
Read_Master_Log_Pos: 605
Relay_Log_File: hadoop02-relay-bin.000005
Relay_Log_Pos: 473
Relay_Master_Log_File: mysql-bin.000002
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
- Slave_IO_Running: Yes
- Slave_SQL_Running: Yes
配置成功