Mysql
 sql >> Cơ Sở Dữ Liệu >  >> RDS >> Mysql

Sao lưu cơ sở dữ liệu logic bằng MySQL Shell

Mysqldump là một công cụ sao lưu logic phổ biến cho MySQL, được viết bởi Igor Romanenko.

Mysqldump thực hiện sao lưu lôgic (tập hợp các câu lệnh SQL). Theo mặc định, mysqldump không kết xuất các bảng information_schema và nó không bao giờ lấy performance_schema. Nhưng nhược điểm chính của mysqldump là nó chỉ sử dụng một luồng trong khi sao lưu và khôi phục. (Ngay cả máy chủ của bạn có 64 lõi). Để khắc phục nhược điểm này, MySQL đã giới thiệu các tiện ích mới trên máy khách Shell. Trong blog này, tôi sẽ giải thích các tiện ích sao lưu mới này.

Tổng quan về MySQL Shell

MySQL shell là một trình soạn thảo mã và ứng dụng khách mạnh mẽ và nâng cao dành cho máy chủ MySQL. MySQL shell 8.0.21 bao gồm một số tiện ích mới thú vị để tạo kết xuất hợp lý và thực hiện khôi phục hợp lý cho toàn bộ phiên bản cơ sở dữ liệu, bao gồm cả người dùng.

MySQL shell 8.0.22 bao gồm một bản sao lưu hợp lý của các bảng cụ thể và khôi phục.

Tiện ích

  • use.dumpInstance () - Kết xuất toàn bộ phiên bản cơ sở dữ liệu, bao gồm cả người dùng
  • use.dumpSchemas () - Kết xuất một tập hợp các lược đồ
  • use.loadDump () - Tải kết xuất vào cơ sở dữ liệu đích
  • use.dumpTables () - Tải các bảng và chế độ xem cụ thể.

use.dumpInstance ()

Tiện ích dumpInstance () sẽ kết xuất tất cả các cơ sở dữ liệu được trình bày trong thư mục dữ liệu MySQL. Nó sẽ loại trừ các lược đồ information_schema, mysql, ndbinfo, performance_schema và sys trong khi xử lý kết xuất.

Cú pháp

util.dumpInstance(outputUrl[, options]) 

Nó sẽ kết xuất vào hệ thống tệp cục bộ, outputUrl là một chuỗi chỉ định đường dẫn đến thư mục cục bộ nơi đặt các tệp kết xuất. Bạn có thể chỉ định đường dẫn tuyệt đối hoặc đường dẫn liên quan đến thư mục làm việc hiện tại.

Trong tiện ích này, có một tùy chọn chạy khô để kiểm tra các lược đồ và xem các vấn đề tương thích, sau đó chạy kết xuất với các tùy chọn tương thích thích hợp được áp dụng để loại bỏ các vấn đề.

Tùy chọn

Chúng ta hãy xem xét một số tùy chọn quan trọng cho tính khả thi này.

ocimds:[Đúng | Sai]

Khi tùy chọn này được đặt thành true, Nó sẽ kiểm tra từ điển dữ liệu, từ điển chỉ mục và các tùy chọn mã hóa trong các câu lệnh CREATE TABLE được nhận xét trong tệp DDL, để đảm bảo rằng tất cả các bảng đều nằm trong Thư mục dữ liệu MySQL và sử dụng mã hóa lược đồ mặc định.

Và nó sẽ kiểm tra bất kỳ công cụ lưu trữ nào trong các câu lệnh CREATE TABLE ngoài InnoDB, để cấp các đặc quyền không phù hợp cho người dùng hoặc vai trò và các vấn đề tương thích khác.

Nếu tìm thấy bất kỳ câu lệnh SQL nào không phù hợp, một ngoại lệ sẽ được đưa ra và kết xuất bị tạm dừng.

Vì vậy, chúng tôi đề xuất sử dụng tùy chọn dryRun để liệt kê tất cả các vấn đề với các mục trong bãi chứa trước khi bắt đầu quá trình kết xuất. Sử dụng tùy chọn tương thích để tự động khắc phục các sự cố trong đầu ra kết xuất.

Lưu ý:Tùy chọn này chỉ hỗ trợ cho tiện ích kết xuất phiên bản và tiện ích kết xuất lược đồ.

Ví dụ 1

MySQL  localhost:3306 ssl  cart  JS > util.dumpInstance("/home/vagrant/production_backup", {ocimds: true,compatibility: ["strip_restricted_grants"]})

Acquiring global read lock

Global read lock acquired

All transactions have been started

Locking instance for backup

