MongoDB

Installation

Aus den MongoDB-Repos, hier am Beispiel von Mongo 5.0:

cat > /etc/yum.repos.d/mongodb-org.repo << 'EOF'
[mongodb-org-5.0]
name=MongoDB Repository
baseurl=https://repo.mongodb.org/yum/redhat/$releasever/mongodb-org/5.0/x86_64/
gpgcheck=1
enabled=1
gpgkey=https://www.mongodb.org/static/pgp/server-5.0.asc
EOF
dnf -y install mongodb-org-server
systemctl enable --now mongod

Im Kernel „Transparent Huge Pages“ deaktivieren:

mkdir /etc/tuned/kernel_settings
cat > /etc/tuned/kernel_settings/tuned.conf << 'EOF'
[main]
include=virtual-guest

[vm]
transparent_hugepages=madvise
EOF
tuned-adm profile virtual-guest kernel_settings

Mongo-Shell

Installation:

dnf -y install mongodb-org-shell

Verwendung:

# connect to localhost:
mongo

# connect to mongodb0.example.com:28015
mongo "mongodb://mongodb0.example.com:28015"

Backup und Restore

Tipp

LFOps bietet eine MongoDB-Ansible-Rolle, die ein MongoDB-Backup-Script gleich mitliefert.

dnf -y install mongodb-org-tools

Backup Single Instance:

mongodump --out /backup/mongodb-dump

Backup eines Replica-Sets - hier müssen alle Member angegeben werden:

mongodump --host="myReplicaSetName/mongodb0.example.com:27017,mongodb1.example.com:27017,mongodb2.example.com" [additional options]

Bash-Script für MongoDB-Dump (Binary BSON Dumps) mit diversen Optionen:

HOST='127.0.0.1'
PORT='27017'
BACKUP_DIR='/backup/mongodb-dump'
LOGFILE='/tmp/mongodb-dump-log'

# detect paths
MONGO=$(which mongo)
MONGODUMP=$(which mongodump)

# make sure we can connect to server
$MONGO --host $HOST --port $PORT --eval 'db.getUsers()' &>/dev/null

if [ $? -ne 0 ]
then
    echo "Cannot connect to '$HOST'" >&2
    exit 1
fi

# remove old backups
rm -rf $BACKUP_DIR/
mkdir -p $BACKUP_DIR

$MONGODUMP --host $HOST --port $PORT --oplog --out "$BACKUP_DIR/temp" > $LOGFILE 2>&1
if [ $? -ne 0 ]; then
    cat $LOGFILE >&2
fi
rm $LOGFILE

cd "$BACKUP_DIR" 1> /dev/null
tar -cvzf "mongodb-dump.tar.gz" -C "$BACKUP_DIR/temp/" . 1> /dev/null
rm -rf "$BACKUP_DIR/temp/" 1> /dev/null

Restore:

mongorestore /backup/mongodb-dump
mongorestore --host $HOST --port $PORT /backup/mongodb-dump

# to drop each collection from the database before restoring from backups:
mongorestore --drop /backup/mongodb-dump

Upgrading MongoDB Community Edition, Single Instance

Ist ein funktionierendes Backup vorhanden? Nur dann sollte ein Upgrade durchgeführt werden.

Installierte Version ermitteln:

mongod --version

Vorbereitungen treffen:

dnf -y install yum-utils

Upgrade von MongoDB 4.0 auf MongoDB 4.2

cat > /etc/yum.repos.d/mongodb-org.repo << 'EOF'
[mongodb-org-4.2]
name=MongoDB Repository
baseurl=https://repo.mongodb.org/yum/redhat/$releasever/mongodb-org/4.2/x86_64/
gpgcheck=1
enabled=1
gpgkey=https://www.mongodb.org/static/pgp/server-4.2.asc
EOF
systemctl stop mongod
yum-config-manager --disable mongodb-org-4.0
dnf -y update mongodb-org-*
# MongoDB starts automatically

mongod --version

Upgrade von MongoDB 4.2 auf MongoDB 4.4+

Funktioniert ohne Anpassung nicht, da es Probleme mit der Storage-Engine WiredTiger gibt (Failed to start up WiredTiger under any compatibility version. This may be due to an unsupported upgrade or downgrade. und Fatal Assertion 28595 at src/mongo/db/storage/wiredtiger/wiredtiger_kv_engine.cpp). Um das Problem zu lösen, VORHER dieses Setting setzen (siehe auch https://docs.mongodb.com/v4.4/reference/command/setFeatureCompatibilityVersion/) - und erst danach das Upgrade durchführen.

# start mongo shell
mongo
db.adminCommand( { getParameter: 1, featureCompatibilityVersion: 1 } )
db.adminCommand( { setFeatureCompatibilityVersion: "4.2" } )
exit

Erst jetzt:

cat > /etc/yum.repos.d/mongodb-org.repo << 'EOF'
[mongodb-org-4.4]
name=MongoDB Repository
baseurl=https://repo.mongodb.org/yum/redhat/$releasever/mongodb-org/4.4/x86_64/
gpgcheck=1
enabled=1
gpgkey=https://www.mongodb.org/static/pgp/server-4.4.asc
EOF
systemctl stop mongod
yum-config-manager --disable mongodb-org-4.2
dnf -y update mongodb-org-*
# MongoDB starts automatically

mongod --version

Upgrade von MongoDB 4.4 auf MongoDB 5.0+

cat > /etc/yum.repos.d/mongodb-org.repo << 'EOF'
[mongodb-org-5.0]
name=MongoDB Repository
baseurl=https://repo.mongodb.org/yum/redhat/$releasever/mongodb-org/5.0/x86_64/
gpgcheck=1
enabled=1
gpgkey=https://www.mongodb.org/static/pgp/server-5.0.asc
EOF
systemctl stop mongod
yum-config-manager --disable mongodb-org-4.4
dnf -y update mongodb-org-*
# MongoDB starts automatically

mongod --version

Siehe auch https://docs.mongodb.com/manual/tutorial/upgrade-revision/

Built on 2024-04-18