简体   繁体   中英

Copy mongodb data directory to another server

I am running 2 CentOS7 server with mongodb database. Can I copy data directory from first server to second server after deleting data directory from second server?

I tried but getting error

[root@CentOS-73-64-minimal ~]# sudo service mongod start
Starting mongod (via systemctl):  Job for mongod.service failed because the control process exited with error code. See "systemctl status mongod.service" and "journalctl -xe" for details.
                                                           [FAILED]
[root@CentOS-73-64-minimal ~]# systemctl status mongod.service
● mongod.service - SYSV: Mongo is a scalable, document-oriented database.
   Loaded: loaded (/etc/rc.d/init.d/mongod; bad; vendor preset: disabled)
   Active: failed (Result: exit-code) since Tue 2017-02-28 14:00:53 UTC; 8s ago
     Docs: man:systemd-sysv-generator(8)
  Process: 30516 ExecStop=/etc/rc.d/init.d/mongod stop (code=exited, status=0/SUCCESS)
  Process: 32249 ExecStart=/etc/rc.d/init.d/mongod start (code=exited, status=1/FAILURE)

Feb 28 14:00:53 CentOS-73-64-minimal systemd[1]: Starting SYSV: Mongo is a scalable, document-oriented database....
Feb 28 14:00:53 CentOS-73-64-minimal mongod[32249]: Error starting mongod. /var/run/mongodb/mongod.pid exists.
Feb 28 14:00:53 CentOS-73-64-minimal systemd[1]: mongod.service: control process exited, code=exited status=1
Feb 28 14:00:53 CentOS-73-64-minimal systemd[1]: Failed to start SYSV: Mongo is a scalable, document-oriented database..
Feb 28 14:00:53 CentOS-73-64-minimal systemd[1]: Unit mongod.service entered failed state.
Feb 28 14:00:53 CentOS-73-64-minimal systemd[1]: mongod.service failed.
[root@CentOS-73-64-minimal ~]# mongod
2017-02-28T14:01:17.068+0000 I CONTROL  [initandlisten] MongoDB starting : pid=32273 port=27017 dbpath=/data/db 64-bit host=CentOS-73-64-minimal
2017-02-28T14:01:17.068+0000 I CONTROL  [initandlisten] db version v3.2.12
2017-02-28T14:01:17.068+0000 I CONTROL  [initandlisten] git version: ef3e1bc78e997f0d9f22f45aeb1d8e3b6ac14a14
2017-02-28T14:01:17.068+0000 I CONTROL  [initandlisten] OpenSSL version: OpenSSL 1.0.1e-fips 11 Feb 2013
2017-02-28T14:01:17.068+0000 I CONTROL  [initandlisten] allocator: tcmalloc
2017-02-28T14:01:17.068+0000 I CONTROL  [initandlisten] modules: none
2017-02-28T14:01:17.068+0000 I CONTROL  [initandlisten] build environment:
2017-02-28T14:01:17.068+0000 I CONTROL  [initandlisten]     distmod: rhel70
2017-02-28T14:01:17.068+0000 I CONTROL  [initandlisten]     distarch: x86_64
2017-02-28T14:01:17.068+0000 I CONTROL  [initandlisten]     target_arch: x86_64
2017-02-28T14:01:17.068+0000 I CONTROL  [initandlisten] options: {}
2017-02-28T14:01:17.085+0000 I STORAGE  [initandlisten] exception in initAndListen: 29 Data directory /data/db not found., terminating
2017-02-28T14:01:17.085+0000 I CONTROL  [initandlisten] dbexit:  rc: 100
[root@CentOS-73-64-minimal ~]#

mogod out put now

[root@CentOS-73-64-minimal ~]# mongod
2017-03-01T14:09:03.658+0000 I CONTROL  [initandlisten] MongoDB starting : pid=2                 304 port=27017 dbpath=/data/db 64-bit host=CentOS-73-64-minimal
2017-03-01T14:09:03.658+0000 I CONTROL  [initandlisten] db version v3.2.12
2017-03-01T14:09:03.658+0000 I CONTROL  [initandlisten] git version: ef3e1bc78e9                 97f0d9f22f45aeb1d8e3b6ac14a14
2017-03-01T14:09:03.658+0000 I CONTROL  [initandlisten] OpenSSL version: OpenSSL                  1.0.1e-fips 11 Feb 2013
2017-03-01T14:09:03.658+0000 I CONTROL  [initandlisten] allocator: tcmalloc
2017-03-01T14:09:03.658+0000 I CONTROL  [initandlisten] modules: none
2017-03-01T14:09:03.658+0000 I CONTROL  [initandlisten] build environment:
2017-03-01T14:09:03.658+0000 I CONTROL  [initandlisten]     distmod: rhel70
2017-03-01T14:09:03.658+0000 I CONTROL  [initandlisten]     distarch: x86_64
2017-03-01T14:09:03.658+0000 I CONTROL  [initandlisten]     target_arch: x86_64
2017-03-01T14:09:03.658+0000 I CONTROL  [initandlisten] options: {}
2017-03-01T14:09:03.775+0000 I -        [initandlisten] Detected data files in /                 data/db created by the 'wiredTiger' storage engine, so setting the active storag                 e engine to 'wiredTiger'.
2017-03-01T14:09:03.775+0000 I STORAGE  [initandlisten] wiredtiger_open config:                  create,cache_size=18G,session_max=20000,eviction=(threads_max=4),config_base=fal                 se,statistics=(fast),log=(enabled=true,archive=true,path=journal,compressor=snap                 py),file_manager=(close_idle_time=100000),checkpoint=(wait=60,log_size=2GB),stat                 istics_log=(wait=0),
2017-03-01T14:09:06.114+0000 I CONTROL  [initandlisten] ** WARNING: You are runn                 ing this process as the root user, which is not recommended.
2017-03-01T14:09:06.114+0000 I CONTROL  [initandlisten]
2017-03-01T14:09:06.115+0000 I FTDC     [initandlisten] Initializing full-time d                 iagnostic data capture with directory '/data/db/diagnostic.data'
2017-03-01T14:09:06.115+0000 I NETWORK  [HostnameCanonicalizationWorker] Startin                 g hostname canonicalization worker
2017-03-01T14:09:06.252+0000 I NETWORK  [initandlisten] waiting for connections                  on port 27017

First of all, I am assuming the data in both servers is the same and you just need a copy of the first (A) in the second (B).

I would mongodump the data from A to B. It is cleaner (and easier) than messing around with datafiles.

There is enough documentation on MongoDB site covering this topic.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM