Can't connect to MongoDB running in docker container - mongodb

I run MongoDB in a docker container like this
docker run --name mongo -d -p 27017:27107 mongo
Check with docker ps shows
77f1a11295c3 mongo "docker-entrypoint.s…" 20 minutes ago Up 20 minutes 27017/tcp, 0.0.0.0:27017->27107/tcp mongo
so it's running with the port mapped correctly.
When I try to setup a connection via Intellij
it fails with
com.mongodb.MongoTimeoutException: Timed out after 10000 ms while waiting to connect. Client view of cluster state is {type=UNKNOWN, servers=[{address=localhost:27017, type=UNKNOWN, state=CONNECTING, exception={com.mongodb.MongoSocketReadException: Exception receiving message}, caused by {java.net.SocketException: Connection reset}}].
When I try to connect to connect from a golang web server
clientOptions := options.Client().ApplyURI("mongodb://localhost:27017")
client, err := mongo.Connect(context.TODO(), clientOptions)
I get this error and it shuts down the web server:
server selection error: server selection timeout, current topology: { Type: Unknown, Servers: [{ Addr: localhost:27017, Type: Unknown, Average RTT: 0, Last error: connection() error occured during connection handshake: connection(localhost:27017[-64]) incomplete read of message header: read tcp 127.0.0.1:40700->127.0.0.1:27017: read: connection reset by peer }, ] }
Is this a bug in the MongoDB docker image, or is there something else I need to do?

I think you have a typo mistake while creating a container. 27017 and 27107
docker run --name mongo -d -p 27017:27017 mongo
This is clearly visible in docker ps command.
Can you try and see if it solves the problem ?

As #fly2matrix said you have a typo, and I believe you have another issue:
you forward the ports 27017:27107, in which the container port is incorrect. Check 27107 instead of 27017
It seems you're connecting to localhost instead of mongo as I see the phrase localhost in your code.

Related

Mongo db - connection exception from another container in Docker

In Jenkins I am trying to run in docker container my backend services, which are dependant on Mongo DB.
Currently I have it like this:
stage('Backend clone and build') {
steps { git ([url : 'https://github...', branch : 'develop', credentialsId : '***'])
sh 'docker pull mongo'
sh 'docker run -d -p 27017:27017 --name mongodb mongo'
sh 'docker build -t myimage2 .'
sh 'docker run myimage2'
}
}
After I run the build, I have this exception:
com.mongodb.MongoSocketOpenException: Exception opening socket
at com.mongodb.internal.connection.SocketStream.open(SocketStream.java:70) ~[mongodb-driver-core-4.1.2.jar!/:na]
at com.mongodb.internal.connection.InternalStreamConnection.open(InternalStreamConnection.java:143) ~[mongodb-driver-core-4.1.2.jar!/:na]
at com.mongodb.internal.connection.DefaultServerMonitor$ServerMonitorRunnable.lookupServerDescription(DefaultServerMonitor.java:188) ~[mongodb-driver-core-4.1.2.jar!/:na]
at com.mongodb.internal.connection.DefaultServerMonitor$ServerMonitorRunnable.run(DefaultServerMonitor.java:144) ~[mongodb-driver-core-4.1.2.jar!/:na]
at java.base/java.lang.Thread.run(Thread.java:834) ~[na:na]
Caused by: java.net.ConnectException: Connection refused (Connection refused)
I tried to link the container with mongodb container as : sh 'docker run --link mongodb:27017 myimage2', but no success.
I was also thinking to define the host via mongodb://localhost:27017 but this command is not accepted in docker. I did not find any solution how to get rid of that exception.
If I dive deeper to that exception, I also found there this:
Timed out after 30000 ms while waiting to connect. Client view of cluster state is {type=UNKNOWN, servers=[{address=localhost:27017, type=UNKNOWN, state=CONNECTING, exception={com.mongodb.MongoSocketOpenException: Exception opening socket}, caused by {java.net.ConnectException: Connection refused (Connection refused)}}]; nested exception is com.mongodb.MongoTimeoutException: Timed out after 30000 ms while waiting to connect.
So for some reason it is not able to connect to localhost:27017 where the mongodb should be running.
To help others who may have this same problem, you have 2 options to set up correctly:
Create a docker network and start all the containers on this network so that the containers can discover each other, the default is the bridge network type. In this case, the containers are isolated from the host networking interface and can't be accessed via "localhost".
If you want the containers to run on the host network and be accessible via the host network interface aka localhost or a host IP, do as docker suggests in it's official docs here: https://docs.docker.com/network/host/

mongos Socket operation timed out

I've got a mongos router running on amazon ECS in an EC2 machine, and also hosted on a route53 hosted zone.
The thing is that I can reach the mongos service via HTTP with the record name:
https://mongos-router.awsqa.xxx.net/
With the response:
It looks like you are trying to access MongoDB over HTTP on the native
driver port.
Which means that mongos is running correctly.
The ecs logs I get shows its running correctly also.
But trying to connect to it via mongo command:
$ mongo --host mongos-router.awsqa.xxx.net
MongoDB shell version v4.4.4
connecting to: mongodb://mongos-router.awsqa.xxx.net:27017/?compressors=disabled&gssapiServiceName=mongodb
Error: couldn't connect to server mongos-router.awsqa.xxx.net:27017, connection attempt failed: NetworkTimeout: Error connecting to mongos-router.awsqa.xxx.net:27017 (xx.xxx.xx.xx:27017) :: caused by :: Socket operation timed out :
connect#src/mongo/shell/mongo.js:374:17
#(connect):2:6
exception: connect failed
exiting with code 1
I've confirmed that the container port is correct also:
"portMappings": [
{
"hostPort": 0,
"protocol": "tcp",
"containerPort": 27017
}
],
a part of mongos.conf file:
net:
bindIp: 0.0.0.0
port: 27017
Dockerfile final command:
CMD [ "mongos", "--config", "/etc/mongos.conf" ]
Any idea at all to what is causing this network timeout error will be helpful

opsmanager application not able to connect to opsmanager database

I have installed opsmanager database in one instance and application in other instance
Taken points from this link
In application database server when i netstat -nltp it shows 127.0.0.1:27017 running mongod
After installing application in other instance I edit the mongo.mongoUri as mongodb://db_instance_publicip:27017 in /opt/mongodb/mms/conf/conf-mms.properties
When I start by sudo service mongodb-mms start is shows below error
uri=mongodb://db_instance_publicip:27017/?maxPoolSize=150} Error: Timed out after 30000 ms while waiting to connect.......
Error: Timed out after 30000 ms while waiting to connect. Client view of cluster state is {type=UNKNOWN, servers=[{address=db_instance_publicip:27017, type=UNKNOWN, state=CONNECTING, exception={com.mongodb.MongoSocketOpenException: Exception opening socket}, caused by {java.net.ConnectException: Connection refused (Connection refused)}}]
Was facing the same issue. The issue in my case was that my Mongo DB was binding to only localhost thanks to this line in /etc/mongod.conf:
bindIp: 127.0.0.1 # Enter 0.0.0.0,:: to bind to all IPv4 and IPv6 addresses or, alternatively, use the net.bindIpAll setting.
As the commented part in the same line suggests, I had to replace "127.0.0.1" with "0.0.0.0" and restart Mongo DB and it started working!

Can't login to Wekan DB CentOS console

I have recently installed Wekan on a CentOS (CentOS Linux release 7.6.1810) server and it works well. However I need to login to the MongoDB. I have followed the installation guide of MongoDB and set a password for 'admin' user. But when When the "mongo -u admin -p" command is entered it prompts for password and I get the following error. However I have another issue not starting "mongod", but it is enabled.
connecting to: test
2020-06-12T01:20:23.618-0400 W NETWORK [thread1] Failed to connect to 127.0.0.1:27017, in(checking socket for error after poll), reason: errno:111 Connection refused
2020-06-12T01:20:23.618-0400 E QUERY [thread1] Error: couldn't connect to server 127.0.0.1:27017, connection attempt failed :
connect#src/mongo/shell/mongo.js:229:14
#(connect):1:6
exception: connect failed
I created this user in mongo which does not seem to work when trying to login to the DB.
$ mongo
db.createUser(
{
user: "admin",
pwd: "abcd1234",
roles: [ { role: "root", db: "admin" } ]
}
)
Firstly I installed the latest version of mongodb and then I downgraded to to 3.2 which works with wekan. However Wekan still works fine without any issue.
This is the error I get when starting the mongod.
$ systemctl start mongod
Job for mongod.service failed because the control process exited with error code.
See "systemctl status mongod.service" and "journalctl -xe" for details.
$ systemctl enable mongod
mongod.service is not a native service, redirecting to /sbin/chkconfig.
Executing /sbin/chkconfig mongod on
The port mongod uses is 27019 which I found from the following command.
$ ss -nutlp
tcp LISTEN 0 128 127.0.0.1:27019 *:* users:(("mongod",pid=11578,fd=6))
What am I doing wrong here?
In the output of the mongo shell connection failure:
Failed to connect to 127.0.0.1:27017, in(checking socket for error after poll), reason: errno:111 Connection refused
This tells you that it is attempting to connect to port 27017 on localhost, and that the operating system actively refused the connection because nothing is listening on that port.
The ss output shows that mongod is listening on port 27019.
Try adding --port 27019 to your mongo command line.

Mongodb 3.4 - fail to start mongodb

How can I start mongodb. The child process fail.
-bash-4.1$ mongod --config /mongodb/etc/mongod-ctr.conf
about to fork child process, waiting until server is ready for connections.
forked process: 22965
ERROR: child process failed, exited with error number 48
-bash-4.1$ mongo
MongoDB shell version v3.4.17
connecting to: mongodb://127.0.0.1:27017
2018-11-27T13:52:13.826-0500 W NETWORK [thread1] Failed to
connect to 127.0.0.1:27017, in(checking socket for error after
poll), reason: Connection refused
According to the MongoDB error code manual, and your error status, there seems to be another process either running on port 27017, or you just don't have the right permissions.
You can use the command lsof -i -P -n to see a list of all applications, and the ports they are using.