MongoDB impossible to connect when start as a service - mongodb

I did install mongodb(3.2.18) on my ubuntu server ( 16.04 ) named automationTaskV3 . It work when i'm working on my server, but when i try to connect from my computer it failed only when mongodb is start as a service.
For example if i start mongod this way :
sudo mongod
it will work i can access my dbs
But if i do :
sudo service mongod start
it will be impossible for me to connect from my computer.
Otherwise the mongod service status seems ok :
sudo service mongod status
● mongod.service - High-performance, schema-free document-oriented database
Loaded: loaded (/lib/systemd/system/mongod.service; enabled; vendor preset: enabled)
Active: active (running) since Thu 2017-11-30 17:34:46 UTC; 8min ago
Docs: https://docs.mongodb.org/manual
Main PID: 17712 (mongod)
CGroup: /system.slice/mongod.service
└─17712 /usr/bin/mongod --quiet --auth --config /etc/mongod.conf
Nov 30 17:34:46 automationTaskV3 systemd[1]: Started High-performance, schema-free document-oriented database.
Nov 30 17:38:09 automationTaskV3 systemd[1]: Started High-performance, schema-free document-oriented database.
Nov 30 17:39:29 automationTaskV3 systemd[1]: Started High-performance, schema-free document-oriented database.
For the moment i'm trying to access without credentials.
Here my mongod.service file :
[Unit]
Description=High-performance, schema-free document-oriented database
After=network.target
Documentation=https://docs.mongodb.org/manual
[Service]
User=mongodb
Group=mongodb
ExecStart=/usr/bin/mongod --quiet --config /etc/mongod.conf
# file size
LimitFSIZE=infinity
# cpu time
LimitCPU=infinity
# virtual memory size
LimitAS=infinity
# open files
LimitNOFILE=64000
# processes/threads
LimitNPROC=64000
# total threads (user+kernel)
TasksMax=infinity
TasksAccounting=false
# Recommended limits for for mongod as specified in
# http://docs.mongodb.org/manual/reference/ulimit/#recommended-settings
[Install]
WantedBy=multi-user.target
and here my mongod.conf file
# mongod.conf
# for documentation of all options, see:
# http://docs.mongodb.org/manual/reference/configuration-options/
# Where and how to store data.
storage:
dbPath: /var/lib/mongodb
journal:
enabled: true
# engine:
# mmapv1:
# wiredTiger:
# where to write logging data.
systemLog:
destination: file
logAppend: true
path: /var/log/mongodb/mongod.log
# network interfaces
net:
port: 27017
bindIp: 127.0.0.1
#processManagement:
Is it possible that's a user right problem ? It seems strange as i follow the steps from thoses tutorials :
https://www.linode.com/docs/databases/mongodb/install-mongodb-on-ubuntu-16-04
https://docs.mongodb.com/manual/tutorial/install-mongodb-on-ubuntu/

As I said in the comment that its the IP binding issue. So you can even try this solution.
You can access the 27017 port of your remote machine by using ssh tunnelling as it is most the easiest way and it's also a secure way.
ssh -L 27017:localhost:27017 username#remoteaddress.com
This command says forward 27017 port of your local machine to address localhost:27017 of your remote machine.
You will be prompted for the password (of remote machine) if you don't have ssh keys set up. Now you might get prompt for ssh of your remote machine don't close it unless you want to close port forwarding, open a new tab of your terminal or Robomongo you can now connect to localhost:27017. So it's like MongoDB is installed on your local machine but actually, it is serving from the remote machine using ssh.
To know more about ssh tunneling read here : https://www.ssh.com/ssh/tunneling

Related

Can't start mongodb through systemctl, error logs won't show up

I cannot boot MongoDB via systemctl.
When I try, I get an error, but nothing comes through the log.
When I try and run it manually via $ /usr/bin/mongod --config /etc/mongod.conf no error message comes through the log.
When I turn off logging in mongod.conf, an error message comes out that I don't have rights to the directory with the data (which is to be expected).
However, I can boot the process with sudo such as:
$ sudo /usr/bin/mongod --config /etc/mongod.conf, the process runs but it is tied to my terminal so if my session closes so does MongoDB. Hence why I am trying to run it with systemctl.
Then when I try and run with sudo in my service such as:
ExecStart=/usr/bin/sudo /usr/bin/mongod --config /etc/mongod.conf
Then it doesn't start and no error log comes through.
My conf file:
# mongod.conf
# for documentation of all options, see:
# Where and how to store data.
storage:
dbPath: /var/lib/mongodb
journal:
enabled: true
# where to write logging data.
systemLog:
destination: file
logAppend: true
path: /var/log/mongodb/mongod.log
# network interfaces
net:
port: 27017
bindIp: 127.0.0.1
# how the process runs
processManagement:
timeZoneInfo: /usr/share/zoneinfo
My service:
[Unit]
Description=MongoDB Database Server
Documentation=https://docs.mongodb.org/manual
After=network.target
[Service]
Restart=always
User=mongodb
Group=mongodb
EnvironmentFile=-/etc/default/mongod
ExecStart=/usr/bin/sudo /usr/bin/mongod --config /etc/mongod.conf
PIDFile=/var/run/mongodb/mongod.pid
# file size
LimitFSIZE=infinity
# cpu time
LimitCPU=infinity
# virtual memory size
LimitAS=infinity
# open files
LimitNOFILE=64000
# processes/threads
LimitNPROC=64000
# locked memory
LimitMEMLOCK=infinity
# total threads (user+kernel)
TasksMax=infinity
TasksAccounting=false
# Recommended limits for mongod as specified in
# https://docs.mongodb.com/manual/reference/ulimit/#recommended-ulimit-settings
So it becomes an impossible question. When I try and boot it through systemctl no error log comes through /var/log/mongodb/mongod.log. If I run it manually I get an error saying that I don't have access to the data folder since the data folder is owned by the mongodb user, which is expected.
What can I do to see what is causing my service above to fail? I don't want to use sudo in my ExecStart command but whether I do or I don't I get the same problem: the service fails to start and no logs come through my Mongo log file.
Check of your service is failed
systemctl is-failed mongodb.service
Get failure logs
Journalctl is a utility for querying and displaying logs from journald, systemd's logging service.
get latest 20 lines of logs which will you failure details
journalctl | grep mongod | tail -n20
Use journalctl -u service and tail to get top 20 logs
journalctl -u mongodb.service | tail -n20
Get list of all failed services
systemctl list-units --failed
Read - https://www.cyberciti.biz/faq/systemd-systemctl-list-all-failed-units-services-on-linux/

MongoDB Connection Refused, installation

