久久r热视频,国产午夜精品一区二区三区视频,亚洲精品自拍偷拍,欧美日韩精品二区

您的位置:首頁技術(shù)文章
文章詳情頁

淺談MySQL8.0 異步復(fù)制的三種方式

瀏覽:3日期:2023-10-10 17:19:28

本實(shí)驗(yàn)中分別針對(duì)空庫、脫機(jī)、聯(lián)機(jī)三種方式,配置一主兩從的mysql標(biāo)準(zhǔn)異步復(fù)制。只做整服務(wù)器級(jí)別的復(fù)制,不考慮對(duì)個(gè)別庫表或使用過濾復(fù)制的情況。

實(shí)驗(yàn)環(huán)境

[root@slave2 ~]# cat /etc/hosts192.168.2.138 master192.168.2.192 slave1192.168.2.130 slave2mysql> select version();+-----------+| version() |+-----------+| 8.0.16 |+-----------+1 row in set (0.00 sec)

一、空庫

1.查看主庫二進(jìn)制信息

mysql> show master status;+------------------+----------+--------------+------------------+-------------------+| File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |+------------------+----------+--------------+------------------+-------------------+| mysql-bin.000004 | 155 | | | |+------------------+----------+--------------+------------------+-------------------+1 row in set (0.00 sec)

2.在主庫上建立復(fù)制用戶

mysql> create user ’repl’@’%’ identified with mysql_native_password by ’wwwwww’;Query OK, 0 rows affected (0.03 sec)mysql> grant replication client,replication slave on *.* to ’repl’@’%’;Query OK, 0 rows affected (0.04 sec)mysql> flush privileges;Query OK, 0 rows affected (0.00 sec)

3.在從庫創(chuàng)建主庫信息

mysql> stop slave;mysql> change master to master_host=’192.168.2.138’, master_port=3306, master_user=’repl’, master_password=’wwwwww’, master_log_file=’mysql-bin.000004’, master_log_pos=155;Query OK, 0 rows affected, 2 warnings (0.00 sec)mysql> start slave;Query OK, 0 rows affected (0.00 sec)mysql> show slave statusG

剛才我們并沒有在從庫上建立repl用戶,但由于create user語句是在起始位置點(diǎn)后執(zhí)行的,因此可以正常復(fù)制到從庫,查詢mysql.user表即可確認(rèn)。

sql> select * from mysql.user where user=’repl’G

二、脫機(jī)

如果數(shù)據(jù)庫已經(jīng)存在應(yīng)用數(shù)據(jù),但允許一個(gè)可接受的脫機(jī)時(shí)間窗口做復(fù)制,這種場(chǎng)景下常用的做法是先直接將主庫的數(shù)據(jù)目錄整體拷貝到從庫,再啟動(dòng)復(fù)制。具體步驟如下。

1.在master節(jié)點(diǎn)創(chuàng)建測(cè)試庫和測(cè)試表

CREATE DATABASE test;Query OK, 1 row affected (0.04 sec)mysql> USE test;Database changedmysql> CREATE TABLE t(id int(10));Query OK, 0 rows affected (0.09 sec)mysql> INSERT INTO t VALUES (111);Query OK, 1 row affected (0.05 sec)mysql> INSERT INTO t VALUES (222);Query OK, 1 row affected (0.00 sec)mysql> INSERT INTO t VALUES (333);Query OK, 1 row affected (0.00 sec)

2.在主庫創(chuàng)建復(fù)制用戶

mysql> create user ’repl’@’%’ identified with mysql_native_password by ’wwwwww’;Query OK, 0 rows affected (0.03 sec)mysql> grant replication client,replication slave on *.* to ’repl’@’%’;Query OK, 0 rows affected (0.04 sec)mysql> flush privileges;Query OK, 0 rows affected (0.00 sec)

3.停止復(fù)制的所有實(shí)例,在master、slave1、slave2分別執(zhí)行

[root@master ~]# ln -s /usr/local/mysql/bin/mysqladmin /usr/bin/mysqladmin[root@master ~]# mysqladmin -hlocalhost -uroot -pwwwwww shutdown

4.復(fù)制數(shù)據(jù)至slave1、slave2

[root@master data]# cd /data[root@master data]# scp -r mysql/ slave1:/data/[root@master data]# scp -r mysql/ slave2:/data/

5.在slave1、slave2從庫執(zhí)行命令,刪除auto.cnf文件

[root@slave1 mysql]# cd /data/mysql[root@slave1 mysql]# rm -rf auto.cnf[root@slave2 mysql]# cd /data/mysql[root@slave2 mysql]# rm -rf auto.cnf

6.重啟實(shí)例,在三個(gè)節(jié)點(diǎn)都需要執(zhí)行

