Unable to create/open lock file: /data/mongod.lock errno:13 Permission denied - mongodb

How to I get mongo to use a mounted drive on ec2? I really do not understand. I attached a volume on ec2 formatted the drive as root and start as root and yet as root I cant access? I am running on ubuntu 12.04. No other mongo is running
I see that mongo made a 'db' dir in /data i.e. /data/db
cd /
ls -al
drwxr-xr-x 4 root root 4096 Mar 5 16:28 data
cd /data
ls -al
total 28
drwxr-xr-x 4 root root 4096 Mar 5 16:28 .
drwxr-xr-x 24 root root 4096 Mar 5 16:28 ..
drwxr-xr-x 2 root root 4096 Mar 5 16:28 db
drwx------ 2 root root 16384 Mar 5 16:20 lost+found
sudo mkfs.ext3 /dev/xvdh
sudo mkdir /data
sudo su - -c 'echo "/dev/xvdh %s auto noatime 0 0" | sudo tee -a /etc/fstab'
sudo mount /data
sudo service mongodb start
mongodb start/running, process 17169
sudo ps -ef | grep mongod
ubuntu 15763 15634 0 16:32 pts/2 00:00:00 tail -f mongodb.log
ubuntu 18049 15766 0 16:43 pts/3 00:00:00 grep --color=auto mongod
Tue Mar 5 16:33:15 [initandlisten] MongoDB starting : pid=15890 port=27017 dbpath=/data 64-bit host=aws-mongo-server-east-staging-20130305161917
Tue Mar 5 16:33:15 [initandlisten] db version v2.2.3, pdfile version 4.5
Tue Mar 5 16:33:15 [initandlisten] git version: f570771a5d8a3846eb7586eaffcf4c2f4a96bf08
Tue Mar 5 16:33:15 [initandlisten] build info: Linux ip-10-2-29-40 2.6.21.7-2.ec2.v1.2.fc8xen #1 SMP Fri Nov 20 17:48:28 EST 2009 x86_64 BOOST_LIB_VERSION=1_49
Tue Mar 5 16:33:15 [initandlisten] options: { bind_ip: "10.157.60.27", config: "/etc/mongodb.conf", dbpath: "/data", logappend: "true", logpath: "/var/log/mongodb/mongodb.log", replSet: "heythat" }
Tue Mar 5 16:33:15 [initandlisten] exception in initAndListen: 10309 Unable to create/open lock file: /data/mongod.lock errno:13 Permission denied Is a mongod instance already running?, terminating
Tue Mar 5 16:33:15 dbexit:
Tue Mar 5 16:33:15 [initandlisten] shutdown: going to close listening sockets...
Tue Mar 5 16:33:15 [initandlisten] shutdown: going to flush diaglog...
Tue Mar 5 16:33:15 [initandlisten] shutdown: going to close sockets...
Tue Mar 5 16:33:15 [initandlisten] shutdown: waiting for fs preallocator...
Tue Mar 5 16:33:15 [initandlisten] shutdown: lock for final commit...
Tue Mar 5 16:33:15 [initandlisten] shutdown: final commit...
Tue Mar 5 16:33:15 [initandlisten] shutdown: closing all files...
Tue Mar 5 16:33:15 [initandlisten] closeAllFiles() finished
Tue Mar 5 16:33:15 [initandlisten] shutdown: removing fs lock...
Tue Mar 5 16:33:15 [initandlisten] couldn't remove fs lock errno:9 Bad file descriptor
Tue Mar 5 16:33:15 dbexit: really exiting now
Below is if I restart when I remove a lock file....
Tue Mar 5 16:59:15 [initandlisten] MongoDB starting : pid=21091 port=27017 dbpath=/data 64-bit host=aws-mongo-server-east-staging-20130305161917
Tue Mar 5 16:59:15 [initandlisten] db version v2.2.3, pdfile version 4.5
Tue Mar 5 16:59:15 [initandlisten] git version: f570771a5d8a3846eb7586eaffcf4c2f4a96bf08
Tue Mar 5 16:59:15 [initandlisten] build info: Linux ip-10-2-29-40 2.6.21.7-2.ec2.v1.2.fc8xen #1 SMP Fri Nov 20 17:48:28 EST 2009 x86_64 BOOST_LIB_VERSION=1_49
Tue Mar 5 16:59:15 [initandlisten] options: { bind_ip: "10.157.60.27", config: "/etc/mongodb.conf", dbpath: "/data", logappend: "true", logpath: "/var/log/mongodb/mongodb.log", replSet: "heythat" }
Tue Mar 5 16:59:15 [initandlisten] exception in initAndListen: 10309 Unable to create/open lock file: /data/mongod.lock errno:13 Permission denied Is a mongod instance already running?, terminating
Tue Mar 5 16:59:15 dbexit:
Tue Mar 5 16:59:15 [initandlisten] shutdown: going to close listening sockets...
Tue Mar 5 16:59:15 [initandlisten] shutdown: going to flush diaglog...
Tue Mar 5 16:59:15 [initandlisten] shutdown: going to close sockets...
Tue Mar 5 16:59:15 [initandlisten] shutdown: waiting for fs preallocator...
Tue Mar 5 16:59:15 [initandlisten] shutdown: lock for final commit...
Tue Mar 5 16:59:15 [initandlisten] shutdown: final commit...
Tue Mar 5 16:59:15 [initandlisten] shutdown: closing all files...
Tue Mar 5 16:59:15 [initandlisten] closeAllFiles() finished
Tue Mar 5 16:59:15 [initandlisten] shutdown: removing fs lock...
Tue Mar 5 16:59:15 [initandlisten] couldn't remove fs lock errno:9 Bad file descriptor
Tue Mar 5 16:59:15 dbexit: really exiting now

I use this method to solve the problem:
sudo chown -R mongodb:mongodb /data/db

I was having the same problem on a Ubuntu ec2 instance. I was following this amazon article on page 7:
http://d36cz9buwru1tt.cloudfront.net/AWS_NoSQL_MongoDB.pdf
Mongodb path in /etc/mongodb.conf was set to /var/lib/mongodb (primary install location and working). When I changed to /data/db (EBS volume) I was getting 'errno:13 Permission denied'.
First I ran sudo service mongodb stop.
Then I used ls -la to see what group & owner mongodb assigned to /var/lib/mongodb (existing path) and I changed the /data/db (new path) with chown and chgrp to match. (example: sudo chown -R mongodb:mongodb /data/db)
Then I updated the path in etc/mongodb.conf to /data/db and deleted the old mongo files in /var/lib/mongodb directory.
Then I ran sudo service mongodb start and waited about a minute. If you try to connect to 27017 immediately you won't be able to.
After a minute check /data/db (EBS volume) and mongo should have placed a journal, mongod.lock, local.ns, local.0, etc. If not try sudo service mongodb restart and check a minute later.
I just spent over a hour with this. Changing the group and deleting the old files is probably not necessary, but that's what worked for me.
This is a great video about mounting a ebs volume to ec2 instance:
http://www.youtube.com/watch?v=gBII3o3BofU

In my case (AWS EC2 instance, Ubuntu) helped:
$ sudo mkdir -p /data/db/
$ sudo chown `USERNAME` /data/db
And after that everything worked fine.

You just have to give access to your /data/db folder.
Type sudo chown -R <USERNAME> /data/db, replace <USERNAME> by your username.
You can find your username by typing whoami.

I installed mongodb with EBS on an EC2 with Ubuntu 14.04 following this tutorial:
http://docs.mongodb.org/ecosystem/platforms/amazon-ec2/
But instead of the suggested chown I did:
sudo chown -R mongodb:mongodb /data /log /journal
To fix the problem

I had similar issue, the actual reason was that there was mongod session running already from my previous attempt.
I ran
killall mongod
and everything else ran just as expected.
killall command would send a TERM signal to all processes with a real UID. So this kills all the running instances of mongod so that you could start your own.

For mac users:
Run ls -ld /data/db/
Output should be something like drwrx-xr-x 20 singh wheel 680 21 Jul 05:49 /data/db/
Where singh is the owner and wheel is the group it belongs to.
Run sudo chown -R singh:wheel /data/db
Run mongod

As of today, I tried to get my way through the to create/open lock file: /data/db/mongod.lock errno:13 Permission denied Is a mongod instance already running?, terminating, and tried all the answer posted above to solve this problem, hence nothing worked out by adding
sudo chown -R mongodb:mongodb /data/db
Unless I added my current user permission to the location path by
sudo chown $USER /data/db
Hope this helps someone. Also I just installed Mongo DB on my pi. Cheers!

I had a similar issue and followed all the instructions above regarding changing owners using sudo chown etc. I still had an instance of mongodb running in the background after the changes. Running
ps auxw | grep mongo
showed me other tasks using mongo running in background that weren't closed properly. I then ran kill on all the ones running and then could start my server.

Removing the mongodb.lock file was not the issue in my case. I did so and got an error about the port being in use: [initandlisten] listen(): bind() failed errno:98 Address already in use for socket: 0.0.0.0:27017. I found another solution here: unable to start mongodb local server with instructions to kill the process:
Find out from netstat which process is running mongodb port (27017)
sudo netstat -tulpn | grep :27017
Output will be: tcp 0 0 0.0.0.0:27017 0.0.0.0:* LISTEN 1412/mongod
Kill the appropriate process.
sudo kill 1412 (replace 1412 with your process ID found in step 1)
And I was able to successfully start mongodb again. I believe mine was still running from an improper shut down.

