Mongo command error SocketException: Error connecting to 127.0.0.1:27017 while attempting to connecto to Vagrant db

I have followed the video instructions and cannot connect the the mongod running in vagrant. I can see that the mongod is running but cannot connect to it from the second terminal, and i get the following error:

MongoDB shell version v4.0.5
connecting to: mongodb://127.0.0.1:27017/?gssapiServiceName=mongodb
2019-02-17T09:58:02.705+0100 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:328:13
@(connect):1:6
exception: connect failed

I am using Ubuntu 16.04, could it be that since I have the SSH session with vagrant open on IP 127.0.0.1 and the OS blocks additional connections on that port?

the mongod version in vagrant is 3.6 while the one I have installed on my OS is 4.05, I think this shouldn’t be a problem right?

Has anyone had a similar problem and figured out how to solve it? I have tried many recommendations i found online but none worked (commenting out the bind and port in mongo.conf doesnt work for example).
thanks!

You are connecting to port 27017. But if you are following course instructions then your mongod is running on port 27000.
Please check port number of your mongod instance

1 Like

Well, yeah, if you were connecting to port 22 with MongoD then sure :slight_smile: But you’re not.

As @Mahesh_56388 points out: double-check the port that your MongoD is running on. That’s the one that you need to connect to.

Also, check the IP that the MongoD is listening on. Perhaps it’s not listening on 127.0.0.1, but only on 192.168.103.100.

Hi, thanks both for replying, i forgot to mention that I already checked the port that mongo is listening on and it is indeed 27017 (I also tried 27000 to be on the safe side) but as you can see from the screen dump below it says 27017 (the default one). I followed the course instructions for this and used the handouts.
when I run mongod in the vargant ssh I see 2 warnings, one regarding access control that is not enabled which I assume is ok since it will not check for user/pwd combination, and teh second is regarding the binding to locahost, which is the address I am using:
vagrant@m103:~$ mongod
2019-02-17T20:32:20.666+0000 I CONTROL [initandlisten] MongoDB starting : pid=1914 port=27017 dbpath=/data/db 64-bit host=m103
2019-02-17T20:32:20.666+0000 I CONTROL [initandlisten] db version v3.6.10
2019-02-17T20:32:20.666+0000 I CONTROL [initandlisten] git version: 3e3ab85bfb98875af3bc6e74eeb945b0719f69c8
2019-02-17T20:32:20.666+0000 I CONTROL [initandlisten] OpenSSL version: OpenSSL 1.0.1f 6 Jan 2014
2019-02-17T20:32:20.666+0000 I CONTROL [initandlisten] allocator: tcmalloc
2019-02-17T20:32:20.666+0000 I CONTROL [initandlisten] modules: enterprise
2019-02-17T20:32:20.666+0000 I CONTROL [initandlisten] build environment:
2019-02-17T20:32:20.666+0000 I CONTROL [initandlisten] distmod: ubuntu1404
2019-02-17T20:32:20.666+0000 I CONTROL [initandlisten] distarch: x86_64
2019-02-17T20:32:20.666+0000 I CONTROL [initandlisten] target_arch: x86_64
2019-02-17T20:32:20.666+0000 I CONTROL [initandlisten] options: {}
2019-02-17T20:32:20.667+0000 I - [initandlisten] Detected data files in /data/db created by the ‘wiredTiger’ storage engine, so setting the active storage engine to ‘wiredTiger’.
2019-02-17T20:32:20.668+0000 I STORAGE [initandlisten]
2019-02-17T20:32:20.668+0000 I STORAGE [initandlisten] ** WARNING: Using the XFS filesystem is strongly recommended with the WiredTiger storage engine
2019-02-17T20:32:20.668+0000 I STORAGE [initandlisten] ** See http://dochub.mongodb.org/core/prodnotes-filesystem
2019-02-17T20:32:20.668+0000 I STORAGE [initandlisten] wiredtiger_open config: create,cache_size=488M,session_max=20000,eviction=(threads_min=4,threads_max=4),config_base=false,statistics=(fast),compatibility=(release=“3.0”,require_max=“3.0”),log=(enabled=true,archive=true,path=journal,compressor=snappy),file_manager=(close_idle_time=100000),statistics_log=(wait=0),verbose=(recovery_progress),
2019-02-17T20:32:21.421+0000 I STORAGE [initandlisten] WiredTiger message [1550435541:421672][1914:0x7f59dc95fa80], txn-recover: Main recovery loop: starting at 9/6784
2019-02-17T20:32:21.508+0000 I STORAGE [initandlisten] WiredTiger message [1550435541:508366][1914:0x7f59dc95fa80], txn-recover: Recovering log 9 through 10
2019-02-17T20:32:21.563+0000 I STORAGE [initandlisten] WiredTiger message [1550435541:563192][1914:0x7f59dc95fa80], txn-recover: Recovering log 10 through 10
2019-02-17T20:32:21.606+0000 I STORAGE [initandlisten] WiredTiger message [1550435541:606367][1914:0x7f59dc95fa80], txn-recover: Set global recovery timestamp: 0
2019-02-17T20:32:21.622+0000 I CONTROL [initandlisten]
2019-02-17T20:32:21.622+0000 I CONTROL [initandlisten] ** WARNING: Access control is not enabled for the database.
2019-02-17T20:32:21.622+0000 I CONTROL [initandlisten] ** Read and write access to data and configuration is unrestricted.
2019-02-17T20:32:21.623+0000 I CONTROL [initandlisten]
2019-02-17T20:32:21.623+0000 I CONTROL [initandlisten] ** WARNING: This server is bound to localhost.
2019-02-17T20:32:21.623+0000 I CONTROL [initandlisten] ** Remote systems will be unable to connect to this server.
2019-02-17T20:32:21.624+0000 I CONTROL [initandlisten] ** Start the server with --bind_ip to specify which IP
2019-02-17T20:32:21.624+0000 I CONTROL [initandlisten] ** addresses it should serve responses from, or with --bind_ip_all to
2019-02-17T20:32:21.625+0000 I CONTROL [initandlisten] ** bind to all interfaces. If this behavior is desired, start the
2019-02-17T20:32:21.625+0000 I CONTROL [initandlisten] ** server with --bind_ip 127.0.0.1 to disable this warning.
2019-02-17T20:32:21.625+0000 I CONTROL [initandlisten]
2019-02-17T20:32:21.626+0000 I CONTROL [initandlisten]
2019-02-17T20:32:21.626+0000 I CONTROL [initandlisten] ** WARNING: /sys/kernel/mm/transparent_hugepage/enabled is ‘always’.
2019-02-17T20:32:21.626+0000 I CONTROL [initandlisten] ** We suggest setting it to ‘never’
2019-02-17T20:32:21.627+0000 I CONTROL [initandlisten]
2019-02-17T20:32:21.627+0000 I CONTROL [initandlisten] ** WARNING: /sys/kernel/mm/transparent_hugepage/defrag is ‘always’.
2019-02-17T20:32:21.628+0000 I CONTROL [initandlisten] ** We suggest setting it to ‘never’
2019-02-17T20:32:21.628+0000 I CONTROL [initandlisten]
2019-02-17T20:32:21.644+0000 I FTDC [initandlisten] Initializing full-time diagnostic data capture with directory ‘/data/db/diagnostic.data’
2019-02-17T20:32:21.645+0000 I NETWORK [initandlisten] waiting for connections on port 27017

this doesnt happen if I run Mongo directly without vagrant, which is why i think it has to do with the virtual machine setup or vagrant setup.

Thanks again for your help,

It looks like you are starting mongod without any parameters. I don’t recall any labs asking for that. Can you indicate us what lab or exercise you are trying to accomplish? From what I see mongod is starting correctly and simply awaits connections from mongo shell or other applications.

1 Like

+1 with steevej-1495 : you should follow instructions of the lab, and run your mongod with specific parameters.

1 Like

Hi steevej-1495, i am actually following the instructions of Chapter 1 lesson 1:

Blockquote

Chapter 1: The Mongod

The Mongod

Lecture Notes

You can read more about the MongoDB server in the daemon documentation.

Note: The --fork option is not available on the Windows operating system.

Lecture Instructions

Connect to the vagrant box:

cd m103-vagrant-env vagrant ssh

Launch mongod in our first shell:

mongod

Connect to the Mongo shell in our second shell:

mongo

Blockquote

–>This is where i get stuck, i cannot connect using the mongo shell, I can successfully start mongod in one terminal, but cannot connect to it via the shell from a second terminal because the connection is refused. I used the course handouts to setup vagrant + virtual box as explained.

I will try the first lab instructions to see if it works, I just assumed that it should be working at this step before creating the users, because when I test outside of vagrant, it does indeed work. Thanks everyone for the replies.

On the second windows you aldo have to go to the vagrant vm with vagrant ssh.

3 Likes

Hi Everyone, I have an update, I followed the lab instructions and started mongod with --bind_ip set to 192.168.103.100 and localhost as the lab requires, and I managed to connect via mongo only to 192.168.103.100, but not with localhost. I will continue to investigate why this is happening but for the meantime thanks everyone for your answers and support

1 Like

Hi, George - One possibly silly question - when you are specifying your --bind_ip argument, are you giving it BOTH localhost AND 192.168.103.100? If not, your mongod will only listen on the one ip you gave it.

Try ssh’ing into your vagrant box, then:
mongod --port 27000 --dbpath /data/db/ --bind_ip “localhost,192.168.103.100” --auth --logpath /data/db/log.log --fork

Which should start a background daemon for you. Then, you can use the same terminal window if you want, (prompt in either terminal window should be: vagrant@m103:~$ ) and create your admin user by copying the text from the lab.
Once that is done, you can authenticate using:
mongo --port 27000 -u “m103-admin” -p “m103-pass” --authenticationDatabase “admin”

3 Likes