Published on Dec 13 2022 in MongoDB NoSQL Directadmin

The plugin provides UI for managing local MongoDB NoSQL databases and users. Import and export are supported.

Features

See demo screencast below.

Prerequisites

As a prerequsite you should have MongoDB server installed. Below you will find MongoDB 5.10 installation manuals. Also check if your processor supports AVX otherwise you will get Invalid instruction from mongod.

lscpu | grep -q avx && echo OK || echo fail

Alternatively you may install an earlier version of MongoDB like 4.4.9. If you are using a KVM based server you may need to switch CPU to SandyBridge or an other one that will expose host’s AVX CPU instruction set.

CentOS/AlmaLinux/RockyLinux/CloudLinux 8

In CentOS family mongo user is mongod and it’s home directory is /var/lib/mongo.

Set up MongoDB repository for easy installation and future upgrades then install mongodb-org and start MongoDB server.

cat >/etc/yum.repos.d/mongodb-org-5.repo<<'EOF'
[MongoDB]
name=MongoDB Repository
baseurl=http://repo.mongodb.org/yum/redhat/$releasever/mongodb-org/5.0/$basearch/
gpgcheck=1
enabled=1
gpgkey=https://www.mongodb.org/static/pgp/server-5.0.asc
EOF

yum clean all && yum makecache && yum -y install mongodb-org

mongo -version | head -1 | sed 's/.* v//'
systemctl start mongod

Determine $MONGOUSER, $MONGOHOME and generate admin user password and create admin user with root role.

export MONGOHOME=`awk -F: '/dbPath:/{print $2}' /etc/mongod.conf | xargs`
export MONGOUSER=`ls -ld $MONGOHOME | awk '{print $3}'`
MONGOPASS=`openssl rand -base64 9 | tr '/' '_'` && export MONGOPASS=$MONGOPASS
echo "MONGOUSER=$MONGOUSER MONGOHOME=$MONGOHOME MONGOPASS=$MONGOPASS"
mongo --quiet admin --eval "db.createUser({user: 'admin', pwd: '$MONGOPASS', roles: [ 'root' ]})"

Update config file so that MongoDB server is available from remote locations and password is mandatory.

sed -i.bak -r 's/^#(security:)/\1\n  authorization: "enabled"/' /etc/mongod.conf
sed -i -r 's/(bindIp:) 127.0.0.1/\1 0.0.0.0/' /etc/mongod.conf
diff -Nu /etc/mongod.conf /etc/mongod.conf.bak
systemctl restart mongod

Now, run below for easy access to mongo shell as root (optional) and access by admin (mandatory for the plugin).

cat > /root/.mongorc.js <<EOF
db = connect("mongodb://admin:$MONGOPASS@localhost:27017/admin")
host = db.serverStatus().host;
prompt = function() { return db+"@"+host+"$ "; }
EOF

chmod 600 /root/.mongorc.js

cat > $MONGOHOME/.mongorc.js <<EOF
db = connect("mongodb://admin:$MONGOPASS@localhost:27017/admin")
EOF
chown $MONGOUSER: $MONGOHOME/.mongorc.js
chmod 600 $MONGOHOME/.mongorc.js

unset MONGOPASS MONGOHOME MONGOUSER

Test access from command line and enable MongoDB server autostart.

mongo --nodb --quiet
systemctl enable mongod

Debian 10

In Debian family mongo user is mongodb and it’s home directory is /var/lib/mongodb.

apt update && apt-get upgrade
apt-get install gnupg
wget -qO - https://www.mongodb.org/static/pgp/server-5.0.asc | sudo apt-key add -
echo "deb http://repo.mongodb.org/apt/debian buster/mongodb-org/5.0 main" | sudo tee /etc/apt/sources.list.d/mongodb-org-5.0.list
apt-get update
apt-get install -y mongodb-org

mongo -version | head -1 | sed 's/.* v//'

Then installation continues using the same commands as earlier in CentOS 8 section.

MongoDB 3.4.17 with AGPL licence on CentOS 8

If you need to use latest MongoDB version with AGPL license the plugin supports it too. For CentOS 7 with MongoDB 3.x you can just use respective repository for version 3 (follow CentOS 8 tutorial).

As CentOS 8 packages for 3.4.17 are not available on Mongo website we can use binary distibution instead.

cd /opt
wget https://fastdl.mongodb.org/linux/mongodb-linux-x86_64-3.4.17.tgz
tar xzf mongodb-linux-x86_64-3.4.17.tgz
mv mongodb-linux-x86_64-3.4.17 mongodb
cd /usr/bin
for f in /opt/mongodb/bin/*; do ln -s $f; done
rm -rf /var/lib/mongo
rm -rf /var/log/mongodb/*
mkdir -p /var/log/mongodb /var/lib/mongo /run/mongodb
chown mongod: /var/log/mongodb /var/lib/mongo
chmod 700 /var/log/mongodb /var/lib/mongo

Now we need to create mongod service systemd unit (we will use the one from RHEL 8) and config file.

cat >/etc/systemd/system<<'EOF'
[Unit]
Description=MongoDB Database Server
Documentation=https://docs.mongodb.org/manual
After=network-online.target
Wants=network-online.target

[Service]
User=mongod
Group=mongod
Environment="OPTIONS=-f /etc/mongod.conf"
EnvironmentFile=-/etc/sysconfig/mongod
ExecStart=/usr/bin/mongod $OPTIONS
ExecStartPre=/usr/bin/mkdir -p /var/run/mongodb
ExecStartPre=/usr/bin/chown mongod:mongod /var/run/mongodb
ExecStartPre=/usr/bin/chmod 0755 /var/run/mongodb
PermissionsStartOnly=true
PIDFile=/var/run/mongodb/mongod.pid
Type=forking
# 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

[Install]
WantedBy=multi-user.target
EOF


cat >/etc/mongod.conf<<'EOF'
# 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:
#  wiredTiger:

# how the process runs
processManagement:
  fork: true  # fork and run in background
  pidFilePath: /var/run/mongodb/mongod.pid  # location of pidfile

# network interfaces
net:
  port: 27017
  bindIp: 127.0.0.1 # Enter 0.0.0.0,:: to bind to all IPv4 and IPv6 addresses or, alternatively, use the net.bindIpAll setting.

#security:

#operationProfiling:

#replication:

#sharding:

## Enterprise-Only Options

#auditLog:

#snmp
EOF

mongo -version | head -1 | sed 's/.* v//'
systemctl start mongod

And then you proceed with the same instructions as in the CentOS 8 tutorial above.

Plugin installation

You will receive plugin download URL in your license email. Access Directadmin as admin and install the plugin using Extra Features - Plugin Manager.

Then (still as user admin) check plugin’s Config page to see if everything looks good (green). After you drop mongo.lic into plugin’s directory additional lines with licence info will appear.

Plugin demo

Notes

  1. To add your language, copy existing language in /usr/local/directadmin/plugins/mongo/lang to a new directory named according to your language and translate strings inside messages.po. To translate menu follow instructions in HOWTO file in lang directory.

  2. If you intend to allow your users to access MongoDB remotely, do not forget to open port 27017 in your firewall.

Feel free to discuss additional features below or by using the form on plugin’s Config page.