ChatGPT解决这个技术问题 Extra ChatGPT

Couldn't connect to server 127.0.0.1:27017

I'm getting the following error:

alex@alex-K43U:/$ mongo
MongoDB shell version: 2.2.0
connecting to: test
Thu Oct 11 11:46:53 Error: couldn't connect to server 127.0.0.1:27017 src/mongo/shell/mongo.js:91
exception: connect failed
alex@alex-K43U:/$ 

This is what happens when I try to start mongodb:

* Starting database mongodb                                             [fail]

I already tried mongo --repair

I made chown and chmod to var, lib, and data/db and log mongodb.

Not sure what else to do. Any suggestions?

mongodb.log:

***** SERVER RESTARTED *****


Thu Oct 11 08:29:40 
Thu Oct 11 08:29:40 warning: 32-bit servers don't have journaling enabled by default. Please use --journal if you want durability.
Thu Oct 11 08:29:40 
Thu Oct 11 08:29:41 [initandlisten] MongoDB starting : pid=1052 port=27017 dbpath=/var/lib/mongodb 32-bit host=alex-K43U
Thu Oct 11 08:29:41 [initandlisten] 
Thu Oct 11 08:29:41 [initandlisten] ** NOTE: when using MongoDB 32 bit, you are limited to about 2 gigabytes of data
Thu Oct 11 08:29:41 [initandlisten] **       see http://blog.mongodb.org/post/137788967/32-bit-limitations
Thu Oct 11 08:29:41 [initandlisten] **       with --journal, the limit is lower
Thu Oct 11 08:29:41 [initandlisten] 
Thu Oct 11 08:29:41 [initandlisten] db version v2.2.0, pdfile version 4.5
Thu Oct 11 08:29:41 [initandlisten] git version: f5e83eae9cfbec7fb7a071321928f00d1b0c5207
Thu Oct 11 08:29:41 [initandlisten] build info: Linux domU-12-31-39-01-70-B4 2.6.21.7-2.fc8xen #1 SMP Fri Feb 15 12:39:36 EST 2008 i686 BOOST_LIB_VERSION=1_49
Thu Oct 11 08:29:41 [initandlisten] options: { config: "/etc/mongodb.conf", dbpath: "/var/lib/mongodb", logappend: "true", logpath: "/var/log/mongodb/mongodb.log" }
Thu Oct 11 08:29:41 [initandlisten] Unable to check for journal files due to: boost::filesystem::basic_directory_iterator constructor: No such file or directory: "/var/lib/mongodb/journal"
************** 
Unclean shutdown detected.
Please visit http://dochub.mongodb.org/core/repair for recovery instructions.
*************
Thu Oct 11 08:29:41 [initandlisten] exception in initAndListen: 12596 old lock file, terminating
Thu Oct 11 08:29:41 dbexit: 
Thu Oct 11 08:29:41 [initandlisten] shutdown: going to close listening sockets...
Thu Oct 11 08:29:41 [initandlisten] shutdown: going to flush diaglog...
Thu Oct 11 08:29:41 [initandlisten] shutdown: going to close sockets...
Thu Oct 11 08:29:41 [initandlisten] shutdown: waiting for fs preallocator...
Thu Oct 11 08:29:41 [initandlisten] shutdown: closing all files...
Thu Oct 11 08:29:41 [initandlisten] closeAllFiles() finished
Thu Oct 11 08:29:41 dbexit: really exiting now

EDIT:

I removed the lock then did mongod repair and got this error:

Thu Oct 11 12:05:37 [initandlisten] exception in initAndListen: 10309 Unable to create/open lock file: /data/db/mongod.lock errno:13 Permission denied Is a mongod instance already running?, terminating

so I did it with sudo:

alex@alex-K43U:~$ sudo mongod --repair
Thu Oct 11 12:05:42 
Thu Oct 11 12:05:42 warning: 32-bit servers don't have journaling enabled by default. Please use --journal if you want durability.
Thu Oct 11 12:05:42 
Thu Oct 11 12:05:42 [initandlisten] MongoDB starting : pid=5129 port=27017 dbpath=/data/db/ 32-bit host=alex-K43U
Thu Oct 11 12:05:42 [initandlisten] 
Thu Oct 11 12:05:42 [initandlisten] ** NOTE: when using MongoDB 32 bit, you are limited to about 2 gigabytes of data
Thu Oct 11 12:05:42 [initandlisten] **       see http://blog.mongodb.org/post/137788967/32-bit-limitations
Thu Oct 11 12:05:42 [initandlisten] **       with --journal, the limit is lower
Thu Oct 11 12:05:42 [initandlisten] 
Thu Oct 11 12:05:42 [initandlisten] db version v2.2.0, pdfile version 4.5
Thu Oct 11 12:05:42 [initandlisten] git version: f5e83eae9cfbec7fb7a071321928f00d1b0c5207
Thu Oct 11 12:05:42 [initandlisten] build info: Linux domU-12-31-39-01-70-B4 2.6.21.7-2.fc8xen #1 SMP Fri Feb 15 12:39:36 EST 2008 i686 BOOST_LIB_VERSION=1_49
Thu Oct 11 12:05:42 [initandlisten] options: { repair: true }
Thu Oct 11 12:05:42 [initandlisten] Unable to check for journal files due to: boost::filesystem::basic_directory_iterator constructor: No such file or directory: "/data/db/journal"
Thu Oct 11 12:05:42 [initandlisten] finished checking dbs
Thu Oct 11 12:05:42 dbexit: 
Thu Oct 11 12:05:42 [initandlisten] shutdown: going to close listening sockets...
Thu Oct 11 12:05:42 [initandlisten] shutdown: going to flush diaglog...
Thu Oct 11 12:05:42 [initandlisten] shutdown: going to close sockets...
Thu Oct 11 12:05:42 [initandlisten] shutdown: waiting for fs preallocator...
Thu Oct 11 12:05:42 [initandlisten] shutdown: closing all files...
Thu Oct 11 12:05:42 [initandlisten] closeAllFiles() finished
Thu Oct 11 12:05:42 [initandlisten] shutdown: removing fs lock...
Thu Oct 11 12:05:42 dbexit: really exiting now

But still having the same problem.

i think this link will help you stackoverflow.com/questions/9647561/… by changing the db path of mongo
sudo service mongod restart worked for me

N
Nanhe Kumar
Step 1: Remove lock file.
sudo rm /var/lib/mongodb/mongod.lock

Step 2: Repair mongodb. 
sudo mongod --repair 

Step 3: start mongodb.
sudo start mongodb 
or
sudo service mongodb start

Step 4: Check status of mongodb.
sudo status mongodb 
or   
sudo service mongodb status

Step 5: Start mongo console.
mongo 

hi Nanhe Kumnar what is initial path in terminal. Is we should go to cd /usr/local/opt/mongodb/ folder or another, i'm facing following problem Vijayvir-Singh:~ vijayvir$ sudo rm /var/lib/mongodb/mongod.lock rm: /var/lib/mongodb/mongod.lock: No such file or directory Vijayvir-Singh:~ vijayvir$
@vvss first find the path of file. use this. locate mongod.lock
In version 2.4.8 it's /data/db/mongod.lock instead of /var/lib/mongodb/mongod.lock
e
eloone

Did you run mongod before running mongo?

I followed installation instructions for mongodb from http://docs.mongodb.org/manual/tutorial/install-mongodb-on-os-x/ and I had the same error as you only when I ran mongo before actually running the mongo process with mongod. I thought installing mongodb would also launch it but you need to launch it manually with mongod before you do anything else that needs mongodb.


This was my issue. I'm surprised this isn't made clear upfront in the "Getting Started With Mongo" docs.
This really makes no sense from a User Experience perspective. mongo.exe should be the one that starts the DB.
J
Javier Gomez

This it's because the mongod process it's down, you must to run the commands bellow in order to get up the mongod process:

~$ sudo service mongodb stop

~$ sudo rm /var/lib/mongodb/mongod.lock

~$ sudo mongod --repair --dbpath /var/lib/mongodb

~$ sudo mongod --fork --logpath /var/lib/mongodb/mongodb.log --dbpath /var/lib/mongodb

