I created /data/db
in root directory and ran ./mongod
:
[initandlisten] exception in initAndListen: 20 Attempted to create a lock fi
If you are On Windows, and you are trying to setup MongoDB, run cmd as Admnistrator is the way forward as Enrique suggested above see it here
On a Mac, I had to do the following:
sudo chown -R $USER /data/db
sudo chown -R $USER /tmp/
because there was also a file inside /tmp
which Mongo also needed access
The problem is that the directory you created, /data/db
is owned by and only writable by the root user, but you are running mongod as yourself. There are several ways to resolve this, but ultimately, you must give the directory in question the right permissions. If this is for production, I would advise you to check the docs and think this over carefully -- you probably want to take special care.
However, if this is just for testing and you just need this to work and get on with it, you could try this, which will make the directory writable by everyone:
> sudo chmod -R go+w /data/db
or this, which will make the directory owned by you:
> sudo chown -R $USER /data/db
First of all stop all the mongoDB services, then create a directory on /
, it means root, if you don't have, and remove the port file also.
give all permission to that directory, become that directory owner, run below command:
sudo service mongod stop
sudo rm -rf /tmp/mongod*
sudo mkdir -p /data/db
sudo chmod -R a+wxr /data
sudo chown -R $USER:$USER /data
Now you're done, just start the MongoDB service, if didn't help, try to change the port like:
sudo service mongod restart && mongod # if didn't help run below cmd
mongod --port 27018
Note: For me all this stuff works and hoping would work for you, guy!
I experienced the same problem and following solution solved this problem. You should try the following solution.
sudo mkdir -p /data/db
sudo chown -R 'username' /data/db
If you do not need to have the database directories in root, you can create data/db
in your home directory (mkdir -p ~/data/db
).
Then you can start the MongoDB server with the --dbpath
option:
mongod --dbpath=$(echo ~)/data/db
(This is assuming you're starting it from a terminal. For some strange reason, mongod
does not accept ~ as the home directory path, hence the bash echo substitution trickery, you may as well write /home/yourusername/data/db
)
In fact, it does not have to be ~/data/db
, it can be anywhere you want and this way you can have multiple database paths and organize everything nicely. May not be the best option for production (depending on what you want to do), but for developing works fine.