NOTE: Backup lock is not supported in MySQL 5.7 and DDL changes will not be blocked. The dump may fail with an error or not be completely consistent if schema changes are made while dumping.

Global read lock has been released

Checking for compatibility with MySQL Database Service 8.0.22

NOTE: MySQL Server 5.7 detected, please consider upgrading to 8.0 first. You can check for potential upgrade issues using util.checkForServerUpgrade().

NOTE: User 'backupuser'@'localhost' had restricted privileges (RELOAD, SUPER, CREATE TABLESPACE) removed

NOTE: User 'root'@'127.0.0.1' had restricted privileges (RELOAD, SHUTDOWN, FILE, SUPER, CREATE TABLESPACE) removed

NOTE: User 'root'@'::1' had restricted privileges (RELOAD, SHUTDOWN, FILE, SUPER, CREATE TABLESPACE) removed

NOTE: User 'root'@'localhost' had restricted privileges (RELOAD, SHUTDOWN, FILE, SUPER, CREATE TABLESPACE, PROXY) removed

ERROR: Table 'cart'.'sales' uses unsupported storage engine MyISAM (fix this with 'force_innodb' compatibility option)

Compatibility issues with MySQL Database Service 8.0.22 were found. Please use the 'compatibility' option to apply compatibility adaptations to the dumped DDL.

Util.dumpInstance: Compatibility issues were found (RuntimeError)

Vì vậy, chúng tôi có một bảng myisam trong cơ sở dữ liệu giỏ hàng của mình. Tùy chọn chạy khô rõ ràng sẽ giải quyết lỗi.

Nếu bạn muốn tự động sửa các lỗi này trong tệp kết xuất của mình, hãy chuyển tùy chọn tương thích làm đối số trong lệnh của bạn.

Ví dụ 2

MySQL  localhost:3306 ssl  cart  JS > util.dumpInstance("/home/vagrant/production_backup", {dryRun: true ,ocimds: true,compatibility: ["strip_restricted_grants","force_innodb"]})

Acquiring global read lock

Global read lock acquired

All transactions have been started

Locking instance for backup

NOTE: Backup lock is not supported in MySQL 5.7 and DDL changes will not be blocked. The dump may fail with an error or not be completely consistent if schema changes are made while dumping.

Global read lock has been released

Checking for compatibility with MySQL Database Service 8.0.22

NOTE: MySQL Server 5.7 detected, please consider upgrading to 8.0 first. You can check for potential upgrade issues using util.checkForServerUpgrade().

NOTE: User 'backupuser'@'localhost' had restricted privileges (RELOAD, SUPER, CREATE TABLESPACE) removed

NOTE: User 'root'@'127.0.0.1' had restricted privileges (RELOAD, SHUTDOWN, FILE, SUPER, CREATE TABLESPACE) removed

NOTE: User 'root'@'::1' had restricted privileges (RELOAD, SHUTDOWN, FILE, SUPER, CREATE TABLESPACE) removed

NOTE: User 'root'@'localhost' had restricted privileges (RELOAD, SHUTDOWN, FILE, SUPER, CREATE TABLESPACE, PROXY) removed

NOTE: Table 'cart'.'sales' had unsupported engine MyISAM changed to InnoDB

Compatibility issues with MySQL Database Service 8.0.22 were found and repaired. Please review the changes made before loading them.

Writing global DDL files

Writing users DDL

Writing DDL for schema `cart`

Writing DDL for table `cart`.`salaries`

Writing DDL for table `cart`.`sales`

Writing DDL for table `cart`.`t1`

Preparing data dump for table `cart`.`salaries`

Data dump for table `cart`.`salaries` will be chunked using column `id`

Preparing data dump for table `cart`.`sales`

Data dump for table `cart`.`sales` will be chunked using column `id`

Preparing data dump for table `cart`.`t1`

NOTE: Could not select a column to be used as an index for table `cart`.`t1`. Chunking has been disabled for this table, data will be dumped to a single file.

Bây giờ chạy khô là tốt và không có ngoại lệ. Cho phép chạy lệnh phiên bản kết xuất để sao lưu phiên bản.

Thư mục đích phải trống trước khi quá trình xuất diễn ra. Nếu thư mục chưa tồn tại trong thư mục mẹ của nó, tiện ích sẽ tạo nó.

Ví dụ 3

MySQL  localhost:3306 ssl  cart  JS > util.dumpInstance("/home/vagrant/production_backup", {compatibility: ["strip_restricted_grants","force_innodb"],threads : 12})

Acquiring global read lock

Global read lock acquired

All transactions have been started

Locking instance for backup

Global read lock has been released

Writing global DDL files

Writing users DDL