~$ sudo service mongodb start

Hope this helps you.


Problem solved. Thank you. I'd add the commands sudo service mongod stop and sudo service mongodb stop before the first command because some people might still have them running.
It helped me restart the mongo. however i am not able to access the old database anymore. Could you please help me out here.
T
Trott

The log indicates that mongodb is terminating because there is an old lock file.

If you are not and were not running with journaling, remove the lock file, run repair, and start mongodb again.

If you are or were running with journaling turned on, see the relevant Mongo DB docs. Note that they say "If you are running with Journaling you should not do a repair to recover to a consistent state." So if you were journaling, the repair may have made things worse.


That's bad that a repair can damage things!
In journaling enabled situation what to do if i get an connection refused error very freqeuntly?
A
Arun Kumar Mohan

Try

sudo service mongodb start

This solved my issue.


What is the difference between sudo service mongod start and sudo service mongodb start
I'm not so sure but guessing mongodb is the name of service, and mongod is the name of the program file.
M
Mohammadreza Abdoli

MAC OS

if you have mac and useing mongodb4.4, you can run:

brew services start mongodb-community@4.4

m
msnfreaky

Check your filesystem free space and increase it if its less. This also could cause the mongo not to start. Check the /var/log/mongodb/mongodb.log file.

ERROR: Insufficient free space for journal files
Please make at least 3379MB available in /var/lib/mongodb/journal or use --smallfiles

This happened to me and I added smallfiles=true to the file /etc/mongod.conf
M
Max

Try running mongod before mongo.

sudo /usr/sbin/mongod on my opensuse

This solved my problem,


u
user3470929

so first you have to remove the mongod.lock file by below command

sudo rm /var/lib/mongodb/mongod.lock

and then restart the mongo service by issuing below command

sudo service mongod restart 

t
theProcrastinator

In Windows run cmd as Admin:

1.Create Directory:

mkdir c:\mongo\data\db

2.Install Service:

mongod.exe --install --logpath c:\mongo\logs --logappend --bind_ip 127.0.0.1 --dbpath c:\mongo\data\db --directoryperdb

3.Start MongoDB:

net start MongoDB

4.Start Mongo Shell:

c:\mongo\bin\mongo.exe

This solution worked for me.


E
Efren

You can check with netstat -anp | grep 27017 to see if the port is in use by another process.


strange, I got this: alex@alex-K43U:~$ netstat -anp | grep 27017 (Not all processes could be identified, non-owned process info will not be shown, you would have to be root to see it all.)
That is a normal result when executing as a non-root user, since netstat will show only processes your user is permitted to see.
you can use sudo netstat -anp | grep 27017 to by pass that warning
This one works for me, Thanks a lot. I get this when I ran the given code unix 2 [ ] STREAM 31095 - /tmp/mongodb-27017.sock and I removed the .sock file, now everything is fine.
R
RileyManda

For future reference,Follow these steps to avoid similar errors:

1.Download MondoDB https://www.mongodb.com/

2.Open a terminal and cd into your downloads folder or whatever folder youve saved your mondodb download(Ensure you extract your mongodb folder before you cd into it)

cd Downloads

3.Move mongodb to your usr/local path

sudo mv mongodb-osx-... /usr/local/mongodb

4.cd into your local folder

cd /usr/local/mongodb

5.make a new directory

sudo mkdir -p /data/db

6.cd into the new directory just created above

cd /data/db

7.give mongo permisions

sudo chown YourMacUserName /data/db

8.Then go/open your .bash_profile

To do so,follow these steps:

In your a new terminal

1.cd 2.pwd 3.ls -l

Check if the .bash_profile appears on your list of files on your terminal

if not create the -bash_profile

Creating .bash_profile:

In your terminal

touch .bash_profile

//skip this step if you already have a .bash_profile

Step8:

Next in your terminal:

open .bash_profile

And in your bash file that pops open add the following:

MONGO_PATH=/usr/local/mongodb
export PATH=$PATH:$MONGO_PATH/bin

And then save.(File Save or command S/CMD+S)

Step9:back in your terminal:

source .bash_profile

Now open two terminals.One will for your mondo daemon the other for your mongo.

