1. 程式人生 > 其它 >GITD實現主從複製

GITD實現主從複製

一、GTID介紹

1、GTID複製:(global transaction id 全域性事務識別符號) MySQL5.6版本開始支援,GTID複製不像傳統的複製方式(非同步複製、半同步複製)需要找到binlog和POS點,只需知道master的IP、埠、賬號、密碼即可。開啟GTID後,執行change master to master_auto_postion=1即可,它會自動尋找同步
2、GTID作用
1)、Gtid採用了新的複製協議舊協議是,首先從伺服器上在一個特定的偏移量位置連線到主伺服器上一個給定的二進位制日誌檔案,然後主伺服器再從給定的連線點開始傳送所有的事件。
2)、新協議有所不同,支援以全域性統一事務ID (GTID)為基礎的複製。當在主庫上提交事務或者被從庫應用時,可以定位和追蹤每一個事務。GTID複製是全部以事務為基礎,使得檢查主從一致性變得非常簡單。如果所有主庫上提交的事務也同樣提交到從庫上,一致性就得到了保證。

3、GTID工作原理
①當一個事務在master執行並提交時,產生GTID,一同記錄到binlog日誌中。
②binlog傳輸到slave,並存儲到slave的relaylog後,讀取這個GTID的這個值設定gtid_next變數,即告訴Slave,下一個要執行的GTID值。
③sql執行緒從relay log中獲取GTID,然後對比slave端的binlog是否有該GTID。
④如果有記錄,說明該GTID的事務已經執行,slave會忽略。
⑤如果沒有記錄,slave就會執行該GTID事務,並記錄該GTID到自身的binlog,在讀取執行事務前會先檢查其他session持有該GTID,確保不被重複執行。
⑥在解析過程中會判斷是否有主鍵,如果沒有就用二級索引,如果沒有就用全部掃描。

二、基於mysql 5.7的GTID複製

先實現master

[root@master ~]# tar xvf mysql-5.7.35-el7-x86_64.tar.gz -C /usr/local/
2、建立使用者和組
[root@master ~]# groupadd mysql
[root@master ~]# useradd -r -s /bin/bash mysql mysql

3、配置環境變數
[root@master local]# mv mysql-5.7.35-el7-x86_64 mysql
[root@master local]# echo 'PATH=/usr/local/mysql/bin:$PATH' > /etc/profile.d/mysql.sh


[root@master local]# . /etc/profile.d/mysql.sh
[root@master local]# echo $PATH /usr/local/mysql/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin

4、生成配置檔案,密碼
[root@master local]# mysqld --initialize --user=mysql --datadir=/data/mysql 2021-11-21T05:29:07.097551Z 1 [Note] A temporary password is generated for root@localhost: f!sBc-RC%4SD
登入密碼為:f!sBc-RC%4SD

5、mysql配置檔案
[root@master data]# vim /etc/my.cnf
[mysqld]
server-id=1
log-bin=/data/mysql_back/mysql_bin
datadir=/data/mysql
socket=/data/mysql/mysql.sock
log-error=/data/mysql/mysql.log
pid-file=/data/mysql/mysql.pid
[client]
socket=/data/mysql/mysql.sock

6、建立mysql_back目錄並修改許可權
[root@master ~]# mkdir /data/mysql_back/
[root@master ~]# chown -R mysql.mysql /data
[root@master ~]# chmod 2755 /data
2755:表示在該目錄下建立檔案將繼承/data/

7、開機啟動
[root@master ]# chkconfig --add mysqld

`[root@master ~]# chkconfig --list

mysqld 0:off 1:off 2:on 3:on 4:on 5:on 6:off`

[root@master ]# service mysqld start
[root@master ~]# ps axu | grep mysqld
root 3591 0.0 0.0 11816 1608 pts/1 S 13:37 0:00 /bin/sh /usr/local/mysql/bin/mysqld_safe --datadir=/data/mysql --pid-file=/data/mysql/m
ysql.pidmysql 3777 0.0 10.3 1121220 169696 pts/1 Sl 13:37 0:00 /usr/local/mysql/bin/mysqld --basedir=/usr/local/mysql --datadir=/data/mysql --plugin-d
ir=/usr/local/mysql/lib/plugin --user=mysql --log-error=/data/mysql/mysql.log --pid-file=/data/mysql/mysql.pid --socket=/data/mysql/mysql.sockroot 3978 0.0 0.0 112704 972 pts/1 S+ 13:46 0:00 grep --color=auto mysqld

8、修改密碼
[root@master ~]# mysqladmin -uroot -p'f!sBc-RC%4SD' password 123456
mysqladmin: [Warning] Using a password on the command line interface can be insecure.
Warning: Since password will be sent to server in plain text, use ssl connection to ensure password safety.