I am new with mongoDB, I have just installed mongoDB following this doucumantation and when i try to run using mongo i get the following error
MongoDB shell version v4.2.3
connecting to: mongodb://127.0.0.1:27017/?compressors=disabled&gssapiServiceName=mongodb
2020-03-02T17:59:27.311+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
2020-03-02T17:59:27.313+0530 F - [main] exception: connect failed
2020-03-02T17:59:27.313+0530 E - [main] exiting with code 1
The status by running the command - systemctl status mongod is
● mongod.service - MongoDB Database Server
Loaded: loaded (/lib/systemd/system/mongod.service; enabled; vendor preset: enabled)
Active: failed (Result: exit-code) since Mon 2020-03-02 17:32:55 IST; 30min ago
Docs: https://docs.mongodb.org/manual
Main PID: 16322 (code=exited, status=14)
Mar 02 17:32:55 hp-laptop-15-bs0xx systemd[1]: Started MongoDB Database Server.
Mar 02 17:32:55 hp-laptop-15-bs0xx systemd[1]: mongod.service: Main process exited, code=exite
Mar 02 17:32:55 hp-laptop-15-bs0xx systemd[1]: mongod.service: Failed with result 'exit-code'.
lines 1-9/9 (END)
`
logs from /etc/mongod.conf is
# Where and how to store data.
storage:
dbPath: /var/lib/mongodb
journal:
enabled: true
# where to write logging data.
systemLog:
destination: file
logAppend: true
path: /var/log/mongodb/mongod.log
# network interfaces
net:
port: 27017
bindIp: 127.0.0.1
# how the process runs
processManagement:
timeZoneInfo: /usr/share/zoneinfo
I also have started the database using systemctl start mongod
I also have uninstalled mongodb and reinstalled it again. I can't seem to figure out whats wrong.
In your config file the processManagement section should be like this:
# how the process runs
processManagement:
fork: true
pidFilePath: /var/run/mongodb/mongod.pid
timeZoneInfo: /usr/share/zoneinfo
Default for processManagement.fork = false, thus the service may not start.

MongoDB status failed after editing mongo.conf (code-exited, status=2)

I'm trying to connect remotely to my Google Cloud Server with Robo 3T to connect too MongoDB v4.0.11 just so I can see the data from windows.
I am running Ubuntu 18.04LTS Minimal. To enable remote connections to MongoDB I have first created both an administrative and root user in my admin database.
My problem is once I add authorization: "enabled" to security: in /etc/mongod.conf and restart MongoDB, I get the following status when running $:systemctl status mongod
● mongod.service - MongoDB Database Server
Loaded: loaded (/lib/systemd/system/mongod.service; disabled; vendor preset: enabled)
Active: failed (Result: exit-code) since Tue 2019-08-06 16:43:51 BST; 1min 20s ago
Docs: https://docs.mongodb.org/manual
Process: 6517 ExecStart=/usr/bin/mongod --config /etc/mongod.conf (code=exited, status=2)
Main PID: 6517 (code=exited, status=2)
Aug 06 16:43:51 instance-1 systemd[1]: Started MongoDB Database Server.
Aug 06 16:43:51 instance-1 mongod[6517]: Unrecognized option: processManagement.authorization
Aug 06 16:43:51 instance-1 mongod[6517]: try '/usr/bin/mongod --help' for more information
Aug 06 16:43:51 instance-1 systemd[1]: mongod.service: Main process exited, code=exited, status=2/INVALIDARGUMENT
Aug 06 16:43:51 instance-1 systemd[1]: mongod.service: Failed with result 'exit-code'.
Example of /etc/mongod.conf
# mongod.conf
# for documentation of all options, see:
# http://docs.mongodb.org/manual/reference/configuration-options/
# Where and how to store data.
storage:
dbPath: /var/lib/mongodb
journal:
enabled: true
# engine:
# mmapv1:
# wiredTiger:
# where to write logging data.
systemLog:
destination: file
logAppend: true
path: /var/log/mongodb/mongod.log
# network interfaces
net:
port: 27017
bindIp: 127.0.0.1
# how the process runs
processManagement:
timeZoneInfo: /usr/share/zoneinfo
#security:
authorization: "enabled"
#operationProfiling:
#replication:
#sharding:
## Enterprise-Only Options:
#auditLog:
#snmp:
It appears there is an invalid argument in /etc/mongod.conf but from what I have read online I can't see the problem.
wrong:
#security:
authorization: "enabled"
correct:
security:
authorization: enabled
For the 4.x release it appears you need to modify the existing security section you have listed
#security:
authorization: "enabled"
And replace it with security.authorization: enabled or
security:
authorization: "enabled"
You should then be able to reload and test authentication is working as expected.
If you dig into the docs you will find the relevant section here.
security.authorization
Type: string
Default: disabled
Enable or disable Role-Based Access Control (RBAC) to govern each user’s access to database resources and operations.
Set this option to one of the following:
Value Description
enabled A user can access only the database resources and actions for which they have been granted privileges.
disabled A user can access any database and perform any action.
In my case the problem was that I used a tab to indent the authorization: enabled line. When I replaced the tab with 2 spaces it worked.

Always get "Active: failed" on 'sudo systemctl status mongod' command

I followed this link to install MongoDB on my machine. Everything was fine and it worked.
Currently, when I run sudo systemctl status mongod, I get this error:
● mongod.service - High-performance, schema-free document-oriented database
Loaded: loaded (/lib/systemd/system/mongod.service; enabled; vendor preset: enabled)
Active: failed (Result: exit-code) since سه<U+200C>شنبه 2019-05-21 15:50:14 +0430; 25min ago
Docs: https://docs.mongodb.org/manual
Process: 5278 ExecStart=/usr/bin/mongod --config /etc/mongod.conf (code=exited, status=1/FAILURE)
Main PID: 5278 (code=exited, status=1/FAILURE)
مه 21 15:50:14 [my-username] systemd[1]: Started High-performance, schema-free document-oriented database.
مه 21 15:50:14 [my-username] systemd[1]: mongod.service: Main process exited, code=exited, status=1/FAILURE
مه 21 15:50:14 [my-username] systemd[1]: mongod.service: Unit entered failed state.
مه 21 15:50:14 [my-username] systemd[1]: mongod.service: Failed with result 'exit-code'.
This is what I have done after it was ok, that might have caused the problem:
I decided to add mongodb extension using pecl.
I was not able to make a new database, neither by MongodDB Compassm, nor by Sell.
I uninstalled it.
I installed it from the same link and ran the same commands, but when I wanted to install it again, after running sudo apt-key adv --keyserver hkp://keyserver.ubuntu.com:80 --recv 0C49F3730359A14518585931BC711F9BA15703C6
command, I got gpg: unchanged: 1 at the end, not gpg: imported: 1 (RSA: 1) which I got for the first time.
I even deleted and recreated my dbPath folder (/var/lib/mongodb) which seems that the problem is somehow associated with this parameter which is on /etc/mongod.conf.
When I run sudo systemctl status mongod, I still get Active: failed.
What should I do to run MongoDB properly again and make its status Active: Running?
Update: This is my /etc/mongod.conf file:
# mongod.conf
# for documentation of all options, see:
# http://docs.mongodb.org/manual/reference/configuration-options/
# Where and how to store data.
storage:
dbPath: /var/lib/mongodb
journal:
enabled: true
# engine:
# mmapv1:
# wiredTiger:
# where to write logging data.
systemLog:
destination: file
logAppend: true
path: /var/log/mongodb/mongod.log
# network interfaces
net:
port: 27017
bindIp: 127.0.0.1
#processManagement:
security:
authorization: "enabled"
#operationProfiling:
#replication:
#sharding:
## Enterprise-Only Options:
#auditLog:
#snmp:
I solved the problem by uninstalling MongoDB and installing a specific version of MongoDB, not the latest version, using this link. This might not be the actual solution but works for now.

mongod service failed to restarted, 27017 port not in use - even getting address already in use error

I am not able to start mongo db service, if I try to start mongod using systemctl restart mongod getting error
Job for mongod.service failed because the control process exited with error code. See "systemctl status mongod.service" and "journalctl -xe" for details.
Logs: vi /var/log/mongo/mongod.log
In log file, if I see below message, it looks like port is already in use
exception in initAndListen std::exception: listen: Address already in use, terminating
netstat -apt| grep "27017"
No process id.
Below is mongod status:
I am not getting any solution, your help will be greatly appreciated. Thanks.
Trying starting mongodb on different port and with different data directory, that might be helpful, use below command to start with different port and different data directory
mongod --port 27027 --dbpath </path/to/your/data/directory>
If this work then some process might be using 27017 or or you might have mongod running on background, so first check the process id and manually kill it as below
ps -Aef | grep 27017
or
ps -Aef | grep mongod
Find the process id and kill it
sudo kill -9 <processId>
Then try to restart it as you are doing:
systemctl restart mongod
or:
/etc/init.d/mongodb restart
Hope this help you out.
One issue was the lock file used by MongoDB might have an incorrect ownership:
-rw-r--r-- 1 root root 0 Mar 16 02:27 /var/lib/mongo/mongod.lock
Suggest you to simply removed it which will allow MongoDB to recreate it properly. This file is only needed by MongoDB if it's currently running, and ideally the file should only exist if it's running too, since it only contains a copy of the current process ID which MongoDB is running under.
The second issue will be the configuration file itself: /etc/mongod.conf.
MongoDB expects the file to be in YAML format where apparently blank spaces matter. I have share the sample configuration file which worked in our server.
Basically, each indentation should be made using two spaces for any directives underneath their parent directive.
Additinal notes:
For example, the "net:" heading is not indented because it's the top of its configuration tier, as where "port:" and "bindIp:" are each indented with 2 spaces since "net:" is their parent directive.
The "ssl:" directive is also indented with two spaces since its parent is "net:", but since "mode:", "CAFile:" and "PEMKeyFile:" along belong to "ssl:" they go underneath it and again each take 2 additional spaces for further indentation for a total of 4 spaces.
Finally the following mongo restart will work:
systemctl restart mongod
or
systemctl restart mongod.service
# mongod.conf
# for documentation of all options, see:
# http://docs.mongodb.org/manual/reference/configuration-options/
# where to write logging data.
systemLog:
destination: file
logAppend: true
path: /var/log/mongodb/mongod.log
# Where and how to store data.
storage:
dbPath: /var/lib/mongo
journal:
enabled: true
# engine:
# mmapv1:
# wiredTiger:
# how the process runs
processManagement:
fork: true # fork and run in background
pidFilePath: /var/run/mongodb/mongod.pid # location of pidfile
timeZoneInfo: /usr/share/zoneinfo
# network interfaces
net:
port: 27017
bindIp: 0.0.0.0 # Listen to local interface only, comment to listen on all interfaces.
ssl:
mode: preferSSL
CAFile: /etc/ssl/mongodb/RootCA/root-ca.pem
PEMKeyFile: /etc/ssl/mongodb/host.omkieitsolutions.com.pem
security:
authorization: enabled
clusterAuthMode: keyFile
#operationProfiling:
#replication:
"/etc/mongod.conf" 50L, 990C