Mongodb /var/log/mongodb/mongod.log prevents mongod from starting - database

I am using mongodb 3.6.3 on a centos 7 aws ec2 instance.
2 Questions (only one needs to be answered):
Why is the --logpath preventing the mongod command if done manually through the cli with sudo mongod --storageEngine etc
Why is sudo service mongod status showing the failure that it is?
When I run the below command, (since currently, sudo service mongod start isn't working but the below works) it fails when I specify the --logpath but will run without it. Unfortunately, when I run it without it, all of my logs end up in the / which is absolutely the wrong location.
sudo mongod --storageEngine wiredTiger --dbpath /data --bind_ip 127.0.0.1,apiIP --logpath /var/log/mongodb/mongod.log --auth --fork
Below is what happens when I try to run that above line as is.
I CONTROL [main] log file "/var/log/mongodb/mongod.log" exists; moved to "/var/log/mongodb/mongod.log.2018-03-16T15-16-01".
From what I can tell, it is conflicting with the currently existent
Just for reference, sudo service mongod status returns:
mongod.service - mongodb database
Loaded: loaded (/usr/lib/systemd/system/mongod.service; enabled; vendor preset: disabled)
Active: failed (Result: exit-code) since Fri 2018-03-16 15:11:53 UTC; 9min ago
Process: 26620 ExecStart=/usr/bin/mongod $OPTIONS run (code=exited, status=2)
Main PID: 26620 (code=exited, status=2)
Mar 16 15:11:53 ip-* systemd[1]: Started mongodb database.
Mar 16 15:11:53 ip-* systemd[1]: Starting mongodb database...
Mar 16 15:11:53 ip-* mongod[26620]: F CONTROL [main] Failed global initialization: FileNotOpen: Failed to open "/var/log/mongodb/mongod.log"
Mar 16 15:11:53 ip-* systemd[1]: mongod.service: main process exited, code=exited, status=1/FAILURE
Mar 16 15:11:53 ip-* systemd[1]: Unit mongod.service entered failed state.
Mar 16 15:11:53 ip-* systemd[1]: mongod.service failed.
/etc/mongod.conf
# 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: /data
# logpath: /log/mongod.log
journal:
enabled: true
engine: wiredTiger
# 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: 127.0.0.1, apiIP
Thanks to #AlexBlex for noticing the spacing issue in the yaml file.
With that sorted, the status error has this line
Unrecognized option: storage.wiredTiger

cd /var/log
sudo mkdir -m 777 mongodb
sudo service mongod start
sudo service mongod status
Hopefully, that's will do!

sudo systemctl enable mongod
sudo systemctl restart mongod
sudo journalctl -u mongod.service
Hopefully, that will do!
Reference: https://stackoverflow.com/a/51286526/122441

Related

Problem using and running brew services start mongodb-community#4.2

I have MacOs Catalina version 10.15.5 and I installed mongoDB following the steps indicated at their site https://docs.mongodb.com/manual/tutorial/install-mongodb-on-os-x/ using Homebrew. When I check my mongoDB if its correctly installed I use which mongo command and I get /usr/local/bin/mongo response. When I use mongo --version and mongod --version I get:
MongoDB shell version v4.2.7
git version: 51d9fe12b5d19720e72dcd7db0f2f17dd9a19212
allocator: system
modules: none
build environment:
distarch: x86_64
target_arch: x86_64
and
db version v4.2.7
git version: 51d9fe12b5d19720e72dcd7db0f2f17dd9a19212
allocator: system
modules: none
build environment:
distarch: x86_64
target_arch: x86_64
respectevely which I think it means its correctly installed and should work.
But when I open terminal and run the command brew services start mongodb-community#4.2 as indicated on the site I get:
Error: undefined method `resolve_alias' for Formula:Class
/usr/local/bin/brew-services.rb:154:in `service'
/usr/local/bin/brew-services.rb:203:in `check'
/usr/local/bin/brew-services.rb:193:in `run!'
/usr/local/bin/brew-services.rb:397:in `<top (required)>'
/System/Library/Frameworks/Ruby.framework/Versions/2.6/usr/lib/ruby/2.6.0/rubygems/core_ext/kernel_require.rb:54:in `require'
/System/Library/Frameworks/Ruby.framework/Versions/2.6/usr/lib/ruby/2.6.0/rubygems/core_ext/kernel_require.rb:54:in `require'
/usr/local/Homebrew/Library/Homebrew/utils.rb:81:in `require?'
/usr/local/Homebrew/Library/Homebrew/brew.rb:112:in `<main>'
NOTE: my brew is updated and upgraded successfully!
I searched on the internet any solution but I cant find anything that solves the problem... If anyone had faced similar problem please explain step by step and with details how you had solved it?!?!
If I directly run command mongod I get:
2020-06-04T01:19:41.351+0200 I CONTROL [main] Automatically disabling TLS 1.0, to force-enable TLS 1.0 specify --sslDisabledProtocols 'none'
2020-06-04T01:19:41.355+0200 W ASIO [main] No TransportLayer configured during NetworkInterface startup
2020-06-04T01:19:41.355+0200 I CONTROL [initandlisten] MongoDB starting : pid=24712 port=27017 dbpath=/data/db 64-bit host=Air-de-Ruslan.home
2020-06-04T01:19:41.355+0200 I CONTROL [initandlisten] db version v4.2.7
2020-06-04T01:19:41.355+0200 I CONTROL [initandlisten] git version: 51d9fe12b5d19720e72dcd7db0f2f17dd9a19212
2020-06-04T01:19:41.355+0200 I CONTROL [initandlisten] allocator: system
2020-06-04T01:19:41.355+0200 I CONTROL [initandlisten] modules: none
2020-06-04T01:19:41.355+0200 I CONTROL [initandlisten] build environment:
2020-06-04T01:19:41.355+0200 I CONTROL [initandlisten] distarch: x86_64
2020-06-04T01:19:41.355+0200 I CONTROL [initandlisten] target_arch: x86_64
2020-06-04T01:19:41.355+0200 I CONTROL [initandlisten] options: {}
2020-06-04T01:19:41.355+0200 E NETWORK [initandlisten] Failed to unlink socket file /tmp/mongodb-27017.sock Permission denied
2020-06-04T01:19:41.355+0200 F - [initandlisten] Fatal Assertion 40486 at src/mongo/transport/transport_layer_asio.cpp 684
2020-06-04T01:19:41.355+0200 F - [initandlisten]
***aborting after fassert() failure
and if I run directly command mongo I get:
MongoDB shell version v4.2.7
connecting to: mongodb://127.0.0.1:27017/?compressors=disabled&gssapiServiceName=mongodb
2020-06-04T01:21:20.317+0200 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-06-04T01:21:20.320+0200 F - [main] exception: connect failed
2020-06-04T01:21:20.320+0200 E - [main] exiting with code 1
SOLVED
I had to remove /usr/local/bin/brew-services.rb with the command
rm /usr/local/bin/brew-services.rb it could ask for permissions so add
sudo rm /usr/local/bin/brew-services.rb.
After that I tried running brew services start mongodb-community#4.2 and I got
Successfully started mongodb-community (label: homebrew.mxcl.mongodb-commu
but when I put mongo from different terminal to start the shell I got
Socket exception errorso I checked also the mongod.log file and I saw it was a socket issue so I tried
ls -ls /tmp/mongodb-27017.sock
and I got
0 srwx------ 1 root root 0 June 04 14:51 /tmp/mongodb-27017.sock
that means the owner is root and mongodb cant access it so I did
sudo rm -rf /tmp/mongodb-27017.sock
to delete the file and then start again mongodb with
brew services start mongodb-community#4.2 that created new .sock file whose owner is directly mongodb or the actual user whoami and when I checked again
ls -lsah /tmp/mongodb-27017.sock
I proved I am the owner and then I could open new terminal and run mongo and get in with enabled connection and use the shell!
Tested on macos Big Sur and mongodb-community#5.0.1
If you're using homebrew and upgraded to mongodb-community#5.0, you may encounter with error when starting the mongodb-community service.
Use rm -rf /usr/local/var/mongodb to remove previous dbpath folder if exists then create it again using cd /usr/local/var && mkdir mongodb.
Then brew services start mongodb-community should work fine.
On MacOS Big Sur (tested for mongodb-community#5.0), the following always returns an error (which means mongodb-community service will be loaded automatically) :
~ brew services start mongodb-community
mongodb-community error zaffer /Users/zaffer/Library/LaunchAgents/homebrew.mxcl.mongodb-community.plist
By default mongodb-community configuration is stored in /usr/local/etc/mongod.conf
By default the dbpath parameter is set as /usr/local/var/mongodb
On analyzing the logs, I found that the problem is mostly related to permissions of this folder /usr/local/var/mongodb
The following worked for me :
~ cd /usr/local/var/mongodb
~ sudo chown -R $(whoami) .
~ cd ..
~ sudo chown -R $(whoami) mongodb
~ brew services restart mongodb-community
If you are still facing problems, I suggest changing file permissions of all files of this folder (/usr/local/var/mongodb) :
~ chmod -R 777 /usr/local/var/mongodb
~ brew services restart mongodb-community
or use sudo if permission is denied
~ sudo chmod -R 777 /usr/local/var/mongodb
~ brew services restart mongodb-community
If you are still facing issues, observe the errors when running mongod as a background process using one of the following :
~ mongod
~ mongod --dbpath /usr/local/var/mongodb
~ mongod --config /usr/local/etc/mongod.conf
You may also encounter problems when upgrading from a lower version of mongodb-community, for eg. upgrading from 3.6 to 5.0, in my case, I was inclined towards saving the existing databases, otherwise I could have created a new folder for mongodb data, and updated the dbpath in /usr/local/etc/mongod.conf configuration file.
The recommended way to do this is to do an incremental upgrade, i.e. upgrade from 3.6 to 4.0, then 4.0 to 4.4, then 4.4 to 5.0. What this does is it sets the recommended compatibility versions for migrating the data from one version to another. You can set the compatibility versions manually from mongodb shell using (for this, you need a working mongodb server):
> db.adminCommand( { setFeatureCompatibilityVersion: <version> } )
link to doc : https://docs.mongodb.com/manual/reference/command/setFeatureCompatibilityVersion/

MongoDB Catalina: connection attempt failed: SocketException: Error connecting to 127.0.0.1:27017

I just updated my mac to Catalina 10.15.2 and I can't running MongoDB.
When I send the command mongo I receive this message
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 :
and the only way to make Mongo work is to restart the Mac.
If I check the process with the command ps I don't see the process already on and the port 27017 is available.
I already tried to run the follow command:
brew tap mongodb/brew
brew reinstall mongodb-community
brew services restart mongodb-community
and if I run the follow command:
ps aux | grep -v grep | grep mongod
no results.
I tried to run mongod before mongo as well and the result is:
2019-12-18T12:17:45.916+0100 I CONTROL [main] Automatically disabling TLS 1.0, to force-enable TLS 1.0 specify --sslDisabledProtocols 'none'
2019-12-18T12:17:45.920+0100 I CONTROL [initandlisten] MongoDB starting : pid=9375 port=27017 dbpath=/data/db 64-bit host=Marcos-MacBook-Pro.local
2019-12-18T12:17:45.920+0100 I CONTROL [initandlisten] db version v4.2.1
2019-12-18T12:17:45.920+0100 I CONTROL [initandlisten] git version: edf6d45851c0b9ee15548f0f847df141764a317e
2019-12-18T12:17:45.920+0100 I CONTROL [initandlisten] allocator: system
2019-12-18T12:17:45.920+0100 I CONTROL [initandlisten] modules: none
2019-12-18T12:17:45.920+0100 I CONTROL [initandlisten] build environment:
2019-12-18T12:17:45.920+0100 I CONTROL [initandlisten] distarch: x86_64
2019-12-18T12:17:45.920+0100 I CONTROL [initandlisten] target_arch: x86_64
2019-12-18T12:17:45.920+0100 I CONTROL [initandlisten] options: {}
2019-12-18T12:17:45.920+0100 E NETWORK [initandlisten] Failed to unlink socket file /tmp/mongodb-27017.sock Permission denied
2019-12-18T12:17:45.920+0100 F - [initandlisten] Fatal Assertion 40486 at src/mongo/transport/transport_layer_asio.cpp 693
2019-12-18T12:17:45.920+0100 F - [initandlisten]
***aborting after fassert() failure
Someone can help me please? I appreciate it
An alternative way to fix this Catalina Mac OS root & mongodb connection issue, do the following:
Install Homebrew, if you have it, reinstall it again
run the following commands in the terminal
/usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"
brew untap mongodb/brew && brew tap mongodb/brew
brew install mongodb-community#4.2
The issue is mongo cannot find /db/data because there is no directory, so you have to create one:
cd ~ (this should take you to your Users folder)
from the Users folder make your own db/data folder by running the follow commands:
mkdir db & cd inside db folder
inside the db folder run: mkdir data & cd inside data folder
run this command: mongod --dbpath ~/data/db
(inside the Users/data/db/ folder you just created)
Now open a new terminal tab &
run: cd ~ (brings you back to Users) -> now run : cd .. & cd .. again (do this twice)
(now you should be in the folder before Users)
(now find tmp folder) cd into the /tmp folder
delete the sock file (this is giving you connection issues)
delete it by running: rm -rf mongodb-27017.sock
now run command: mongo (this should work now)
inside mongo shell run command: db.verion()
if you see a version, your connection works.
From now on to run mongodb database & connection you will have to have two separate tabs open at all times whenever you want to work on your projects
Example:
in one terminal tab run command -> mongod --dbpath ~/data/db
(this starts the connection)
in the other terminal tab run command: mongo
(this starts the shell)
These both have to be running..
Side Note:
you no longer need to start
the connection by running
brew services start mongo-community anymore.
remember mongod --dbpath ~/data/db is basically -> mongod command now.. The Mac OS Catalina Update created root permission problems, that is why mongod command alone never worked before.
Hope this helped you. Goodluck.
Faced a similar issue with macOS Big Sur. After trying all the approaches mentioned above including
updating homebrew
reinstalling MongoDB, MongoDB community and setting up a /System/Volumes/Data/data/dbfolder
This command worked for me mongod --dbpath=/System/Volumes/Data/data/db
reference: https://medium.com/codespace69/mongodb-troubleshooting-errors-for-beginners-and-macos-catalina-31befd99f6c8
This error is caused due to ownership issues.
Here we are changing the ownership to 'mongodb' user.
Command 1: deletes the /tmp/mongodb-27017.sock file
$sudo rm -rf /tmp/mongodb-27017.sock
Command 2: starts mongod service
$sudo service mongod start
Command 3: shows the file with its ownership details
ls -lsah /tmp/mongodb-27017.sock
output
0 srwx------ 1 mongodb mongodb 0 Aug 24 04:01 /tmp/mongodb-27017.sock
Got the same issue after upgrading to Catalina. Tried lots of ways to solve the issue, what eventually helped was reinstalling brew, and then following steps listed on official mongodb site.
So:
Uninstall brew:
/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/uninstall.sh)
Install it again:
ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/uninstall)"
Follow steps to install and run monbo service:
https://docs.mongodb.com/manual/tutorial/install-mongodb-on-os-x/

Postgres Database in Dockerfile

I am trying to build a PostgreSQL image in DockerManually becuase I need to add some certain configuration for the database that doesnot exit in the postgres images provided with docker.
I wrote the following dockerfile
FROM alpine:3.7
#update the OS
RUN apk update
#installing alpine software kit
RUN apk add alpine-sdk
RUN apk update \
postgresql-$PG_MAJOR-postgis-$POSTGISV \
postgresql-$PG_MAJOR-postgis-$POSTGISV-scripts \
postgresql-$PG_MAJOR-pgrouting \
postgresql-$PG_MAJOR-pgrouting-scripts \
postgresql-server-dev-$PG_MAJOR
#installing postgresql
RUN apk add postgresql
RUN apk add openrc --no-cache
RUN apk update && apk upgrade
RUN apk add --no-cache --virtual .base_deps build-base openssl-dev zlib-dev libxml2-dev wget gnupg ca-certificates
RUN apk add --no-cache readline-dev glib-lang libssl1.0 postgresql postgresql-client
RUN apk add --update binutils
RUN apk add --no-cache sudo
RUN apk --purge del .base_deps
RUN echo "postgres ALL=(ALL) NOPASSWD: ALL" > /etc/sudoers.d/postgres
RUN chmod 600 /etc/sudoers.d/postgres
RUN sync
#RUN /etc/init.d/postgresql start
#RUN postgres -D /usr/local/pgsql/data
EXPOSE 5432
then I am trying to lunch the dockerfile with interactive shell with
docker run -it processID
then the follwoing command to instantite database
initdb Database
The files belonging to this database system will be owned by user "postgres".
This user must also own the server process.
The database cluster will be initialized with locales
COLLATE: C
CTYPE: C.UTF-8
MESSAGES: C
MONETARY: C
NUMERIC: C
TIME: C
The default database encoding has accordingly been set to "UTF8".
The default text search configuration will be set to "english".
Data page checksums are disabled.
creating directory Database ... ok
creating subdirectories ... ok
selecting default max_connections ... 100
selecting default shared_buffers ... 128MB
selecting default timezone ... UTC
selecting dynamic shared memory implementation ... posix
creating configuration files ... ok
running bootstrap script ... ok
performing post-bootstrap initialization ... sh: locale: not found
2019-11-18 08:04:06.670 UTC [89] WARNING: no usable system locales were found
ok
syncing data to disk ... ok
WARNING: enabling "trust" authentication for local connections
You can change this by editing pg_hba.conf or using the option -A, or
--auth-local and --auth-host, the next time you run initdb.
Success.
Then I am trying to lunch the database using the following command
postgres -D Database
2019-11-18 08:05:27.358 UTC [91] LOG: listening on IPv4 address "127.0.0.1", port 5432
2019-11-18 08:05:27.358 UTC [91] LOG: could not bind IPv6 address "::1": Address not available
2019-11-18 08:05:27.358 UTC [91] HINT: Is another postmaster already running on port 5432? If not, wait a few seconds and retry.
2019-11-18 08:05:27.360 UTC [91] FATAL: could not create lock file "/run/postgresql/.s.PGSQL.5432.lock": No such file or directory
2019-11-18 08:05:27.360 UTC [91] LOG: database system is shut down
I am getting some errors I do not know exactly why? any help would really be appreciated.
I did switch yo docker-compose, and here is an example of the file
version: "3"
services:
# Create a service named db.
db:
# Use the Docker Image postgres. This will pull the newest release.
image: "postgres:12.1-alpine"
# Give the container the name my_postgres. You can changes to something else.
container_name: "theNameOfDockerCompose"
# Setup the username, password, and database name. You can changes these values.
environment:
- POSTGRES_USER=DatabaseName
- POSTGRES_PASSWORD=Password
- POSTGRES_DB=development
# Maps port 54320 (localhost) to port 5432 on the container. You can change the ports to fix your needs.
ports:
- "5432:5432"
# Set a volume some that database is not lost after shutting down the container.
# I used the name postgres-data but you can changed it to something else.
volumes:
- ./postgres-data:/var/lib/postgresql/data
This is file is what I am using now.
But with Dockerfile, I still cannot enable the endpoints to access the dataBase.
Possible Duplicate of postgres-docker-bind-address-ipv6
update listen_addresses = '*' into postgresql/data/postgresql.conf
2019-11-18 08:05:27.360 UTC [91] FATAL: could not create lock file "/run/postgresql/.s.PGSQL.5432.lock": No such file or directory
points to a missing folder. Add
RUN mkdir -p /run/postgresql
to your Dockerfile
In addition, you need to change the listen_addresses = "*" in your postgresql/data/postgresql.conf. Otherwise, your database will be bound to localhost in your container and you will not be able to access it from anywhere except within that particular container. You can see that in your logs here
2019-11-18 08:05:27.358 UTC [91] LOG: listening on IPv4 address "127.0.0.1", port 5432
for the localhost binding. In addition, this will fix the following error message in your log
2019-11-18 08:05:27.358 UTC [91] LOG: could not bind IPv6 address "::1": Address not available
in which postgres failed to the bind for ipv6.

mongod : Failed to unlink socket file

I am trying to start mongod.
I run
sudo service mongod start
but I get:
Failed to unlink socket file /tmp/mongodb-27017.sock Unknown error
Fatal Assertion 40486 at src/mongo/transport/transport_layer_asio.cpp
685
Theres a question here but I have tried all solutions to no avail
Os: Ubuntu 16.04
When I run
ls -lsah /tmp/mongodb-27017.sock
I see:
0 srwx------ 1 mongodb mongodb 0 Jun 14 11:45 /tmp/mongodb-27017.sock
Then I run
rm /tmp/mongodb-27017.sock
sudo service mongod start
When I run
ls -lsah /tmp/mongodb-27017.sock
again, I see:
0 srwx------ 1 mongodb mongodb 0 Jun 14 11:45 /tmp/mongodb-27017.sock
When I run mongod, it gives me the same error.
I had the same problem and noticed this old answer: https://stackoverflow.com/a/34290982/2683681.
You can use their command:
sudo chown `whoami` /tmp/mongodb-27017.sock
But if you're using root then you'd better be doing:
sudo chown mongodb:mongodb /tmp/mongodb-27017.sock
My problem stemmed from the fact that root owned the socket.

MongoDB couldn't start because have error

I recently installed mongodb on my vps, but it returned an error for me:
Jan 31 01:35:47 servidor-1 mongod[6960]: Error reading config file: No
such file or directory Jan 31 01:35:47 servidor-1 mongod[6960]: try
'/usr/bin/mongod --help' for more information
service mongodb status
I'm using Ubuntu 18.04
Please do check or create this file.
/etc/systemd/system/mongodb.service
It specifies the location of the configuration file. It should either be /etc/mongodb.conf or /etc/mongod.conf depending on your machine.
#Unit contains the dependencies to be satisfied before the service is started.
[Unit]
Description=MongoDB Database
After=network.target
Documentation=https://docs.mongodb.org/manual
# Service tells systemd, how the service should be started.
# Key `User` specifies that the server will run under the mongodb user and
# `ExecStart` defines the startup command for MongoDB server.
[Service]
User=mongodb
Group=mongodb
ExecStart=/usr/bin/mongod --quiet --config /etc/mongodb.conf
# Install tells systemd when the service should be automatically started.
# `multi-user.target` means the server will be automatically started during boot.
[Install]
WantedBy=multi-user.target
After this run basic commands to check.
sudo systemctl enable mongodb.service
sudo systemctl start mongodb
sudo systemctl stop mongodb
sudo systemctl status mongodb
sudo systemctl restart mongodb

Resources