數據庫損壞了(業務不能使用數據庫)
緣由:mysql
解決方案:linux
複製是MySQL的一項功能,容許服務器將更改從一個實例複製到另外一個實例。web
異步複製過程
整體來講,複製有3個步驟:sql
兩臺主機安裝mysql 5.6
注意在兩臺主機的配置文件中/etc/my.cnf中須要添加server-id=
參數。shell
建立複製用戶數據庫
grant replication slave on *.* to repl@'10.0.0.%' identified by '123';
初始化從庫安全
[root@db01 ~]# mysqldump -uroot -poldboy123 -A -F > /tmp/server.sql [root@db01 ~]# scp /tmp/server.sql 10.0.0.8:/tmp 在從庫中source執行
開啓主從複製服務器
mysql> mysql> show master; +----------------+----------+--------------+------------------+-------------------+ | File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set | +----------------+----------+--------------+------------------+-------------------+ | log-bin.000013 | 120 | | | | +----------------+----------+--------------+------------------+-------------------+ 1 row in set (0.00 sec)
mysql> change master to master_host='10.0.0.51', master_port=3306, master_user='repl', master_password='123', master_log_file='log-bin.000013', master_log_pos=120;
注意:也能夠設置relay-bin的名稱。網絡
檢查狀態架構
start slave; show slave status\G
成功的標誌是:
Slave_IO_Running: Yes Slave_SQL_Running: Yes
mysql> show slave status\G *************************** 1. row *************************** Slave_IO_State: Waiting for master to send event Master_Host: 10.0.0.51 Master_User: repl Master_Port: 3306 Connect_Retry: 60 Master_Log_File: log-bin.000014 Read_Master_Log_Pos: 120 Relay_Log_File: web01-relay-bin.000002 Relay_Log_Pos: 281 Relay_Master_Log_File: log-bin.000014 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: 0 Last_Error: Skip_Counter: 0 Exec_Master_Log_Pos: 120 Relay_Log_Space: 454 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: 0 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: 1 Master_UUID: d4519488-d005-11e7-a4ac-000c2924dc94 Master_Info_File: /application/mysql-5.6.38-linux-glibc2.12-x86_64/data/master.info SQL_Delay: 0 SQL_Remaining_Delay: NULL Slave_SQL_Running_State: Slave has read all relay log; waiting for the slave I/O thread to update it 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 1 row in set (0.00 sec)
Slave_*_Running:
主服務器日誌座標:
中繼日誌座標:
Last_IO_Error、Last_SQL_Error:
+分別致使I/O 線程或SQL 線程中止的最新錯誤的錯誤消息。在正常複製過程當中,這些字段是空的。若是發生錯誤並致使消息顯示在以上任一字段中,則錯誤值也顯示在錯誤日誌中。
Last_IO_Errno、Last_SQL_Errno:
從庫binlog落後於主庫
Master_Log_File: log-bin.000014 Read_Master_Log_Pos: 120
從庫的logbin比主庫的logbin慢的緣由:
【擴展】
延時節點概念:是SQL線程延時,不是IO線程延時。
解決方法:
stop slave; set global sql_slave_skip_counter = 1; start slave;
也能夠在配置文件中跳過錯誤號碼:
[mysqld] slave-skip-errors = 1032,1062,1007
stop slave; #<==臨時中止同步開關。 set global sql_slave_skip_counter= 1 ; #<==將同步指針向下移動一個,若是屢次不一樣步,能夠重複操做。 start slave; /etc/my.cnf slave-skip-errors = 1032,1062,1007
備份
問題:
若是從庫只是做爲備份服務器使用,那麼主庫的壓力會增長,由於全部的業務都在主庫進行讀寫(dump線程讀取併發送給binlog)
解決方法:
性能
相似於一主一從的部署
不一樣之處在於主從之間多了一箇中間服務器
[mysqld] basedir = /application/mysql/ datadir = /application/mysql/data/ socket = /application/mysql/tmp/mysql.sock character_set_server=utf8 sql_mode=NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES server-id = 2 log-bin=/tmp/log-bin binlog-format=row autocommit=1 log-slave-updates [client] socket = /application/mysql/tmp/mysql.sock
在中間服務器的my.cnf文件中須要開啓binlog並添加log-slave-updates
參數,表示強制刷新binlog,不然binlog日誌不會刷新。
至關於作了兩套主從。
reset slave;
重置slave(關閉狀態)
複製延時是在SQL線程的層面進行控制,不容許SQL線程實時的執行relay log中的操做。
stop slave;
change master to master_delay = 30; #單位是秒
start slave;
結果: ```shell mysql> show slave status\G SQL_Delay: 30 SQL_Remaining_Delay: NULL
生產場景中通常延時3-6小時
部署:
一、加載插件
主:
INSTALL PLUGIN rpl_semi_sync_master SONAME 'semisync_master.so';
從:
INSTALL PLUGIN rpl_semi_sync_slave SONAME 'semisync_slave.so';
二、查看是否加載成功:
show plugins;
三、啓動:
主:
SET GLOBAL rpl_semi_sync_master_enabled = 1;
從:
SET GLOBAL rpl_semi_sync_slave_enabled = 1;
只是臨時啓動,須要寫入配置文件中。
四、重啓從庫上的IO線程
STOP SLAVE IO_THREAD; START SLAVE IO_THREAD;
五、查看是否在運行
主:
show status like 'Rpl_semi_sync_master_status';
從:
show status like 'Rpl_semi_sync_slave_status';
MHA設計理念:
主服務器宕掉了,可是多臺從服務器的數據和主服務器同步不完整,這時就須要整合多臺從服務器中的同步的數據到新的主服務器中,儘可能保證數據的完整性。
中繼日誌(relay log):記錄了events和position號
在執行的事務中打上一個惟一標籤,這樣就能夠保證事務之間的連續性及惟一性
爲了failover出現的更好的複製,5.6出現,5.7完善
GTID(Global Transaction ID)是對於一個已提交事務的編號,而且是一個全局惟一的編號。
它的官方定義以下:
GTID = source_id :transaction_id
7E11FA47-31CA-19E1-9E56-C43AA21293967:29
[root@web01 ~]# cat /application/mysql/data/auto.cnf [auto] server-uuid=0b920fba-d0fa-11e7-aae4-000c292741de
注意:若是是克隆的mysql數據庫,那麼server-uuid相同會致使slave-IO沒法啓動,須要修改server-uuid
一、修改配置文件
主:
[mysqld] log_bin = /tmp/log-bin binlog-format = row basedir = /application/mysql/ datadir = /application/mysql/data socket = /application/mysql/tmp/mysql.sock server_id = 1 gtid-mode = on #啓用gtid類型,不然就是普通的複製架構 enforce-gtid-consistency = true #強制GTID的一致性 log-slave-updates = 1 #slave更新是否記入日誌 autocommit = 1 sql_mode=NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES character_set_server=utf8 [client] socket = /application/mysql/tmp/mysql.sock
從:
[mysqld] log_bin = /tmp/log-bin binlog-format=ROW basedir = /application/mysql/ datadir = /application/mysql/data/ server_id = 2 socket = /application/mysql/tmp/mysql.sock gtid-mode = on enforce-gtid-consistency = true log_slave_updates = 1 sql_mode=NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES character_set_server=utf8 autocommit = 1 [client] socket = /application/mysql/tmp/mysql.sock
注意:若是是新建的數據庫能夠不須要從庫初始化;若是不是須要從庫初始化,同步主從的結構屬性
二、在主服務器添加複製用戶
grant replication slave on *.* to repl@'10.0.0.%' identified by '123';
三、在從服務器上設置change master
mysql> change master to master_host='10.0.0.51', master_port=3306, master_user='repl', master_password='123', master_auto_position=1;
四、開啓slave
start slave;
五、查看效果
在主庫中添加一個數據,查看master
mysql> show master status; +----------------+----------+--------------+------------------+------------------------------------------+ | File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set | +----------------+----------+--------------+------------------+------------------------------------------+ | log-bin.000003 | 552 | | | ff185ff4-cec5-11e7-9c86-000c2924dc94:1-2 | +----------------+----------+--------------+------------------+------------------------------------------+ 1 row in set (0.00 sec)
再從庫中查看slave:
mysql> show slave status\G *************************** 1. row *************************** Slave_IO_State: Waiting for master to send event Master_Host: 10.0.0.51 Master_User: repl Master_Port: 3306 Connect_Retry: 60 Master_Log_File: log-bin.000003 Read_Master_Log_Pos: 552 Relay_Log_File: db02-relay-bin.000004 Relay_Log_Pos: 442 Relay_Master_Log_File: log-bin.000003 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: 0 Last_Error: Skip_Counter: 0 Exec_Master_Log_Pos: 552 Relay_Log_Space: 1252 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: 0 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: 1 Master_UUID: ff185ff4-cec5-11e7-9c86-000c2924dc94 Master_Info_File: /application/mysql-5.6.38/data/master.info SQL_Delay: 0 SQL_Remaining_Delay: NULL Slave_SQL_Running_State: Slave has read all relay log; waiting for the slave I/O thread to update it Master_Retry_Count: 86400 Master_Bind: Last_IO_Error_Timestamp: Last_SQL_Error_Timestamp: Master_SSL_Crl: Master_SSL_Crlpath: Retrieved_Gtid_Set: ff185ff4-cec5-11e7-9c86-000c2924dc94:1-2 Executed_Gtid_Set: ff185ff4-cec5-11e7-9c86-000c2924dc94:1-2 Auto_Position: 1 1 row in set (0.00 sec)