Unable to connect to mongodb in docker container - mongodb

Getting connection refused when trying to connect to MongoDb in Docker container.
Here's the output of docker ps:
docker ps CONTAINER ID IMAGE COMMAND
CREATED STATUS PORTS
NAMES 21063296d7d8 mongo "/entrypoint.sh mongo"
44 minutes ago Up 29 minutes 0.0.0.0:57017->27017/tcp
mongodb-dev
Here's the snippet of docker logs mongodb-dev:
2015-11-28T05:36:36.363+0000 I CONTROL [initandlisten] db version
v3.0.7 2015-11-28T05:36:36.363+0000 I CONTROL [initandlisten] git
version: 6ce7cbe8c6b899552dadd907604559806aa2e9bd
2015-11-28T05:36:36.363+0000 I CONTROL [initandlisten] build info:
Linux ip-10-183-78-195 3.2.0-4-amd64 #1 SMP Debian 3.2.46-1 x86_64
BOOST_LIB_VERSION=1_49 2015-11-28T05:36:36.363+0000 I CONTROL
[initandlisten] allocator: tcmalloc 2015-11-28T05:36:36.363+0000 I
CONTROL [initandlisten] options: {} 2015-11-28T05:36:36.365+0000 I
NETWORK [initandlisten] waiting for connections on port 27017
Here's the error when connecting to MongoDB in docker using port 57017
mongo localhost:57017
MongoDB shell version: 3.0.7 connecting to: localhost:57017/test
2015-11-27T22:09:22.733-0800 W NETWORK Failed to connect to
127.0.0.1:57017, reason: errno:61 Connection refused 2015-11-27T22:09:22.734-0800 E QUERY Error: couldn't connect to
server localhost:57017 (127.0.0.1), connection attempt failed
at connect (src/mongo/shell/mongo.js:181:14)
at (connect):1:6 at src/mongo/shell/mongo.js:181 exception: connect failed
Please let me know what I'm missing here. Thank you for your help!

It depends if you are using docker directly on a Linux host or on a Mac.
In the latter case, you would need to make sure you have port forwarded 57017 in your VirtualBox connection settings, as I mention in "How to connect mysql workbench to running mysql inside docker?"
VBoxManage controlvm "boot2docker-vm" --natpf1 "tcp-port57017 ,tcp,,57017,,57017"
VBoxManage controlvm "boot2docker-vm" --natpf1 "udp-port57017 ,udp,,57017,,57017"

Related

loading docker container from existing project, mongo will not connect to database Ubuntu 18.04