[root@master data]# service mysqld startStarting MySQL.. SUCCESS!

7.在主庫查看二進(jìn)制日志

mysql> show master status;+------------------+----------+--------------+------------------+-------------------+| File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |+------------------+----------+--------------+------------------+-------------------+| mysql-bin.000005 | 155 | | | |+------------------+----------+--------------+------------------+-------------------+1 row in set (0.00 sec)

8.在slave1、slave2從庫執(zhí)行命令

mysql> stop slave;Query OK, 0 rows affected (0.01 sec)mysql> change master to master_host=’192.168.2.138’, master_port=3306, master_user=’repl’, master_password=’wwwwww’, master_log_file=’mysql-bin.000005’, master_log_pos=155;Query OK, 0 rows affected, 2 warnings (0.02 sec)mysql> start slave;Query OK, 0 rows affected (0.03 sec)mysql> show slave statusG

9.在slave1、slave2從庫執(zhí)行命令查看庫和表是否同步過來

mysql> use test;Reading table information for completion of table and column namesYou can turn off this feature to get a quicker startup with -ADatabase changedmysql> show tables;+----------------+| Tables_in_test |+----------------+| t |+----------------+1 row in set (0.00 sec)mysql> select * from t;+------+| id |+------+| 111 || 222 || 333 |+------+3 rows in set (0.00 sec)

三、mysqldump聯(lián)機(jī)

脫機(jī)建立復(fù)制的需求太過理想化,大多數(shù)情況下,復(fù)制是被要求在不影響線上業(yè)務(wù)的情況下,聯(lián)機(jī)創(chuàng)建的,而且還要求對(duì)線上庫的影響越小越好。例如,復(fù)制過程化中對(duì)主庫加鎖會(huì)影響對(duì)主庫的訪問,因此通常是不被允許的。這種場(chǎng)景下有兩種備選的復(fù)制方案:使用mysqldump程序或使用如XtraBackup的第三方工具。這兩種方案有各自的適用場(chǎng)合。使用mysqldump聯(lián)機(jī)建立復(fù)制的過程如下。

1.在主庫創(chuàng)建測(cè)試的數(shù)據(jù)庫和表

mysql> CREATE DATABASE test;Query OK, 1 row affected (0.04 sec)mysql> use test;Database changedmysql> CREATE TABLE t(id int(10));Query OK, 0 rows affected (0.10 sec)mysql> INSERT INTO t VALUES(111);Query OK, 1 row affected (0.09 sec)mysql> INSERT INTO t VALUES(222);Query OK, 1 row affected (0.00 sec)mysql> INSERT INTO t VALUES(333);Query OK, 1 row affected (0.05 sec)mysql> INSERT INTO t VALUES(444);Query OK, 1 row affected (0.00 sec)

2.在主庫創(chuàng)建復(fù)制用戶

mysql> create user ’repl’@’%’ identified with mysql_native_password by ’wwwwww’;Query OK, 0 rows affected (0.01 sec)mysql> grant replication client,replication slave on *.* to ’repl’@’%’;Query OK, 0 rows affected (0.01 sec)mysql> flush privileges;Query OK, 0 rows affected (0.01 sec)

3.在slave1、slave2從庫創(chuàng)建主庫信息

mysql> change master to master_host=’192.168.2.138’, master_port=3306, master_user=’repl’, master_password=’wwwwww’;Query OK, 0 rows affected, 2 warnings (0.04 sec)

4.在slave1、slave2從庫使用mysqldump命令復(fù)制數(shù)據(jù)

[root@slave2 ~]# mysqldump --single-transaction --all-databases --master-data=1 --host=192.168.2.138 --user=root --password=wwwwww --apply-slave-statements | mysql -uroot -pwwwwww -hlocalhostmysql: [Warning] Using a password on the command line interface can be insecure.mysqldump: [Warning] Using a password on the command line interface can be insecure.

參數(shù)說明

?single-transaction參數(shù)可以對(duì)Innodb表執(zhí)行非鎖定導(dǎo)出。此選項(xiàng)將事務(wù)隔離模式設(shè)置為REPEATABLE READ,并在轉(zhuǎn)儲(chǔ)數(shù)據(jù)之前向服務(wù)器發(fā)送START TRANSACTION SQL語句。它僅適用于Innodb等事務(wù)表,因?yàn)樗鼤?huì)在發(fā)出START TRANSACTION時(shí)轉(zhuǎn)儲(chǔ)數(shù)據(jù)庫的一致狀態(tài),而不會(huì)阻塞任何應(yīng)用程序。因此這里假定:1. 所有的應(yīng)用數(shù)據(jù)表都使用Innodb引擎。2. 所有系統(tǒng)表數(shù)據(jù)在備份過程中不會(huì)發(fā)生變化。

