国产成人精品久久免费动漫-国产成人精品天堂-国产成人精品区在线观看-国产成人精品日本-a级毛片无码免费真人-a级毛片毛片免费观看久潮喷

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

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

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

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

實驗環(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.查看主庫二進制信息

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語句是在起始位置點后執(zhí)行的,因此可以正常復(fù)制到從庫,查詢mysql.user表即可確認。

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

二、脫機

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

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

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ù)制的所有實例,在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.重啟實例,在三個節(jié)點都需要執(zhí)行

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

7.在主庫查看二進制日志

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)機

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

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

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

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

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

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

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

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

執(zhí)行過程中報錯,

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ù)制的三種方式

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

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

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

6.在從庫查看二進制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.啟動從庫

[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.在從庫測試數(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高的多,而且對主庫的影響極小,非常適用于從頭聯(lián)機創(chuàng)建高負載、大數(shù)據(jù)量、全實例從庫的場景。

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

相關(guān)文章:
主站蜘蛛池模板: 国产精品一一在线观看 | 国产成人咱精品视频免费网站 | 日本精品久久久久久久 | 欧美视频一区二区三区四区 | 美女午夜色视频在线观看 | 国产美女91视频 | 国产久草视频在线 | 精品国产96亚洲一区二区三区 | 国产欧美成人一区二区三区 | 久青草青综合在线视频 | 日本阿v精品视频在线观看 日本阿v视频在线观看高清 | 午夜在线精品不卡国产 | 国产美女无遮挡软件 | 久久亚洲国产欧洲精品一 | 亚洲操图| 国产主播大尺度精品福利 | 亚洲毛片免费视频 | 成人在线视频国产 | 男人透女人超爽视频免费 | avav在线看| 台湾黄三级高清在线观看播放 | 欧美yyy| 国产成人香蕉在线视频网站 | 在线播放波多野结衣 | 亚洲成人免费视频 | 国产精品1区 2区 3区 | 成人做爰视频www片 成人做爰视频www视频 | 国产日韩欧美视频 | 8888奇米四色在线 | 欧美精品 日韩 | 亚洲黄色小视频 | 欧美videofree性欧美另类 | 久久久久久久久久久视频国内精品视频 | 免费一级欧美大片久久网 | a级毛片毛片免费观看永久 a级毛片毛片免费很很综合 | 欧美aaaaaaaa| 国产精品欧美亚洲 | 亚洲视频手机在线 | 狠狠色丁香婷婷综合久久来 | 日本特黄特色大片免费看 | 黄在线观看在线播放720p |