ETC

[MongoDB] db/collection 다른 서버로 옮기기

김민둉 2023. 7. 7. 13:24

mongodb의 db와 컬렉션을 다른 서버로 옮기는 방법이다.

백업과 복구 시에도 동일하게 사용하면 된다.

 

 

[백업]

mongodump --host {host} --port {port} -db {db} --out {outpath} -u username -p 'password' --authenticationDatabase={db}

ex) mongodump --host 127.0.0.1 --port 27017 --db testdb --out ~/testdb_backup -u user -p 'p1234' --authenticationDatabase=testdb

 

 

[복구]

mongorestore --host {host} --port {port} --db {db} {backup_dir_path} -u username -p 'password' --authenticationDatabase={db}

ex) mongorestore --host 127.0.0.1 --port 27017 --db testdb2 ~/testdb_backup/testdb -u user -p 'p1234' --authenticationDatabase=testdb

 


두 서버가 서로 통신 가능하다면, 백업 서버에서 복구를 해당 서버의 mongo 주소로 잡고 진행하는게 편하다.

통신이 불가능하다면, 백업 후 백업 디렉토리를 수동으로 복구 서버로 옮기고 복구 서버에서 restore 하면 된다.