setup MongoDB for core4 on Amazon Web Services (AWS)

The following shell protocol installs MongoDB version 4.06 on Amazon Web Services (AWS). The protocol is based on a setup of Debian GNU/Linux 9 (Stretch) on a t3.micro instance.

This MongoDB server is the core4 system database used by core4. See setup core4 on Amazon Web Services (AWS).

update the system

sudo apt-get update --yes

install essentials

sudo apt-get install python3-pip python3-venv git runit runit-systemd --yes

install mongodb

wget https://fastdl.mongodb.org/linux/mongodb-linux-x86_64-debian92-4.0.5.tgz
tar -xvf mongodb-linux-x86_64-debian92-4.0.5.tgz

sudo mv mongodb-linux-x86_64-debian92-4.0.5 /srv/
sudo mkdir /srv/mongodb
sudo mkdir /srv/mongodb/data
sudo mkdir /srv/mongodb/log
sudo ln -s /srv/mongodb-linux-x86_64-debian92-4.0.5/bin /srv/mongodb/bin

mongodb setup

IP=`ip route get 1.2.3.4 | awk '{print $7}'`
sudo tee /srv/mongodb/local.conf <<EOF
systemLog:
    destination: file
    path: "/srv/mongodb/log/mongodb.log"

storage:
    dbPath: "/srv/mongodb/data"
    engine: "wiredTiger"

net:
    bindIp: "127.0.0.1,$IP"

processManagement:
    pidFilePath: "/srv/mongodb/mongod.lock"
EOF

create mongo OS user

sudo adduser --system --no-create-home --disabled-login --group mongo
sudo chown -v -R -f -L mongo:mongo /srv/mongodb

create mongo db user

sudo /usr/bin/chpst -u mongo /srv/mongodb/bin/mongod --fork -f /srv/mongodb/local.conf

/srv/mongodb/bin/mongo --host 127.0.0.1 --port 27017 <<EOF
conn = new Mongo('mongodb://localhost:27017');
db = conn.getDB("admin");
db.createUser(
  {
    user: "core",
    pwd: "654321",
    roles: [ { role: "root", db: "admin" } ]
  }
);
quit()
EOF

daemonize mongodb

sudo killall /srv/mongodb/bin/mongod

sudo mkdir /etc/sv/mongodb
sudo tee /etc/sv/mongodb/run <<EOF
#!/bin/sh

exec chpst -umongo /srv/mongodb/bin/mongod --config /srv/mongodb/local.conf --auth
EOF

sudo chmod 755 /etc/sv/mongodb/run
sudo ln -s /etc/sv/mongodb /etc/service/mongodb

test mongodb

sudo sv status mongodb
/srv/mongodb/bin/mongo --host $IP --port 27017 \
--username core --password 654321 --authenticationDatabase admin