For those of you experiencing this error on Windows using Task Manager end the instance of "mongod.exe" that is running. Once that is done permanently delete the mongo.lock file and run mongod.exe. It should work perfectly after that.

My mongo (3.2.9) was installed on Ubuntu, and my log file had the following lines:
2016-09-28T11:32:07.821+0100 E STORAGE [initandlisten] WiredTiger (13) [1475058727:821829][6785:0x7fa9684ecc80], file:WiredTiger.wt, connection: /var/lib/mongodb/WiredTiger.turtle: handle-open: open: Permission denied
2016-09-28T11:32:07.822+0100 I - [initandlisten] Assertion: 28595:13: Permission denied
2016-09-28T11:32:07.822+0100 I STORAGE [initandlisten] exception in initAndListen: 28595 13: Permission denied, terminating
2016-09-28T11:32:07.822+0100 I CONTROL [initandlisten] dbexit: rc: 100
So the problem was in permissions on /var/lib/mongodb folder.
sudo chown -R mongodb:mongodb /var/lib/mongodb/
sudo chmod -R 755 /var/lib/mongodb
Restart the server
Fixed it, although I do realise that may be not too secure (it's my own dev box I'm in my case), bit following the change both db and authentication worked.

In Mycase
In mongodb version 2.6.11 default databse directory is /var/lib/mongodb/
$ sudo chown -R id -u /var/lib/mongodb/
$ sudo chown -R id -u /var/lib/mongodb/mongod.lock
$ sudo /etc/init.d/mongod stop
$ sudo /etc/init.d/mongod start

I got the same issue when I ran mongod command after installing it on Windows10.
I stopped the mongodb service and started it again. Working like a charm
Command to stop mongodb service (in windows): net stop mongodb
Command to start mongodb server: mongod --dbpath PATH_TO_DATA_FOLDER

On a Fedora 18 with Mongo 2.2.4 instance I was able to get around a similar error by disabling SELinux by calling setenforce 0 as root.
BTW, this was a corporate environment, not an Amazon EC2 instance, but the symptoms were similar.

In my case the issue was solved by removing the log file.
sudo rm /log/mongod.log
Although the error message refers specifically to the lock file:
exception in initAndListen: 10309 Unable to create/open lock file:
/data/mongod.lock errno:13 Permission denied
Is a mongod instance already running?, terminating

After I killed mongod, I just had the same problem: couldn't start mongod.
$> sudo kill `pidof mongod`
2015-08-03T05:58:41.339+0000 [initandlisten] exception in initAndListen: 10309 Unable to create/open lock file: /data/mongodbtest/replset/data/mongod.lock errno:13 Permission denied Is a mongod instance already running?, terminating
After I delete the lock directly, I can restart the mongod process.
$> rm -rf /data/mongodbtest/replset/data/mongod.lock

This is what I did to fix the problem:
$sudo mkdir -p /data/db
$export PATH=/usr/local/Cellar/mongodb/3.0.7/bin:$PATH
$sudo chown -R id -u /data/db
and then to start mongo...
$mongod

I had the same problem.
I solved it by changing selinux status to permissive with below command:
setenforce 0

Do ls -lato know the user and group of /var/log/mongodb.
Then do sudo chown -R user:group /data/db
Now run sudo service mongodb start.
Check the status with sudo service mongodb status

On windows be sure the console is started as aministrator

You could try by these ways.
1st.
sudo chown -R mongod:mongod /data/db
but at some times,this is not useful.
2nd.
if the above way is not useful,you can try to do this:
mkdir /data/db #as the database storage path
nohup mongod --dbpath /data/db &
or type:
mongod --dbpath /data/db
to get the output stream

For me on CentOS 6.x:
sudo chown -R mongodb:mongodb <db-path>
sudo service mongod restart
And I have set a custom db-path in /etc/mongod.conf.

If you literally want a one line equivalent to the commands in your original question, you could alias:
mongo --eval "db.getSiblingDB('admin').shutdownServer()"
https://stackoverflow.com/a/11777141/7160782

In Centos Server
this works for me
chown -R mongod:mongod /var/lib/mongo

Got a similar error, fixed with removing all records (in my case directory journals, and file mongo.lock...), after that check port with sudo lsof -i:27017, if smth running on it kill <PID of the process>, and try to run ./mongod again

Fix: sudo mongod
I had the same problem, running mongod with sudo privileges fixed it.
Coming from a windows environment, I used just mongod to start the daemon, well it looks like we need the superuser privileges to access /data/db.
You can also give non root users Read and Write permissions to that path. check answers above for a guide!

