Moving MongoDB's data folder?

后端 未结 4 1436
我在风中等你
我在风中等你 2021-02-12 18:08

I have 2 computers in different places (so it\'s impossible to use the same wifi network). One contains about 50GBs of data (MongoDB files) that I want to move to the second on

相关标签:
4条回答
  • 2021-02-12 18:39

    Maybe what you didn't do was export or dump the database. Databases aren't portable therefore must be exported or created as a dumpfile.

    Here is another question where the answer is further explained

    0 讨论(0)
  • 2021-02-12 18:54

    I did this myself recently, and I wanted to provide some extra considerations to be aware of, in case readers (like me) run into issues.

    The following information is specific to *nix systems, but it may be applicable with very heavy modification to Windows.

    If the source data is in a mongo server that you can still run (preferred)

    Look into and make use of mongodump and mongorestore. That is probably safer, and it's the official way to migrate your database.

    If you never made a dump and can't anymore

    Yes, the data directory can be directly copied; however, you also need to make sure that the mongodb user has complete access to the directory after you copy it.

    My steps are as follows. On the machine you want to transfer an old database to:

    • Edit /etc/mongod.conf and change the dbPath field to the desired location.
    • Use the following script as a reference, or tailor it and run it on your system, at your own risk.
      • I do not guarantee this works on every system --> please verify it manually.
      • I also cannot guarantee it works perfectly in every case.
      • WARNING: will delete everything in the target data directory you specify.
      • I can say, however, that it worked on my system, and that it passes shellcheck.
      • The important part is simply copying over the old database directory, and giving mongodb access to it through chown.
    #!/bin/bash
    
    TARGET_DATA_DIRECTORY=/path/to/target/data/directory  # modify this
    SOURCE_DATA_DIRECTORY=/path/to/old/data/directory  # modify this too
    
    echo shutting down mongod...
    sudo systemctl stop mongod
    
    if test "$TARGET_DATA_DIRECTORY"; then
      echo removing existing data directory...
      sudo rm -rf "$TARGET_DATA_DIRECTORY"
    fi
    
    echo copying backed up data directory...
    sudo cp -r "$SOURCE_DATA_DIRECTORY" "$TARGET_DATA_DIRECTORY"
    sudo chown -R mongodb "$TARGET_DATA_DIRECTORY"
    
    echo starting mongod back up...
    sudo systemctl start mongod
    sudo systemctl status mongod  # for verification
    
    0 讨论(0)
  • 2021-02-12 18:57

    quite easy for windows, just move the data folder to the target location run cmd "C:\your\mongodb\bin-path\mongod.exe" --dbpath="c:\what\ever\path\data\db"

    0 讨论(0)
  • 2021-02-12 18:58

    When you start mongodprocess you provide an argument to it --dbpath /directory which is how it knows where the data folder is.

    All you need to do is:

    1. stop the mongod process on the old computer. wait till it exits.
    2. copy the entire /data/db directory to the new computer
    3. start mongod process on the new computer giving it --dbpath /newdirectory argument.

    The mongod on the new machine will use the folder you indicate with --dbpath. There is no need to "recognize" as there is nothing machine specific in that folder, it's just data.

    0 讨论(0)
提交回复
热议问题