9、登入
[root@master ~]# mysql -uroot -p123456
mysql> create database xzcdc;
mysql> create table xzcdc.xz(id int(10) NOT NULL PRIMARY KEY )

10、給使用者授權
mysql> grant replication slave on *.* to xzcdc@'192.168.100.%' identified by '123456';

實現slave

1、建立使用者組
[root@centos_9 ~]# groupadd mysql
[root@centos_9 ~]# useradd -r -s /bin/bash -g mysql mysql
2、解壓
[root@centos_9 ~]# tar xvf mysql-5.7.35-el7-x86_64.tar.gz -C /usr/local/
[root@centos_9 local]# mv mysql-5.7.35-el7-x86_64/ mysql

3、配置環境變數
[root@centos_9 ~]# echo 'PATH=/usr/local/mysql/bin:$PATH' > /etc/profile.d/mysql.sh
[root@centos_9 ~]# . /etc/profile.d/mysql.sh

4、生成資料庫檔案及密碼
[root@centos_9 local]# mysqld --initialize --user=mysql --datadir=/data/mysql
codU/rEdv1xM
[root@centos_9 mysql]# ll
total 122960
-rw-r----- 1 mysql mysql 56 Nov 21 19:39 auto.cnf
-rw------- 1 mysql mysql 1680 Nov 21 19:39 ca-key.pem
-rw-r--r-- 1 mysql mysql 1112 Nov 21 19:39 ca.pem
-rw-r--r-- 1 mysql mysql 1112 Nov 21 19:39 client-cert.pem
-rw------- 1 mysql mysql 1680 Nov 21 19:39 client-key.pem
-rw-r----- 1 mysql mysql 436 Nov 21 19:39 ib_buffer_pool
-rw-r----- 1 mysql mysql 12582912 Nov 21 19:42 ibdata1
-rw-r----- 1 mysql mysql 50331648 Nov 21 19:42 ib_logfile0
-rw-r----- 1 mysql mysql 50331648 Nov 21 19:39 ib_logfile1
-rw-r----- 1 mysql mysql 12582912 Nov 21 19:42 ibtmp1
drwxr-x--- 2 mysql mysql 4096 Nov 21 19:39 mysql
-rw-r----- 1 mysql mysql 3773 Nov 21 19:42 mysql.log
-rw-r----- 1 mysql mysql 5 Nov 21 19:42 mysql.pid
srwxrwxrwx 1 mysql mysql 0 Nov 21 19:42 mysql.sock
-rw------- 1 mysql mysql 5 Nov 21 19:42 mysql.sock.lock

5、從庫修改自己的配置檔案
vim /etc/my.cnf
[mysqld]
server-id=2
read-only
gtid_mode=ON
enforce_gtid_consistency
datadir=/data/mysql
socket=/data/mysql/mysql.sock
log-error=/data/mysql/mysql.log
pid-file=/data/mysql/mysql.pid
[client]
socket=/data/mysql/mysql.sock

6、啟動指令碼
[root@centos_9 local]# cp mysql/support-files/mysql.server /etc/init.d/mysqld

7、啟動:
[root@centos_9 local]# service mysqld start
Starting MySQL.Logging to '/data/mysql/mysql.log'.
SUCCESS!

8、修改登入密碼
[root@centos_9 mysql]# mysqladmin -uroot -p'codU/rEdv1xM' password 123456
mysqladmin: [Warning] Using a password on the command line interface can be insecure.
Warning: Since password will be sent to server in plain text, use ssl connection to ensure password safety.

三、複製實現

上面所有的步驟實現完成後,開始實現GTID複製,

1、master 檢視binlog日誌
mysql> show master logs;
+------------------+-----------+
| Log_name | File_size |
+------------------+-----------+
| mysql_bin.000001 | 710 |
+------------------+-----------+
1 row in set (0.00 sec)

2、建立一個數據庫
mysql> create database xzcdc;
Query OK, 1 row affected (0.00 sec)

3、在次檢視日誌
mysql> show master logs;
+------------------+-----------+
| Log_name | File_size |
+------------------+-----------+
| mysql_bin.000001 | 872 |
+------------------+-----------+
1 row in set (0.00 sec)

4、在slave進行復制
5、檢視CHANGE MASTER TO用法
mysql> help CHANGE MASTER TO;
mysql> CHANGE MASTER TO
-> MASTER_HOST='192.168.100.8',
-> MASTER_USER='xzcdc',
-> MASTER_PASSWORD='123456',
-> MASTER_PORT=3306,
-> MASTER_AUTO_POSITION=1; #副本嘗試使用基於 GTID 的複製的自動定位功能而不是基於二進位制日誌檔案的位置連線到源
Query OK, 0 rows affected, 2 warnings (0.01 sec)

6、檢視
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| sys |
+--------------------+
4 rows in set (0.00 sec)

在master上新建的庫沒有複製過來,那是因為還沒有啟動執行緒

