Unable to connect to mongo on remote server - mongodb

I have installed mongo on machine1(Ubuntu 14.04.3 LTS server) in my local network. I have also opened the port 27017 as mentioned in this guide using following commands:
sudo iptables -A INPUT -p tcp --destination-port 27017 -m state --state NEW,ESTABLISHED -j ACCEPT
sudo iptables -A OUTPUT -p tcp --source-port 27017 -m state --state ESTABLISHED -j ACCEPT
Current rules are (iptables -L):
Chain INPUT (policy ACCEPT)
target prot opt source destination
ACCEPT tcp -- anywhere anywhere tcp dpt:27017 state NEW,ESTABLISHED
Chain FORWARD (policy ACCEPT)
target prot opt source destination
Chain OUTPUT (policy ACCEPT)
target prot opt source destination
ACCEPT tcp -- anywhere anywhere tcp spt:27017 state ESTABLISHED
But I am not able to connect to this port from machine2 (in the same network):
$ mongo --host 192.168.0.108
MongoDB shell version: 3.0.4
connecting to: 192.168.0.108:27017/test
2016-01-23T18:02:14.848+0530 W NETWORK Failed to connect to 192.168.0.108:27017, reason: errno:61 Connection refused
2016-01-23T18:02:14.850+0530 E QUERY Error: couldn't connect to server 192.168.0.108:27017 (192.168.0.108), 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
I also tried checking if port is opened or not:
$ nc -v 192.168.0.108 27017
nc: connectx to 192.168.0.108 port 27017 (tcp) failed: Connection refused
I am not sure what I am missing. Must be some silly mistake as I am setting up mongo for the first time. Pls help.
Update (for the doubts raised in current answers)
Yeah its running. I am able to connect to mongo form the same machine but not from the other machine. The daemon is running: $ service mongod status mongod start/running, process 31205 and the port is 27017 $ sudo netstat -tulpn |grep 27017 tcp 0 0 127.0.0.1:27017 0.0.0.0:* LISTEN 31205/mongod
I changed the bind_ip to 0.0.0.0 and restarted mongo. Still the same error is coming.
Mongo logs:
$ cat /var/log/mongodb/mongod.log
2016-01-23T16:28:13.155+0530 I CONTROL [initandlisten] MongoDB starting : pid=31205 port=27017 dbpath=/var/lib/mongodb 64-bit host=dexter
2016-01-23T16:28:13.156+0530 I CONTROL [initandlisten] db version v3.2.1
2016-01-23T16:28:13.156+0530 I CONTROL [initandlisten] git version: a14d55980c2cdc565d4704a7e3ad37e4e535c1b2
2016-01-23T16:28:13.156+0530 I CONTROL [initandlisten] OpenSSL version: OpenSSL 1.0.1f 6 Jan 2014
2016-01-23T16:28:13.156+0530 I CONTROL [initandlisten] allocator: tcmalloc
2016-01-23T16:28:13.156+0530 I CONTROL [initandlisten] modules: none
2016-01-23T16:28:13.156+0530 I CONTROL [initandlisten] build environment:
2016-01-23T16:28:13.156+0530 I CONTROL [initandlisten] distmod: ubuntu1404
2016-01-23T16:28:13.156+0530 I CONTROL [initandlisten] distarch: x86_64
2016-01-23T16:28:13.156+0530 I CONTROL [initandlisten] target_arch: x86_64
2016-01-23T16:28:13.156+0530 I CONTROL [initandlisten] options: { config: "/etc/mongod.conf", net: { bindIp: "127.0.0.1", port: 27017 }, storage: { dbPath: "/var/lib/mongodb", journal: { enabled: true } }, systemLog: { destination: "file", logAppend: true, path: "/var/log/mongodb/mongod.log" } }
2016-01-23T16:28:13.173+0530 I STORAGE [initandlisten] wiredtiger_open config: create,cache_size=1G,session_max=20000,eviction=(threads_max=4),config_base=false,statistics=(fast),log=(enabled=true,archive=true,path=journal,compressor=snappy),file_manager=(close_idle_time=100000),checkpoint=(wait=60,log_size=2GB),statistics_log=(wait=0),
2016-01-23T16:28:14.444+0530 I CONTROL [initandlisten]
2016-01-23T16:28:14.444+0530 I CONTROL [initandlisten] ** WARNING: /sys/kernel/mm/transparent_hugepage/enabled is 'always'.
2016-01-23T16:28:14.444+0530 I CONTROL [initandlisten] ** We suggest setting it to 'never'
2016-01-23T16:28:14.444+0530 I CONTROL [initandlisten]
2016-01-23T16:28:14.444+0530 I CONTROL [initandlisten] ** WARNING: /sys/kernel/mm/transparent_hugepage/defrag is 'always'.
2016-01-23T16:28:14.444+0530 I CONTROL [initandlisten] ** We suggest setting it to 'never'
2016-01-23T16:28:14.444+0530 I CONTROL [initandlisten]
2016-01-23T16:28:14.444+0530 I FTDC [initandlisten] Initializing full-time diagnostic data capture with directory '/var/lib/mongodb/diagnostic.data'
2016-01-23T16:28:14.444+0530 I NETWORK [HostnameCanonicalizationWorker] Starting hostname canonicalization worker
2016-01-23T16:28:14.949+0530 I NETWORK [initandlisten] waiting for connections on port 27017
2016-01-23T16:38:37.046+0530 I NETWORK [initandlisten] connection accepted from 127.0.0.1:37032 #1 (1 connection now open)
2016-01-23T16:39:31.447+0530 I NETWORK [conn1] end connection 127.0.0.1:37032 (0 connections now open)
2016-01-23T16:49:24.240+0530 I NETWORK [initandlisten] connection accepted from 127.0.0.1:37033 #2 (1 connection now open)
2016-01-23T16:49:38.249+0530 I NETWORK [conn2] end connection 127.0.0.1:37033 (0 connections now open)
2016-01-23T16:51:51.707+0530 I NETWORK [initandlisten] connection accepted from 127.0.0.1:37034 #3 (1 connection now open)
2016-01-23T16:51:55.785+0530 I NETWORK [conn3] end connection 127.0.0.1:37034 (0 connections now open)
2016-01-23T17:32:15.546+0530 I NETWORK [initandlisten] connection accepted from 127.0.0.1:37036 #4 (1 connection now open)
2016-01-23T17:32:21.180+0530 I NETWORK [conn4] end connection 127.0.0.1:37036 (0 connections now open)
2016-01-23T18:11:57.885+0530 I NETWORK [initandlisten] connection accepted from 127.0.0.1:37037 #5 (1 connection now open)
2016-01-23T18:29:55.365+0530 I NETWORK [conn5] end connection 127.0.0.1:37037 (0 connections now open)

make sure that mongodb daemon is running, and listening on 0.0.0.0, but not 127.0.0.1 port
for example, for my local mongodb, it has this config:
[vodolaz095#steel ~]$ cat /etc/mongod.conf
##
### Basic Defaults
##
# Comma separated list of ip addresses to listen on (all local ips by default)
bind_ip = 127.0.0.1
# Specify port number (27017 by default)
#port = 27017
for mongodb server to listen for remote connections, you can change
# Comma separated list of ip addresses to listen on (all local ips by default)
bind_ip = 0.0.0.0

With mongoDB server version 3.6.4, Ubuntu 16.4.4 I solved this by setting the net section in /etc/mongod.conf like this:
net:
port: 27017
bindIpAll: true
# bindIp: 127.0.0.1

The issue was bindIp didn't change. There was some issue in restarting mongo from my side.
The habit should be to verify if the bindIp actually changed or not. (using sudo netstat -tulpn | grep 27017)

I have tried a different way to solve this issue. I have changed the config file from
bind_ip = 127.0.0.1
#port = 27017
to
bind_ip = 0.0.0.0
#port = 27017
Hope it helps. Happy coding ;)

Make sure you have a daemon running on your machine1 which listen to port 27017. We were getting the same error and found that daemon was not running.

Related

Mongo Shell from local is not getting connected to mongo server running in docker