Terminal 1: in your terminal type:mongod

mongodb

https://i.stack.imgur.com/vdsr6.png

Terminal 2:

mongo

https://i.stack.imgur.com/0YYqY.png

Also make sure you dont make the following typo error when starting your mongod in your terminal: This is incorrect

mongo d

gives off the following error: Failed to connect to 127.0.0.1:27017, in(checking socket for error after poll), reason: Connection refused

This is Correct:

mongod

(There should be no space between the words mongo and d..mondod

Finally always keep in mind that you have to run mondod before you run mongo on your terminals.


A
Abel

First try:

sudo service mongod restart 

If u get error: Active: failed (Result: exit-code) since Mon 2021-03-29 19:59:07 WEST; 3s

This may occur because the permission setting on /var/lib/mongodb and /tmp/mongodb-27017.lock are wrong. You will have to change the owner to monogdb user

Try:

chown -R mongodb:mongodb /var/lib/mongodb
chown mongodb:mongodb /tmp/mongodb-27017.sock

sudo service mongod restart

that was my issue, thanks. Why is that keeps occurring over some time? I had to fix it in the same 2 weeks ago
s
shA.t

This worked for me:

sudo rm /var/lib/mongodb/mongod.lock    
sudo service mongodb restart

A
Ahmad Al-Kurdi

In the terminal run these commands

1)

$ sudo service mongod start

2)

 $ mongo

y
yangsibai

I followed the doc at http://docs.mongodb.org/manual/tutorial/install-mongodb-on-red-hat/.

After configured and reboot, I executed sudo service mongod start and got ... [FAILED].

At last, I found that mongod had started. I think the yum install added it to auto start.

To check if your mongod is running: service mongod status .

Hope this can help someone has same problem.


s
shA.t

After frequent attempt finally I got to troubleshoot the problem...

Step 1: ps aux | grep mongo
Step 2: sudo rm /var/lib/mongodb/mongod.lock
Step 3: sudo mongod --repair
Step 4: mongo

H
Haoming Zhang

This error could be caused by the MongoDB's bind IP setting. You can check MongoDB's config file by

$ sudo vi /etc/mongodb.conf

In my case, the bind IP is set to server's intranet address, just as following:

bind_ip = 10.10.1.14 
#port = 27017

So I have give mongo an IP parameter to connect to shell by type:

$ mongo 10.10.1.14

Don't forget to restart mongodb service if you changed the config.


t
thehme

I have mongo version 3.2.1 and had to delete the lock file from /data/db/ and after this, ran mongod and it started successfully.

>rm /data/db/mongod.lock
>mongod

k
kta

After removing mongod.lock which was inside the data directory in my windows OS,it was still showing the same error message. I had to run mongod with --dbpath to make the mongo command run without errors.


x
xameeramir

Although the answers are received, I would wish to discuss about network errors in MongoDB.

https://lh3.googleusercontent.com/HJnIrLrd4gm35QhZZfacjLtb7TkNZ2wdbQgxq2iZIc5Z9UCcf5entavFy-5m7iFojDQVynpMxj2NYT_U3dAcT-6Aek8O4m5TDfxoCVEfkn01sboUP7BgIib7BrH9JPG35xRFfl5cS2SUDH_tKVoQ4uGaQ3iiBeJhxP4CewBNGyfe44xSSX7gGh_wLnLplzNNbjDOuR5n_QNh3X23Ngh2hM6X7MGzdL4oR2pIy2ReHXSKp0f-citqdSJabig3puMFyx_hvCdrQUzcW5fX2rGlHad8vXs8bbEV_OQBesPAiczoJ6ifKMI-CDwFM94dBaQXGM3Y5iSkTNCWeZ6BWnL5LFxneY2ygEMjZzBuxDZSQyZnKBbSYmyUgDe_9OVmaQ7KVzJm9opwY-rgWjKZ1952rarW9gNxuqkG5bdx-pbL-VaDrXgBf2Cee7tBc2OGXdISLO8fgeT6eDddMavOlEStTKlpt55P1do-SNDtEzbjc0mA-z4vMoZApl3kS99ZNU1kDC-mYnkSSi5bsu442p4ET2I2v3PJODOwJa6xHn0_iaGNNMPe-RxPHDshiAre0qUiqCKfwmuZH8uUZQ2n2aPnkUX5XemyNekycbqAucXXbCnkG8b4=w514-h158-no

