Mysql主从复制笔记

原创
02/23 16:23
阅读数 98

    写这个笔记其目的还是记录下自己之前做的细节,原以为弄过的东西会深深的印入脑海无需啰里啰嗦的记录,今天回忆的时候发现我已经不记得是从何入手了想想就觉得可怕还是老老实实把以前的备份实操记录下来把。

    主从复制常见的用途:数据分布,负载均衡,备份,高可用性和故障切换,mysql升级。


注:
    1.MySql支持两种复制方式:基本航的复制和基于语句的复制。
    2.MySql复制大部分是向后兼容,新版本服务器可以作为老版本服务器的备库,但是反过来,将老版本作为新版本的备库通常情况下是不行的。因为老版本的库可能无法解析新版本所采用的新特性或语法。所使用的二进制文件格式也可能不相同。
    3.复制通常不会增加主库开销,主要是启用二进制曰志带来的开销。当备库读取曰志文件时候,可能会造成更高的I/O开销,另外锁的竞争也可能阻碍事务的提交。(5000或更高TPS主库复制到多个备库,唤醒多个线程发送事件开销将会累加)
    4.MySql 4.0之前的主从复制跟之后版本变化很大。例如mysql 复制功能并没有使用中继曰志,复制只用到2个线程。二不是现在三个线程。
    5.主备份配置 sync_binlog=1 如果开启此项Mysql每次会在提交事务之前会将二进制文件同步到磁盘上,保证服务器崩溃时不会丢失数据事件,如果禁用服务器崩溃会损坏或者丢失信息,建议在不是主备份上该项可以不设置减少性能开销
    6.在使用InnoDB推荐设置 innodb_flush_logs_at_trx_commit innodb_support_xa=1 innodb_safe_binlog

语句复制(逻辑复制)在MySql 3.23版本就存在。行的复制通过主库上记录二进制曰志,在备库重放曰志方式实现异步数据复制。(同一时间备库上的数据可能与主库上的数据存在不一致问题,无法保证主备之间的延迟)。
在设计这块通常读操作指向备库,写操作指向主库,否则并不适合通过复制来扩展写操作。所以主库多备架构中,写操作会被多次执行,这个时候整个系统性能取决写入最慢的那个部分。
缺点就是:1台服务数据N台数据备库。那么这N台服务器都存在相同数据,所以这个不是一种经济的方式。

主从工作原理:
    1.主库将数据更改记录二进制曰志(Binary Log)
    2.备库将主库的曰志复制到自己的中继曰志(Relay Log)
    3.备库读取中继曰志中的事件,将其重放到备库数据上。

实际演练:
    1.创建复制账号

    2.开启主服务器binLog

在my.conf 中添加  log_bin = mysql-bin   server_id = 10 server_id可以由自己来决定取值,但是id是服务器唯一ID。修改完毕重启mysql输入

mysql> show master status;
+------------------+----------+--------------+------------------+-------------------+
| File             | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+------------------+----------+--------------+------------------+-------------------+
| mysql-bin.000001 |      154 |              |                  |                   |
+------------------+----------+--------------+------------------+-------------------+
1 row in set (0.01 sec)


备库也需要在my.conf中增加类似配置
log_bin = mysql-bin
server_id=2
#中继曰志路径
relay_log = D:\mysql-5.7.32-winx64\realay_log
#允许备库将其重放的事件记录也到自身的二进制曰志中
log_slave_updates = 1
#阻止任何没有特权的线程修改数据(所以最好不要给与用户超过需要的权限)
read_only = 1

3.启动复制
这一步告诉备库如何连接到主库并重放其二进制曰志
CHANGE MASTER TO MASTER_HOST='192.168.200.221', MASTER_USER='root', MASTER_PASSWORD='xxxx' ,MASTER_LOG_FILE='mysql-bin.000001', MASTER_LOG_POS=0;

# MASTER_LOG_POS=0 因为曰志要从头开始执行

mysql> show slave status;