I ran mongo server in docker and the logs came up just fine.
2019-04-12T10:39:51.334+0000 I CONTROL [initandlisten] MongoDB starting : pid=1 port=27017 dbpath=/data/db 64-bit host=8a03346e57d7
2019-04-12T10:39:51.335+0000 I CONTROL [initandlisten] db version v3.2.22
2019-04-12T10:39:51.335+0000 I CONTROL [initandlisten] git version: 105acca0d443f9a47c1a5bd608fd7133840a58dd
2019-04-12T10:39:51.335+0000 I CONTROL [initandlisten] OpenSSL version: OpenSSL 1.0.1e-fips 11 Feb 2013
2019-04-12T10:39:51.335+0000 I CONTROL [initandlisten] allocator: tcmalloc
2019-04-12T10:39:51.335+0000 I CONTROL [initandlisten] modules: none
2019-04-12T10:39:51.335+0000 I CONTROL [initandlisten] build environment:
2019-04-12T10:39:51.335+0000 I CONTROL [initandlisten] distmod: rhel70
2019-04-12T10:39:51.335+0000 I CONTROL [initandlisten] distarch: x86_64
2019-04-12T10:39:51.335+0000 I CONTROL [initandlisten] target_arch: x86_64
2019-04-12T10:39:51.335+0000 I CONTROL [initandlisten] options: { net: { port: 27017 }, processManagement: { pidFilePath: "/var/run/mongodb/mongod.pid" }, storage: { dbPath: "/data/db" } }
2019-04-12T10:39:51.339+0000 I STORAGE [initandlisten] wiredtiger_open config: create,cache_size=1G,session_max=20000,eviction=(threads_min=4,threads_max=4),config_base=false,statistics=(fast),log=(enabled=true,archive=true,path=journal,compressor=snappy),file_manager=(close_idle_time=100000),checkpoint=(wait=60,log_size=2GB),statistics_log=(wait=0),verbose=(recovery_progress),
2019-04-12T10:39:51.389+0000 I CONTROL [initandlisten] ** WARNING: You are running this process as the root user, which is not recommended.
2019-04-12T10:39:51.389+0000 I CONTROL [initandlisten]
2019-04-12T10:39:51.406+0000 I NETWORK [HostnameCanonicalizationWorker] Starting hostname canonicalization worker
2019-04-12T10:39:51.406+0000 I FTDC [initandlisten] Initializing full-time diagnostic data capture with directory '/data/db/diagnostic.data'
2019-04-12T10:39:51.406+0000 I NETWORK [initandlisten] waiting for connections on port 27017
But, while connecting the mongodb server from local shell, the connection is getting timedout.
~/mongodb/bin/mongo --host 172.17.0.2
MongoDB shell version v3.6.11
connecting to: mongodb://172.17.0.2:27017/?gssapiServiceName=mongodb
2019-04-12T16:04:45.750+0530 W NETWORK [thread1] Failed to connect to 172.17.0.2:27017 after 5000ms milliseconds, giving up.
2019-04-12T16:04:45.753+0530 E QUERY [thread1] Error: couldn't connect to server 172.17.0.2:27017, connection attempt failed :
connect#src/mongo/shell/mongo.js:263:13
#(connect):1:6
exception: connect failed
where 172.17.0.2 is the IP obtained by running docker inspect <container-id
I was able to connect mongo shell with in the docker using the command docker exec --it <container-id> bash.
Here's the Docker file for refrence.
RUN echo -e "\
[mongodb]\n\
name=MongoDB Repository\n\
baseurl=https://repo.mongodb.org/yum/redhat/7Server/mongodb-org/3.2/x86_64/\n\
gpgcheck=0\n\
enabled=1\n" >> /etc/yum.repos.d/mongodb.repo
# Install mongodb
RUN yum update -y && yum install -y mongodb-org
# Set up directory requirements
RUN mkdir -p /data/db /var/log/mongodb /var/run/mongodb
VOLUME ["/data/db", "/var/log/mongodb"]
# Expose port 27017 from the container to the host
EXPOSE 27017
# Start mongodb
ENTRYPOINT ["/usr/bin/mongod"]
CMD ["--port", "27017", "--dbpath", "/data/db", "--pidfilepath", "/var/run/mongodb/mongod.pid"]
Let me know if I am missing something.
Using MacOS

