MySQL 主从复制是一个异步的复制过程,主库发送更新事件到从库,从库读取更新记录,并执行更新记录,使得从库的内容与主库保持一致。每一个主从复制的连接,都有三个线程。拥有多个从库的主库为每一个连接到主库的从库创建一个 log dump 输出线程,每一个从库都有它自己的 I/O 线程和 SQL 线程。
步骤:
1.主库会将所有的更新记录保存到 Binarylog 文件。
2.每当有从库连接到主库的时候,主库都会创建一个 log dump 线程发送 Binarylog 文件到从库。
3.当从库复制开始的时候,从库就会创建两个线程进行处理,一个 I/O 线程,一个 SQL 线程。
4.I/O 线程去请求主库的 Binarylog文件,并将得到的 Binarylog 文件写到 Relaylog 文件中。
5.SQL 线程会读取 Relaylog 文件中的日志,并解析成具体操作,来实现主从的操作一致,而最终数据一致。
配置:
Master 配置文件:
port = 3307
server_id =1
log-bin=mysql-log-bin
binlog-do-db=test
第二步 授权
mysql> grant replication slave on *.* to [email protected] identified by '123456' ;
mysql> flush privileges;
mysql> show master status;
+----------------------+----------+--------------+------------------+-------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+----------------------+----------+--------------+------------------+-------------------+
| mysql-log-bin.000003 | 410 | test | | |
+----------------------+----------+--------------+------------------+-------------------+
1 row in set (0.00 sec)
Slave 配置文件:
# For advice on how to change settings please see
# http://dev.mysql.com/doc/refman/5.6/en/server-configuration-defaults.html
# *** DO NOT EDIT THIS FILE. It's a template which will be copied to the
# *** default location during install, and will be replaced if you
# *** upgrade to a newer version of MySQL.
[client]
no-beep
# pipe
# socket=mysql
port=3308
[mysqld]
# Remove leading # and set to the amount of RAM for the most important data
# cache in MySQL. Start at 70% of total RAM for dedicated server, else 10%.
# innodb_buffer_pool_size = 128M
# Remove leading # to turn on a very important data integrity option: logging
# changes to the binary log between backups.
# log_bin
# These are commonly set, remove the # and set as required.
basedir = E:\mysql5.6.39\mysql-slave
datadir = E:\mysql5.6.39\mysql-slave\data
port = 3308
server_id =2
replicate-do-db=test
# Remove leading # to set options mainly useful for reporting servers.
# The server defaults are faster for transactions and fast SELECTs.
# Adjust sizes as needed, experiment to find the optimal values.
# join_buffer_size = 128M
# sort_buffer_size = 2M
# read_rnd_buffer_size = 2M
sql_mode=NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES
第二步:
mysql> change master to master_host='192.168.1.103',master_user='slave',master_password='123456', master_log_file='mysql-log-bin.000003',master_log_pos=527,master
mysql> start slave;
mysql> show slave status \G;
*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: 192.168.1.103
Master_User: slave
Master_Port: 3307
Connect_Retry: 60
Master_Log_File: mysql-log-bin.000003
Read_Master_Log_Pos: 527
Relay_Log_File: CGH9JTCMLXWEA3L-relay-bin.000002
Relay_Log_Pos: 287
Relay_Master_Log_File: mysql-log-bin.000003
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
Replicate_Do_DB: test
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: 527
Relay_Log_Space: 470
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: 1
Master_UUID: 2f27782e-05a2-11e8-8243-1c1b0d4a6010
Master_Info_File: E:\mysql5.6.39\mysql-slave\data\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