Mysql主從複製筆記

    寫這個筆記其目的還是記錄下自己之前做的細節,原以爲弄過的東西會深深的印入腦海無需囉裏囉嗦的記錄,今天回憶的時候發現我已經不記得是從何入手了想想就覺得可怕還是老老實實把以前的備份實操記錄下來把。

    主從複製常見的用途:數據分佈,負載均衡,備份,高可用性和故障切換,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;

 

 

 

 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章