I am trying to start mongoDB server using the command mongod. However I get the following error

This is the error that I get on issuing mongod in the terminal
2017-06-25T14:53:50.591+0530 I CONTROL [initandlisten] MongoDB
starting : pid=7402 port=27017 dbpath=/data/db 64-bit
host=bhagyashree-MS-7994
2017-06-25T14:53:50.591+0530 I CONTROL [initandlisten] db version
v3.2.14
2017-06-25T14:53:50.591+0530 I CONTROL [initandlisten] git version:
92f6668a768ebf294bd4f494c50f48459198e6a3
2017-06-25T14:53:50.591+0530 I CONTROL [initandlisten] OpenSSL
version: OpenSSL 1.0.2g 1 Mar 2016
2017-06-25T14:53:50.591+0530 I CONTROL [initandlisten] allocator:
tcmalloc
2017-06-25T14:53:50.591+0530 I CONTROL [initandlisten] modules: none
2017-06-25T14:53:50.591+0530 I CONTROL [initandlisten] build
environment:
2017-06-25T14:53:50.591+0530 I CONTROL [initandlisten] distmod:
ubuntu1604
2017-06-25T14:53:50.591+0530 I CONTROL [initandlisten] distarch:
x86_64
2017-06-25T14:53:50.591+0530 I CONTROL [initandlisten]
target_arch: x86_64
2017-06-25T14:53:50.591+0530 I CONTROL [initandlisten] options: {}
2017-06-25T14:53:50.609+0530 E NETWORK [initandlisten] listen():
bind() failed errno:98 Address already in use for socket:
0.0.0.0:27017
2017-06-25T14:53:50.609+0530 E NETWORK [initandlisten] addr already
in use
2017-06-25T14:53:50.609+0530 E STORAGE [initandlisten] Failed to set
up sockets during startup.
2017-06-25T14:53:50.609+0530 I CONTROL [initandlisten] dbexit: rc:
48
And on issuing a sudo netstat -plnt I get the following output,
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address Foreign Address
State PID/Program name
tcp 0 0 127.0.1.1:53 0.0.0.0:*
LISTEN 1920/dnsmasq
tcp 0 0 127.0.0.1:6942 0.0.0.0:*
LISTEN 3725/java
tcp 0 0 127.0.0.1:27017 0.0.0.0:*
LISTEN 7272/mongod
tcp 0 0 127.0.0.1:63342 0.0.0.0:*
LISTEN
3725/java
Thank you in advance!! I appreciate all the help!!
The error clearly tells the problem. You are trying to start a process that is already running or assign it to a port which is occupied. There are few ways to solve this.
1. Stop the service and start it again
sudo service mongod stop
sudo mongod
2. Quick fix ( may not be the best according to their documentations)
killall mongod
3. Deal with the process
Fins out which process is running the mongodb port
sudo netstat -tulpn | grep :27017
Next, kill the process that is using it by the command
sudo kill PID
(PID - Process id)
Now, you can start it again. It should be up and running!

MongoDB Self-signed SSL connection: SSL peer certificate validation failed

