Can't reliably create a directory on startup after ec2 instance mounts it's ephemeral drives - mongodb

This particular instance type mounts two ephemeral drives, in /dev/ and /mnt/. I have to create working directories for one of my services in these paths at startup or the services won't launch. Sometimes this script below works, sometimes it does not. I suspect it's a race between the folder being mounted, and my rc.local script kicking off. Is there a more reliable place I can create these directories? The last time I booted up, the /mnt/mongodb dir did get created, but the /dev/ one did not. I'm running the 12.04 HVM ubuntu instance from amazon.
Here is my rc.local file:
cd /mnt/
sudo mkdir mongodb
sudo chown -R mongodb mongodb
sudo chgrp -R mongodb mongodb
cd /dev/
sudo mkdir mongodb
sudo chown -R mongodb mongodb
sudo chgrp -R mongodb mongodb
sudo service mongodb start
exit 0

Related

How to fix connection of postgresql after move data directory from the root HD to another HDD?

I'm work with postgresql over large datasets and I move my database from the same HDD were is Ubuntu 18.04 installed to a other HDD.
I did the same process and move the data_directory to "Home" (which is at same HDD of the Ubuntu, but in other partition), in this location the postgresql works fine. The problem is I need a big space to upload my data (like 2 TB HDD) because that I try to move to another HDD, empty and formmated exclusive for the DB.
I follow to this tutorial Link of change Data Folder. Using the follow commands in Ubuntu terminal:
su postgres
/usr/lib/postgresql/10/bin/pg_ctl -D /media/path/postgresql/10/main -l logfile start
sudo systemctl stop postgresql
sudo systemctl status postgresql
sudo rsync -av /var/lib/postgresql /media/path/postgresql
sudo mv /var/lib/postgresql/10/main /var/lib/postgresql/10/main.bak
sudo gedit /etc/postgresql/10/main/postgresql.conf (change "data_directory")
sudo systemctl start postgresql
sudo systemctl status postgresql
In addition, I pass to the postgres the ownership of the folder as
chown -R postgres:postgres /media/path/postgresql/10/main
I write the modifications in "/path/postgresql.conf" to adjust to new data directory, reestart the server and I can't connect to server.
At terminal I get this message if I try to connect
"could not connect to server: No such file or directory Is the server running locally and accepting connections on Unix domain socket "/var/run/postgresql/.s.PGSQL.5432"?"
At Pgadmin client I get the problem is manifested as no connection.
In my case, rsync did not copied several files for some reason. May be they were not ready yet after the server stopped or something else.
After I made it once more next day, it started well:
sudo rsync -av /var/lib/postgresql/10/main.bak/ /media/disk1/postgres/postgresql/10/main/
Also try to check log for the actual reason:
tail -n 100 /var/log/postgresql/postgresql-10-main.log
At my case it was
postgres: could not find the database system
Expected to find it in the directory "/media/disk1/postgres/postgresql/10/main",
but could not open file "/media/disk1/postgres/postgresql/10/main/global/pg_control": No such file or directory
pg_ctl: could not start server
Examine the log output.

mongodb 3.4.3 Permission denied wiredtiger_kv_engine.cpp 267 error with ubuntu 16