Every time you when you try to start mongod
just type
sudo mongod
or if permanently want to fix this just try to give rwx premission to /data/db folder
chmod +rwx data/

Related

mongodb works when executing the binary, but not when starting the service ("Unable to determine status of lock file")

I know this is a duplicate, but all answers suggest either changing owner of the data directory to mongodb:mongodb or permissions to 777. I've tried both and it hasn't worked.
I have a machine whose sole purpose is to run mongodb. I'm logged
in as root. I'd like to change my data directory to /root/mongodb. I have updated dbpath in /etc/mongod.conf.
If I run systemctl start mongod or sudo systemctl start mongod, I get the following error:
2018-02-07T18:39:53.246+0100 I STORAGE [initandlisten] exception in initAndListen: 28596 Unable to determine status of lock file in the data directory /root/mongodb: boost::filesystem::status: Permission denied: "/root/mongodb/mongod.lock", terminating
I've tried chown -R mongodb:mongodb /root/mongodb and chmod -R 777 /root/mongodb, but still see the same error. This are the permissions after running both:
drwxrwxrwx 4 mongodb mongodb 4096 Feb 7 18:27 mongodb
However, if I run mongodb --dbpath=/root/mongodb, it starts up fine.
Version info:
db version v3.4.11
git version: 34f5bec2c9d827d71828fe858167f89a28b29a2a
OpenSSL version: OpenSSL 1.0.2g 1 Mar 2016
allocator: tcmalloc
modules: none
build environment:
distmod: ubuntu1604
distarch: x86_64
target_arch: x86_64
Any thoughts on how to fix this?

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:
why is my dbpath set to /usr/local/var/mongodb?
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!
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 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
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.
For me it must have:
mongod --dbpath=/whatever/data/path
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
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.
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.
sudo mongod --dbpath ~/data
This made it work for me.
very simple:
sudo chown mongodb:mongodb /var/lib/mongodb/mongod.lock
Scenario: MongoDB(version v4.0.9).
Set custom folder(with name: myCustomDatabases), where to store databases.
In custom folder(with name: myCustomDatabases), have to create database (with name: newDb).
Resolve:
Create custom folder(with name: myCustomDatabases):
D:>md myCustomDatabases
Run 'mongod --dbpath' with path to custom folder(with name: myCustomDatabases):
mongod --dbpath "D:\myCustomDatabases"
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.
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.
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.
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
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
Shut down MongoDB (while mongo is running type in Terminal: use admin press enter and on a next line db.shutdownServer().
Close the Terminal window.
Open ~/.zshrc or when your macOS is using bash-shell open ~/.bash_profile
Add in the file: alias -g mongod="MongoDB --dbpath /YOUR/PATH/TO/DATA/
Save your .zshrc or .bash_profile file
Open Terminal
Type source ~/.zshrc or for bash-shell users: source ~/.bash_profile
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.

Error while connecting mongodb shell [duplicate]

when i setup mongodb in my ubuntu , i try : ./mongo it show this error :
couldn't connect to server 127.0.0.1 shell/mongo.js
so what can i do ,
thanks
Manually remove the lockfile: sudo rm /var/lib/mongodb/mongod.lock
Run the repair script: sudo -u mongodb mongod -f /etc/mongodb.conf --repair
Please note the following:
You must run this command as the mongodb user. If you run it as root,
then root will own files in /var/lib/mongodb/ that are necessary to
run the mongodb daemon and therefore when the daemon trys to run
later as the mongodb user, it won't have permissions to start. In
that case you'll get this error: Unable to create / open lock file
for lockfilepath: /var/lib/mongodb/mongod.lock errno:13 Permission
denied, terminating.
On Ubuntu, you must specify the configuration file /etc/mongodb.conf
using the -f flag. Otherwise it will look for the data files in the
wrong place and you will see the following error: dbpath (/data/db/)
does not exist, terminating.
sudo rm /var/lib/mongodb/mongod.lock
sudo -u mongodb mongod -f /etc/mongodb.conf --repair
sudo service mongodb start
Here is all, sometimes, it takes a little while to start mongo after performing these operations.
Trying running $mongod
If you get en error such as
MongoDB shell version: 2.0.5
connecting to: test
Fri Jun 1 11:20:33 Error: couldn't connect to server 127.0.0.1 shell/mongo.js:84
exception: connect failed
hisham-agil:~ hisham$ mongod
mongod --help for help and startup options
Fri Jun 1 11:24:47 [initandlisten] MongoDB starting : pid=53452 port=27017 dbpath=/data/db/ 64-bit host=hisham-agil.local
Fri Jun 1 11:24:47 [initandlisten] db version v2.0.5, pdfile version 4.5
Fri Jun 1 11:24:47 [initandlisten] git version: nogitversion
Fri Jun 1 11:24:47 [initandlisten] build info: Darwin gamma.local 11.3.0 Darwin Kernel Version 11.3.0: Thu Jan 12 18:48:32 PST 2012; root:xnu-1699.24.23~1/RELEASE_I386 i386 BOOST_LIB_VERSION=1_49
Fri Jun 1 11:24:47 [initandlisten] options: {}
Fri Jun 1 11:24:47 [initandlisten] exception in initAndListen: 10296 dbpath (/data/db/) does not exist, terminating
Fri Jun 1 11:24:47 dbexit:
Fri Jun 1 11:24:47 [initandlisten] shutdown: going to close listening sockets...
Fri Jun 1 11:24:47 [initandlisten] shutdown: going to flush diaglog...
Fri Jun 1 11:24:47 [initandlisten] shutdown: going to close sockets...
Fri Jun 1 11:24:47 [initandlisten] shutdown: waiting for fs preallocator...
Fri Jun 1 11:24:47 [initandlisten] shutdown: lock for final commit...
Fri Jun 1 11:24:47 [initandlisten] shutdown: final commit...
Fri Jun 1 11:24:47 [initandlisten] shutdown: closing all files...
Fri Jun 1 11:24:47 [initandlisten] closeAllFiles() finished
Fri Jun 1 11:24:47 dbexit: really exiting now
Then you've run into a basic startup error that is pretty common.
By default mongod will try to use /data/db for its database files, which in this case, does not exist.
You can't start
mongo
until you handle
mongod.
Try creating those directories and make sure they are writable by the same user that is running the mongod process.
**See similar question-- Getting an error, "Error: couldn't connect to server 127.0.0.1 shell/mongo.js" & when trying to run mongodb on mac osx lion
This is actually not an error... What happens here is that Mongo relies on a daemon in order to run the local database server, so in order to "fire up" the mongo server in your shell, you have to start the mongo service first.
For Fedora Linux (wich is the Distro I use) You have to run these commands:
1 sudo service mongod start
2 mongo
And there you have it! the server is going to run. Now, If you want Mongo service
to Start when the system boots then you have to run:
sudo chkconfig --levels 235 mongod on
And that's all! If you do that, now in the shell you just have to type mongo in order
to start the server but that's pretty much it, the problem is you have to start the SERVICE first and then the SERVER :)
P.S. The commands I posted might work on other linux distros as well, not just in fedora... In case not maybe you have to tweak some words depending on the distro you're using ;)
I got the same problem when I tried to install mongo. I got Error as,
Error
"Error: couldn't connect to server 127.0.0.1 shell/mongo.js:84"
Solution:
First install mongod by using:
sudo apt-get install mongodb-server
Then type
mongod --dbpath /mongo/db
Then
sudo rm /var/lib/mongodb/mongod.lock
Then
sudo -u mongodb mongod -f /etc/mongodb.conf --repair
Thank You
You need to delete the lockfile mongod.lock or /var/lib/mongodb/mongod.lock on ubuntu, then you need to run mongod.exe or service mongodb start on ubuntu first, then run mongo.exe or mongo on ubuntu.
Either your mongod is not running (check using "ps" command) or it is listening on some outside IP address and not on localhost. So first check the process list if 'mongod' is running. If yes, check with "netstat -nap" for the related port.
Of course you can start mongod on the console manually or even look into the mongod logfile
(if there is one configured...depending on how you installed mongod).
First you have to make sure that all the files and directories in your /var/lib/mongodb/ folder (or whichever folder dbpath points to) belong to the mongodb user and mongodb group.
cd /var/lib/mongodb/
sudo chown mongodb filename.*
sudo chgrp mongodb filename.*
sudo chown -R mongodb directory
sudo chgrp -R mongodb directory
(Replace filename and directory with their respective names)
Then you can remove the lock, repair the database and restart the daemon as other people already mentioned:
sudo rm /var/lib/mongodb/mongod.lock
sudo -u mongodb mongod -f /etc/mongodb.conf --repair
sudo service mongodb start
First start your mongo server by
Users-MacBook-Pro:csv1 Admin$ mongod
all output going to: /usr/local/var/log/mongodb/mongo.log
Then open another terminal window and open shell
Users-MacBook-Pro:csv1 Admin$ mongo
Also check that your root partition has enough space to start mongod.
df -h /
You'll see smth like this on mongod launch:
Mon Aug 12 17:02:59.159 [initandlisten] recover : no journal files present, no recovery needed
Mon Aug 12 17:02:59.159 [initandlisten]
Mon Aug 12 17:02:59.159 [initandlisten] ERROR: Insufficient free space for journal files
Mon Aug 12 17:02:59.159 [initandlisten] Please make at least 3379MB available in /var/lib/mongodb/journal or use --smallfiles
Mon Aug 12 17:02:59.159 [initandlisten]
Mon Aug 12 17:02:59.159 [initandlisten] exception in initAndListen: 15926 Insufficient free space for journals, terminating
Mon Aug 12 17:02:59.159 dbexit:
Mon Aug 12 17:02:59.159 [initandlisten] shutdown: going to close listening sockets...
On Ubuntu, try this:
sudo invoke-rc.d mongodb start
It could be combination of $PATH and Permission issue.
Try following steps given below:
Update your $PATH variable to point to your MongoDB bin file. In my case brew install MongoDB to this folder:
/usr/local/Cellar/mongodb/2.4.6/
In order to update your $PATH variable, do following:
$ sudo vi /etc/paths
Then, press ‘i’ to insert text in Vi and append the your MongoDB path to the end of the ‘paths’ file and restart the terminal.
/usr/local/Cellar/mongodb/2.4.6/bin
Use ‘Esc : w q’ to save and exit from Vi editor.
Use echo to display your path variable:
$ echo $PATH
/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/bin:/usr/local/Cellar/mongodb/2.4.6/bin
Now try to check the Mongo version, if you get following, then you are on the right track!
$ mongo --version
MongoDB shell version: 2.4.6
Now we need to create the database directory. I used the default ‘/data/db’ location suggested in MongoDB docs. I also created a log directory to avoid any permission issues while Mongo tries to create any logs. Change ownership and that will do the job.
$ sudo mkdir /data/db
$ sudo mkdir /data/log
$ whoami
username
$ chown -R username /data
Now, we will create a default config file for MongoDB to be provided for the first time we run ‘mongod’ command. Now, I will also like to point out that ‘mongod’ will start a service, which will listen for incoming data connections. This is similar having ‘$service mysqld start’ executed.Let’s go ahead and create the config file. Please keep in mind that I have created very basic config file. However, you can add many other variables to configure MongoDB. This is the first time I am playing with MongoDB, so I just know as much as I read on MongoDB docs!I created ‘mongodb.conf’.
$ sudo vi /etc/mongodb.conf
Add following:
fork = true
port = 27017
quiet = true
dbpath = /data/db
logpath = /data/log/mongod.log
logappend = true
journal = true
Please note that the default port for MongoDB server is 27017. Use your own path for dbpath and logpath you created in Step – 5. Don’t forget to close and save the conf file.
Now we are all set to start our MongoDB service. Open two instances of Terminal.In Terminal 1, type in:
$ sudo mongod -f /etc/mongodb.conf
about to fork child process, waiting until server is ready for connections.
forked process: 3516
all output going to: /data/log/mongod.log
child process started successfully, parent exiting
If you get above message, then know that you have successfully started your Mongod service.
Now, to connect to it, in Terminal 2 type following:
$mongo test
MongoDB shell version: 2.4.6
connecting to: test
Server has startup warnings:
Tue Sep 3 16:55:43.527 [initandlisten]
Tue Sep 3 16:55:43.527 [initandlisten] ** WARNING: soft rlimits too low. Number of files is 256, should be at least 1000
>
Ignore the warnings, but you are successfully connected to the ‘test’ database! Cool!
That's all. I applied this solution, when I tried to install copy of MongoDB on my Mac for the first time. See if this help you too.
For detailed post you can go here - http://arcanebytes.com/2013/09/03/mongodb-installation-on-mac-os-x/#comment-1036112094.
I hope it helps!
Cheers,
Chinmay
I solved this problem on ubuntu 12.04 by following steps:
1) sudo rm /var/log/mongodb
2) sudo rm /var/lib/mongodb
3) I removed mongo and then installed it again
4) sudo service mongodb restart
and All is Well
For Ubuntu:
Just Open the terminal and enter the below command.
You just have to restart your mongoDB.
sudo service mongodb restart