Writing DDL for schema `cart`

Writing DDL for view `cart`.`price`

Writing DDL for table `cart`.`dummy`

Writing DDL for table `cart`.`salaries`

Writing DDL for schema `sbtest`

Writing DDL for table `sbtest`.`sbtest1`

Writing DDL for table `sbtest`.`sbtest10`

.

.

.

1 thds dumping - 99% (624.55K rows / ~625.40K rows), 896.15K rows/s, 10.13 MB/s uncompressed, 3.73 MB/s compressed 

Duration: 00:00:00s                                                                                               

Schemas dumped: 2                                                                                                 

Tables dumped: 18                                                                                                 

Uncompressed data size: 7.14 MB                                                                                   

Compressed data size: 2.79 MB                                                                                     

Compression ratio: 2.6                                                                                            

Rows written: 624550                                                                                              

Bytes written: 2.79 MB                                                                                            

Average uncompressed throughput: 7.14 MB/s                                                                        

Average compressed throughput: 2.79 MB/s

Ở trên chúng tôi đã sử dụng một tùy chọn tương thích. Vì vậy, trong khi xử lý kết xuất, nó sẽ chuyển đổi các bảng myisam thành innodb và lưu trữ chúng vào tệp.

Nhật ký

[[email protected] production_backup]$ cat [email protected]

-- MySQLShell dump 1.0.1  Distrib Ver 8.0.22 for Linux on x86_64 - for MySQL 8.0.22 (MySQL Community Server (GPL)), for Linux (x86_64)

--

-- Host: localhost    Database: cart    Table: sales

-- ------------------------------------------------------

-- Server version 5.7.32

--

-- Table structure for table `sales`

--

/*!40101 SET @saved_cs_client     = @@character_set_client */;

/*!50503 SET character_set_client = utf8mb4 */;

CREATE TABLE IF NOT EXISTS `sales` (

  `id` int(10) unsigned NOT NULL AUTO_INCREMENT,

  `name` varchar(30) DEFAULT NULL,

  `address` varchar(30) DEFAULT NULL,

  PRIMARY KEY (`id`)

) ENGINE=InnoDB DEFAULT CHARSET=latin1;

/*!40101 SET character_set_client = @saved_cs_client */;

Nếu bạn đang sử dụng mysqldump, nó sẽ lưu kết quả đầu ra vào một tệp duy nhất. Nhưng ở đây nó tạo ra nhiều tệp hơn như chúng tôi sẽ giải thích bên dưới.

Đây là các tệp có sẵn trong thư mục sao lưu.

[[email protected] production_backup]$ ls -lrth

total 52K

-rw-r-----. 1 vagrant vagrant  707 Nov  6 02:36 @.json

-rw-r-----. 1 vagrant vagrant  287 Nov  6 02:36 cart.json

-rw-r-----. 1 vagrant vagrant  240 Nov  6 02:36 @.sql

-rw-r-----. 1 vagrant vagrant  240 Nov  6 02:36 @.post.sql

-rw-r-----. 1 vagrant vagrant 2.6K Nov  6 02:36 @.users.sql

-rw-r-----. 1 vagrant vagrant  733 Nov  6 02:36 [email protected]

-rw-r-----. 1 vagrant vagrant  486 Nov  6 02:36 cart.sql

-rw-r-----. 1 vagrant vagrant  575 Nov  6 02:36 [email protected]

-rw-r-----. 1 vagrant vagrant    8 Nov  6 02:36 [email protected]@0.tsv.zst.idx

-rw-r-----. 1 vagrant vagrant    8 Nov  6 02:36 [email protected]@@1.tsv.zst.idx

-rw-r-----. 1 vagrant vagrant   47 Nov  6 02:36 [email protected]@0.tsv.zst

-rw-r-----. 1 vagrant vagrant   24 Nov  6 02:36 [email protected]@@1.tsv.zst

-rw-r-----. 1 vagrant vagrant  252 Nov  6 02:36 @.done.json
  • Tệp @ .json này chứa thông tin chi tiết về máy chủ và danh sách người dùng, tên cơ sở dữ liệu và bộ ký tự của họ.
  • Tệp cart.json này chứa các tên chế độ xem, SP, hàm cùng với danh sách các bảng.
  • Các tệp @ .sql và @ .post.sql này chứa thông tin chi tiết về phiên bản máy chủ MySQL.
  • Tệp @ .users.sql này chứa danh sách người dùng cơ sở dữ liệu.
  • Tệp [email protected] này chứa cấu trúc bảng.
  • Tệp cart.sql này chứa một câu lệnh cơ sở dữ liệu.
  • Tệp [email protected] này chứa tên cột và bộ ký tự.
  • Tệp example @ sqldat.com @ 0.tsv.zst.idx là một tệp nhị phân. Nó lưu trữ thống kê chỉ mục bảng.
  • Tệp example @ sqldat.com @ 0.tsv.zst là một tệp nhị phân và nó lưu trữ dữ liệu.
  • Tệp @ .done.json này chứa thời gian kết thúc sao lưu và kích thước tệp dữ liệu tính bằng KB.