?master-data參數(shù)會(huì)導(dǎo)致轉(zhuǎn)儲(chǔ)輸出包含類似 CHANGE MASTER TO MASTER_LOG_FILE=‘binlog.000004’, MASTER_LOG_POS=1480; 的SQL語句,該語句指示主庫的二進(jìn)制日志坐標(biāo)(文件名和位置)。如果選項(xiàng)值為2,則CHANGE MASTER TO語句將寫為SQL注釋,因此僅提供信息,不會(huì)執(zhí)行。如果參數(shù)值為1,則該語句不會(huì)寫為注釋,并在重新加載轉(zhuǎn)儲(chǔ)文件時(shí)執(zhí)行。如果未指定選項(xiàng)值,則默認(rèn)值為1。

?apply-slave-statements參數(shù)會(huì)在CHANGE MASTER TO語句之前添加STOP SLAVE語句,并在輸出結(jié)尾處添加START SLAVE語句,用來自動(dòng)開啟復(fù)制。

通過管道操作符,導(dǎo)出導(dǎo)入一步進(jìn)行,不需要中間落盤生成文件。

5.在從庫確認(rèn)復(fù)制狀態(tài)

mysql> show slave statusG

6.在從庫查看庫和表是否復(fù)制成功

use test;Reading table information for completion of table and column namesYou can turn off this feature to get a quicker startup with -ADatabase changedmysql> select * from t;+------+| id |+------+| 111 || 222 || 333 || 444 || 555 |+------+5 rows in set (0.00 sec)

mysqldump方式的優(yōu)點(diǎn)是可以進(jìn)行部分復(fù)制,如在配置文件中定義replicate-do-table=db1.*,則用這種方法可以只復(fù)制db1庫而忽略其它復(fù)制事件。缺點(diǎn)是由于mysqldump會(huì)生成主庫轉(zhuǎn)儲(chǔ)數(shù)據(jù)的SQL語句,實(shí)際是一種邏輯備份方式所以速度較慢,不適用于大庫。

四、XtraBackup聯(lián)機(jī)復(fù)制

聯(lián)機(jī)建立復(fù)制的另一種可選方案是使用XtraBackup。XtraBackup是Percona公司的開源項(xiàng)目,用以實(shí)現(xiàn)類似Innodb官方的熱備份工具InnoDB Hot Backup的功能,它支持在線熱備份,備份時(shí)不影響數(shù)據(jù)讀寫。到目前為止,最新的版本為Percona XtraBackup 8.0.6,可以從https://www.percona.com/downloads/下載安裝包。XtraBackup有很多功能和優(yōu)點(diǎn),例如支持全備、增量備份、部分備份;支持壓縮備份;備份不影響數(shù)據(jù)讀寫、事務(wù)等,但是也有缺陷不足:例如不支持脫機(jī)備份、不支持直接備份到磁帶設(shè)備、不支持Cloud Back,MyISAM的備份也會(huì)阻塞。不過這些小瑕疵不影響XtraBackup成為一款流行的MySQL備份工具。另外,注意XtraBackup只支持Linux平臺(tái),不支持Windows平臺(tái)。下面演示用XtraBackup聯(lián)機(jī)搭建主從復(fù)制的過程,主庫已經(jīng)建立了用于執(zhí)行復(fù)制的用戶repl。

在主庫創(chuàng)建復(fù)制用戶

mysql> create user ’repl’@’%’ identified with mysql_native_password by ’wwwwww’;Query OK, 0 rows affected (0.01 sec)mysql> grant replication client,replication slave on *.* to ’repl’@’%’;Query OK, 0 rows affected (0.01 sec)mysql> flush privileges;Query OK, 0 rows affected (0.01 sec)

1.在主庫和從庫都安裝XtraBackupv

[root@master ~]# yum -y install libev[root@master home]# yum localinstall percona-xtrabackup-80-8.0.6-1.el7.x86_64.rpm -y

2.配置主庫到從庫的SSH免密碼連接

[root@master home]# ssh-keygen Generating public/private rsa key pair.Enter file in which to save the key (/root/.ssh/id_rsa): Enter passphrase (empty for no passphrase): Enter same passphrase again: Your identification has been saved in /root/.ssh/id_rsa.Your public key has been saved in /root/.ssh/id_rsa.pub.The key fingerprint is:SHA256:GBLbrw17UVck8RyCa/fbYyLkSNZIRc5p+jPQmpkD+bI root@masterThe key’s randomart image is:+---[RSA 2048]----+| . .o+o+ || + +..* . || o o o*. o || . +.o*.. || ooS+oo . || =o=Bo . || o.=B++ o || .o..oo..o.|| E . o .|+----[SHA256]-----+[root@master home]# ssh-copy-id 192.168.2.138[root@master home]# ssh-copy-id 192.168.2.192[root@master home]# ssh-copy-id 192.168.2.130