At first I had a problem where the mongod service stopped working altogether. Thereupon I completely uninstalled mongodb and reinstalled it and followed instructions per this thread: https://askubuntu.com/questions/921753/failed-to-start-mongod-service-unit-mongod-service-not-found
This caused the mongod service to finally start.
In order for it to work, I also had to follow instructions from this thread:
mongo - couldn't connect to server 127.0.0.1:27017
So far this configuration has worked only 1 time and that was after I followed the instructions to remove the .lock file in the mongod directory and then repair mongod.
I also tried to remove the sock file like stated here: MongoDB Failing to Start - ***aborting after fassert() failure
Full project reset
Then I tried just disconnecting the project entirely; close nodemon for backend, exit ng serve on frontend, stop and remove mongo docker container... And tried again. Here is my step-by-step process after everything should have been fixed:
Restart mongod service
sudo service mongod restart
Restart frontend (executed from frontend directory)
ng serve frontend
Restart backend (executed from backend directory)
npx nodemon backend
Restart mongo container (executed from deploy directory, mongo is the docker container name, I'm building it from existing provided deploy script)
sudo docker-compose up -d mongo
sudo docker ps -a # check container was created and is running
Debugging information:
sudo service mongod status
Loaded: loaded (/lib/systemd/system/mongod.service; enabled; vendor preset:
Active: active (running) since Thu 2020-01-09 11:21:43 CET; 3min 40s ago
Docs: https://docs.mongodb.org/manual
Main PID: 17242 (mongod)
CGroup: /system.slice/mongod.service
└─17242 /usr/bin/mongod --config /etc/mongod.conf
janv. 09 11:21:43 junior-LIFEBOOK-E752 systemd[1]: Started MongoDB Database Server
sudo mongod2020-01-09T11:25:45.983+0100 I CONTROL [main] Automatically disabling TLS 1.0, to force-enable TLS 1.0 specify --sslDisabledProtocols 'none'
2020-01-09T11:25:45.987+0100 I CONTROL [initandlisten] MongoDB starting : pid=17727 port=27017 dbpath=/data/db 64-bit host=junior-LIFEBOOK-E752
2020-01-09T11:25:45.987+0100 I CONTROL [initandlisten] db version v4.2.2
2020-01-09T11:25:45.987+0100 I CONTROL [initandlisten] git version: a0bbbff6ada159e19298d37946ac8dc4b497eadf
2020-01-09T11:25:45.987+0100 I CONTROL [initandlisten] OpenSSL version: OpenSSL 1.1.1d 10 Sep 2019
2020-01-09T11:25:45.987+0100 I CONTROL [initandlisten] allocator: tcmalloc
2020-01-09T11:25:45.987+0100 I CONTROL [initandlisten] modules: none
2020-01-09T11:25:45.987+0100 I CONTROL [initandlisten] build environment:
2020-01-09T11:25:45.987+0100 I CONTROL [initandlisten] distmod: ubuntu1804
2020-01-09T11:25:45.987+0100 I CONTROL [initandlisten] distarch: x86_64
2020-01-09T11:25:45.987+0100 I CONTROL [initandlisten] target_arch: x86_64
2020-01-09T11:25:45.987+0100 I CONTROL [initandlisten] options: {}
2020-01-09T11:25:45.987+0100 E STORAGE [initandlisten] Failed to set up listener: SocketException: Address already in use
2020-01-09T11:25:45.987+0100 I CONTROL [initandlisten] now exiting
2020-01-09T11:25:45.987+0100 I CONTROL [initandlisten] shutting down with code:48
sudo mongo
connecting to: mongodb://127.0.0.1:27017/?compressors=disabled&gssapiServiceName=mongodb
Implicit session: session { "id" : UUID("8e813fc2-049c-440d-83ce-0b1d8516c4d0") }
MongoDB server version: 4.2.2
Server has startup warnings:
2020-01-09T11:21:43.352+0100 I STORAGE [initandlisten]
2020-01-09T11:21:43.352+0100 I STORAGE [initandlisten] ** WARNING: Using the XFS filesystem is strongly recommended with the WiredTiger storage engine
2020-01-09T11:21:43.352+0100 I STORAGE [initandlisten] ** See http://dochub.mongodb.org/core/prodnotes-filesystem
2020-01-09T11:21:44.848+0100 I CONTROL [initandlisten]
2020-01-09T11:21:44.848+0100 I CONTROL [initandlisten] ** WARNING: Access control is not enabled for the database.
2020-01-09T11:21:44.848+0100 I CONTROL [initandlisten] ** Read and write access to data and configuration is unrestricted.
2020-01-09T11:21:44.848+0100 I CONTROL [initandlisten]
Enable MongoDB's free cloud-based monitoring service, which will then receive and display
metrics about your deployment (disk utilization, CPU, operation statistics, etc).
The monitoring data will be available on a MongoDB website with a unique URL accessible to you
and anyone you share the URL with. MongoDB may use this information to make product
improvements and to suggest MongoDB products and deployment options to you.
To enable free monitoring, run the following command: db.enableFreeMonitoring()
To permanently disable this reminder, run the following command: db.disableFreeMonitoring()
So something weird is happening here, it doesn't find the database from the project and I have no clue why. As far as I can see, it creates a new database including my system details.
How can I fix this please and connect to the project database? I can't move further until I do.
it's been resolved. First I found out I didn't need to have the mongod service running locally because mongo was already included in the docker container. But actually this wasn't a database issue at all. There were a few things going wrong like there was a proxy but the command had been changed to execute the connection, endpoints were on localhost but weren't supposed to be, and the content I was expecting to see had been hidden by a careless push from another developer. Fun times! But we can move on now. Thanks to the people who tried to help!

How to start mongo v4.2.0 [duplicate]

This question already has an answer here:
Installing MongoDB on Ubuntu 16.04 [closed]
(1 answer)
Closed 3 years ago.
I have successfully installed Mongo v4.2.0.
mongo --version
MongoDB shell version v4.2.0
git version: a4b751dcf51dd249c5865812b390cfd1c0129c30
OpenSSL version: OpenSSL 1.1.1c 28 May 2019
allocator: tcmalloc
modules: none
build environment:
distmod: ubuntu1804
distarch: x86_64
target_arch: x86_64
But when i start mongo, it is showing error. Kindly help me out. I am using ubuntu 18.04.
mongo
MongoDB shell version v4.2.0
connecting to: mongodb://127.0.0.1:27017/?compressors=disabled&gssapiServiceName=mongodb
2019-10-07T11:35:13.915+0530 E QUERY [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 :: Connection refused :
connect#src/mongo/shell/mongo.js:341:17
#(connect):2:6
2019-10-07T11:35:13.916+0530 F - [main] exception: connect failed
2019-10-07T11:35:13.916+0530 E - [main] exiting with code 1
First start your mongodb instance
mongod --port <your-choice-of-port> --dbpath=/path-of-your-db
and then connect to it.

Unable to connect local mongod server from EC2, whereas able to connect from vice versa

Unable to connect my local MongoDB server from AWS EC2 instance, whereas I am able to connect vice versa.
From EC2 to Local Mongod server:
root#ip-172-31-31-13:~# mongo -u krishna -p Kmk123## 192.168.0.104/admin
MongoDB shell version v4.0.12
connecting to: mongodb://192.168.0.104:27017/admin?gssapiServiceName=mongodb
2019-08-26T16:48:14.271+0000 E QUERY [js] Error: couldn't connect to server 192.168.0.104:27017, connection attempt failed: SocketException: Error connecting to 192.168.0.104:27017 :: caused by :: Connection timed out :
connect#src/mongo/shell/mongo.js:344:17
#(connect):2:6
exception: connect failed
root#ip-172-31-31-13:~#
From Local to AWS EC2 :
[root#krishna ~]# mongo -u root -p root123 ec2-18-206-199-178.compute-1.amazonaws.com/admin
MongoDB shell version v4.0.12
connecting to: mongodb://ec2-18-206-199-178.compute-1.amazonaws.com:27017/admin?gssapiServiceName=mongodb
Implicit session: session { "id" : UUID("a6b35f6a-3e71-4ada-b10a-c0783743837b") }
MongoDB server version: 4.0.12
Server has startup warnings:
2019-08-26T16:36:21.897+0000 I STORAGE [initandlisten]
2019-08-26T16:36:21.897+0000 I STORAGE [initandlisten] ** WARNING: Using the XFS filesystem is strongly recommended with the WiredTiger storage engine
2019-08-26T16:36:21.897+0000 I STORAGE [initandlisten] ** See http://dochub.mongodb.org/core/prodnotes-filesystem
2019-08-26T16:36:22.912+0000 I CONTROL [initandlisten]
2019-08-26T16:36:22.912+0000 I CONTROL [initandlisten] ** WARNING: Access control is not enabled for the database.
2019-08-26T16:36:22.913+0000 I CONTROL [initandlisten] ** Read and write access to data and configuration is unrestricted.
2019-08-26T16:36:22.913+0000 I CONTROL [initandlisten] ** WARNING: You are running this process as the root user, which is not recommended.
2019-08-26T16:36:22.913+0000 I CONTROL [initandlisten]
2019-08-26T16:36:22.913+0000 I CONTROL [initandlisten]
2019-08-26T16:36:22.913+0000 I CONTROL [initandlisten] ** WARNING: /sys/kernel/mm/transparent_hugepage/enabled is 'always'.
2019-08-26T16:36:22.913+0000 I CONTROL [initandlisten] ** We suggest setting it to 'never'
2019-08-26T16:36:22.913+0000 I CONTROL [initandlisten]
2019-08-26T16:36:22.913+0000 I CONTROL [initandlisten] ** WARNING: /sys/kernel/mm/transparent_hugepage/defrag is 'always'.
2019-08-26T16:36:22.913+0000 I CONTROL [initandlisten] ** We suggest setting it to 'never'
2019-08-26T16:36:22.913+0000 I CONTROL [initandlisten]
Enable MongoDB's free cloud-based monitoring service, which will then receive and display
metrics about your deployment (disk utilization, CPU, operation statistics, etc).
The monitoring data will be available on a MongoDB website with a unique URL accessible to you
and anyone you share the URL with. MongoDB may use this information to make product
improvements and to suggest MongoDB products and deployment options to you.
To enable free monitoring, run the following command: db.enableFreeMonitoring()
To permanently disable this reminder, run the following command: db.disableFreeMonitoring()
>
>
I have edited SG of EC2 instace as below:
SSH TCP 22 0.0.0.0/0 Custom TCP Rule TCP 27017 0.0.0.0/0 Custom TCP
Rule TCP 27017::/0
You need to update mongod.conf file to allow your mongodb accessed from remote. It works for me when I do all the things mentioned in following page.
Ref: https://ianlondon.github.io/blog/mongodb-auth/

MongoDB failed to start on Widnows 7 SP1 Machine

Can't Start mongoDB on Windows 7 SP1 Machine..
2017-08-24T11:46:10.280+0300 E NETWORK [initandlisten] listen(): bind() failed
An operation was attempted on something that is not a socket. for socket: 0.0.
0.0:27017
2017-08-24T11:46:10.281+0300 E NETWORK [initandlisten] Failed to set up sockets
during startup.
2017-08-24T11:46:10.281+0300 E STORAGE [initandlisten] Failed to set up listene
r: InternalError: Failed to set up sockets
Any help is appreciated.
If you can run the following command to check whether the port 27017 is accessible or open for connections.
netstat -a -n | find "27017"
This will let you know if the MongoDB port is available for connection.
Alternately, you could open Task Manager and close all the open mongod processes.
Hope this helps!

How to restart/delete an instance of mongod?

This has been covered in other SO posts, but unfortunately those solutions are not working.
How does one restart/delete a mongod instance?
Entering
$ mongod
I get the following error:
2016-01-24T09:49:19.557-0500 I CONTROL [initandlisten] MongoDB starting : pid=43675 port=27017 dbpath=/data/db 64-bit host=NAME-MacBook-Pro.local
2016-01-24T09:49:19.558-0500 I CONTROL [initandlisten] db version v3.0.8
2016-01-24T09:49:19.558-0500 I CONTROL [initandlisten] git version: 83d8cc25e00e42856924d84e220fbe4a839e605d
2016-01-24T09:49:19.558-0500 I CONTROL [initandlisten] build info: Darwin NAME-MacBook-Pro.local 15.2.0 Darwin Kernel Version 15.2.0: Fri Nov 13 19:56:56 PST 2015; root:xnu-3248.20.55~2/RELEASE_X86_64 x86_64 BOOST_LIB_VERSION=1_49
2016-01-24T09:49:19.558-0500 I CONTROL [initandlisten] allocator: system
2016-01-24T09:49:19.558-0500 I CONTROL [initandlisten] options: {}
2016-01-24T09:49:19.558-0500 E NETWORK [initandlisten] listen(): bind() failed errno:48 Address already in use for socket: 0.0.0.0:27017
2016-01-24T09:49:19.558-0500 E NETWORK [initandlisten] addr already in use
2016-01-24T09:49:19.558-0500 I STORAGE [initandlisten] exception in initAndListen: 98 Unable to create/open lock file: /data/db/mongod.lock errno:13 Permission denied Is a mongod instance already running?, terminating
2016-01-24T09:49:19.558-0500 I CONTROL [initandlisten] dbexit: rc: 100
I explicitly gave myself permissions with
sudo chown -R <USERNAME> /data/db
This didn't work.
I tried to remove the directory entirely
sudo rm -rf /data/db
and recreate it
mkdir -p /data/db
but unfortunately this doesn't work.
I thought the solution would be to remove the file /data/db/mongod.lock but it doesn't exist:
rm /data/db/mongod.lock
Anything else I should try?
EDIT: After running with the different port using mongod --port 12345, when I use use admin and then db.shutdownServer(), I get the following error:
2016-01-24T10:32:25.039-0500 I NETWORK trying reconnect to 127.0.0.1:27017 (127.0.0.1) failed
2016-01-24T10:32:25.039-0500 I NETWORK reconnect 127.0.0.1:27017 (127.0.0.1) ok
2016-01-24T10:32:25.040-0500 I NETWORK DBClientCursor::init call() failed
server should be down...
EDIT 2:
Restarting the machine does nothing. I've tried to access the default port with mongod --port 27017, and then
> use admin
> db.shutdownServer()
I get the output:
2016-01-25T12:05:32.163-0500 I NETWORK DBClientCursor::init call() failed
server should be down...
2016-01-25T12:05:32.166-0500 I NETWORK trying reconnect to 127.0.0.1:27017 (127.0.0.1) failed
2016-01-25T12:05:32.166-0500 W NETWORK Failed to connect to 127.0.0.1:27017, reason: errno:61 Connection refused
2016-01-25T12:05:32.166-0500 I NETWORK reconnect 127.0.0.1:27017 (127.0.0.1) failed failed couldn't connect to server 127.0.0.1:27017 (127.0.0.1), connection attempt failed
In the log it say:
2016-01-24T09:49:19.558-0500 E NETWORK [initandlisten] listen(): bind() failed errno:48 Address already in use for socket: 0.0.0.0:27017
2016-01-24T09:49:19.558-0500 E NETWORK [initandlisten] addr already in use
It means that the port is already in use either by another process or another mongo instance.
You should use another port for this instance.
Refer to this link