Skip to content

Instantly share code, notes, and snippets.

@spelcaster
Last active April 6, 2020 02:08
Show Gist options
  • Save spelcaster/0a239008932d13874a7a9f9eee392aa0 to your computer and use it in GitHub Desktop.
Save spelcaster/0a239008932d13874a7a9f9eee392aa0 to your computer and use it in GitHub Desktop.
Scripts to configure a mongodb environment
storage:
dbPath: /var/lib/mongodb
repairPath: /var/lib/mongodb/.repair
directoryPerDB: true
journal:
enabled: true
engine: "mmapv1"
mmapv1:
smallFiles: true
systemLog:
destination: file
path: /var/log/mongodb/mongod.log
logAppend: true
timeStampFormat: iso8601-utc
processManagement:
fork: true
pidFilePath: /run/mongod/mongod.pid
net:
bindIp: 127.0.0.1
port: 27017
unixDomainSocket:
enabled : true
security:
authorization: disabled
setParameter:
enableLocalhostAuthBypass: false
storage:
dbPath: /var/lib/mongodb
directoryPerDB: true
journal:
enabled: true
engine: "wiredTiger"
wiredTiger:
engineConfig:
#cacheSizeGB: 0
journalCompressor: zlib
collectionConfig:
blockCompressor: zlib
indexConfig:
prefixCompression: true
systemLog:
destination: file
path: /var/log/mongodb/mongod.log
logAppend: true
timeStampFormat: iso8601-utc
processManagement:
fork: true
pidFilePath: /run/mongod/mongod.pid
net:
bindIp: 127.0.0.1
port: 27017
unixDomainSocket:
enabled : true
security:
authorization: disabled
setParameter:
enableLocalhostAuthBypass: false
{
"username": ":username:",
"password": ":password:",
"host": ":host:",
"port": ":port:",
"uri": ":uri:"
}
#!/bin/bash
function setup_mongo_resource_limits() {
local bytes=1073741824
# we're using 70% of the system available memory
local mongo_usage=$(echo "(70 / 100.0)" | bc -l)
local total_mem=$(cat /proc/meminfo| grep -i memtotal | awk '{print $2}')
local container_memory=$(echo "(${total_mem} / (1024 * 1024)) * ${mongo_usage}" | bc -l)
local cache_memory=$(printf "%.0f" ${container_memory})
# 50% of the container memory - 1GB
cache_memory=$(echo "((${cache_memory} * 0.5) - 1) / 1" | bc)
# normalize the mongo service and configuration file
sed -i '/\(MemoryAccounting\|MemoryLimit\)/d' /lib/systemd/system/mongod.service
sed -i "s:\\(#\)\(cacheSizeGB\:\):\2:" /etc/mongod.conf
# set the cacheSizeGB
sed -i "s:\(cacheSizeGB\:\).*$:\1 ${cache_memory}:" /etc/mongod.conf
if [[ $(echo "${cache_memory} > 0" | bc) -eq 0 ]]
then
# comment the cacheSizeGB
sed -i "s:\(cacheSizeGB\:.*$\):#\1:" /etc/mongod.conf
fi
container_memory=$(echo "${container_memory} * ${bytes}" | bc)
container_memory=${container_memory%.*}
# set container memory
sed -i "/LimitNPROC=64000/ a\\
MemoryAccounting=yes\n\
MemoryLimit=${container_memory}" /lib/systemd/system/mongod.service
}
setup_mongo_resource_limits
systemctl daemon-reload
systemctl restart mongod.service
#!/bin/bash
if [ \( -n "$1" \) -a \( "$1" = "-h" -o "$1" = "--help" \) ]; then
echo "Argument List:"
echo "username (\$1) - The username to connect to the database (default: \"\")"
echo "password (\$2) - The password used for the user (default: \"\")"
echo "host (\$3) - The host where the database is running (default: 127.0.0.1)"
echo "port (\$4) - The port that the database is listening (default: 27017)"
echo ""
exit 0
fi
function enable_mongodb_auth() {
sed -i "s:\(authorization\:\).*$:\1 enabled:" /etc/mongod.conf
}
function disable_mongodb_auth() {
sed -i "s:\(authorization\:\).*$:\1 disabled:" /etc/mongod.conf
}
function setup_mongo_credentials() {
local credentials_file=/tmp/credentials.json
sed -i "s|\(\"username\"\:\)\( \".*\"\)|\1 \"${USERNAME}\"|g" ${credentials_file}
sed -i "s|\(\"password\"\:\)\( \".*\"\)|\1 \"${PASSWORD}\"|g" ${credentials_file}
sed -i "s|\(\"host\"\:\)\( \".*\"\)|\1 \"${HOST}\"|g" ${credentials_file}
sed -i "s|\(\"port\"\:\)\( \".*\"\)|\1 \"${PORT}\"|g" ${credentials_file}
sed -i "s|\(\"uri\"\:\)\( \".*\"\)|\1 \"${URI}\"|g" ${credentials_file}
}
USERNAME="${1-}"
PASSWORD="${2-}"
HOST="${3-127.0.0.1}"
PORT="${4-27017}"
URI="mongodb://"
disable_mongodb_auth
if [ ! -z "${USERNAME}" ]; then
enable_mongodb_auth
URI="${URI}${USERNAME}:${PASSWORD}@"
fi
URI="${URI}${HOST}:${PORT}/"
setup_mongo_credentials
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment