MYSQL主从复制
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了MYSQL主从复制相关的知识,希望对你有一定的参考价值。
为什么做主从复制?
主从复制可以使mysql数据库主服务器的主数据库,复制到一个或多个MySQL从服务器从数据库,能够实现实时灾备,用于故障灾备。
复制过程
master将改变记录到二进制日志(binary log)中(这些记录叫做二进制日志事件,binary log events)
slave将master的binary log events拷贝到它的中继日志(relay log) -- (首先,slave开始一个工作线程——I/O线程。I/O线程在master上打开一个普通的连接,然后开始binlog dump process,Binlog dump process从master的二进制日志中读取事件,如果已经跟上master,它会睡眠并等待master产生新的事件。I/O线程将这些事件写入中继日志)
slave重做中继日志中的事件,将改变反映它自己的数据。
主机环境
Master:192.168.174.132
Slave:192.168.174.133
全部关闭防火墙
Master创建复制账号
grant replication slave on *.* to [email protected]'%' identified by '[email protected]';
拷贝数据
假如两台·MYSQL为新安装则不需要,不同步时则关停Master服务器,将Master中的数据拷贝到Slave服务器中,使得Master和Slave中的数据同步,并且确保在全部设置操作结束前,禁止在Master和slave服务器中进行写操作,使得两数据库中的数据一定要相同!
配置Master
server-id=1 指定唯一server-id log-bin=mysql-bin 打开二进制日志
配置Slave
server_id = 2 log-bin=mysql-bin log_slave_updates=1 将复制时间写进自己的二进制日志 relay_log=mysql-relay-bin read_only=1
Master状态检查
show master status; +------------------+----------+--------------+------------------+-------------------+ | File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set | +------------------+----------+--------------+------------------+-------------------+ | mysql-bin.000002 | 154 | | | | +------------------+----------+--------------+------------------+-------------------+ 1 row in set (0.04 sec)
Slave连接Master
change master to master_host='192.168.174.132', master_user='backup', master_password='[email protected]', master_log_file='mysql-bin.000002', master_log_pos=154;
启动从服务器复制线程
start slave;
查看状态
show slave status\G; *************************** 1. row *************************** Slave_IO_State: Waiting for master to send event Master_Host: 192.168.174.132 Master_User: backup Master_Port: 3306 Connect_Retry: 60 Master_Log_File: mysql-bin.000002 Read_Master_Log_Pos: 154 Relay_Log_File: mysql-relay-bin.000002 Relay_Log_Pos: 320 Relay_Master_Log_File: mysql-bin.000002 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: 154 Relay_Log_Space: 527 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: 8682459f-6fe1-11e6-861a-000c29a9d958 Master_Info_File: /usr/local/mysql/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: 1 row in set (0.00 sec) ERROR: No query specified
Slave_IO_Running: Yes Slave_SQL_Running: Yes
全为Yes即为成功
这时在Master上写入任何数据,在slave上都可以查看到。
以上是关于MYSQL主从复制的主要内容,如果未能解决你的问题,请参考以下文章