3.停止從庫,并刪除從庫里面的數(shù)據(jù)

[root@slave1 home]# service mysql stop[root@slave2 home]# service mysql stop[root@slave1 home]# rm -rf /data/mysql/*[root@slave2 home]# rm -rf /data/mysql/*

4.備份數(shù)據(jù)并傳輸

[root@master tmp]# xtrabackup -uroot -pwwwwww --socket=/data/mysql/mysql.sock --no-lock --backup --compress --stream=xbstream --parallel=4 --target-dir=./ | ssh root@192.168.2.192 'xbstream -x -C /data/mysql/ --decompress'

執(zhí)行過程中報(bào)錯(cuò),

190606 01:21:47 >> log scanned up to (19597291)

190606 01:21:47 Selecting LSN and binary log position from p_s.log_status

Error: failed to fetch query result SELECT server_uuid, local, replication, storage_engines FROM performance_schema.log_status: Access denied; you need (at least one of) the BACKUP_ADMIN privilege(s) for this operation

mysql> grant BACKUP_ADMIN on *.* to ’root’@’%’;Query OK, 0 rows affected (0.01 sec)

行如下命令,刪除192.168.2.192:/data/mysql/*的內(nèi)容,再次執(zhí)行命令,發(fā)現(xiàn)已經(jīng)正確了。成功執(zhí)行如下所示:

淺談MySQL8.0 異步復(fù)制的三種方式

這條命令連接主庫,進(jìn)行并行壓縮流式備份,同時(shí)將備份通過管道操作符傳輸?shù)綇膸欤⒅苯咏鈮嚎s到從庫的數(shù)據(jù)目錄。所有操作一條命令完成,不需要中間落盤生成文件。

5.在從庫恢復(fù)備份

[root@slave1 /]# xtrabackup --prepare --target-dir=/data/mysql[root@slave2 /]# xtrabackup --prepare --target-dir=/data/mysql

6.在從庫查看二進(jìn)制bin-log日志

[root@slave1 mysql]# cat xtrabackup_binlog_info mysql-bin.000008 155[root@slave2 mysql]# cat xtrabackup_binlog_info mysql-bin.000009 155

7.啟動(dòng)從庫

[root@slave1 data]# service mysqld startStarting MySQL... SUCCESS! [root@slave2 data]# service mysqld startStarting MySQL... SUCCESS!

8.創(chuàng)建主庫信息,其中的master_log_file和master_log_pos值來自第6步

mysql> change master to master_host=’192.168.2.138’, master_port=3306, master_user=’repl’, master_password=’wwwwww’, master_log_file=’mysql-bin.000008’, master_log_pos=155;Query OK, 0 rows affected, 2 warnings (0.04 sec)mysql> start slave;Query OK, 0 rows affected (0.00 sec)mysql> show slave statusG

9.在從庫測(cè)試數(shù)據(jù)

mysql> use test;Reading table information for completion of table and column namesYou can turn off this feature to get a quicker startup with -ADatabase changedmysql> select * from t;+------+| id |+------+| 111 || 222 || 333 || 444 || 555 |+------+5 rows in set (0.00 sec)

XtraBackup是物理復(fù)制,性能比mysqldump高的多,而且對(duì)主庫的影響極小,非常適用于從頭聯(lián)機(jī)創(chuàng)建高負(fù)載、大數(shù)據(jù)量、全實(shí)例從庫的場(chǎng)景。

到此這篇關(guān)于淺談MySQL8.0 異步復(fù)制的三種方式的文章就介紹到這了,更多相關(guān)MySQL8.0 異步復(fù)制內(nèi)容請(qǐng)搜索好吧啦網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持好吧啦網(wǎng)!

標(biāo)簽: MySQL 數(shù)據(jù)庫
相關(guān)文章:
主站蜘蛛池模板: 麻江县| 无极县| 迭部县| 南涧| 郎溪县| 梁河县| 天津市| 淮滨县| 长丰县| 奉化市| 方山县| 盈江县| 海门市| 马公市| 西充县| 醴陵市| 扎赉特旗| 德令哈市| 翁源县| 龙井市| 晴隆县| 青浦区| 阳东县| 泽州县| 阜新| 新昌县| 麻栗坡县| 西安市| 张家口市| 长兴县| 普兰店市| 永新县| 广丰县| 集贤县| 昌平区| 永顺县| 芜湖县| 新宾| 江陵县| 扎兰屯市| 化州市|