+----------------+-----------------+-------------+-------------+---------------+------------------+---------------------+----------------+---------------+-----------------------+------------------+-------------------+-----------------+---------------------+--------------------+------------------------+-------------------------+-----------------------------+------------+------------+--------------+---------------------+-----------------+-----------------+----------------+---------------+--------------------+--------------------+--------------------+-----------------+-------------------+----------------+-----------------------+-------------------------------+---------------+---------------+----------------+----------------+-----------------------------+------------------+-------------+-----------------------------------------+-----------+---------------------+-------------------------+--------------------+-------------+-------------------------+--------------------------+----------------+--------------------+--------------------+-------------------+---------------+----------------------+--------------+--------------------+
| Slave_IO_State | Master_Host     | Master_User | Master_Port | Connect_Retry | Master_Log_File  | Read_Master_Log_Pos | Relay_Log_File | Relay_Log_Pos | Relay_Master_Log_File | Slave_IO_Running | Slave_SQL_Running | 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 | Until_Log_File | Until_Log_Pos | Master_SSL_Allowed | 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 | Last_IO_Errno | Last_IO_Error | Last_SQL_Errno | Last_SQL_Error | Replicate_Ignore_Server_Ids | Master_Server_Id | Master_UUID | Master_Info_File                        | SQL_Delay | SQL_Remaining_Delay | Slave_SQL_Running_State | 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 |
+----------------+-----------------+-------------+-------------+---------------+------------------+---------------------+----------------+---------------+-----------------------+------------------+-------------------+-----------------+---------------------+--------------------+------------------------+-------------------------+-----------------------------+------------+------------+--------------+---------------------+-----------------+-----------------+----------------+---------------+--------------------+--------------------+--------------------+-----------------+-------------------+----------------+-----------------------+-------------------------------+---------------+---------------+----------------+----------------+-----------------------------+------------------+-------------+-----------------------------------------+-----------+---------------------+-------------------------+--------------------+-------------+-------------------------+--------------------------+----------------+--------------------+--------------------+-------------------+---------------+----------------------+--------------+--------------------+
|                | 192.168.200.221 | root        |        3306 |            60 | mysql-bin.000001 |                   4 | mysql-5.7      |             4 | mysql-bin.000001      | No               | No                |                 |                     |                    |                        |                         |                             |          0 |            |            0 |                   4 |             154 | None            |                |             0 | No                 |                    |                    |                 |                   |                | NULL                  | No                            |             0 |               |              0 |                |                             |                0 |             | D:\mysql-5.7.32-winx64\data\master.info |         0 | NULL                |                         |              86400 |             |                         |                          |                |                    |                    |                   |             0 |                      |              |                    |
+----------------+-----------------+-------------+-------------+---------------+------------------+---------------------+----------------+---------------+-----------------------+------------------+-------------------+-----------------+---------------------+--------------------+------------------------+-------------------------+-----------------------------+------------+------------+--------------+---------------------+-----------------+-----------------+----------------+---------------+--------------------+--------------------+--------------------+-----------------+-------------------+----------------+-----------------------+-------------------------------+---------------+---------------+----------------+----------------+-----------------------------+------------------+-------------+-----------------------------------------+-----------+---------------------+-------------------------+--------------------+-------------+-------------------------+--------------------------+----------------+--------------------+--------------------+-------------------+---------------+----------------------+--------------+--------------------+


1 row in set (0.02 sec)
Slave_SQL_Running No

根据报错提醒,得知是主库server1和从库server3的数据不一致造成的


Slave_IO_State, Slave_IO_Running,Slave_SQL_Running 显示当前备库复制尚未运行。Read_Master_Log_Pos 已经是4而不是0 因为0不是曰志真正开始的位置 它仅仅意味 在曰志文件头 MySql第一个事件是从文件第4位开始的(真正的曰志起始位置是98,一旦备库开始连接主库就开始工作,但是现在尚未发生)
开始复制命令
mysql> start slave;

mysql> show slave status;

