您的位置:首页 > 数据库 > MySQL

MySQL5.7.18主从复制搭建(一主一从)教程详解

2017-08-06 16:07 836 查看

一、复制原理

主服务器将更新写入二进制日志文件,并维护文件的一个索引以跟踪日志循环。这些日志可以记录发送到从服务器的更新.当一个从服务器连接主服务器时,它通知主服务器从服务器在日志中读取的最后一次成功更新的位置。从服务器接收从那时起发生的任何更新,然后封锁并等待主服务器通知新的更新。

MySQL使用3个线程来执行复制功能(其中1个在主服务器上,另两个在从服务器上。当发出START SLAVE时,从服务器创建一个I/O线程,以连接主服务器并让它发送记录在其二进制日志中的语句。主服务器创建一个线程将二进制日志中的内容发送到从服务器。

该线程为主服务器上的Binlog Dump线程。从服务器I/O线程读取主服务器Binlog Dump线程发送的内容并将该数据拷贝到从服务器数据目录中的本地文件中,即中继日志。第3个线程是SQL线程,是从服务器创建用于读取中继日志并执行日志中包含的更新。

二、服务器准备

操作系统版本:Red Hat Enterprise Linux Server release 6.7 (Santiago)

Master(主)            ip:172.16.115.245  主机名称:mysql2    server_id:245 

Slave(从)             ip:172.16.115.247  主机名称:mysql3    server_id:247

主从服务器上都已安装MySQL5.7.18

三、主从复制实施细节

1.主服务器上为服务器设置一个连接账户并授予REPLICATION SLAVE权限。

GRANT REPLICATION SLAVE ON *.* TO 'repl'@'%' IDENTIFIED BY 'repl@20170509';

2.修改master配置文件my.cnf

server-id = 245
log_bin = /data/mysqllog/3306/bin_log/binlog

这两个值必须设置,设置好之后,重启MySQL。

3.备份master上一份完整的数据

mysqldump -uroot -p'密码' --master-data=2 --single-transaction -R --triggers -A > /backup/all.sql

说明:

--master-data=2代表备份时刻记录master的Binlog位置和Position
--single-transaction意思是获取一致性快照
-R意思是备份存储过程和函数
--triggres的意思是备份触发器
-A代表备份所有的库

4.查看主库备份时的binlog名称和位置

SHOW MASTER STATUS;
mysql> SHOW MASTER STATUS;
+---------------+----------+--------------+------------------+-------------------+
| File     | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+---------------+----------+--------------+------------------+-------------------+
| binlog.000004 | 79394496 |       |         |          |
+---------------+----------+--------------+------------------+-------------------+

或者到刚才备份的数据库文件中看:

vi all.sql

5.修改slave库配置文件my.cnf

server-id = 247 (唯一,不能与主库一样,一般设为服务器IP后3位)
log_bin = /data/mysql/logdir/3306/bin_log/binlog
innodb_file_per_table = ON
skip_name_resolve = ON
relay_log = /data/mysql/logdir/3306/relay_log/relay.log
binlog-format = row
log-slave-updates = true

read_only=ON   (只读模式)

设置完之后,重启MySQL。

6.在slave服务器上恢复master备份

mysql -u root -p'密码' < all.sql

7.停止从库,并配置主从参数,打开从库。

mysql> stop slave; #暂停从库
mysql>CHANGE MASTER TO MASTER_HOST='172.16.115.245',MASTER_USER='repl', MASTER_PASSWORD='repl@20170509',MASTER_LOG_FILE='binlog.000004',MASTER_LOG_POS=154;
mysql> start slave; #启动复制
mysql> show slave status\G
*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: 172.16.115.245
Master_User: repl
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: binlog.000004
Read_Master_Log_Pos: 104634190
Relay_Log_File: relay.000003
Relay_Log_Pos: 104632819
Relay_Master_Log_File: binlog.000004
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: 104634190
Relay_Log_Space: 104634713
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: 245
Master_UUID: 4f545573-3170-11e7-b903-000c29462d8c
Master_Info_File: /data/mysql/datadir/3306/data/master.info
SQL_Delay: 0
SQL_Remaining_Delay: NULL
Slave_SQL_Running_State: Slave has read all relay log; waiting for more updates
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
Replicate_Rewrite_DB:
Channel_Name:
Master_TLS_Version:

8.查看master、slave相关进程

master Binlog Dump线程:

mysql> SHOW PROCESSLIST \G
*************************** 1. row ***************************
Id: 13
User: repl
Host: 172.16.115.247:44602
db: NULL
Command: Binlog Dump
Time: 76514
State: Master has sent all binlog to slave; waiting for more updates
Info: NULL

slave IO/SQL线程:

mysql> SHOW PROCESSLIST \G
*************************** 1. row ***************************
Id: 10
User: system user
Host:
db: NULL
Command: Connect
Time: 81148
State: Waiting for master to send event
Info: NULL
*************************** 2. row ***************************
Id: 12
User: system user
Host:
db: NULL
Command: Connect
Time: 5
State: Reading event from the relay log
Info: NULL

9.至此,主从配置已经完成,可以到master服务器上创建数据库、表等操作,看slave数据库是否同步!

总结

以上所述是小编给大家介绍的MySQL5.7.18主从复制搭建(一主一从)教程详解,希望对大家有所帮助,如果大家有任何疑问欢迎给我留言,小编会及时回复大家的!

您可能感兴趣的文章:

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息