how to set mongod --dbpath

前端 未结 12 1699
囚心锁ツ
囚心锁ツ 2020-12-29 19:50

very new to mongodb and databases in general. whenever i run mongo i receive this error message: ​​​

MongoDB shell version: 2.         


        
相关标签:
12条回答
  • 2020-12-29 20:31

    First you will have a config file in /etc/mongodb.conf, therefore this sounds like a homebrew install which will use some more standardized paths. The whole /data/db/ thing is referenced in a lot of manual install documentation.

    So basically from your log the server is not running, it's shutting down, so there is nothing for the shell to connect to. Seems like you have had some unclean shutdowns/restarts which has led to the inconsistency.

    Clear the files in the journal /usr/local/var/mongodb/journal/ on your config.

    Also:

    sudo rm /var/lib/mongodb/mongod.lock
    

    Just in case, even though that part looks clean. And then restart.

    0 讨论(0)
  • 2020-12-29 20:35

    Scenario: MongoDB(version v4.0.9).

    1. Set custom folder(with name: myCustomDatabases), where to store databases.
    2. In custom folder(with name: myCustomDatabases), have to create database (with name: newDb).

    Resolve:

    1. Create custom folder(with name: myCustomDatabases):

      D:>md myCustomDatabases
      
    2. Run 'mongod --dbpath' with path to custom folder(with name: myCustomDatabases):

      mongod --dbpath "D:\myCustomDatabases"
      
    3. From another 'cmd' run 'mongo':

      D:>mongo
      

      3.1. Show all databases, stored in custom folder(with name: myCustomDatabases):

      >show dbs
      admin   0.000GB
      config  0.000GB
      local   0.000GB
      

      3.2. Use database with name newDb:

      > use newDb
      switched to db newDb
      

      3.3. Show all databases, stored in custom folder(with name: myCustomDatabases):

      >show dbs
      admin   0.000GB
      config  0.000GB
      local   0.000GB
      

      !!! Noticed, that newDb is NOT in the list !!!

      3.4. Have to create a collection with a document, which will create the database newDb.

      > db.Cats.insert({name: 'Leo'})
      WriteResult({ "nInserted" : 1 })
      

      The insert({name: 'Leo'}) operation creates: the database newDB and the collection Cats, because they do not exist.

      3.5. Now the new created database newDb will be displayed in the list.

      > show dbs
      admin   0.000GB
      config  0.000GB
      local   0.000GB
      newDb   0.000GB
      

      3.6. Now in custom folder D:\myCustomDatabases, have database newDb.

    0 讨论(0)
  • 2020-12-29 20:36

    You can set dbPath in the mongodb.conf file:

    storage:
        dbPath: "/path/to/your/database/data/db"
    

    It's a YAML-based configuration file format (since Mongodb 2.6 version), so pay attention no tabs only spaces, and space after ": "

    usually this file located in the *nix systems here: /etc/mongodb.conf

    So then just run

    $ mongod -f /etc/mongodb.conf
    

    And mongod process will start...

    (on the Windows something like)

    > C:\MongoDB\bin\mongod.exe -f C:\MongoDB\mongod.conf
    
    0 讨论(0)
  • 2020-12-29 20:37
    mongod  --port portnumber --dbpath /path_to_your_folder
    

    By default portnumber is 27017 and path is /var/lib/mongodb

    You can set your own port number and path where you want to keep all your database.

    0 讨论(0)
  • 2020-12-29 20:41

    very simple:

    sudo chown mongodb:mongodb /var/lib/mongodb/mongod.lock 
    
    0 讨论(0)
  • 2020-12-29 20:43

    You could also configure mongod to run on start up so that it is automatically running on start up and the dbpath is set upon configuration. To do this try:

    mongod --smallfiles --config /etc/mongod.conf

    The --smallfiles tag is there in case you get an error with size. It is, of course, optional. Doing this should solve your problem while also automating your mongodb setup.

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