MySQL主从复制

it2022-05-05  117

主从同步使得数据可以从一个数据库服务器复制到其他服务器上,在复制数据时,一个服务器充当主服务器(master),其余的服务器充当从服务器(slave)。因为复制是异步进行的,所以从服务器不需要一直连接着主服务器,从服务器甚至可以通过拨号断断续续地连接主服务器。通过配置文件,可以指定复制所有的数据库,某个数据库,甚至是某个数据库上的某个表。

使用主从同步的好处:

1、通过增加从服务器来提高数据库的性能,在主服务器上执行写入和更新,在从服务器上向外提供读功能,可以动态地调整从服务器的数量,从而调整整个数据库的性能。 2、提高数据安全-因为数据已复制到从服务器,从服务器可以终止复制进程,所以,可以在从服务器上备份而不破坏主服务器相应数据 3、在主服务器上生成实时数据,而在从服务器上分析这些数据,从而提高主服务器的性能

主从同步机制:

Mysql服务器之间的主从同步是基于二进制日志机制,主服务器使用二进制日志来记录数据库的变动情况,从服务器通过读取和执行该日志文件来保持和主服务器的数据一致。

在使用二进制日志时,主服务器的所有操作都会被记录下来,然后从服务器会接收到该日志的一个副本。从服务器可以指定执行该日志中的哪一类事件(譬如只插入数据或者只更新数据),默认会执行日志中的所有语句。

每一个从服务器会记录关于二进制日志的信息:文件名和已经处理过的语句,这样意味着不同的从服务器可以分别执行同一个二进制日志的不同部分,并且从服务器可以随时连接或者中断和服务器的连接。

主服务器和每一个从服务器都必须配置一个唯一的ID号(在my.cnf文件的[mysqld]模块下有一个server-id配置项),另外,每一个从服务器还需要通过CHANGE MASTER TO语句来配置它要连接的主服务器的ip地址,日志文件名称和该日志里面的位置(这些信息存储在主服务器的数据库里)

mysql 主从同步原理:

master 将操作记录到二进制日志(binary log)中;slave IO 线程 将master的binary log events读写到它的中继日志(relay log);slave SQL进程读取中继日志,将重做记录数据到数据库中。

" 配置主从同步

有很多种配置主从同步的方法,可以总结为如下的步骤: 1.在主服务器上,必须开启二进制日志机制和配置一个独立的ID 2.在每一个从服务器上,配置一个唯一的ID,创建一个用来专门复制主服务器数据的账号 3.在开始复制进程前,在主服务器上记录二进制文件的位置信息 4.如果在开始复制之前,数据库中已经有数据,就必须先创建一个数据快照(可以使用mysqldump导出数据库,或者直接复制数据文件) 5.配置从服务器要连接的主服务器的IP地址和登陆授权,二进制日志文件名和位置

配置:

================= 在两个完全一致的环境下==============

1、对两台服务器环境进行初始化。

同步时间: yum -y install ntp ntpdate ntpdate cn.pool.ntp.org hwclock --systohc 关闭防火墙 systemctl stop firewalld systemctl disable firewalld setenforce 0

在主库(master)上操作:

2、更改主库配置文件,在/etc/my.cnf文件中添加log-bin和server-id

[mysqld] log-bin=mysql-bin server-id=1 log-bin-index=master-bin.index 重启服务: [root@localhost ~]# systemctl stop mysql [root@localhost ~]# systemctl start mysql

3、创建用户,每一个从服务器都需要用到一个账户名和密码来连接主服务器,可以为每一个从服务器都创建一个账户,也可以让全部服务器使用同一个账户。

mysql> create user 'copy'@'%' identified with mysql_native_password by 'Admin@123'; mysql> grant replication slave on *.* to 'copy'@'%';

4、刷新授权表信息

mysql> flush privileges; 获取主节点当前binary log文件名和位置(position) mysql> show master status; +------------------+----------+--------------+------------------+-------------------+ | File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set | +------------------+----------+--------------+------------------+-------------------+ | mysql-bin.000001 | 155 | | | | +------------------+----------+--------------+------------------+-------------------+ 1 row in set (0.00 sec)

在从库(slave)上操作: 5、修改配置文件,必须指定中继日志的名称

[mysqld] server_id=66 relay-log=relay-log relay-log-index=relay-log.index 重启服务 [root@localhost ~]# systemctl stop mysql [root@localhost ~]# systemctl start mysql

6、在从(Slave)节点上设置主节点参数

CHANGE MASTER TO MASTER_HOST='192.168.65.144', MASTER_USER='copy', MASTER_PASSWORD='Admin@123', MASTER_LOG_FILE='mysql-bin.000001', MASTER_LOG_POS=825;

查看主从同步状态 mysql> show slave status\G *************************** 1. row *************************** Slave_IO_State: Master_Host: 192.168.42.175 Master_User: copy Master_Port: 3306 Connect_Retry: 60 Master_Log_File: mysql-bin.000001 Read_Master_Log_Pos: 155 Relay_Log_File: relay-log.000001 Relay_Log_Pos: 4 Relay_Master_Log_File: mysql-bin.000001 Slave_IO_Running: No Slave_SQL_Running: No 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: 155 Relay_Log_Space: 155 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: NULL 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: 0 Master_UUID: Master_Info_File: mysql.slave_master_info SQL_Delay: 0 SQL_Remaining_Delay: NULL Slave_SQL_Running_State: 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: Master_public_key_path: Get_master_public_key: 0 1 row in set (0.00 sec)

7、开启主从同步

start slave; 再次查看主从同步状态 mysql> show slave status\G *************************** 1. row *************************** Slave_IO_State: Master_Host: 192.168.42.175 Master_User: copy Master_Port: 3306 Connect_Retry: 60 Master_Log_File: mysql-bin.000001 Read_Master_Log_Pos: 155 Relay_Log_File: relay-log.000001 Relay_Log_Pos: 4 Relay_Master_Log_File: mysql-bin.000001 Slave_IO_Running: No 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: 155 Relay_Log_Space: 155 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: NULL Master_SSL_Verify_Server_Cert: No Last_IO_Errno: 13117 Last_IO_Error: Got fatal error 1236 from master when reading data from binary log: 'Could not find first log file name in binary log index file' Last_SQL_Errno: 0 Last_SQL_Error: Replicate_Ignore_Server_Ids: Master_Server_Id: 27 Master_UUID: Master_Info_File: mysql.slave_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: 190721 20:19:32 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: Master_public_key_path: Get_master_public_key: 0 1 row in set (0.00 sec)

排错: 当出现:Got fatal error 1236 from master when reading data from binary log: ‘Could not find first log file name in binary log index file’ 这个错误时 可以

stop slave reset slave; start slave;

根据实际错误来排错

直到出现两个yes Slave_IO_Running: Yes Slave_SQL_Running: Yes

================= 在两个不一致的环境下==============

1、对两台服务器环境进行初始化。

同步时间: yum -y install ntp ntpdate ntpdate cn.pool.ntp.org hwclock --systohc 关闭防火墙 systemctl stop firewalld systemctl disable firewalld setenforce 0

在主库(master)上操作:

2、更改主库配置文件,在/etc/my.cnf文件中添加log-bin和server-id

[mysqld] log-bin=mysql-bin server-id=1 log-bin-index=master-bin.index 重启服务: [root@localhost ~]# systemctl stop mysql [root@localhost ~]# systemctl start mysql

3、创建用户,每一个从服务器都需要用到一个账户名和密码来连接主服务器,可以为每一个从服务器都创建一个账户,也可以让全部服务器使用同一个账户。

mysql> create user 'copy'@'%' identified with mysql_native_password by 'Admin@123'; mysql> grant replication slave on *.* to 'copy'@'%';

4、刷新授权表信息

mysql> flush privileges;

5、获取二进制日志的信息并导出数据库

首先登陆数据库,然后刷新所有的表,同时给数据库加上一把锁,阻止对数据库进行任何的写操作 flush tables with read lock; 获取二进制日志的信息 show master status; 查看 二进制日志情况: show variables like '%log_bin%'; 锁表之后,再导出数据库里的数据 导出某一个数据库 mysqldump -u root -p test -F > /tmp/test_backup_`date +%Y_%m_%d_%H_%M_%S`.sql 导出所有数据库 mysqldump -uroot -pAdmin@123 -A -B |gzip > /date/mysql_alldb_$(date +%F).sql.gz 然后将数据库文件传到从服务器中 scp 要传的文件 192.168.91.132:目标目录 这时可以对数据库解锁,恢复对主数据库的操作 unlock tables;

6、在从(Slave)节点上设置主节点参数

CHANGE MASTER TO MASTER_HOST='192.168.65.144', MASTER_USER='copy', MASTER_PASSWORD='Admin@123', MASTER_LOG_FILE='mysql-bin.000001', MASTER_LOG_POS=825;

**导入刚才传过来的数据库

导入指定数据库** mysql -u root -p test< /tmp/test_backup_2015_10_18_23_33_30.sql **不指定** mysql -uroot -pAdmin@123 <mysql_alldb_2019-04-19.sql

7、启动主从同步进程

start slave;

8、检查状态

注意:在主从库维护中,有时候需要跳过某个无法执行的命令,需要在slave处于stop状态下,执行 set global sql_slave_skip_counter=N以跳过命令。常用的且不易用错的是N=1的情况,但N>1时,则不那么顾名思义,本文详细介绍N的意义,及使用注意事项。 set GLOBAL sql_slave_skip_counter=1;


最新回复(0)