MySQL数据库的主从同步复制配置

时间:2022-09-21 18:51:38

一、主从同步机制原理

  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、可以在从数据库上进行日常备份

三、主从同步复制过程

MySQL数据库的主从同步复制配置

  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=
log-bin=mysql-bin

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

mysql> grant replication slave on *.* to 'repl'@'%' identified by '';
Query OK, rows affected (0.01 sec)
mysql> flush privileges;
Query OK, rows affected (0.01 sec)

  3、查询master的状态

mysql> show master status;
+------------------+----------+--------------+------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB |
+------------------+----------+--------------+------------------+
| mysql-bin. | | | |
+------------------+----------+--------------+------------------+
row in set (0.00 sec)

  从数据库slave修改:

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

   服务器:192.168.2.102

server-id=
log-bin=mysql-bin

  服务器:192.168.2.103

server-id=
log-bin=mysql-bin

  2、执行同步命令

mysql> change master to master_host='10.0.30.131',master_user='repl',master_password='',master_log_file='mysql-bin.000001',master_log_pos=;
Query OK, rows affected, warnings (0.03 sec)
mysql> start slave;
Query OK, rows affected (0.00 sec)

  3、查看从数据库的状态

mysql> show slave status\G
*************************** . row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: 10.0.30.131
Master_User: repl
Master_Port:
Connect_Retry:
Master_Log_File: mysql-bin.
Read_Master_Log_Pos:
Relay_Log_File: localhost-relay-bin.
Relay_Log_Pos:
Relay_Master_Log_File: mysql-bin.
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:
Last_Error:
Skip_Counter:
Exec_Master_Log_Pos:
Relay_Log_Space:
Until_Condition: None
Until_Log_File:
Until_Log_Pos:
Master_SSL_Allowed: No
Master_SSL_CA_File:
Master_SSL_CA_Path:
Master_SSL_Cert:
Master_SSL_Cipher:
Master_SSL_Key:
Seconds_Behind_Master:
Master_SSL_Verify_Server_Cert: No
Last_IO_Errno:
Last_IO_Error:
Last_SQL_Errno:
Last_SQL_Error:
Replicate_Ignore_Server_Ids:
Master_Server_Id:
Master_UUID:
Master_Info_File: /usr/local/mysql/data/master.info
SQL_Delay:
SQL_Remaining_Delay: NULL
Slave_SQL_Running_State: Slave has read all relay log; waiting for more updates
Master_Retry_Count:
Master_Bind:
Last_IO_Error_Timestamp:
Last_SQL_Error_Timestamp:
Master_SSL_Crl:
Master_SSL_Crlpath:
Retrieved_Gtid_Set:
Executed_Gtid_Set:
Auto_Position:
Replicate_Rewrite_DB:
Channel_Name:
Master_TLS_Version:
row in set (0.00 sec)

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

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

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