mysql强同步复制怎么配置_MySQL数据库的主从同步复制配置

一、主从同步机制原理

MYSQL主从同步是在MySQL主从复制(Master-Slave Replication)基础上实现的,通过设置在Master MySQL上的binlog(使其处于打开状态),Slave MySQL上通过一个I/O线程从Master MySQL上读取binlog,然后传输到Slave MySQL的中继日志中,然后Slave MySQL的SQL线程从中继日志中读取中继日志,然后应用到Slave MySQL的数据库中。这样实现了主从数据同步功能。

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

二、主从同步复制的作用

1、主数据库出现问题,可以切换到从数据库。

2、可以进行数据库层面的读写分离,

3、可以在从数据库上进行日常备份

三、主从同步复制过程

52020117d97f55055af1779fd312f75c.png

Binary log:主数据库的二进制日志

Relay log:从服务器的中继日志

第一步:master在每个事务更新数据完成之前,将该操作记录串行地写入到binlog文件中。

第二步:salve开启一个I/O Thread,该线程在master打开一个普通连接,主要工作是binlog dump process。如果读取的进度已经跟上了master,就进入睡眠状态并等待master产生新的事件。I/O线程最终的目的是将这些事件写入到中继日志中。

第三步:SQL Thread会读取中继日志,并顺序执行该日志中的SQL事件,从而与主数据库中的数据保持一致。

四、服务器准备

Master(主)            IP:10.0.30. 131  server_id:131

Slave(从)             IP:192.168.2.102  server_id:102

Slave(从)             IP:192.168.2.103  server_id:103

五、主从同步复制具体操作

主数据库master修改:

1、修改主服务器数据库配置文件

server_id=131log-bin=mysql-bin

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

mysql> grant replication slave on *.* to 'repl'@'%' identified by '123456';

Query OK,0 rows affected (0.01sec)

mysql>flush privileges;

Query OK,0 rows affected (0.01 sec)

3、查询master的状态

mysql>show master status;+------------------+----------+--------------+------------------+

| File | Position | Binlog_Do_DB | Binlog_Ignore_DB |

+------------------+----------+--------------+------------------+

| mysql-bin.000001 | 107 | | |

+------------------+----------+--------------+------------------+

1 row in set (0.00 sec)

从数据库slave修改:

1、修改从数据库配置文件

服务器:192.168.2.102

server-id=102log-bin=mysql-bin

服务器:192.168.2.103

server-id=103log-bin=mysql-bin

2、执行同步命令

mysql> change master to master_host='10.0.30.131',master_user='repl',master_password='123456',master_log_file='mysql-bin.000001',master_log_pos=0;

Query OK,0 rows affected, 2 warnings (0.03sec)

mysql>start slave;

Query OK,0 rows affected (0.00 sec)

3、查看从数据库的状态

mysql>show slave status\G*************************** 1. row ***************************Slave_IO_State: Waitingfor master to send eventMaster_Host:10.0.30.131Master_User: repl

Master_Port:3306Connect_Retry:60Master_Log_File: mysql-bin.000001Read_Master_Log_Pos:107Relay_Log_File: localhost-relay-bin.000002Relay_Log_Pos:304Relay_Master_Log_File: mysql-bin.000001Slave_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:0Last_Error:

Skip_Counter:0Exec_Master_Log_Pos:107Relay_Log_Space:515Until_Condition: None

Until_Log_File:

Until_Log_Pos:0Master_SSL_Allowed: No

Master_SSL_CA_File:

Master_SSL_CA_Path:

Master_SSL_Cert:

Master_SSL_Cipher:

Master_SSL_Key:

Seconds_Behind_Master:0Master_SSL_Verify_Server_Cert: No

Last_IO_Errno:0Last_IO_Error:

Last_SQL_Errno:0Last_SQL_Error:

Replicate_Ignore_Server_Ids:

Master_Server_Id:131Master_UUID:

Master_Info_File:/usr/local/mysql/data/master.info

SQL_Delay:0SQL_Remaining_Delay: NULL

Slave_SQL_Running_State: Slave has read all relay log; waitingformore updates

Master_Retry_Count:86400Master_Bind:

Last_IO_Error_Timestamp:

Last_SQL_Error_Timestamp:

Master_SSL_Crl:

Master_SSL_Crlpath:

Retrieved_Gtid_Set:

Executed_Gtid_Set:

Auto_Position:0Replicate_Rewrite_DB:

Channel_Name:

Master_TLS_Version:1 row in set (0.00 sec)

注:Slave_IO_Running及Slave_SQL_Running进程必须正常运行,即YES状态,否则说明同步失败。

如果都是正确的,则说明配置成功!

以上就是我们给大家整理的关于MySQL数据库的主从同步复制配置的全部内容,希望能够帮助到你。

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值