I have followed this guide Self-signed SSL connection using PyMongo, by Wan Bachtiar to create three .pem files; server.pem, client.pem and ca.pem.
I am using Ubuntu 16.04 and MongoDB v3.2.11.
The purpose is to secure the MongoDB before opening it to the public internet.
lets start the mongod:
$ mongod --auth --port 27017 --dbpath /data/db1
--sslMode requireSSL --sslPEMKeyFile /etc/ssl/server.pem
--sslCAFile /etc/ssl/ca.pem --sslAllowInvalidHostnames &
Output:
root#tim:/etc/ssl# 2017-01-13T12:58:55.150+0000 I CONTROL [initandlisten] MongoDB starting : pid=19058 port=27017 dbpath=/data/db1 64-bit host=tim
2017-01-13T12:58:55.150+0000 I CONTROL [initandlisten] db version v3.2.11
2017-01-13T12:58:55.151+0000 I CONTROL [initandlisten] git version: 009580ad490190ba33d1c6253ebd8d91808923e4
2017-01-13T12:58:55.151+0000 I CONTROL [initandlisten] OpenSSL version: OpenSSL 1.0.2g 1 Mar 2016
2017-01-13T12:58:55.152+0000 I CONTROL [initandlisten] allocator: tcmalloc
2017-01-13T12:58:55.152+0000 I CONTROL [initandlisten] modules: none
2017-01-13T12:58:55.152+0000 I CONTROL [initandlisten] build environment:
2017-01-13T12:58:55.152+0000 I CONTROL [initandlisten] distmod: ubuntu1604
2017-01-13T12:58:55.152+0000 I CONTROL [initandlisten] distarch: x86_64
2017-01-13T12:58:55.152+0000 I CONTROL [initandlisten] target_arch: x86_64
2017-01-13T12:58:55.153+0000 I CONTROL [initandlisten] options: { net: { port: 27017, ssl: { CAFile: "/etc/ssl/ca.pem", PEMKeyFile: "/etc/ssl/server.pem", allowInvalidHostnames: true, mode: "requireSSL" }
}, security: { authorization: "enabled" }, storage: { dbPath: "/data/db1" } }
2017-01-13T12:58:55.211+0000 I - [initandlisten] Detected data files in /data/db1 created by the 'wiredTiger' storage engine, so setting the active storage engine to 'wiredTiger'.
2017-01-13T12:58:55.212+0000 W - [initandlisten] Detected unclean shutdown - /data/db1/mongod.lock is not empty.
2017-01-13T12:58:55.212+0000 W STORAGE [initandlisten] Recovering data from the last clean checkpoint.
2017-01-13T12:58:55.212+0000 I STORAGE [initandlisten] wiredtiger_open config: create,cache_size=1G,session_max=20000,eviction=(threads_max=4)
,config_base=false,statistics=(fast),log=(enabled=true,archive=true,path=journal,compressor=snappy),file_manager=(close_idle_time=100000),checkpoint=(wait=60,log_size=2GB),statistics_log=(wait=0),
2017-01-13T12:58:55.886+0000 I CONTROL [initandlisten] ** WARNING: You are running this process as the root user, which is not recommended.
2017-01-13T12:58:55.886+0000 I CONTROL [initandlisten]
2017-01-13T12:58:55.895+0000 I FTDC [initandlisten] Initializing full-time diagnostic data capture with directory '/data/db1/diagnostic.data'
2017-01-13T12:58:55.897+0000 I NETWORK [initandlisten] waiting for connections on port 27017 ssl
2017-01-13T12:58:55.897+0000 I NETWORK [HostnameCanonicalizationWorker] Starting hostname canonicalization worker
2017-01-13T12:58:56.026+0000 I FTDC [ftdc] Unclean full-time diagnostic data capture shutdown detected, found interim file, some metrics may have been lost. OK
After running the mongod, I start the mongo shell:
$ mongo --port 27017 -u "my username" -p "my password"
--authenticationDatabase "" --ssl --sslPEMKeyFile /etc/ssl/client.pem
--sslCAFile /etc/ssl/ca.pem --host tim
The output is similar to the question by Marshall Farrier; lets have a look.
MongoDB shell version: 3.2.11
connecting to: 127.0.0.1:27017/datatest
2017-01-13T12:35:58.247+0000 I NETWORK [initandlisten] connection accepted from 127.0.0.1:38902 #8 (1 connection now open)
2017-01-13T12:35:58.259+0000 E NETWORK [thread1] SSL peer certificate validation failed: self signed certificate
2017-01-13T12:35:58.259+0000 E QUERY [thread1] Error: socket exception [CONNECT_ERROR] for SSL peer certificate validation failed: self signed certificate :
connect#src/mongo/shell/mongo.js:231:14
#(connect):1:6
2017-01-13T12:35:58.263+0000 E NETWORK [conn8] SSL peer certificate validation failed: self signed certificate
2017-01-13T12:35:58.263+0000 I NETWORK [conn8] end connection 127.0.0.1:38902 (0 connections now open)
What am I doing wrong?
After some searching, it seems like this error is due to the fact that the hostname "CN" was incorrect.
From digitalocean:
Whenever you generate a CSR, you will be prompted to provide information regarding the certificate. This information is known as a Distinguised Name (DN). An important field in the DN is the Common Name (CN), which should be the exact Fully Qualified Domain Name (FQDN) of the host that you intend to use the certificate with.
Also from MongoDB documentation:
If your MongoDB deployment uses SSL, you must also specify the --host option. mongo verifies that the hostname of the mongod or mongos to which you are connecting matches the CN or SAN of the mongod or mongos‘s --sslPEMKeyFile certificate. If the hostname does not match the CN/SAN, mongo will fail to connect.
SOLUTION:
I regenerated the keys, replaced localhost with any other hostname in the CN = <hostname> and completed the guide by Wan Bachtiar.
Running the following command after completion worked:
$ mongo --port 27017 -u '<_username_>' -p '<_password_>'
--authenticationDatabase "<_my db_>" --ssl --sslPEMKeyFile
/etc/ssl/client.pem --sslCAFile /etc/ssl/ca.pem --host localhost
Note:
The MongoDB folows a strict ruling of who has access to what db, a quick test in the mongo shell:
> show dbs
return an error. However, my user actually only have access to the db specified in "<my db>", so looping through the rows in "<my db>" works perfectly.

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