I'm having problems lauching mongod as a service:
How is it possible that it works when I do sudo mongod -f /etc/mongod.conf but when launching it with sudo service mongod start I get an error in the log
Assertion: 28595:13: Permission denied src/mongo/db/storage/wiredtiger/wiredtiger_kv_engine.cpp 267
I'm running mongodb on ubuntu 16
I followed exactly the instructions in the mongodb documentation for installation of that version, so is this a bug? Any suggestions how to solve this are appreciated.
Additional information:
The mongodb service startup script looks like this and runs it as user mongodb, could this be connected to the error?
lib/systemd/system/mongodb.service:
[Unit]
Description=MongoDB Database Service
Wants=network.target
After=network.target
[Service]
ExecStart=/usr/bin/mongod --config /etc/mongod.conf
ExecReload=/bin/kill -HUP $MAINPID
Restart=always
User=mongodb
Group=mongodb
StandardOutput=syslog
StandardError=syslog
[Install]
WantedBy=multi-user.target
I'm having problems lauching mongod as a service: How is it possible that it works when I do sudo mongod -f /etc/mongod.conf but when launching it with sudo service mongod start I get an error in the log
The sudo command starts mongod with root permissions (aka superuser access). If you run mongod as a service the user and group are configured in the service definition (mongodb for both in your example).
There is no need to run the mongod process as the root user, and this is strongly discouraged as per the common security practice of Principle of least privilege.
If you want to test a configuration from the command-line, you could use sudo to run with a specified user instead of the default (root) user.
For example:
sudo -u mongodb mongod -f /etc/mongod.conf
In general, it's best to use a service configuration rather than running mongod manually. With manual invocation you will also have to remember to include parameters like the config file path (as there is no default config path). Without a configuration file, mongod also uses default options such as a dbPath of /data/db.
Assertion: 28595:13: Permission denied src/mongo/db/storage/wiredtiger/wiredtiger_kv_engine.cpp 267
The likely cause of your permission errors is having previously started mongod as the root user. Some directories and files may now be owned by the root user, so the mongodb user cannot access those. Your specific error relates to accessing files in the data directory (i.e. the configured storage.dbPath in mongod.conf).
Assuming you haven't changed the default paths in your mongod.conf file, you should be able to recursively adjust permissions to match what the mongod.service definition expects.
First, ensure you have stopped your mongod instance if it is currently running.
Then, recursively adjust permissions to the expected user and group:
# storage.dbPath
sudo chown -R mongodb:mongodb /var/lib/mongodb
# systemLog.path
sudo chown -R mongodb:mongodb /var/log/mongodb
Now you should be able to start mongod as a service. If the service fails to start, there should be further detail in the mongod log file (assuming the log file is writable by the mongodb service user).
Have same problem.
What been in /var/log/mongodb/mongod.log:
2017-05-13T13:46:41.152+0700 E STORAGE [initandlisten] WiredTiger error (13) [1494658001:152518][15821:0x7fb843803cc0], connection: /var/lib/mongodb/journal/WiredTigerPreplog.0000000002: file-remove: unlink: Permission denied
2017-05-13T13:46:41.159+0700 I - [initandlisten] Assertion: 28595:13: Permission denied src/mongo/db/storage/wiredtiger/wiredtiger_kv_engine.cpp 267
So wee see that something can't remove file "WiredTigerPreplog.0000000002" in /var/lib/mongodb/journal/
So id just gave permissions, i just did:
sudo chmod 764 /var/lib/mongodb/journal/
If not help, try:
sudo chown -R mongodb:mongodb /var/lib/mongodb/ && sudo chmod 764 /var/lib/mongodb/journal/
There are three set-ups that triggers this kind of problem :
MongoDB installation is configured to create database files at a given path and this path does not exist on your current system. This path is called dbpath in mongo.
In your case, check if /data/db exist. If it doesn't or if it is empty, mongod is trying the wrong dbpath. You need to find it, it's usually under /var/lib/mongodb.
Once you found it there's two thing you can do. First, copy all the file from there to /data/db. Second, change your dbpath under the mongod.conf file, which is located (in linux) at /etc/mongod.conf. Make sure to start mongod with the --config specifying the configuration file.
MongoDB does not have the permission to read one or more files or directories corresponding to its dbpath.
chown mongodb:mongodb dbpath -R.
MongoDB is missing WiredTiger.wt . This can happen if you remove files under the dbpath or if there's a device failure. We do it for testing a recovery strategy for example.
If you're sure dbpath is correct and that there's no instance of WiredTiger.wt there. Your database is broken. There are no ways to ensure integrity if you lose this file. Reinstall mongodb by :
sudo apt-get purge mongodb-org*
sudo rm -r dbpath
sudo apt-get install mongodb-org
Edit :
Or copy dbpath from one of your replicas.
I had same problem in my mongod.log:
2021-09-16T16:06:43.782+0200 F STORAGE [initandlisten] Reason: 13: Permission denied
2021-09-16T16:06:43.782+0200 F - [initandlisten] Fatal Assertion 28595 at src/mongo/db/storage/wiredtiger/wiredtiger_kv_engine.cpp 789
So I looked in my dbPath folder (specified in mongodb config file /etc/mongod.conf, section storage:dbPath) and found that same file was owned by root:
WiredTiger.turtle
WiredTigerLAS.wt
journal/WiredTigerLog.0000000112
From dbPath folder, using the command below, I change owner and group to mongodb:
sudo chown mongodb:mongodb WiredTiger.turtle WiredTigerLAS.wt journal/WiredTigerLog.0000000112
After this, I can start mongodb server as
"service mongod start"
and check its status as
"service mongod status" with output:
mongod.service - MongoDB Database Server
Loaded: loaded (/lib/systemd/system/mongod.service; enabled; vendor preset: enabled)
Active: active (running) since Thu 2021-09-16 16:19:47 CEST; 54min ago
Docs: https://docs.mongodb.org/manual
Main PID: 8459 (mongod)
CGroup: /system.slice/mongod.service
└─8459 /usr/bin/mongod --config /etc/mongod.conf
Sep 16 16:19:47 svi5-ubu16 systemd[1]: Started MongoDB Database Server.
Note that above commands are working as normal user, not as root.
My configuration: Ubuntu 16.04.2 LTS, MongoDB shell version v4.2.1.
Other file or folder owned by mongodb (user and group):
/var/log/mongodb
/var/lib/mongodb
/tmp/mongodb-27028.sock
I spent a lot of time in solving this issue, thank you very match to every one has posted here and guided me to solution.
I wish append a comment to the previous answer, but unfortunately I cannot yet.
I completely agree with the explanation of Stennie. It is exactly what was happened to me.
I've always run mongod as a service but today, because some changes that I've made, I've tried to run the process using sudo mongod --auth --dbpath /data/mongodb/.. to test authorizations and db changing location.
After that the mongod service didn't run anymore, due to this permissions problem.
I've to say that the command sudo chown -R mongodb:mongodb /data/mongodb/ didn't immediately fix the problem as expected. I've had to reboot several times, remove the mongod.lock file under /data/mongodb/, reissue the sudo chown command again.. and finally everything gone well.
I have a similar issue but with custom log path and data dir. updating the owner and group access to them did not fix the issue for running as a service
updating the group and owner to mongodb:mongodb works on running mongod by itself as stated earlier
sudo -u mongodb mongod -f /etc/mongod.conf
or
sudo mongod -f /etc/mongod.conf
If you find yourself with the same problem on CentOS, but the permissions look correct, it could be because of SELinux policies. On my system, MongoDB files use a specific SELinux context. The mongod server failed to start, outputting the same permissions errors, until I corrected the SELinux contexts.
If it doesn't exist, create the MongoDB data folder (either /var/lib/mongo or /var/lib/mongodb, check your /etc/mongod.conf file):
$ mkdir -p /var/lib/mongodb
Then try to restore the SELinux contexts:
$ restorecon -v /var/lib/mongodb
$ restorecon -v /var/lib/mongodb/*
If that still doesn't work, try to apply the contexts directly:
$ chcon system_u:object_r:mongod_var_lib_t:s0 /var/lib/mongodb
$ chcon system_u:object_r:mongod_var_lib_t:s0 /var/lib/mongodb/*
Verify the contexts are correct:
$ ls -lZ -d /var/lib/mongodb
drwxr-xr-x. mongod mongod system_u:object_r:mongod_var_lib_t:s0 /var/lib/mongodb
sudo chmod -R 666 /var/lib/mongodb
is another solution...

MonogoDB service cannot start after storage extension under Ubuntu 12.04

Recently I extended my server's storage size. Everything else is working fine, but I cannot manage to start MongoDB by sudo service mongodb start. After I typed in this command, it prompted mongodb start/running, process 1279. It looks OK. but when I check the status of the service, it shows the service is not running. So I checked the mongodb.log. What I found is the service will start, and after a while, the service process will receive a signal 2: Thu Jun 6 06:09:06.963 got signal 2 (Interrupt), will terminate after current cmd ends.
I also try to configure the mongodb to output more verbose log, but it didn't help me to figure out the source if the interruption.
Now I can only start MongoDB with sudo mongod --config /path/to/conf_file. Anyone knows how to fix it? Thanks!
What I did was being sure that all the files and directories in your /var/lib/mongodb/ folder (or whichever folder dbpath points to) belong to the mongodb user and mongodb group.
cd /var/lib/mongodb/
sudo chown -R mongodb *
sudo chgrp -R mongodb *
Then you can remove the lock, repair the database and restart the daemon:
sudo rm /var/lib/mongodb/mongod.lock
sudo -u mongodb mongod --config /etc/mongodb.conf --repair
sudo service mongodb start

Mongo insecure world dir

I'm extremely new to using terminal commands. I'm trying to run an app locally on my Mac and i'm having difficulty with that so I'm trying to pick through the possible issues. The first one is the when using the mongod command I get an error of the flowing
/usr/local/bin/mongod:3: warning: Insecure world writable dir /usr/local in PATH, mode 040777
all output going to: /usr/local/var/log/mongodb/mongo.log
Does this seem to be a problem or erroneous?
Having /usr/local world writable is never a good idea. Normal permissions only allow the superuser (root) to write to these system directories.
To bring you back closer to the default run:
sudo chmod 755 /usr/local
Furthermore you might want to clear the world writable bit on every subdir and file by running:
sudo chmod -R o-w /usr/local
This will mean you have to either start mongod as root or change the database path to somewhere you can write (for example in your homedir).
I run mongod on my developer machine using the following command:
mongod --bind_ip 127.0.0.1 --dbpath ~/mongo -v --profile 2
This means the following:
--bind_ip 127.0.0.1 make sure others in your network can't access your database
--dbpath ~/mongo save database files in your home dir in the directory mongo
-v show queries that are being executed
--profile 2 shows performance and profile information on queries
You should run mongod -h to see all available options.

MongoDB on Ubuntu won't start as a service, nothing in the log

Am running MongoDB 2.2 on Ubuntu and if I run:
sudo mongod
I get an error that it can't find /data/db, which is not where the database is. In mongod.conf the database path is specified as the Ubuntu 10gen default /var/lib/mongodb which is where the db is located. Seems like mongod is not finding the conf file. So when I run:
sudo mongod -f /etc/mongodb.conf
The server starts up fine and output is logged to the log file: /var/log/mongodb/mongodb.log. All is happy. I can switch to another shell, log into mongo shell, see the databases and run queries.
So, I cancel out of that and try to run as a service:
> sudo status mongodb
mongodb stop/waiting
> sudo start mongodb
mongodb start/running, process 10468
Looks good so far, but the mongo server did not start. Running another:
> sudo status mongodb
mongodb stop/waiting
> mongo
MongoDB shell version: 2.2.0
connecting to: test
Sat Sep 1 19:07:43 Error: couldn't connect to server 127.0.0.1:27017 src/mongo/shell/mongo.js:91
exception: connect failed
"test" is not the correct database, and nothing appears in the log file.
I am at a loss as to what could be wrong. I checked the upstart scripts and they seem fine. /etc/init/mongodb.conf runs:
mongodb --exec /usr/bin/mongod -- --config /etc/mongodb.conf
OK, this all comes down to permissions, but let's take it step by step. When you run sudo mongod it does not load a config file at all, it literally starts with the compiled in defaults - port 27017, database path of /data/db etc. - that is why you got the error about not being able to find that folder. The "Ubuntu default" is only used when you point it at the config file (if you start using the service command, this is done for you behind the scenes).
Next you ran it like this:
sudo mongod -f /etc/mongodb.conf
If there weren't problems before, then there will be now - you have run the process, with your normal config (pointing at your usual dbpath and log) as the root user. That means that there are going to now be a number of files in that normal MongoDB folder with the user:group of root:root.
This will cause errors when you try to start it as a normal service again, because the mongodb user (which the service will attempt to run as) will not have permission to access those root:root files, and most notably, it will probably not be able to write to the log file to give you any information.
Therefore, to run it as a normal service, we need to fix those permissions. First, make sure MongoDB is not currently running as root, then:
cd /var/log/mongodb
sudo chown -R mongodb:mongodb .
cd /var/lib/mongodb
sudo chown -R mongodb:mongodb .
That should fix it up (assuming the user:group is mongodb:mongodb), though it's probably best to verify with an ls -al or similar to be sure. Once this is done you should be able to get the service to start successfully again.
First confirm that the mongodb user/group has permission to write to both the data directory and log file:
$ sudo chown -R mongodb:mongodb /var/lib/mongodb/.
$ sudo chown -R mongodb:mongodb /var/log/mongodb.log
Start up MongoDB as a Daemon (background process) using the following command:
$ mongod --fork --dbpath /var/lib/mongodb/ --smallfiles --logpath
/var/log/mongodb.log --logappend
To Shut Down MongoDB enter the Mongo CLI, access the admin and issue the shutdown command:
$ ./mongo
> use admin
> db.shutdownServer()
Ref: http://www.mongodb.org/display/DOCS/Starting+and+Stopping+Mongo
I too had the same problem. So I went to cd /var/lib/mongodb/ and deleted the mongod.lock file
Then it worked for me.
After checking all permission in the data, journal and log folders as suggested by #nelsonic, my problem was solved by giving permission to lock file in the /tmp folder
sudo chown mongod:mongod mongodb-27017.sock
I was running it as a AWS Amazon Linux instance.
I figured that out by executing as the mongod user as below, and then, researching the error code. It might be useful for other troubleshooting.
sudo -S -u mongod mongod -f /etc/mongod.conf
Just try this command:
sudo chown mongodb /tmp/mongodb-27017.sock
Nothing worked for me, then I've found that it was a permissions problem on /tmp directory:
sudo chmod 1777 /tmp
sudo chown root:root /tmp
None of the above answers worked for me. I finally figured it out by debugging the init script with:
sudo bash -x /etc/init.d/mongodb start
And seeing it was passing the wrong config path to mongod. I simply changed the line in /etc/init.d/mongodb from "CONF=/etc/mongodb.conf" to "CONF=/etc/mongod.conf". Version 2 uses the former, and installing version 3 added /etc/mongod.conf with the new format but apparently did not update the init script.
UPDATE: I now have a much stranger problem where the init script works, but only if I run it with "sudo bash -x /etc/init.d/mongodb start" and not with "sudo service mongodb start". Same thing for stop.
My mongodb was starting when launched from the command line as the mongod user, but not as a service with User=mongod.
After an hour checking permissions, definition of the service, sockets... it was SElinux !
In /etc/selinux/config I switched from enforcing to permissive and reboot. It is now ok.
After none of the above answers worked for me, deleting my log file brought Mongo back to life.
These days this error can occur if you've updated mongod and you are running and old database. Mongod will be using the wiredTiger engine by default and you'll have a mmapv1 database
edit the engine setting in /etc/mongod.conf
# engine: wiredTiger
engine: mmapv1
Careful - YAML is whitespace sensitive
journalctl/systemd won't see this problem. Check the mongod log in /var/log/mongodb/mongod.log
I presume you can convert the database with something like the steps outlined here
https://docs.mongodb.com/manual/tutorial/change-standalone-wiredtiger/