+----------------------------------+-----------------+-------------+-------------+---------------+------------------+---------------------+----------------+---------------+-----------------------+------------------+-------------------+-----------------+---------------------+--------------------+------------------------+-------------------------+-----------------------------+------------+----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+--------------+---------------------+-----------------+-----------------+----------------+---------------+--------------------+--------------------+--------------------+-----------------+-------------------+----------------+-----------------------+-------------------------------+---------------+---------------+----------------+----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+-----------------------------+------------------+--------------------------------------+-----------------------------------------+-----------+---------------------+-------------------------+--------------------+-------------+-------------------------+--------------------------+----------------+--------------------+--------------------+-------------------+---------------+----------------------+--------------+--------------------+
| Slave_IO_State                   | Master_Host     | Master_User | Master_Port | Connect_Retry | Master_Log_File  | Read_Master_Log_Pos | Relay_Log_File | Relay_Log_Pos | Relay_Master_Log_File | Slave_IO_Running | Slave_SQL_Running | 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 | Until_Log_File | Until_Log_Pos | Master_SSL_Allowed | 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 | Last_IO_Errno | Last_IO_Error | Last_SQL_Errno | Last_SQL_Error                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                             | Replicate_Ignore_Server_Ids | Master_Server_Id | Master_UUID                          | Master_Info_File                        | SQL_Delay | SQL_Remaining_Delay | Slave_SQL_Running_State | 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 |
+----------------------------------+-----------------+-------------+-------------+---------------+------------------+---------------------+----------------+---------------+-----------------------+------------------+-------------------+-----------------+---------------------+--------------------+------------------------+-------------------------+-----------------------------+------------+----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+--------------+---------------------+-----------------+-----------------+----------------+---------------+--------------------+--------------------+--------------------+-----------------+-------------------+----------------+-----------------------+-------------------------------+---------------+---------------+----------------+----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+-----------------------------+------------------+--------------------------------------+-----------------------------------------+-----------+---------------------+-------------------------+--------------------+-------------+-------------------------+--------------------------+----------------+--------------------+--------------------+-------------------+---------------+----------------------+--------------+--------------------+
| Waiting for master to send event | 192.168.200.221 | root        |        3306 |            60 | mysql-bin.000001 |               97295 | mysql-5.7      |           391 | mysql-bin.000001      | Yes              | No                |                 |                     |                    |                        |                         |                             |       1594 | Relay log read failure: Could not parse relay log event entry. The possible reasons are: the master's binary log is corrupted (you can check this by running 'mysqlbinlog' on the binary log), the slave's relay log is corrupted (you can check this by running 'mysqlbinlog' on the relay log), a network problem, or a bug in the master's or slave's MySQL code. If you want to check the master's binary log or slave's relay log, you will be able to know their names by issuing 'SHOW SLAVE STATUS' on this slave. |            0 |                   4 |           97698 | None            |                |             0 | No                 |                    |                    |                 |                   |                | NULL                  | No                            |             0 |               |           1594 | Relay log read failure: Could not parse relay log event entry. The possible reasons are: the master's binary log is corrupted (you can check this by running 'mysqlbinlog' on the binary log), the slave's relay log is corrupted (you can check this by running 'mysqlbinlog' on the relay log), a network problem, or a bug in the master's or slave's MySQL code. If you want to check the master's binary log or slave's relay log, you will be able to know their names by issuing 'SHOW SLAVE STATUS' on this slave. |                             |               10 | 157d36cc-a448-11ec-b865-000c29467e7e | D:\mysql-5.7.32-winx64\data\master.info |         0 | NULL                |                         |              86400 |             |                         | 220316 12:40:12          |                |                    |                    |                   |             0 |                      |              |                    |
+----------------------------------+-----------------+-------------+-------------+---------------+------------------+---------------------+----------------+---------------+-----------------------+------------------+-------------------+-----------------+---------------------+--------------------+------------------------+-------------------------+-----------------------------+------------+----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+--------------+---------------------+-----------------+-----------------+----------------+---------------+--------------------+--------------------+--------------------+-----------------+-------------------+----------------+-----------------------+-------------------------------+---------------+---------------+----------------+----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+-----------------------------+------------------+--------------------------------------+-----------------------------------------+-----------+---------------------+-------------------------+--------------------+-------------+-------------------------+--------------------------+----------------+--------------------+--------------------+-------------------+---------------+----------------------+--------------+--------------------+


问题

Got fatal error 1236 from master when reading data from binary log: 'Could not find first log file n

到master机器登陆mysql:
记录master的bin的位置,例如:mysql> show mster status;

刷新曰志:mysql> flush logs;

因为刷新曰志file的位置会+1,此时binlog 文件发生变化
CHANGE MASTER TO MASTER_HOST='192.168.200.221', MASTER_USER='root', MASTER_PASSWORD='xxxx' ,MASTER_LOG_FILE='新曰志', MASTER_LOG_POS=位置;
在启动start slave;  再看看status 状态

查看binlog
show binary logs;

 

 

 

 

 

展开阅读全文
加载中
点击引领话题📣 发布并加入讨论🔥
打赏
0 评论
0 收藏
0
分享
返回顶部
顶部