注意:不能配置該值:skip-grant-tables(免密碼登入),如果配置將在GTID複製master庫的時候報錯如下:

mysql> start slave;
mysql> show slave status\G;
*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: 192.168.100.8
Master_User: xzcdc
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: mysql_bin.000003
Read_Master_Log_Pos: 194
Relay_Log_File: centos_9-relay-bin.000002
Relay_Log_Pos: 367
Relay_Master_Log_File: mysql_bin.000001
Slave_IO_Running: Yes
Slave_SQL_Running: No
Replicate_Do_DB:
Replicate_Ignore_DB:
Replicate_Do_Table:
Replicate_Ignore_Table:
Replicate_Wild_Do_Table:
Replicate_Wild_Ignore_Table:
Last_Errno: 1290
Last_Error: Error 'The MySQL server is running with the --skip-grant-tables option so it cannot execute this statement' on query. Defa
ult database: ''. Query: 'ALTER USER 'root'@'localhost' IDENTIFIED WITH 'mysql_native_password' AS '6BB4837EB74329105EE4568DDA7DC67ED2CA2AD9'' Skip_Counter: 0
Exec_Master_Log_Pos: 154
Relay_Log_Space: 2221
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: NULL
Master_SSL_Verify_Server_Cert: No
Last_IO_Errno: 0
Last_IO_Error:
Last_SQL_Errno: 1290
Last_SQL_Error: Error 'The MySQL server is running with the --skip-grant-tables option so it cannot execute this statement' on query. Defa
ult database: ''. Query: 'ALTER USER 'root'@'localhost' IDENTIFIED WITH 'mysql_native_password' AS '
6BB4837EB74329105EE4568DDA7DC67ED2CA2AD9'' Replicate_Ignore_Server_Ids:
Master_Server_Id: 1
Master_UUID: f1dd566f-4a8b-11ec-8198-000c294ac8c3
Master_Info_File: /data/mysql/master.info
SQL_Delay: 0
SQL_Remaining_Delay: NULL
Slave_SQL_Running_State:
Master_Retry_Count: 86400
Master_Bind:
Last_IO_Error_Timestamp:
Last_SQL_Error_Timestamp: 211121 20:07:59
Master_SSL_Crl:
Master_SSL_Crlpath:
Retrieved_Gtid_Set: f1dd566f-4a8b-11ec-8198-000c294ac8c3:1-3
Executed_Gtid_Set:
Auto_Position: 1
Replicate_Rewrite_DB:
Channel_Name:
Master_TLS_Version:
1 row in set (0.00 sec)

mysql> start slave;
Query OK, 0 rows affected, 1 warning (0.00 sec)

mysql> show slave status\G;
*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: 192.168.100.8
Master_User: xzcdc
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: mysql_bin.000003
Read_Master_Log_Pos: 194
Relay_Log_File: centos_9-relay-bin.000006
Relay_Log_Pos: 407
Relay_Master_Log_File: mysql_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: 194
Relay_Log_Space: 657
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: f1dd566f-4a8b-11ec-8198-000c294ac8c3
Master_Info_File: /data/mysql/master.info
SQL_Delay: 0
SQL_Remaining_Delay: NULL
Slave_SQL_Running_State: Slave has read all relay log; waiting for more updates
Master_Retry_Count: 86400
Master_Bind:
Last_IO_Error_Timestamp:
Last_SQL_Error_Timestamp:
Master_SSL_Crl:
Master_SSL_Crlpath:
Retrieved_Gtid_Set: f1dd566f-4a8b-11ec-8198-000c294ac8c3:1-3
Executed_Gtid_Set: f1dd566f-4a8b-11ec-8198-000c294ac8c3:1-3
Auto_Position: 1
Replicate_Rewrite_DB:
Channel_Name:
Master_TLS_Version:
1 row in set (0.00 sec)

ERROR:
No query specified

檢視master的xzcdc庫
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| sys |
| xzcdc |
+--------------------+

進行建立表,庫的操作

mysql> use xzcdc;
Database changed
mysql> show tables;
Empty set (0.00 sec)

建立表
mysql> create table xz(id int(10) NOT NULL PRIMARY KEY, name VARCHAR(11));
Query OK, 0 rows affected (0.13 sec)

檢視
mysql> show tables;
+-----------------+
| Tables_in_xzcdc |
+-----------------+
| xz |

查看錶結構
mysql> desc xz;
+-------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+-------------+------+-----+---------+-------+
| id | int(10) | NO | PRI | NULL | |
| name | varchar(11) | YES | | NULL | |
+-------+-------------+------+-----+---------+-------+

在從上檢視該表是否同步過來
mysql> show tables;
+-----------------+
| Tables_in_xzcdc |
+-----------------+
| xz |
+-----------------+

到此GTID主從複製完成