use.dumpSchemas ()

Nó sẽ kết xuất các lược đồ cụ thể mà bạn đề cập trong các đối số cho tiện ích này.

Cú pháp

​util.dumpSchemas(schemas, outputUrl[, options])

Ví dụ

MySQL  localhost:3306 ssl  cart  JS > util.dumpSchemas(["cart"], "/home/vagrant/production_backup",{compatibility: ["strip_restricted_grants","force_innodb"],threads :12})

Acquiring global read lock

Global read lock acquired

All transactions have been started

Locking instance for backup

NOTE: Backup lock is not supported in MySQL 5.7 and DDL changes will not be blocked. The dump may fail with an error or not be completely consistent if schema changes are made while dumping.

Global read lock has been released

Writing global DDL files

Writing DDL for table `cart`.`price_tag`

Writing DDL for schema `cart`

Writing DDL for table `cart`.`salaries`

Writing DDL for table `cart`.`sales`

NOTE: Table 'cart'.'sales' had unsupported engine MyISAM changed to InnoDB

Preparing data dump for table `cart`.`price_tag`

Data dump for table `cart`.`price_tag` will be chunked using column `id`

Data dump for table `cart`.`price_tag` will be written to 1 file

Preparing data dump for table `cart`.`salaries`

Data dump for table `cart`.`salaries` will be chunked using column `id`

Data dump for table `cart`.`salaries` will be written to 2 files

Preparing data dump for table `cart`.`sales`

Data dump for table `cart`.`sales` will be chunked using column `id`

Running data dump using 12 threads.

NOTE: Progress information uses estimated values and may not be accurate.

Data dump for table `cart`.`sales` will be written to 1 file                                               

1 thds dumping - 150% (3 rows / ~2 rows), 0.00 rows/s, 0.00 B/s uncompressed, 0.00 B/s compressed          

Duration: 00:00:00s                                                                              

Schemas dumped: 1                                                                                

Tables dumped: 3                                                                                 

Uncompressed data size: 53 bytes                                                                 

Compressed data size: 0 bytes                                                                    

Compression ratio: 53.0                                                                          

Rows written: 3                                                                                  

Bytes written: 0 bytes                                                                           

Average uncompressed throughput: 53.00 B/s                                                       

Average compressed throughput: 0.00 B/s                

use.dumpTables

Nếu bạn muốn kết xuất các bảng cụ thể, chúng ta có thể sử dụng tiện ích dumpTables.

Đối với các bảng lớn hơn, mysqldump sẽ mất nhiều thời gian hơn. Sử dụng tiện ích dumpTables để giảm thời gian.

Cú pháp

util.dumpTables(schema, tables, outputUrl[, options])

Ví dụ

util.dumpTables("sbtest", [ "sbtest14", "sbtest16" ], "/home/vagrant/specific_table",{dryRun: true})

​ MySQL  localhost:33060+ ssl  sbtest  JS > util.dumpTables("sbtest", [ "sbtest14", "sbtest16" ], "/home/vagrant/specific_table",{threads: 12})

Acquiring global read lock

Global read lock acquired

All transactions have been started

Locking instance for backup

Global read lock has been released

Writing global DDL files

Writing DDL for table `sbtest`.`sbtest16`

Writing DDL for table `sbtest`.`sbtest14`

Preparing data dump for table `sbtest`.`sbtest16`

Data dump for table `sbtest`.`sbtest16` will be chunked using column `id`

Preparing data dump for table `sbtest`.`sbtest14`

Data dump for table `sbtest`.`sbtest14` will be chunked using column `id`

Running data dump using 12 threads.

NOTE: Progress information uses estimated values and may not be accurate.

Data dump for table `sbtest`.`sbtest16` will be written to 1 file

Data dump for table `sbtest`.`sbtest14` will be written to 1 file

1 thds dumping - 99% (78.07K rows / ~78.08K rows), 0.00 rows/s, 0.00 B/s uncompressed, 0.00 B/s compressed

Duration: 00:00:00s                                                                                       

Schemas dumped: 1                                                                                         

Tables dumped: 2                                                                                          

Uncompressed data size: 892.39 KB                                                                         

Compressed data size: 348.91 KB                                                                           

Compression ratio: 2.6                                                                                    

Rows written: 78068                                                                                       

Bytes written: 348.91 KB                                                                                  

Average uncompressed throughput: 892.39 KB/s                                                              

Average compressed throughput: 348.91 KB/s 

Tiện ích tải bản đổ

Tiện ích tải kết xuất cung cấp luồng dữ liệu đến bộ lưu trữ từ xa, tải song song các bảng hoặc khối bảng, theo dõi trạng thái tiến trình, khả năng tiếp tục và đặt lại và tùy chọn tải đồng thời trong khi kết xuất vẫn đang diễn ra.

Lưu ý:Tiện ích tải kết xuất sử dụng câu lệnh LOAD DATA LOCAL INFILE, vì vậy chúng tôi cần bật tham số local_infile này trên toàn cầu trong khi nhập.

Tiện ích tải kết xuất kiểm tra xem biến hệ thống sql_require_primary_key có được đặt thành BẬT không và nếu có, sẽ trả về lỗi nếu có một bảng trong tệp kết xuất không có khóa chính.

Cú pháp

util.loadDump(url[, options])

Ví dụ

MySQL  localhost:3306 ssl  sbtest  JS > util.loadDump("/home/vagrant/specific_table", {progressFile :"/home/vagrant/specific_table/log.json",threads :12})

Loading DDL and Data from '/home/vagrant/specific_table' using 12 threads.

Opening dump...

Target is MySQL 8.0.22. Dump was produced from MySQL 8.0.22

Checking for pre-existing objects...

Executing common preamble SQL

[Worker006] Executing DDL script for `sbtest`.`sbtest1`

[Worker004] Executing DDL script for `sbtest`.`sbtest12`

2 thds loading \ 100% (892.39 KB / 892.39 KB), 0.00 B/s, 0 / 2 tables done[Worker001] [email protected]@@0.tsv.zst: Records: 39034  Deleted: 0  Skipped: 0  Warnings: 0

[Worker005] [email protected]@@0.tsv.zst: Records: 39034  Deleted: 0  Skipped: 0  Warnings: 0

Executing common postamble SQL                                                                                                   

2 chunks (78.07K rows, 892.39 KB) for 2 tables in 1 schemas were loaded in 1 sec (avg throughput 892.39 KB/s)

0 warnings were reported during the load.

Theo mặc định, chỉ mục văn bản đầy đủ cho một bảng chỉ được tạo sau khi bảng được tải hoàn toàn, điều này giúp tăng tốc quá trình nhập.

Bạn cũng có thể chọn tắt tính năng tạo chỉ mục trong quá trình nhập và tạo chỉ mục sau đó.

Tiện ích tải kết xuất nhập qua nhiều luồng để tối đa hóa tính song song. Nếu các tệp kết xuất được nén bởi các tiện ích kết xuất của MySQL Shell, thì tiện ích tải kết xuất sẽ xử lý giải nén.

Bạn có thể chọn các bảng hoặc lược đồ riêng lẻ để nhập hoặc loại trừ khỏi quá trình nhập.

Bạn có thể chọn bỏ qua ghi nhật ký nhị phân trên phiên bản MySQL đích trong quá trình nhập bằng câu lệnh SET sql_log_bin =0.

Kết luận

Đây là một trong những tiện ích mạnh mẽ trong MySQL 8.0. Bây giờ có thể kết xuất từ ​​MySQL 5.6 và tải các kết xuất này vào MySQL 5.7 hoặc 8.0. Nhưng kết xuất tài khoản người dùng không được hỗ trợ khi kết xuất từ ​​MySQL 5.6. Trong blog tiếp theo của tôi, chúng ta sẽ so sánh tốc độ sao lưu / khôi phục của MySQLdump và tiện ích shell.


  1. Database
  2.   
  3. Mysql
  4.   
  5. Oracle
  6.   
  7. Sqlserver
  8.   
  9. PostgreSQL
  10.   
  11. Access
  12.   
  13. SQLite
  14.   
  15. MariaDB
  1. MySQL - Nhận Chi phí Truy vấn Cuối cùng Sử dụng TÌNH TRẠNG HIỂN THỊ NHƯ ‘Last_Query_Cost’

  2. Cách thay thế MySQL bằng Percona trên Plesk CentOS 7

  3. Khắc phục sự cố sao chép MySQL:Phần thứ hai

  4. Cách tạo blog trong cơ sở dữ liệu PHP và MySQL - Bài đăng của quản trị viên

  5. Lấy tất cả các hàng cha trong một truy vấn SQL