Setting the safe write concerns is not the full proof method to make sure that we're safe. Let's assume that w=1 & j=true are set, what if the write acknowledgement didn't received from the server? Well, the likelyhood is it didn't happened, but it might have happened. The reason why this might have happened is that there are network errors - there are reasons that we may not receive an affirmative response. So, we can send the request from the application through a driver of language of choice. mongod can complete it successfully and then there could be a TCP reset, and the network actually can get reset in a way that we never receive response. So, we could get an error and on the error, we might assume that we got an error. It didn't happened, but it may happen.

For an insert, it's possible to guard against it. It's possible because if we let the driver create the _id and we do an insert - then we could do that insert multiple times and it would be any harm. Because, if we do this 1st time and we get an error and we're not sure whether or not that insert completed because it's a network error, then we could just do it again. And provided we perform it again, tyr to perform it with the exact _id. The worst case scenario is we'll get a duplicate key error when we try to insert it.

However, an update is where the problem occurs. Especially, the update which is not item potent, that for instance included a $ink command. So, we're telling the database to increment a certain field. Well in that case, if we get a network error and we don't know whether or not the update occurred. Now, maybe we know enough about the values that we can check with them that the update occurred, which is fine. But if we don't know the starting value in the database for that field, then it's not possible for us to know whether or not it occurred or not in case of network error. This kind of issues are extremely rare with a fine network.

And if we really need to avoid it at all costs, what we need to do is turn on all our updates into inserts, by reading the full value of the document out of the database and then potentially deleting it and inserting it again or just inserting a new one.

The reasons why an application may receive an error back even if the write was successful:

The network TCP connection between the application and the server was reset after the server received a write but before a response could be sent.

The MongoDB server terminates between receiving the write and responding to it.

The network fails between the time of the write and the time the client receives a response to the write.


V
Varsh

This Works for me To stop the mongodb use:

use admin
db.shutdownServer()

And to Restart:

sudo service mongod restart

S
Sumukh Bhandarkar

Adding the bin to PATH in Environment Variables helped.

GOTO Installation Path and copy the ../bin to the PATH variables in environment variables in Windows


G
Gireesh k

type windows+r and enter the following

services.msc start MongoDB

now type "mongo" in cmd in the respective path where the mongo.exe is present, it will start working.


I
Ivan Kaloyanov

1.Create new folder in d drive D:/data/db

2.Open terminal on D:/data/db

3.Type mongod and enter.

4.Type mongo and enter.

and your mongodb has strated............


A
Ajay Rawat

Just run mongod --repair from C:\Program Files\MongoDB\Server\4.0\bin

Here is the doc https://docs.mongodb.com/manual/tutorial/recover-data-following-unexpected-shutdown/


S
Shashwat Gupta

simple Run Two commends

sudo rm /var/lib/mongodb/mongod.lock
sudo mongod --repair 

S
Shyambeer Singh

I got a similar error but the root cause was different. After installing the mongodb using homebrew. https://docs.mongodb.com/manual/installation/ I have to start the "mongod" service before giving the "mongo" command on terminal.


L
Laura Corssac

If you are using OS X systems, there is no service commands. So you won't be able to execure sudo service mongod start.

Check this answer https://unix.stackexchange.com/a/155746 for more help!


M
Mohit Rakhade

Err : Couldn't connect to server 127.0.0.1:27017

This is solution for WINDOWS users enter code here 1. Create directory:

mkdir c:\mongo\data\db

Install service: first go to your bin directory in programs files of MongoDB with cd

mongod.exe --install --logpath

Start MongoDB: net start MongoDB

4.Start Mongo Shell:

`c:\mongo\bin\mongo.exe` or

go to until bin and enter mongo

Note : Open terminal in Administrator mode


关注公众号,不定期副业成功案例分享
Follow WeChat

Success story sharing

Want to stay one step ahead of the latest teleworks?

Subscribe Now