vesperae
vesperae

Reputation: 1301

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!

Upvotes: 40

Views: 202608

Answers (14)

Hylke ヒルけ
Hylke ヒルけ

Reputation: 21

With the terminal command mongod --dbpath /YOUR/PATH/TO/DATA/ I need to type it everytime I want to start MongoDB. Somehow setting it in the mongo.conf didn't load the path properly. I found a solution to avoid having to type the long command every time by adding an alias in ~/.zshrc

  1. Shut down MongoDB (while mongo is running type in Terminal: use admin press enter and on a next line db.shutdownServer().
  2. Close the Terminal window.
  3. Open ~/.zshrc or when your macOS is using bash-shell open ~/.bash_profile
  4. Add in the file: alias -g mongod="MongoDB --dbpath /YOUR/PATH/TO/DATA/
  5. Save your .zshrc or .bash_profile file
  6. Open Terminal
  7. Type source ~/.zshrc or for bash-shell users: source ~/.bash_profile
  8. Type mongod

MongoDB should now be running with the right path and connection. Terminal can be closed now. To check whether mongo is running open Terminal, type mongo which gives you the interface in Terminal or type top and check mongo in the list of applications.

Upvotes: 2

vihaan nama
vihaan nama

Reputation: 11

Go to the directory you would like your data to be saved in.

Create a folder called "mongodb" and within it create another directory named "data"

Navigate back to the "mongodb" directory in the terminal window and place the following line of mongod --dbpath=data --bind_ip 127.0.0.1

Run this command each time you want to start up the mongodb database

Upvotes: 0

fucode
fucode

Reputation: 1

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.

Upvotes: 0

Roman
Roman

Reputation: 21873

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.

Upvotes: 0

user8797752
user8797752

Reputation:

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

Upvotes: 4

Ted
Ted

Reputation: 923

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.

Upvotes: 0

phifa
phifa

Reputation: 916

sudo mongod --dbpath ~/data

This made it work for me.

Upvotes: 2

MMezlini
MMezlini

Reputation: 163

very simple:

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

Upvotes: 0

Gareth Thomas
Gareth Thomas

Reputation: 430

For me it must have:

mongod --dbpath=/whatever/data/path

Upvotes: 13

Neil Lunn
Neil Lunn

Reputation: 151220

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.

Upvotes: 18

aaditya
aaditya

Reputation: 773

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.

Upvotes: 3

Andrew
Andrew

Reputation: 38029

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

Upvotes: 19

Sacha Nacar
Sacha Nacar

Reputation: 2308

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!

Upvotes: 26

ABarb
ABarb

Reputation: 180

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.

Upvotes: 2

Related Questions