MongoDB centos7 unable to run with a non standard port as a service

I installed mongodb on a fresh centos 7
I run sudo service mongod start without any problem
When I change the port in /etc/mongod.conf and I restart the mongod service I get the following error
From /var/log/mongodb/mongod.log
2014-11-11T07:38:29.495+0000 [initandlisten] MongoDB starting : pid=12401 port=27022 dbpath=/var/lib/mongo 64-bit host=awscm001
2014-11-11T07:38:29.495+0000 [initandlisten] db version v2.6.5
2014-11-11T07:38:29.495+0000 [initandlisten] git version: e99d4fcb4279c0279796f237aa92fe3b64560bf6
2014-11-11T07:38:29.495+0000 [initandlisten] build info: Linux build8.nj1.10gen.cc 2.6.32-431.3.1.el6.x86_64 #1 SMP Fri Jan 3 21:39:27 UTC 2014 x86_64 BOOST_LIB_VERSION=1_49
2014-11-11T07:38:29.495+0000 [initandlisten] allocator: tcmalloc
2014-11-11T07:38:29.495+0000 [initandlisten] options: { config: "/etc/mongod.conf", net: { port: 27021 }, processManagement: { fork: true, pidFilePath: "/var/run/mongodb/mongod.pid" }, storage: { dbPath: "/var/lib/mongo" }, systemLog: { destination: "file", logAppend: true, path: "/var/log/mongodb/mongod.log" } }
2014-11-11T07:38:29.497+0000 [initandlisten] journal dir=/var/lib/mongo/journal
2014-11-11T07:38:29.497+0000 [initandlisten] recover : no journal files present, no recovery needed
2014-11-11T07:38:29.679+0000 [initandlisten] ERROR: listen(): bind() failed errno:13 Permission denied for socket: 0.0.0.0:27022
2014-11-11T07:38:29.680+0000 [initandlisten] now exiting
2014-11-11T07:38:29.680+0000 [initandlisten] dbexit:
2014-11-11T07:38:29.680+0000 [initandlisten] shutdown: going to close listening sockets...
2014-11-11T07:38:29.680+0000 [initandlisten] shutdown: going to flush diaglog...
I am able though to run with a different port with the following command
sudo mongod -f /etc/mongod.conf
When I run as a service user mongod runs mongodb
When I run from command line root runs mongodb
How can run the mongodb service with a different port?
By the way I succeeded on centos 6.5
Thanks
for those people having problem like me.. my fix is allow mongodb custom port on semanage
semanage port --list | grep mongo
mongod_port_t tcp 27017-27019, 28017-28019
as you can see, only 27017-27019, 28017-28019 is allowed.
in order to add a custom port for mongodb
semanage port -a -t mongod_port_t -p tcp 21010
you now have added the custom port
semanage port --list | grep mongo
mongod_port_t tcp 21010, 27017-27019, 28017-28019
make sure to restart mongodb