当前位置:网站首页>Mongodb cross host database copy and common commands

Mongodb cross host database copy and common commands

2022-06-27 09:59:00 Hilbob

One 、 Tool preparation

Copy mongodb The database inside needs tools mongodump and mongorestore. download mongodb You did not download the above tools , You can download and decompress it directly from here : MongoDB Database Tools

Two 、 Data copy

Go to the path extracted by the above tool , Use the following command to copy the specified database :

mongodump -h dbhost -d dbname -o dbdirectory
#  explain 
-h:
MongoDB  Address of the server , for example :127.0.0.1, Of course, you can also specify the port number :127.0.0.1:27017
-d:
 Database instances that need to be backed up , for example :test
-o:
 Backup data storage location , for example :c:\data\dump, Of course, the directory needs to be established in advance , After the backup is complete , The system is automatically in dump Create one in the directory test Catalog , This directory contains the backup data of the database instance .

3、 ... and 、 Data transfer and recovery

Use the above mongodump The file generated by the command is copied to another host
Execute on another host :

mongorestore -h <hostname><:port> -d dbname <path>
#  explain 
--host <:port>, -h <:port>:
MongoDB Address of the server , The default is : localhost:27017
--db , -d :
 The name of the database instance to be recovered . for example :test, Of course, this name can also be different from the backup time , such as test2
--drop:
 When you recover , Delete the current data first , Then restore the backup data . That is to say , After recovery , The data added and modified after backup will be deleted , Use with caution !
<path>:
mongorestore  The last parameter , Set the location of backup data , That is, the path where the data replication is located , for example :c:\data\dump\test.
 You can't specify at the same time  <path>  and  --dir  Options ,--dir You can also set the backup directory .
--dir:
 Specify the directory to back up 
 You can't specify at the same time  <path>  and  --dir  Options .

This will be done on another host mongodb Restore the copied database .

Four 、mongodb Common commands

 Activate mongodb Environmental Science 
> mongod

 Use after activating the environment mongodb
> mongo

 See which databases are currently available 
> show dbs  

 Check which database is currently connected to , You can input db    
> db  

 Switch to test Under database    
> use test  
   
 You want to view the tables under the current database collection  
> show collections 

Reference resources

Novice tutorial
mongodb file

原网站

版权声明
本文为[Hilbob]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/178/202206270942053254.html