[mongodb] how to set mongod --dbpath

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

MongoDB shell version: 2.4.9
connecting to: test
Thu Jan 30 13:03:33.170 Error: couldn't connect to server 127.0.0.1:27017 
at src/mongo/shell/mongo.js:145
exception: connect failed

running mongod i see this:

Thu Jan 30 13:13:36.588 [initandlisten] MongoDB starting : pid=29408 port=27017 dbpath=/usr/local/var/mongodb 64-bit host=Kimis-MacBook-Air-2.local
Thu Jan 30 13:13:36.588 [initandlisten] 
Thu Jan 30 13:13:36.588 [initandlisten] ** WARNING: soft rlimits too low. Number of files is 256, should be at least 1000
Thu Jan 30 13:13:36.588 [initandlisten] db version v2.4.9
Thu Jan 30 13:13:36.588 [initandlisten] git version: nogitversion
Thu Jan 30 13:13:36.588 [initandlisten] build info: Darwin minilionvm.local 11.4.2 Darwin Kernel Version 11.4.2: Thu Aug 23 16:25:48 PDT 2012; root:xnu-1699.32.7~1/RELEASE_X86_64 x86_64 BOOST_LIB_VERSION=1_49
Thu Jan 30 13:13:36.588 [initandlisten] allocator: tcmalloc
Thu Jan 30 13:13:36.588 [initandlisten] options: { bind_ip: "127.0.0.1", config: "/usr/local/etc/mongod.conf", dbpath: "/usr/local/var/mongodb" }
Thu Jan 30 13:13:36.591 [initandlisten] journal dir=/usr/local/var/mongodb/journal
Thu Jan 30 13:13:36.591 [initandlisten] recover begin
Thu Jan 30 13:13:36.591 [initandlisten] recover lsn: 108155770
Thu Jan 30 13:13:36.591 [initandlisten] recover /usr/local/var/mongodb/journal/j._0
Thu Jan 30 13:13:36.591 [initandlisten] journal file version number mismatch got:4147 expected:4149. if you have just upgraded, recover with old version of mongod, terminate cleanly, then upgrade.
Thu Jan 30 13:13:36.592 [initandlisten] dbexception during recovery: 13536 journal version number mismatch 16711
Thu Jan 30 13:13:36.592 [initandlisten] exception in initAndListen: 13536 journal version number mismatch 16711, terminating
Thu Jan 30 13:13:36.592 dbexit: 
Thu Jan 30 13:13:36.592 [initandlisten] shutdown: going to close listening sockets...
Thu Jan 30 13:13:36.592 [initandlisten] shutdown: going to flush diaglog...
Thu Jan 30 13:13:36.592 [initandlisten] shutdown: going to close sockets...
Thu Jan 30 13:13:36.592 [initandlisten] shutdown: waiting for fs preallocator...
Thu Jan 30 13:13:36.592 [initandlisten] shutdown: lock for final commit...
Thu Jan 30 13:13:36.592 [initandlisten] shutdown: final commit...
Thu Jan 30 13:13:36.592 [initandlisten] shutdown: closing all files...
Thu Jan 30 13:13:36.592 [initandlisten] closeAllFiles() finished
Thu Jan 30 13:13:36.592 [initandlisten] shutdown: removing fs lock...
Thu Jan 30 13:13:36.592 dbexit: really exiting now

when i manually set my mongo dpath to mongod --dbpath /data/db (as should be the default on installation) and keep it running in terminal everything runs fine. but once i close it, everything breaks again. my question is:

  1. why is my dbpath set to /usr/local/var/mongodb?
  2. how do i fix this error so mongo works on my machine?

i'm assuming i either need to permanently set the dbpath to /data/db or reconfigure something so it works with the dbpath as /usr/local/var/mongodb

i fairly new to unix commands as well so not entirely sure how to fix this error.

thank you for any suggestions!

so, digging through the mongodb documentation i read this:

"Unless specified, mongod will look for data files in the default /data/db directory. (Windows systems use the \data\db directory.) If you installed using a package management system. Check the /etc/mongodb.conf file provided by your packages to see the configuration of the dbpath."

when i took a look at the files within /etc/ there is no mongodb.conf file... however, i do see /usr/local/etc/mongod.conf... so if i understand this correctly, i should create a file called /etc/mongodb.conf and set the dbpath to /data/db.

do i need to delete the stuff within /usr/local/ as well?

i think this has something to do with how my PATH are setup... could someone explain to me how to fix this in unix so i won't have this problem?

thanks again!

This question is related to mongodb unix

The answer is


Have only tried this on Mac:

  • Create a data directory in the root folder of your app
  • cd into your wherever you placed your mongo directory when you installed it
  • run this command:

    mongod --dbpath ~/path/to/your/app/data

You should be good to go!


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.


You can use --dbpath flag in order to provide it with the parameters i.e, the directory address. I found it useful for Windows 10 OS.

With the default settings it is usually not required.


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

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.


sudo mongod --dbpath ~/data

This made it work for me.


For me it must have:

mongod --dbpath=/whatever/data/path

Windows environment, local machine. I had an error

[js] Error: couldn't connect to server 127.0.0.1:27017, connection attempt failed: SocketException: 
Error connecting to 127.0.0.1:27017 :: caused by :: 
No connection could be made because the target machine actively refused it. :

After some back and forth attempts I decided

  • to check Windows "Task Manager". I noticed that MongoDB process is stopped.
  • I made it run. Everything starts working as expected.

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.


very simple:

sudo chown mongodb:mongodb /var/lib/mongodb/mongod.lock 

Create a directory db in home, inside db another directory data

cd 
mkdir db
cd db
mkdir data

then type this command--

mongod --dbpath ~/db/data