Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use MySQL to learn backup and recovery in MongoDB

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >

Share

Shulou(Shulou.com)05/31 Report--

In this issue, the editor will bring you about how to use MySQL to learn backup and recovery in MongoDB. The article is rich in content and analyzes and describes for you from a professional point of view. I hope you can get something after reading this article.

In the case of lost or damaged database tables, it is important to back up your database. If a system crash occurs, you must want to be able to restore your watch to the state it was in at the time of the crash as little as possible.

How to use MySQL to learn backup and recovery of MongoDB

1. MySQL backup and recovery

MySQL backup methods are generally divided into the following three types:

Copy database files directly

Back up the database using mysqlhotcopy

Back up the database using mysqldump

(1) copy database files directly

The most direct, fast and convenient, the disadvantage is basically unable to achieve incremental backup. To ensure data consistency, you need to execute the following SQL statement in front of the backend file:

FLUSHTABLESWITHREADLOCK

That is, all the data in memory is flushed to disk, and the data table is locked to ensure that no new data is written during the copy process. The recovery of the data backed up by this method is also very simple and can be copied directly back to the original database directory.

However, for Innodb type tables, you also need to back up its log file, the ib_logfile* file. Because when the Innodb table is corrupted, you can rely on these log files to recover.

(2) back up the database using mysqlhotcopy

Mysqlhotcopy is a perl program. It uses LOCKTABLES, FLUSHTABLES, and cp or scp to quickly back up the database. It is the fastest way to back up a database or a single table, but it can only run on a local server, and mysqlhotcopy can only back up MyISAM tables, but there is no trick for Innodb tables.

(3) back up the database using mysqldump

Mysqldump is a SQL-level backup that guides the datasheet into the SQL foot of this article, which is relatively appropriate when upgrading between different MySQL versions, which is also the most mainstream backup method.

2. MongoDB backup and recovery

MongoDB provides two commands to back up (mongodump) and restore (mongorestore) databases.

(1), mongodump backup tool

Let's take a look at the help information for this tool:

[chinastor.com-root@localhostbin] #. / mongodump--helpoptions:--helpproducehelpmessage-v [--verbose] bemoreverbose (includemultipletimesformoreverbositye.g.-vvvvv)-h [--host] argmongohosttoconnectto (/ S1 disabledbydefault)-- portargserverport.Canalsouse--hosthostname:port--ipv6enableIPv6support (disabledbydefault)-u [--username] argusername-p [--password] argpassword--dbpathargdirectlyaccessmongoddatabasefilesinthegivenpath,insteadofconnectingtoamongodserver-needstolockthedatadirectory,socannotbeusedifamongodiscurrentlyaccessingthesamepath--directoryperdbifdbpathspecified Eachdbisinaseparatedirectory-d [- db] argdatabasetouse-c [--collection] argcollectiontouse (somecommands)-o [--out] arg (= dump) outputdirectoryor "-" forstdout-q [--query] argjsonquery--oplogUseoplogforpoint-in-timesnapshotting--repairtrytorecoveracrasheddatabase [chinastor.com-root@localhostbin] #

How to use MySQL to learn backup and recovery of MongoDB

For example, we have a library called "foo" in our system. Here we will demonstrate how to back up this library:

[chinastor.com-root@localhostbin] #. / mongodump-dfoo-o/data/dumpconnectedto:127.0.0.1DATABASE:footo/data/dump/foofoo.system.indexesto/data/dump/foo/system.indexes.bson3objectsfoo.system.usersto/data/dump/foo/system.users.bson1objectsfoo.t2to/data/dump/foo/t2.bson1objectsfoo.t1to/data/dump/foo/t1.bson2objects [chinastor.com-root@localhostbin] #

Through the information returned by the tool, we can see that the data in foo has been backed up to a file in bson format. Next, let's go to the backup directory to verify it:

[chinastor.com-root@localhostdump] # ll/data/dump/foo/ Total 16-rw-r--r--1rootroot19304-2211:55system.indexes.bson-rw-r--r--1rootroot9104-2211:55system.users.bson-rw-r--r--1rootroot6604-2211:55t1.bson-rw-r--r--1rootroot4904-2211:55t2.bson [chinastor.com-root@localhostdump] #

It turns out that the tables in the foo library have been backed up successfully, and next we will demonstrate how to restore the backup.

(2), mongorestore recovery tool

Let's take a look at the help information for this tool:

[chinastor.com-root@localhostbin] #. / mongorestore--helpusage:./mongorestore [options] [directoryorfilenametorestorefrom] options:--helpproducehelpmessage-v [--verbose] bemoreverbose (includemultipletimesformoreverbositye.g.-vvvvv)-h [--host] argmongohosttoconnectto (/ S1 host s2forsets)-- portargserverport.Canalsouse--hosthostname:port--ipv6enableIPv6support (disabledbydefault)-u [--username] argusername-p [--password] argpassword--dbpathargdirectlyaccessmongoddatabasefilesinthegivenpath,insteadofconnectingtoamongodserver-needstolockthedatadirectory,socannotbeusedifamongodiscurrentlyaccessingthesamepath--directoryperdbifdbpathspecified Eachdbisinaseparatedirectory-d [- db] argdatabasetouse-c [--collection] argcollectiontouse (somecommands)-- objcheckvalidateobjectbeforeinserting--filterargfiltertoapplybeforeinserting--dropdropeachcollectionbeforeimport--oplogReplayreplayoplogforpoint-in-timerestore [chinastor.com-root@localhostbin] #

For example, we first delete the "foo" library:

[chinastor.com-root@localhostbin] #. / mongoMongoDBshellversion:1.8.1connectingto:test > usefooswitchedtodbfoo > db.dropDatabase (); {"dropped": "foo", "ok": 1} > showdbsadmin0.0625GBlocal (empty) test0.0625GB >

Then we will show you how to restore this library:

[chinastor.com-root@localhostbin] #. / mongorestore--directoryperdb/data/dumpconnectedto:127.0.0.1SunApr2212:01:27/data/dump/foo/t1.bsonSunApr2212:01: 27goingintonamespace[foo.t1] SunApr2212:01:272objectsfoundSunApr2212:01:27/data/dump/foo/t2.bsonSunApr2212:01: 27goingintonamespace[foo.t2] SunApr2212:01:271objectsfoundSunApr2212:01:27/data/dump/foo/system.users.bsonSunApr2212:01: 27goingintonamespace[fo o.system.users] SunApr2212:01:271objectsfoundSunApr2212:01 : 27/data/dump/foo/system.indexes.bsonSunApr2212:01: 27goingintonamespace [foo.system.programs] SunApr2212:01:27 {name: "_ id_" Ns: "foo.system.users", key: {_ id:1}, vkey 0} SunApr2212:01:27 {name: "_ id_", ns: "foo.t2", key: {_ id:1}, vkey 0} SunApr2212:01:27 {name: "_ id_", ns: "foo.t1", key: {_ id:1}, v0 vehicle} SunApr2212:01:273objectsfound [chinastor.com-root@localhostbin] #

Through the information returned by the tool, we can see that the data in foo has been restored. Next, let's go to the library to verify it:

[chinastor.com-root@localhostbin] #. / mongoMongoDBshellversion:1.8.1connectingto:test > usefooswitchedtodbfoo > showcollections;system.indexessystem.userst1t2 >

It turns out that the foo database table has been successfully restored.

The above is the editor for you to share how to use MySQL to learn backup and recovery in MongoDB, if you happen to have similar doubts, you might as well refer to the above analysis to understand. If you want to know more about it, you are welcome to follow the industry information channel.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Database

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report