程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 數據庫知識 >> MYSQL數據庫 >> 關於MYSQL數據庫 >> MySQL5.6基於GTID的主從復制

MySQL5.6基於GTID的主從復制

編輯:關於MYSQL數據庫

MySQL 5.6 的新特性之一,是加入了全局事務 ID (GTID) 來強化數據庫的主備一致性,故障恢復,以及容錯能力。

什麼是GTID?

官方文檔:http://dev.mysql.com/doc/refman/5.6/en/replication-gtids.html在這篇文檔裡,我們可以知道全局事務 ID 的官方定義是:GTID = source_id:transaction_id

MySQL 5.6 中,每一個 GTID 代表一個數據庫事務。在上面的定義中,source_id 表示執行事務的主庫 uuid(server_uuid),transaction_id 是一個從 1 開始的自增計數,表示在這個主庫上執行的第 n 個事務。MySQL 會保證事務與 GTID 之間的 1 : 1 映射。

一、環境准備

操作系統:CentOS6.5 64位

數據庫版本:MySQL5.6.23

拓撲如下:

三、安裝主數據庫(masterdb.example.com)

1、准備數據存放目錄、創建用戶

[root@masterdb ~]#mkdir /data/mysqldata -p #創建數據存放目錄
[root@masterdb ~]#mkdir /data/mysqlLog/logs -p #創建日志存放目錄
[root@masterdb ~]#groupadd -r mysql
[root@masterdb ~]#useradd -g mysql -r -s /sbin/nologin -M -d /data/mysqldata mysql
[root@masterdb ~]#chown -R mysql:mysql /data/mysqldata
[root@masterdb ~]#chown -R mysql:mysql /data/mysqlLog/logs

 2、安裝並初始化mysql5.6.23

[root@masterdb ~]# tar xf mysql-advanced-5.6.23-linux-glibc2.5-x86_64.tar.gz -C /usr/local/
[root@masterdb ~]# cd /usr/local/
[root@masterdb ~]# ln -sv mysql-advanced-5.6.23-linux-glibc2.5-x86_64 mysql
[root@masterdb ~]# chown -R root.mysql mysql
[root@masterdb ~]# cd mysql
[root@masterdb ~]# cp support-files/mysql.server /etc/rc.d/init.d/mysqld
[root@masterdb ~]# cp support-files/my-default.cnf /etc/my.cnf
[root@masterdb ~]# chmod +x /etc/rc.d/init.d/mysqld
[root@masterdb ~]# chkconfig --add mysqld
[root@masterdb ~]# chkconfig mysqld on
[root@masterdb ~]# ./scripts/mysql_install_db --user=mysql --datadir=/data/mysqldata/ 

3、輸出mysql的man手冊至man命令的查找路徑:

編輯/etc/man.config,添加如下行即可:
MANPATH  /usr/local/mysql/man

4、輸出mysql的頭文件至系統頭文件路徑/usr/include:

這可以通過簡單的創建鏈接實現:

[root@masterdb ~]#ln -sv /usr/local/mysql/include /usr/include/mysql

5、輸出mysql的庫文件給系統庫查找路徑:

[root@masterdb ~]#echo '/usr/local/mysql/lib' > /etc/ld.so.conf.d/mysql.conf

而後讓系統重新載入系統庫:

[root@masterdb ~]# ldconfig

6、修改PATH環境變量,讓系統可以直接使用mysql的相關命令:

[root@masterdb ~]# vim /etc/profile.d/mysql.sh
export PATH=$PATH:/usr/local/mysql/bin
[root@masterdb ~]#source /etc/profile.d/mysql.sh

從數據庫安裝同上,具體過程略過。

四、分別為主從數據庫提供配置文件/etc/my.cnf

要在MySQL 5.6中使用復制功能,其服務配置段[mysqld]中於少應該定義如下選項:
binlog-format:二進制日志的格式,有row、statement和mixed幾種類型;
需要注意的是:當設置隔離級別為READ-COMMITED必須設置二進制日志格式為ROW,現在MySQL官方認為STATEMENT這個已經不再適合繼續使用;但mixed類型在默認的事務隔離級別下,可能會導致主從數據不一致;
log-slave-updates、gtid-mode、enforce-gtid-consistency、report-port和report-host:用於啟動GTID及滿足附屬的其它需求;
master-info-repository和relay-log-info-repository:啟用此兩項,可用於實現在崩潰時保證二進制及從服務器安全的功能;
sync-master-info:啟用之可確保無信息丟失;
slave-paralles-workers:設定從服務器的SQL線程數;0表示關閉多線程復制功能;
binlog-checksum、master-verify-checksum和slave-sql-verify-checksum:啟用復制有關的所有校驗功能;
binlog-rows-query-log-events:啟用之可用於在二進制日志記錄事件相關的信息,可降低故障排除的復雜度;
log-bin:啟用二進制日志,這是保證復制功能的基本前提;
server-id:同一個復制拓撲中的所有服務器的id號必須惟一;

