Docker-Mongodb - How to connect to the mongo image in local(windows) - mongodb

I am trying to connect to my docker mongo image, I installed mongodb in local, and added the mongodb bin path to windows path environment variable. And I am doing below steps
cd <docker-location>
docker login
docker ps
-- to check already mongo running
docker pull myrepo/mymongo-image:1.0
docker run -p 27017:27017 -d --net=host --name=mytestDB myrepo/mymongo-image:1.0
docker logs mytestDB
Output: MongoDB starting : pid=6 port=27017 dbpath=/data/db 64-bit host=moby
docker ps
Output:
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
34598734598 myrepo/mymongo-image:1.0 "/bin/sh -c /usr/bin/" 12 hours ago Up About a minute mytestDB
start mongo in local:
mongo --port 27017
But I am getting error like this:
MongoDB shell version: 3.2.1
connecting to: 127.0.0.1:27017/test
2016-10-13T20:04:12.273+0530 W NETWORK [thread1] Failed to connect to 127.0.0.1:27017, reason: errno:10061 No connection could be made because the target machine actively refused it.
2016-10-13T20:04:12.277+0530 E QUERY [thread1] Error: couldn't connect to server 127.0.0.1:27017, connection attempt failed :
connect#src/mongo/shell/mongo.js:226:14
#(connect):1:6
exception: connect failed
Please let me know where I did mistake.

I got solution, I like to share it.
'--noauth --bind_ip=0.0.0.0' options are missed in my commands.
This is the blog bring me out.
Tutorial: Setting up MongoDB Image Instance with Docker Toolbox

if you have already installed docker-toolbox and you have created an image but unable to visualize your mongodb database through robo3t
for this illustration i created my image with command
docker run --name blogA -p 27017:27017 -d mongo
Note: since 27017 is port for mongodb
Open up VirtualBox. Right click the "Default" machine.
Select settings from the menu.
Choose the Network tab in the settings menu.
Choose port forwarding at the bottom of the network option.
Click the plus icon to the right of the port forwarding menu.
Make the new entry with the name mongo.
Protocol should be TCP. Host and Guest IP can be left empty.
Set the host port to the port you make use in creating your
image I made use of port 27017. Set the guest port to 27017.
install robo3t from http://robomongo.org/download.html
click icon below file tab to create a new connection
Choose to create a new connection.
Name your connection and set the address to localhost and port to the port you set your Host port too in VirtualBox, in my case is 27017

Related

i'm getting following error while connecting to mongodb server

$ mongo
MongoDB shell version v3.6.3
connecting to: mongodb://127.0.0.1:27017
2018-10-18T17:09:03.003+0530 W NETWORK [thread1] Failed to connect to 127.0.0.1:27017, in(checking socket for error after poll), reason: Connection refused
2018-10-18T17:09:03.004+0530 E QUERY [thread1] Error: couldn't connect to server 127.0.0.1:27017, connection attempt failed :
connect#src/mongo/shell/mongo.js:251:13
#(connect):1:6
exception: connect failed
what OS are you using? In ubuntu you need to check your version:
service mongod status, mongod --version.
If mongod is off then just restart and write the error if possible.
service mongod restart
if you are using linux, try this command
$ sudo mongod
you are getting that because you are running
$ mongo
First go to your mongoDB directory and in that directory go to bin directory and open terminal in that directory(bin), then run the following command:
mongod.exe
After that open another terminal in same directory and run the following command.
mongo.exe
It will resolve this issue.
first you check it mongodb proper installed or not and also check it version compatibility
I am saying windows os setup,
set environment variable is path C:\Program Files\MongoDB\Server\3.6\bin;
then open your command prompt and type
mongod
after open another command prompt and type
mongo
A connection refused failure to connect reason may just be a matter of your network configuration and firewall or more specifically, your operating system's port restrictions. While you may be connecting to a mongoDB server on localhost (127.0.0.1) which can't be blocked by a firewall, it is possible for a firewall to block access to specific ports on a localhost.
If you're on a linux-based system, this can be addressed by using iptables as described here with the applicable commands shown below:
iptables -A INPUT -s 127.0.0.1 -p tcp --destination-port 27017 -m state --state NEW,ESTABLISHED -j ACCEPT
iptables -A OUTPUT -d 127.0.0.1 -p tcp --source-port 27017 -m state --state ESTABLISHED -j ACCEPT
However, in reading the comments on another answer, it may be that you aren't able to connect because the server isn't being launched and ran properly due to a lack of of the /data/db directory. But, a presumption based on the lack of reply to other answers suggesting to check your server is running properly, tells me you're sure that it is. In which case, try changing your hostname to localhost instead of 127.0.0.1 when connecting, as in:
mongoose.connect('mongodb://localhost:27017');
# or as more applicable for connecting to a deployment:
mongosh "mongodb://localhost:27017"
# alternatively, by default, MongoDB Shell already connects to localhost
# so you could simply use the command line option --port such as:
mongosh --port 27017
You can also try specifying more parameters in the url as it applies.
For example, connecting to an instance and authenticate against the foobar database as user rohan:
mongosh "mongodb://localhost:27017" --username rohan --authenticationDatabase foobar
To provide a password with the command instead of using the masked prompt, simply add the --password command line option.
For Windows, you may also try the solutions offered on this page.

