一、MySQL 5.6 以后出現的GTID:
GTID概念:
1. GTID是一個由服務器的UUID和事務序號組成的唯一事務序號
例如: UUID:N
1122-3322-1122:1
1122-3322-1122:2
2. GTID會被當做唯每一個事務的首部,將會自動生成并存到二進制日志中
3. GTID可以用來追蹤主從之間的事務傳輸。
4. GTID主要應用于HA的功能。在多主模型中,標示某一個事務是來源于哪個特定的主服務器。
5. 從服務器不會修改或者添加新的GTID,即便從服務器被配置為其他從服務器的主服務器,所以可以追蹤事務流。
6. GTID開啟后,會在gtid_executed予以顯示
跟復制相關的工具,需要python2.7環境
1. mysqlreplicate: 添加從節點
2. mysqlcheck: 實現校驗機制
3. mysqlrplshow: 發現并顯示,復制拓撲結構, 幾級復制每一級有多少個服務器
4. mysqlfailover: 講一個從節點提升為一個主節點
5. mysqlrpladmin: 管理工具,做手工調度的,把一個正常從節點調度為主節點
借助于GTID的多線程復制
從服務器可以發出多個1/O線程對主服務器進行對mysqldump請求會使得先后讀出來的信息順序混亂。因此通過分割數據庫來達到多線程,每一個數據庫的事務只能有一個線程復制。 但即便如此如果只有一個主服務器,多線程并不能帶來性能提升, 因為只有一個二進制服務器,并且網絡帶寬也有限。真正使性能提升,需要一從多主模型。多線程slave通常為多個sql,一個I/O線程,多個SQL線程。 通過GTID機制,可以把不同的事務通過不同線程來應用。
配置開啟GTID開啟復制的需需要的參數(配置在[mysqld]段下)
1. binlog-format:二進制日志的格式,有row、statement和mixed幾種類型;
需要注意的是:當設置隔離級別為READ-COMMITED必須設置二進制日志格式為ROW,現在MySQL官方認為STATEMENT這個已經不再適合繼續使用;但mixed類型在默認的事務隔離級別下,可能會導致主從數據不一致;
2. log-slave-updates、gtid-mode、enforce-gtid-consistency、report-port和report-host:用于啟動GTID及滿足附屬的其它需求;
master-info-repository和relay-log-info-repository:啟用此兩項,可用于實現在崩潰時保證二進制及從服務器安全的功能;
3. sync-master-info:啟用之可確保無信息丟失;
4. slave-paralles-workers:設定從服務器的SQL線程數;0表示關閉多線程復制功能;
5. binlog-checksum、master-verify-checksum和slave-sql-verify-checksum:啟用復制有關的所有校驗功能;
6. binlog-rows-query-log-events:啟用之可用于在二進制日志記錄事件相關的信息,可降低故障排除的復雜度;
7. log-bin:啟用二進制日志,這是保證復制功能的基本前提;
8. server-id:同一個復制拓撲中的所有服務器的id號必須惟一;
9. report-host: 需要從服務器的主機名和IP地址在從服務器注冊的時候是否報告給主服務器。 在主服務器上使用SHOW SLAVE HOSTS可以查看
The host name or IP address of the slave to be reported to the master during slave registration. This value appears in the output of SHOW SLAVE HOSTS on the master server.
10. report-port:是否報告從服務器鏈接端口給主服務器
The TCP/IP port number for connecting to the slave, to be reported to the master during slave registration.
11. master-info-repository: 從服務是否把從服務器登錄和連接信息記錄在文件master.info或者記錄在mysql.slave_master_info表中
The setting of this variable determines whether the slave logs master status and connection information to a FILE (master.info), or to a TABLE (mysql.slave_master_info)
12. relay-log-info-repository: relay log相關數據記錄文檔或者表
This option causes the server to log its relay log info to a file or a table.
13. log_slave_updates:是否接受從服務器的更新信息
Whether updates received by a slave serv
二、MySQL5.6主從配置并開啟基于GTID的多線程工作模型
1. 配置兩臺主機,重新初始化mysql服務器,并且保證主機名可以解析。 加一下hosts文件復制到兩臺主機即可。
127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4 ::1 localhost localhost.localdomain localhost6 localhost6.localdomain6 192.168.98.129www.playground2.comwww.master1.com 192.168.98.133www.slave.com
2. 同步時間, 可以開啟ntpd服務,通過一些公共時間服務器同步時間。其實這是偷懶的做法,最后自己配置一臺本地時間服務器。
3. 主服務器端編輯配置文件如下
[mysqld] binlog-format=ROW log-bin=/data/binlog/master-bin log-slave-updates=true gtid-mode=on enforce-gtid-consistency=true master-info-repository=TABLE relay-log-info-repository=TABLE sync-master-info=1 slave-parallel-workers=2 binlog-checksum=CRC32 master-verify-checksum=1 slave-sql-verify-checksum=1 binlog-rows-query-log_events=1 server-id=1 report-port=3306 port=3306 datadir=/data/mysqldata socket=/tmp/mysql.sock user=mysql innodb_log_file_size=5M innodb_file_per_table=ON report-host=www.master1.com symbolic-links=0 [mysqld_safe] log-error=/var/log/mysqld.log
配置完成后,可以重新初始化服務器。
4. 從服務器配置
[mysqld] binlog-format=ROW log-slave-update=true gtid-mode=on enforce-gtid-consistency=true master-info-repository=TABLE relay-log-info-repository=TABLE sync-master-info=1 slave-parallel-workers=2 binlog-checksum=CRC32 master-verify-checksum=1 slave-sql-verify-checksum=1 binlog-rows-query-log_events=1 server-id=11 report-port=3306 port=3306 datadir=/data/mysqldata socket=/tmp/mysql.sock user=mysql report-host=www.slave.com # Disabling symbolic-links is recommended to prevent assorted security risks symbolic-links=0 relay-log=/data/relaylog/relay_bin skip-slave-start [mysqld_safe] log-error=/var/log/mysqld.log
其實我覺得很多針對主服務器的配置應該不需要寫在這里。
5. 在主服務器上為從服務器授權用戶。 并查看主服務器二進制日志位置
mysql> GRANT REPLICATION CLIENT,REPLICATION SLAVE ON *.* TO 'slaveuser'@'www.slave.com' IDENTIFIED BY 'slaveuser' ; mysql> FLUSH PRIVILEGES ; mysql> SHOW MASTER STATUS ; +-------------------+----------+--------------+------------------+------------------------------------------+ | File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set | +-------------------+----------+--------------+------------------+------------------------------------------+ | master-bin.000003 | 561 | | | 600383b3-9cb6-11e5-8b6c-000c29622425:1-2 | +-------------------+----------+--------------+------------------+------------------------------------------+
7. 將從服務器指向主服務器,并開啟復制線程。
mysql> CHANGE MASTER TO MASTER_HOST='www.master1.com',MASTER_USER='slaveuser',MASTER_PASSWORD='slaveuser',MASTER_AUTO_POSITION=1; mysql> START SLAVE; mysql> SHOW SLAVE STATUS \G; *************************** 1. row *************************** Slave_IO_State: Waiting for master to send event Master_Host: www.master1.com Master_User: slaveuser Master_Port: 3306 Connect_Retry: 60 Master_Log_File: master-bin.000004 Read_Master_Log_Pos: 191 Relay_Log_File: relay_bin.000003 Relay_Log_Pos: 403 Relay_Master_Log_File: master-bin.000004 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: 191 Relay_Log_Space: 1223 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: 600383b3-9cb6-11e5-8b6c-000c29622425 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 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: 600383b3-9cb6-11e5-8b6c-000c29622425:1-2 Executed_Gtid_Set: 600383b3-9cb6-11e5-8b6c-000c29622425:1-2 Auto_Position: 1
8. 查看從服務器sql線程情況:
mysql> SHOW PROCESSLIST ; +----+-------------+-----------+------+---------+------+-----------------------------------------------------------------------------+------------------+ | Id | User | Host | db | Command | Time | State | Info | +----+-------------+-----------+------+---------+------+-----------------------------------------------------------------------------+------------------+ | 1 | root | localhost | NULL | Query | 0 | init | SHOW PROCESSLIST | | 2 | system user | | NULL | Connect | 224 | Waiting for master to send event | NULL | | 3 | system user | | NULL | Connect | 224 | Slave has read all relay log; waiting for the slave I/O thread to update it | NULL | | 4 | system user | | NULL | Connect | 224 | Waiting for an event from Coordinator | NULL | | 5 | system user | | NULL | Connect | 224 | Waiting for an event from Coordinator | NULL | +----+-------------+-----------+------+---------+------+-----------------------------------------------------------------------------+------------------+ 這里后面兩個4,5應該是SQL寫進程,可以看到變成了另個,和我們設置相符 1號應該是主進程, 2號應該是監聽的守護進程 3號應該是1/O線程 9. 主服務器的線程情況
mysql> SHOW PROCESSLIST; +----+-----------+---------------------+---------+------------------+------+-----------------------------------------------------------------------+------------------+ | Id | User | Host | db | Command | Time | State | Info | +----+-----------+---------------------+---------+------------------+------+-----------------------------------------------------------------------+------------------+ | 2 | root | localhost | hellodb | Query | 0 | init | SHOW PROCESSLIST | | 6 | slaveuser | www.slave.com:36925 | NULL | Binlog Dump GTID | 520 | Master has sent all binlog to slave; waiting for binlog to be updated | NULL | +----+-----------+---------------------+---------+------------------+------+-----------------------------------------------------------------------+------------------+ 只有一個mysql dump 進程
三、使用mariadb 10 實現一主多從構架
1. 克隆一臺主機,主機名改為www.master2.com, 配置和www.master1.com相同
使用vmware workstation 克隆一臺主句,開機后做如下修改 # vim /etc/udev/rules.d/70-persistent-net.rules 把里面的第一行刪掉,第二行eth1改成eth0 然后重新加載網卡 # modprobe -r e1000 # modprobe e1000 然后在三臺主機上安裝,mariadb 10, 基本過程和mysql差不多,不過折騰一陣子
2. 在host文件中添加,第二臺主服務器的的主機名,并且可以完成解析
修改各主機名。
修改hosts文件。
復制相同的hosts文件到各個節點。
過程比較簡單,這里不寫了,請允許我的懶惰。
在三臺主機上安裝mariadb 10, 過程不說了,對于你們這些大牛都是分分鐘的事。
3. 配置方面
master1主機 log-bin=/data/binlog/master1-bin datadir=/data/mysqldata server-id = 1 binlog-format=ROW log-slave-updates=true master-info-repository=TABLE relay-log-info-repository=TABLE sync-master-info=1 binlog-checksum=CRC32 master-verify-checksum=1 binlog-rows-query-log_events=1 report-port=3306 socket=/tmp/mysql.sock user=mysql innodb_log_file_size=5M innodb_file_per_table=ON report-host=www.master1.com master2主機 log-bin=/data/binlog/master2-bin datadir=/data/mysqldata server-id = 2 binlog-format=ROW log-slave-updates=true master-info-repository=TABLE relay-log-info-repository=TABLE sync-master-info=1 binlog-checksum=CRC32 master-verify-checksum=1 binlog-rows-query-log_events=1 report-port=3306 socket=/tmp/mysql.sock user=mysql innodb_log_file_size=5M innodb_file_per_table=ON report-host=www.master2.com slave主機 log-bin=/data/binlog/slave-bin datadir=/data/mysqldata server-id = 11 binlog-format=ROW log-slave-updates=true master-info-repository=TABLE relay-log-info-repository=TABLE sync-master-info=1 slave-parallel-workers=2 binlog-checksum=CRC32 master-verify-checksum=1 slave-sql-verify-checksum=1 binlog-rows-query-log_events=1 server-id=11 report-port=3306 port=3306 report-host=www.slave.com relay-log=/data/relaylog/relay-bin replicate_ignore_db=mysql performance_schema information_schema
3. 重新初始化一下各個節點,并清理各種binlog和relaylog
4. 在兩個主服務器上面分別創建從服務器復制用戶
# 在master1 mysql> GRANT REPLICATION CLIENT, REPLICATION SLAVE ON *.* TO 'slaveuser'@'www.slave.com' IDENTIFIED BY 'slaveuser'; mysql> FLUSH PRIVILEGES ; # 在master2 mysql> GRANT REPLICATION CLIENT, REPLICATION SLAVE ON *.* TO 'slaveuser'@'www.slave.com' IDENTIFIED BY 'slaveuser'; mysql> FLUSH PRIVILEGES ;
5. 從服務器上嘗試連接不同主服務器。
MariaDB [(none)]> CHANGE MASTER 'master1' TO MASTER_HOST='192.168.98.129',MASTER_PORT=3306,MASTER_USER='slaveuser',MASTER_PASSWORD='slaveuser',MASTER_LOG_FILE='master1-bin.000004',MASTER_LOG_POS=686; MariaDB [(none)]> CHANGE MASTER 'master2' TO MASTER_HOST='192.168.98.130',MASTER_PORT=3306,MASTER_USER='slaveuser',MASTER_PASSWORD='slaveuser',MASTER_LOG_FILE='master2-bin.000004',MASTER_LOG_POS=686; MariaDB [(none)]> START SLAVE 'master1'; MariaDB [(none)]> START SLAVE 'master2'; MariaDB [(none)]> SHOW SLAVES All STATUS \G; ## 查看狀態 此時可以分別在兩個數據庫上進行一些修改,正常來說,都會在從服務器上有所顯示。
6. 一些總結
1) 和mysql 5.6 相比,mariadb不支持的參數:
gtid-mode=on
enforce-gtid-consistency=true
2) 修改的參數:
slave-parallel-workers參數修改為slave-parallel-threads
3) 連接至主服務使用的命令:
一個新的參數:MASTER_USER_GTID={current_pos|slave_pos|no}
這個參數在多主一從的試驗中,總是不成功
4)才配置從服務器的時候,最好使用replicate_ignore_db 來忽略掉一些系統庫。
原創文章,作者:以馬內利,如若轉載,請注明出處:http://www.www58058.com/10199