MongoDB not working. "ERROR: dbpath (/data/db) does not exist."

88,447

Solution 1

This should work to ensure that the directory is set up in the right place so that Mongo can find it:

sudo mkdir -p /data/db/

sudo chown `id -u` /data/db

Solution 2

You need to create the directory on root /data/db or set any other path with the following command :

mongod --dbpath /srv/mongodb/

See the example link

Solution 3

I solved the problem with :

sudo mongod --dbpath=/var/lib/mongodb and then mongo to access the mongodb Shell.

Solution 4

Change the user of the new data directory:

chown mongodb [rute_directory]

And try another time to start the mongo service

service mongod start

I solve the same problem with this.

Solution 5

Daemons (usually ending with d) are normally started as services. Starting the service (daemon) will allow mongodb to work as designed (without permission changes if integrates well with your distro). I start it using the service named mongodb instead of starting mongod directly--on distro with systemd enable on startup then run like:

sudo systemctl enable mongodb    
sudo systemctl start mongodb

or, on distro with upstart (if you have /etc/init) or init (if you have /etc/init.d) ( https://www.tecmint.com/systemd-replaces-init-in-linux/ ) instead run:

sudo service mongodb enable
sudo service mongodb start

If you have a distro with rc ("run commands") such as Gentoo (settings in /etc/init.d) (https://forums.gentoo.org/viewtopic-t-854138-start-0.html) run:

rc-update add mongodb default 
/etc/init.d/mongodb start 

In a distro/version of FreeBSD which still has rc (check whether your version switched to systemd, otherwise see below):

  • add the following line to /etc/rc.conf:

    mongod_enable="YES"

  • then:

    sudo service mongod start

After starting the service, an unpriveleged user can use mongo, and each user will have separate data.

Share:
88,447
Admin
Author by

Admin

Updated on July 07, 2022

Comments

  • Admin
    Admin almost 2 years

    I'm getting the following error when I try to run "mongod" in the terminal. I've tried uninstalling, reinstalling, and restarting the machine. Any suggestions on how to get it working would be amazing.

    ERROR:

    dbpath (/data/db) does not exist.
     Create this directory or give existing directory in --dbpath.
     See http://dochub.mongodb.org/core/startingandstoppingmongo
    

    Side note: Node also stopped working on my machine around the same time that I got this error.

    events.js:72
            throw er; // Unhandled 'error' event
                  ^
    Error: failed to connect to [localhost:27017]
    

    Any help would be much appreciated!

  • NYC Tech Engineer
    NYC Tech Engineer over 9 years
    Awesome, but what's happening here?
  • lpappone
    lpappone over 9 years
    mkdir creates a directory where mongo looks to store its data. The -p allows you to create a directory and subdirectories in one line. (Without -p, you'd get an error if the directory 'data' doesn't exist already.) The second line sets permissions so that mongo can write to that directory. ('chown' changes the owner of a file - CHange OWNer.)
  • Andre Pena
    Andre Pena over 7 years
    Sorry for the stupid question, but what does that mean to change the owner of the directory? I mean... Where are you specifying that specificaly mongo can write on that directory?
  • Nearoo
    Nearoo over 7 years
    @andrapena You make your user the owner of the directory. Every process your user spawns, including mongodb, will have the same rights as your user. Before that, you didn't have those rights, so you had to use sudo to create the directory - sudo means that you execute the following command as the root users, which has full access to your system.
  • Kokodoko
    Kokodoko over 5 years
    But what does 'id -u' mean? And who should be the owner of the 'data/db' directory?
  • Aashiq
    Aashiq almost 4 years
    Hi @lpappone,your answer is awesome and for Linux,can you brief you answer for Windows OS?