主數據庫上:

[client]
port = 3306
socket = /tmp/mysql.sock
default-character-set = utf8
[mysql]
no-auto-rehash
default-character-set = utf8

[mysqld]
server-id = 1
port = 3306
user = mysql
basedir = /usr/local/mysql
datadir = /data/mysqldata
socket = /tmp/mysql.sock
default-storage-engine = INNODB
character-set-server = utf8
connect_timeout = 60
interactive_timeout = 28800
wait_timeout = 28800
back_log = 500
event_scheduler = ON
skip_name_resolve = ON;

###########binlog##########
log-bin = /data/mysqlLog/logs/mysql-bin
binlog_format = row
max_binlog_size = 128M
binlog_cache_size = 2M
expire-logs-days = 5
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=4
#rpl_semi_sync_master_enabled = 1

slow_query_log = 1
slow_query_log_file = /data/mysqlLog/logs/mysql.slow
long_query_time = 1

log_error = /data/mysqlLog/logs/error.log
max_connections = 3000
max_connect_errors = 32767
log_bin_trust_function_creators = 1
transaction_isolation = READ-COMMITTED

從數據庫上:

[client]
port = 3306
socket = /tmp/mysql.sock
default-character-set = utf8

[mysql]
no-auto-rehash
default-character-set = utf8

[mysqld]
server-id = 205
port = 3306
user = mysql
basedir = /usr/local/mysql
datadir = /data/mysqldata
socket = /tmp/mysql.sock
default-storage-engine = INNODB
character-set-server = utf8
connect_timeout = 60
wait_timeout = 18000
back_log = 500
event_scheduler = ON

###########binlog##########
log-bin = /data/mysqlLog/logs/mysql-bin
binlog_format = row
max_binlog_size = 128M
binlog_cache_size = 2M
expire-logs-days = 5
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=4
#rpl_semi_sync_slave_enabled = 1
skip-slave-start

slow_query_log = 1
slow_query_log_file = /data/mysqlLog/logs/mysql.slow
long_query_time = 2

log-error = /data/mysqlLog/logs/error.log
max_connections = 3000
max_connect_errors = 10000
log_bin_trust_function_creators = 1
transaction_isolation = READ-COMMITTED

五、分別在主從數據庫上啟動mysqld服務

[root@masterdb ~]# service mysqld start
Starting MySQL......          [ OK ]
[root@masterdb ~]# 


[root@slavedb ~]# service mysqld start
Starting MySQL......          [ OK ]
[root@slavedb ~]# 

六、在主數據庫上創建復制用戶
復制代碼 代碼如下:mysql> GRANT REPLICATION SLAVE ON *.* TO [email protected] IDENTIFIED BY 'replpassword';
說明:172.16.88.205是從節點服務器;如果想一次性授權更多的節點,可以自行根據需要修改;

七、啟動從數據庫上的復制線程

mysql> CHANGE MASTER TO MASTER_HOST='masterdb.example.com', MASTER_USER='repluser', MASTER_PASSWORD='replpassword', 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: masterdb.56xyl.com
     Master_User: repluser
     Master_Port: 3306
    Connect_Retry: 60
    Master_Log_File: mysql-bin.000002
   Read_Master_Log_Pos: 191
    Relay_Log_File: slavedb-relay-bin.000003
    Relay_Log_Pos: 401
  Relay_Master_Log_File: mysql-bin.000002
    Slave_IO_Running: Yes #IO線程已正常運行
   Slave_SQL_Running: Yes #SQL線程已正常運行
    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: 1899
    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: 971d7245-c3f8-11e5-8b6b-000c2999e5a5
    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: 971d7245-c3f8-11e5-8b6b-000c2999e5a5:1-6
   Executed_Gtid_Set: 89e78301-c3f4-11e5-8b51-00505624d26a:1-3,
971d7245-c3f8-11e5-8b6b-000c2999e5a5:1-6
    Auto_Position: 1
1 row in set (0.00 sec)

ERROR: 
No query specified

mysql> 


九、測試

在主庫上創建數據庫:

mysql> create database log_statics;
Query OK, 1 row affected (0.11 sec)
 
mysql> use log_statics;
Database changed
到從數據庫上查看log_statics是否已經復制過去
mysql> show databases;
+--------------------+
| Database   |
+--------------------+
| information_schema |
| log_statics  |
| mysql    |
| performance_schema |
+--------------------+
4 rows in set (0.01 sec)

mysql>

可以看到log_statics數據庫已經存在於從數據庫上。

以上就是本文的全部內容,希望對大家的學習有所幫助。

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved