---恢復內容開始---mysql
環境:centos6.5 mariadb:10.1.13-MariaDBsql
GTID:GTID是有服務器的UUID和事務序號組成的惟一事務序號 ---UUID:Nvim
做用:1.用來追蹤主從之間的事務傳輸。centos
2.進行多線程複製:master服務器中啓動一個binlog線程來相應slave端的I/O線程。安全
一個I/O線程對主服務器進行mysqldump請求,多個SQL線程對slave中的中繼日誌中的不一樣事務經過不一樣的線程進行應用。服務器
master:192.168.88.147多線程
slave:192.168.88.148socket
1、主從複製:ide
1.master和slave上的配置:vim /etc/my.cnfspa
1 mstaer端: 2 binlog-format=ROW #二進制日誌的格式,有row、statement和mixed幾種類型:須要注意的是:當設置隔離級別爲READ-COMMITED必須設置二進制日誌格式爲ROW,如今MySQL官方認爲STATEMENT這個已經再也不適合繼續使用;但mixed類型在默認的事務隔離級別下,可能會致使主從數據不一致; 3 log-bin=slave-bin 4 #gtid-mode=on #在MariaDB上不須要 5 #enforce-gtid-consistency=true #在MariaDB上不須要 6 log-slave-updates=true 7 master-info-repository=TABLE 8 relay-log-info-repository=TABLE 9 sync-master-info=1 10 slave-parallel-threads=2 #在mysql5.6以上版本須寫爲slave-parallel-workers 11 binlog-checksum=CRC32 12 master-verify-checksum=1 13 slave-sql-verify-checksum=1 14 binlog-rows-query-log_events=1 15 server-id=10 16 report-port=3306 17 port=3306 18 datadir=/mydata/data 19 socket=/tmp/mysql.sock 20 report-host=www.master.com 21 22 23 slave端: 24 binlog-format=ROW 25 log-bin=master-bin 26 log-slave-updates=true 27 master-info-repository=TABLE 28 relay-log-info-repository=TABLE 29 sync-master-info=1 30 slave-parallel-threads=2 31 binlog-checksum=CRC32 32 master-verify-checksum=1 33 slave-sql-verify-checksum=1 34 binlog-rows-query-log_events=1 35 server-id=20 36 report-port=3306 37 port=3306 38 datadir=/mydata/data 39 socket=/tmp/mysql.sock 40 report-host=www.master.com 41 42 43 上述選項詳解: 44 binlog-format:二進制日誌的格式,有row、statement和mixed幾種類型; 45 須要注意的是:當設置隔離級別爲READ-COMMITED必須設置二進制日誌格式爲ROW,如今MySQL官方認爲STATEMENT這個已經再也不適合繼續使用;但mixed類型在默認的事務隔離級別下,可能會致使主從數據不一致; 46 log-slave-updates、gtid-mode、enforce-gtid-consistency、report-port和report-host:用於啓動GTID及知足附屬的其它需求; 47 master-info-repository和relay-log-info-repository:啓用此兩項,可用於實如今崩潰時保證二進制及從服務器安全的功能; 48 sync-master-info:啓用之可確保無信息丟失; 49 slave-parallel-workers:設定從服務器的SQL線程數;0表示關閉多線程複製功能;MariaDB上是slave-parallel-threads 50 binlog-checksum、master-verify-checksum和slave-sql-verify-checksum:啓用複製有關的全部校驗功能; 51 binlog-rows-query-log-events:啓用之可用於在二進制日誌記錄事件相關的信息,可下降故障排除的複雜度; 52 log-bin:啓用二進制日誌,這是保證複製功能的基本前提; 53 server-id:同一個複製拓撲中的全部服務器的id號必須唯一;
2.在master上受權複製用戶:
1 MariaDB [(none)]> grant replication slave,replication client on *.* to 'daixiang'@'192.168.88.%' identified by 'daixiang'; 2 MariaDB [(none)]> flush privileges;
3.在slave從服務器上設置將從服務器指向主服務器,並啓動複製線程:
MariaDB [(none)]> change master to MASTER_HOST='192.168.88.147',MASTER_USER='daixiang',MASTER_PASSWORD='daixiang',MASTER_USE_GTID=current_pos;
注意:在mysql5.6之後的版本,須要使用此命令指定主服務器:mysql> CHANGE MASTER TO MASTER_HOST='master.magedu.com', MASTER_USER='repluser', MASTER_PASSWORD='replpass', MASTER_AUTO_POSITION=1;
若是沒有啓用GTID功能,則須要使用下面的命令:
slave> CHANGE MASTER TO MASTER_HOST='172.16.100.6',
-> MASTER_USER='repluser',
-> MASTER_PASSWORD='replpass',
-> MASTER_LOG_FILE='master-bin.000003',
-> MASTER_LOG_POS=1174;
MariaDB [(none)]> START SLAVE; MariaDB [(none)]> show slave status\G *************************** 1. row *************************** Slave_IO_State: Waiting for master to send event Master_Host: 192.168.88.147 Master_User: daixiang Master_Port: 3306 Connect_Retry: 60 Master_Log_File: master-bin.000001 Read_Master_Log_Pos: 808 Relay_Log_File: www-relay-bin.000002 Relay_Log_Pos: 799 Relay_Master_Log_File: master-bin.000001 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: 808 Relay_Log_Space: 1099 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: 10 Master_SSL_Crl: Master_SSL_Crlpath: Using_Gtid: Current_Pos Gtid_IO_Pos: 0-10-3 Replicate_Do_Domain_Ids: Replicate_Ignore_Domain_Ids: Parallel_Mode: conservative 1 row in set (0.00 sec)
4.查看從服務器上的線程狀況:
MariaDB [(none)]> show processlist; +----+-------------+-----------+------+---------+------+-----------------------------------------------------------------------------+------------------+----------+ | Id | User | Host | db | Command | Time | State | Info | Progress | +----+-------------+-----------+------+---------+------+-----------------------------------------------------------------------------+------------------+----------+ | 10 | system user | | NULL | Connect | 2853 | Waiting for master to send event | NULL | 0.000 | | 11 | system user | | NULL | Connect | 2646 | Waiting for work from SQL thread | NULL | 0.000 | | 12 | system user | | NULL | Connect | 2853 | Waiting for work from SQL thread | NULL | 0.000 | | 13 | system user | | NULL | Connect | 2853 | Slave has read all relay log; waiting for the slave I/O thread to update it | NULL | 0.000 | | 15 | root | localhost | NULL | Query | 0 | init | show processlist | 0.000 | +----+-------------+-----------+------+---------+------+-----------------------------------------------------------------------------+------------------+----------+ 5 rows in set (0.00 sec) 這幾個線程分別是: 15號:主進程 13號:I/O線程 12號、11號:SQL線程 10號:監聽的守護進程
5.查看主服務器上的線程狀況:
MariaDB [(none)]> show processlist; +----+----------+----------------------+------+-------------+------+-----------------------------------------------------------------------+------------------+----------+ | Id | User | Host | db | Command | Time | State | Info | Progress | +----+----------+----------------------+------+-------------+------+-----------------------------------------------------------------------+------------------+----------+ | 7 | daixiang | 192.168.88.148:42475 | NULL | Binlog Dump | 3315 | Master has sent all binlog to slave; waiting for binlog to be updated | NULL | 0.000 | | 9 | root | localhost | NULL | Query | 0 | init | show processlist | 0.000 | +----+----------+----------------------+------+-------------+------+-----------------------------------------------------------------------+------------------+----------+ 2 rows in set (0.00 sec)
---恢復內容結束---