mongo複製資料庫和表

2021-09-02 20:35:29 字數 765 閱讀 3388

1.命令:

db.copydatabase(fromdb,todb,fromhost,username,password,mechanism)
引數解釋:

* fromdbt: 源db; 

* todb: 目標db; 

* fromhost: 源db的主機位址,如果在同乙個mongod例項內可以省略; 

* username: 如果開啟了驗證模式,需要源db主機上的mongodb例項的使用者名稱; 

* password: 同上,需要對應使用者的密碼; 

* mechanism: fromhost驗證username和password的機制,有:mongodb-cr、scram-sha-1兩種。

舉例:

db.copydatabase('shirodemoold','shirodemonew','127.0.0.1:27017','root','123456','scram-sha-1')
第一種方式(runcommand):

db.runcommand();
例如:

db.runcommand(}});
copyindexes:是否複製索引

第二種方式( db.clonecollection):

db.clonecollection(fromcollection, tocollection, query)

資料庫表複製

select into from 和 insert into select都是用來複製表,兩者的主要區別為 select into from 要求目標表不存在,因為在插入時會自動建立。insert into select from 要求目標表存在 備份表資料 create table emp as ...

資料庫表資料複製

同乙個資料庫中不同表之間的資料複製,比如我想將乙個資料庫demo中的table1表的資料複製到demo資料庫中表table2中 我們可以這樣寫 insert into table1 name,password money select name,password money from table2 ...

Mongo 備份資料庫和恢復資料庫

備份mongodump h d o 恢復mongorestore h d hostname ip位址 公網ip位址 port 埠號 mongo預設埠27017 dbname 集合名稱 dbdirectory 存放路徑傳輸到另乙個伺服器恢復資料庫scp r home test.tar root hos...