How do I access a remote aws lightsail mongodb over ssh tunnel

I have a Lightsail AWS instance up and running with a MEAN stack. I have an existing MEAN stack running on a different network. At the moment the node server.js connects to localhost for the mongo bit (on same machine) and all I want to do is replace the localhost with a connection to my mongo running on my AWS remote server.
I understand, that for security reasons, it is best to ssh tunnel this connection, which I think I am familiar with.
What I have done so far is this:
In a console on the machine hosting the node server (remote to the db) I have run:
ssh -L 8181:127.0.0.1:80 -i ~/LightsailDefaultPrivateKey-eu-west-2.pem bitnami#31.16.56.125 -N
I can then browse to the RockMongo UI from the local machine using localhost:8181/rockmongo ...yay.
If I then run the following:
ssh -L 8181:127.0.0.1:27017 -i ~/LightsailDefaultPrivateKey-eu-west-2.pem bitnami#31.16.56.125 -N
(27017 being the mongo port)
Then try and access the db from my remote machine using:
mongo --username XXXXXX --password XXXXXX 31.16.56.125:8181/testdata
I get the following error:
~]
2017-12-28T22:11:09.791+0000 Error: couldn't connect to server 31.16.56.125:8181 (31.16.56.125), connection attempt failed at src/mongo/shell/mongo.js:148
exception: connect failed
Am I doing this wrong? i.e. is the tunnel only for http connections and not mongo command line use? Do I need to test the connection some other way?
I've Googled all over the place for this and not had much luck (a lot of the AWS docs suggest punching a hole in the firewall - which one can no longer do!)
OK I've (partially) solved this, there were a few things wrong.
1) The mongo client was 2.6 and mongo running on AWS was 3.4. Upgrading this solved some issues - in that I was getting a more meaningful error message.
One thing I did have trouble with is that apt-get seemed to perform an update, yet the version reported when issuing the mongo command was still 2.6.
To solve this I had to run sudo apt-get purge mongodb-org* (note the asterisk). Then perform the update.... If you need to do this then follow these instructions:
https://docs.mongodb.com/v3.2/tutorial/install-mongodb-on-ubuntu/
2) This command
mongo --username XXXXXX --password XXXXXX 31.16.56.125:8181/testdata
won't work as I've omitted 'admin' from it and not specified localhost!
but
mongo admin --username XXXXXX --password XXXXXX localhost:8181/testdata
doesn't work either and gives the following output.
2018-01-03T22:00:42.380+0000 W NETWORK [thread1] Failed to connect to 127.0.0.1:27017, in(checking socket for error after poll), reason: errno:111 Connection refused
2018-01-03T22:00:42.380+0000 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
The only command I could get to work is:
mongo admin --username XXXXXX --password XXXXXX --port 8181
The default host is localhost, so in this case it uses the tunnel, this will also just connect to the test db, you can then admin from there.
What I haven't got to the bottom of is the specification of the host:port/db as an argument as per the output from running mongo --help
usage: mongo [options] [db address] [file names (ending in .js)]
db address can be:
foo foo database on local machine
192.169.0.5/foo foo database on 192.168.0.5 machine
192.169.0.5:9999/foo foo database on 192.168.0.5 machine on port 9999

connect robomongo to mongoDB docker container

I'm running a NodeJS App with docker-compose. Everything works fine and I can see all my data by connecting to Mongo inside container. But when I connect to RoboMongo I don't see any data.
How can I deal with this problem?
There is another way. You can
SSH with Robomongo into your actual virtual server that hosts your docker applications (SSH tab, check "Use SSH tunnel" and complete the other fields accordingly)
Now ssh into the same machine in your terminal.
docker ps should show you your MongoDB container.
docker inspect <mongo container id> will print out complete information about that container. Look for IPAddress in the end, that will give you the local IP of the container.
In the "Connection" tab in Robomongo use that container IP to connect.
Another sidenote: Make sure that you don't expose your mongodb service ports in any way (neither Dockerfile nor docker-compose.yml), cause that will make your database openly accessible from everywhere. Assuming that you don't have set up a username / password for that service you will be scanned and hacked soon.
The easiest way is to enable forwarding the Mongo Container itself, here's how my docker-compose looks like.
mongo:
image: mongo
restart: always
ports:
- 27017:27017
You should do a Robomongo SSH tunnel connection to MongoDB inside docker container. First of all you should install a ssh server inside your docker container.
https://docs.docker.com/engine/examples/running_ssh_service/
After that you should configure your connection in Robomongo.
Inside "Connection Settings" there are configuration tabs of your Robomongo Connection.
Go to "SSH" Tab and configure your SSH connection to the docker container. After that go to "Connection" Tab and configure your connection to MongoDB as if it was in localhost scope.
I was facing a different problem. I had installed MongoDB locally. So, when the MongoDB on docker was running, it was clashing with the one running on my host. I had installed it using brew.
So, I ran
brew services stop mongodb-community
and then I restarted Robo3t. I saw the databases created in the MongoDB running on the docker.
Voila!
Please note that maybe you won't be able to use ssh because it was just a problem of incompatibility between mongo and robomongo.
'Robomongo v8.5 and lower doesn't support MongoDB 3'. It has nothing to do with docker.
First log in with ssh Login details
ssh -i yourpemfile.pem username#ipaddress
Check running container id for MongoDB
docker ps -a
then check the mongo container id
docker inspect container_id
Then open robo3t
create new connection and add container id
use ssh login details to connect to mongodb
In your docker-compose file, you can expose a port to the host.
For example, the following code will expose port 27017 inside the machine to the port 27018 in the host.
app:
image: node
volumes:
- /app
ports:
- "27018:27017"
Then, if you have docker-machine installed and your machine is default, you can do in a terminal :
docker-machine ip default
It will give you the ip of your host, for example 192.168.2.3. The address of your database (host) will be 192.168.2.3 and the port 27018.
If your docker machine is not virtual and is your OS, the address of your database will be localhost and the port 27018.

Can not connect to Mongo docker instance via mono client on mac

I have installed Mongo docker image and run it using those commands (mac boot2docker is installed)
docker pull mongo
and
docker run --name some-mongo -d mongo
but now I want to connect to it via mongo client running:
mongo --port 27017 --host 127.0.0.1
but I get this error message:
MongoDB shell version: 3.0.4
connecting to: 127.0.0.1:27017/test
2015-07-27T14:22:24.088+0300 W NETWORK Failed to connect to 127.0.0.1:27017, reason: errno:61 Connection refused
2015-07-27T14:22:24.094+0300 E QUERY Error: couldn't connect to server 127.0.0.1:27017 (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
It is clear to me that Docker fails to expose the ports since telnet to 27017 on the localhost fails as well.
What the hack am I doing wrong?
You have 2 problems :
Like h3nrik said you should connect to the boot2docker VMs address. If you don't know it use the following command :
boot2docker ip
And your port isn't open in the first place.
Your Docker run command should look like this :
docker run -p 27017:27017 --name some-mongo -d mongo
Instead of 127.0.0.1 you should use the boot2docker VMs IP address. Usually 192.168.59.103. You can verify to which IP you should connect executing boot2docker ip.
Update: I discovered that you do not export any ports by your containers run statement:
docker run --name some-mongo -d mongo
Without any ports exposed you cannot connect, of course. Try to re-connect after running (depending on your requirements you can add more ports according to the mongodb documentation):
docker run --name some-mongo -d -p 27017:27017 mongo

Unable to connect to mongoDB running in docker container

Following this example: https://docs.docker.com/engine/examples/mongodb/
When trying to connect to mongoDB with: mongo ip:27017
(where ip is the name from boot2docker ip) + the port number from docker ps:
27017/tcp
or with -P
0.0.0.0:49155->27017/tcp
Either way I get the following errors:
warning: Failed to connect to ip:27017, reason: errno:61 Connection
refused
Error: couldn't connect to server ip:27017 (ip), connection attempt
failed at src/mongo/shell/mongo.js:148 exception: connect failed
If you specified the correct port and still not able to connect to mongodb running in docker (like me), make sure you are using the service name (or container name) in your connection URL, e.g. mongodb://mongodb_service:27017/mydb, which is defined in your docker-compose.yml :
services:
mongodb_service:
image: mongo
I was using the hostname value and that's not the correct thing to do. You could verify this by looking at docker inspect mongodb_service in the Aliases section.
I was using port 27017 instead of 49155 (doh, port forwarding)
0.0.0.0:49155->27017/tcp
Thanks to ZeissS
If you are on a Mac and using Docker Machine, do the following:
1. Get the name of the VM running docker daemon
$ docker-machine ls
2. Get the VM's IP info
$ docker-machine env
3. Connect with the mongo client to the VM IP and the mongo mapped port
$ mongo VM-IP:port
Assuming your mongodb is within a container, for other containers to connect to it, they all need to be on the same network.
To have mongodb and other containers (that want to connect it), create a new network using below command
docker network create --driver bridge my_bridge
Then run mongodb and other containers using the --net flag
docker run --net=my_bridge --name mongodb -p 27017:27017 mongodb
docker run --net=my_bridge --name my-service -p 7002:7002 my-service
Now you should be able to connect mongodb with given alias name from those containers
mongo --host "mongodb:27017"
DATABASE_URI=mongodb://mongo:27017/db_name
Should be the Database URI for a service definition like below (and not mongodb://localhost
or mongodb://IP). Use service name or container name.
services
mongo:
container_name: mongo
image: mongo
ports:
- '27017:27017'