I can start the MongoDB shell, but not Mongo Daemon

I'm new to MongoDB, and I'm trying to run mongod. If I type in mongo, I can run the shell (and then I exit nicely with ^C), but when I try typing mongod, I get this:
mongod --help for help and startup options
Sat Nov 24 13:42:34 [initandlisten] MongoDB starting : pid=99910 port=27017 dbpath=/data/db/ 64-bit host=Macbook-err.local
Sat Nov 24 13:42:34 [initandlisten]
Sat Nov 24 13:42:34 [initandlisten] ** WARNING: soft rlimits too low. Number of files is 256, should be at least 1000
Sat Nov 24 13:42:34 [initandlisten] db version v2.2.1, pdfile version 4.5
Sat Nov 24 13:42:34 [initandlisten] git version: nogitversion
Sat Nov 24 13:42:34 [initandlisten] build info: Darwin 172-26-13-128.dynapool.nyu.edu 12.2.0 Darwin Kernel Version 12.2.0: Sat Aug 25 00:48:52 PDT 2012; root:xnu-2050.18.24~1/RELEASE_X86_64 x86_64 BOOST_LIB_VERSION=1_49
Sat Nov 24 13:42:34 [initandlisten] options: {}
Sat Nov 24 13:42:34 [initandlisten] journal dir=/data/db/journal
Sat Nov 24 13:42:34 [initandlisten] recover : no journal files present, no recovery needed
Sat Nov 24 13:42:34 [websvr] ERROR: listen(): bind() failed errno:48 Address already in use for socket: 0.0.0.0:28017
Sat Nov 24 13:42:34 [websvr] ERROR: addr already in use
Sat Nov 24 13:42:34 [initandlisten] ERROR: listen(): bind() failed errno:48 Address already in use for socket: 0.0.0.0:27017
Sat Nov 24 13:42:34 [initandlisten] ERROR: addr already in use
Sat Nov 24 13:42:34 [initandlisten] now exiting
Sat Nov 24 13:42:34 dbexit:
Sat Nov 24 13:42:34 [initandlisten] shutdown: going to close listening sockets...
Sat Nov 24 13:42:34 [initandlisten] shutdown: going to flush diaglog...
Sat Nov 24 13:42:34 [initandlisten] shutdown: going to close sockets...
Sat Nov 24 13:42:34 [initandlisten] shutdown: waiting for fs preallocator...
Sat Nov 24 13:42:34 [initandlisten] shutdown: lock for final commit...
Sat Nov 24 13:42:34 [initandlisten] shutdown: final commit...
Sat Nov 24 13:42:34 [initandlisten] shutdown: closing all files...
Sat Nov 24 13:42:34 [initandlisten] closeAllFiles() finished
Sat Nov 24 13:42:34 [initandlisten] journalCleanup...
Sat Nov 24 13:42:34 [initandlisten] removeJournalFiles
Sat Nov 24 13:42:34 [initandlisten] shutdown: removing fs lock...
Sat Nov 24 13:42:34 dbexit: really exiting now
This seems to be a common problem around here, and I've tried:
deleting the mongod.lock file rm /data/db/mongod.lock
repair: mongod --repair
killing processes: killall -15 mongod and killall -9 mongod
I tried changing permissions: sudo chmod 0755 /data/db and sudo chown $USER /data/db
My permissions seem correct:
drwxr-xr-x 4 slaffont wheel 136 Nov 24 13:28 /data/db/
I've run out of ideas. Has anyone else had this problem? What should I do? :(
From your main comments, it seems you installed MongoDB via macports (given the /opt install location). What I believe might be happening is that the recipe for macports installed MongoDB as a launchd service.
Check this location for a mongodb related plist file: /Library/LaunchDaemons
If you find it, that means your system will automatically keep this service running. If you only want to run the mongod manually, then remove this plist and reboot. Or you can try this command:
launchctl unload /Library/LaunchDaemons/path.to.mongo.plist
Otherwise, run another instance of mongod on a different port.
sudo rm /var/lib/mongodb/mongod.lock
sudo chown -R mongodb:mongodb /var/lib/mongodb/
sudo -u mongodb mongod -f /etc/mongodb.conf --repair
sudo service mongodb start
or instead of the last one try : sudo mongod
"Address already in use for socket: 0.0.0.0:28017"
It looks like mongo is already running or another process is using port 27017
In this case, type the following command
ps wuax | grep mongo
You should see something that looks like this
User 31936 0.5 0.4 2719784 35624 ?? S 7:34pm 0:09.98 mongod
User 31945 0.0 0.0 2423368 184 s000 R+ 8:24pm 0:00.00 grep mongo
Now enter the kill command for the mongod instance (31936 in this case):
kill 31936
It looks like something is already bound to port 27017, making it seem like another mongod is running (that was not killed on the killall). Does the output of ps aux or lsof -i :27017 show any mongod processes running?
If this proves completely fruitless, you can always pass the --port <portnum> option to the mongod process via the command line, telling it to listen on a different port (use the same option for the mongo shell to tell it to connect on a different port than 27017.
As #shelman said, you already have something using that port, however your command to understand if something is running is wrong, try:
sudo netstat -lpn |grep :27017
I have noticed as well:
Sat Nov 24 13:42:34 [initandlisten] ** WARNING: soft rlimits too low. Number of files is 256, should be at least 1000
This is more of an warning but still one you should heed. I answered a question with some one who had this problem recently: Mongod runs, but Mongo returns an error that should help.
Since this seems to be a common question:
As someone who just had/solved this problem, what you're probably running into is this. When you first install Mongo (say, via apt-get), the Mongod process automatically starts running. For example:
terekhov / $ sudo apt-get install mongodb-10gen
--Snip Install Process--
Done. mongodb
start/running, process 4467 Processing triggers for ureadahead ...
terekhov / $ service mongodb start start: Job is already running: mongodb
So, if you don't need a forked daemon etc, then just type mongo to start the command shell; it will automatically search for a MongoDB daemon on port 27017. Happily, that's where ours is.
You can start/stop/restart the mongod process with this command: sudo service mongodb stop

Cannot restart mongodb on Ubuntu 11.04

I have installed mongodb and when i typing the following command i am getting these messages
root#amila:~# service mongodb status
mongodb stop/waiting
root#amila:~# service mongodb restart
restart: Unknown instance:
root#amila:~# service mongodb stop
stop: Unknown instance:
I want to restart mongodb.but unable to restart.
EDIT:
here is the /etc/mongodb.conf
dbpath=/var/lib/mongodb
logpath=/var/log/mongodb/mongodb.log
logappend=true
bind_ip = 127.0.0.1
auth = true
Given the output of the start command, this is starting correctly. When you are running status, it seems to have been stopped. You need to look into the log file:
/var/log/mongodb/mongodb.log
That will tell you why the mongod is stopping. There are a couple of likely causes, but without seeing that output it will be hard to answer definitively. My general recommendations:
Do not use bind_ip - it is a bad idea in general Update (2016): Removing this because the issues with bind_ip that caused me to write this back in 2012 have been fixed, and it is even on by default in official packages now. Worth trying without the setting as a troubleshooting step, but not a bad idea to use it in general.
Check for something else running on port 27017
Specify the port explicitly in the mongodb.conf file, even if you want to use the default
Finally, take a look at this answer for how to fix file permissions, just in case that is a problem (usually caused by running as root at some point):
mongodb crashes after unexpected shutdown
Sorry if you consider this "SO archeology".
I don't know if this is the same issue, but I'm facing something similiar right now.
End of my log:
Mon Nov 19 10:25:37 [initandlisten] exception in initAndListen: 12596 old lock file, terminating
Mon Nov 19 10:25:37 dbexit:
Mon Nov 19 10:25:37 [initandlisten] shutdown: going to close listening sockets...
Mon Nov 19 10:25:37 [initandlisten] shutdown: going to flush diaglog...
Mon Nov 19 10:25:37 [initandlisten] shutdown: going to close sockets...
Mon Nov 19 10:25:37 [initandlisten] shutdown: waiting for fs preallocator...
Mon Nov 19 10:25:37 [initandlisten] shutdown: lock for final commit...
Mon Nov 19 10:25:37 [initandlisten] shutdown: final commit...
Mon Nov 19 10:25:37 [initandlisten] shutdown: closing all files...
Mon Nov 19 10:25:37 [initandlisten] closeAllFiles() finished
Mon Nov 19 10:25:37 dbexit: really exiting now
So I think the lock file is not closed properly (for example when shutting down). You should use
mongod --repair
But it didn't work for me, so I just removed /var/lib/mongodb/mongod.lock and then started the server. This is not the secure way, and there is possibility, that data in your database is corrupt if you do so.
Sorry to rehash this thread, but I'm putting this here because I ran into the exact same issue and this worked.
taken from here:
http://pastebin.com/6hP42r69
sudo -u mongodb mongod --repair -dbpath /var/lib/mongodb
I'm running linux mint 15 and current mongodb build.
I had the same problem and I solved running just:
$ service mongod restart
I solved this manually deleting .lock file like that
sudo rm -i /var/lib/mongodb/mongod.lock
then restart
Looks like Mongo is unable to start for some reason. Apart from the suggestions already provided by Adam.
Check to see if you have write permissions to the data directory.
Check to see if you have write permissions to the /var/log directory.
Is mongod running as root or as another user? Check the init script for mongodb
to see what user is mongod running as.
One of the possible cases might be mongod.lock(which was causing my problem).
Open your log file at
/var/log/mongodb/mongod.log
See if you have any exception because of mongod.lock file. Copy it's location and remove it
sudo rm /var/lib/mongodb/mongod.lock
Then run
sudo service mongod restart
To check if everything is fine now.
sudo service mongod status
Expected output will be mongod start/running
Sometimes the problems can be caused by not having enough free storage for journaling to continue smoothly.
Did a tail -100 /var/log/mongodb/mongod.log and found this error:
exception in initAndListen: 15926 Insufficient free space for journals, terminating
Please make at least 3379MB available in /var/lib/mongodb/journal or use --smallfiles`
Resolved it by putting smallfiles=true in my MongoDB config file /etc/mongod.conf.
Then sudo rm /var/lib/mongodb/mongod.lock
And then restarting the MongoDB instance using service mongod start
Hope this help.