MongoDB throws "BadValue: invalid parameter" during global initialization when it cannot parse the requested locale (LANG / LC_*). Setting a generated UTF-8 locale before mongod/mongos starts and ensuring services inherit those values keeps the server from failing with error code 2.
The "BadValue: invalid parameter" error surfaces before MongoDB finishes global initialization. The server reads glibc locale settings (LANG and LC_* environment variables) and fails immediately once it sees that the requested locale is empty, missing, or not installed, which is reported as an invalid parameter (BadValue 2). Stack Overflow question 27375137 shows the same failure when LANG/LC_ALL are unset, and the MongoDB error-code reference confirms that BadValue is the catch-all thrown whenever MongoDB receives a disallowed startup parameter such as an invalid locale string.
Set LANG and LC_ALL in the shell right before starting mongod, mongos, or the mongo shell so the daemon never sees a blank locale parameter:
export LANG="en_US.UTF-8"
export LC_ALL="en_US.UTF-8"
env | grep -E "^(LANG|LC_)"
mongod --config /etc/mongod.confPersist the exports by appending them to ~/.bashrc, ~/.profile, or your CI manifest so every invocation of the binary inherits the locale. Replace en_US.UTF-8 with another available locale if you are targeting a different language.
Missing locale definitions make any LANG value appear invalid. On Debian/Ubuntu:
sudo apt-get install -y locales
sudo locale-gen en_US.UTF-8
sudo update-locale LANG=en_US.UTF-8 LC_ALL=en_US.UTF-8
locale -a | grep -i en_USOn RHEL/CentOS:
sudo yum install -y glibc-langpack-en
sudo localedef -i en_US -f UTF-8 en_US.UTF-8
localectl list-locales | grep -i en_USAfter generating the locale, restart the shell or service so the new definitions become available to MongoDB at startup.
systemd and container runtimes start mongod with a sanitized environment, so even if you export LANG in your login shell, the service may still see an empty parameter. Create a systemd drop-in to keep the locale available:
sudo mkdir -p /etc/systemd/system/mongod.service.d
cat <<'EOF' | sudo tee /etc/systemd/system/mongod.service.d/locale.conf
[Service]
Environment="LANG=en_US.UTF-8"
Environment="LC_ALL=en_US.UTF-8"
EOF
sudo systemctl daemon-reload
sudo systemctl restart mongod
systemctl show mongod -p EnvironmentFor Docker Compose or docker run, pass the same variables explicitly:
environment:
LANG: en_US.UTF-8
LC_ALL: en_US.UTF-8This ensures every container or service process starts with the valid locale parameter that MongoDB expects.
Refer to Stack Overflow question 27375137 and the Medium write-up about MongoDB on CentOS for the same startup failure and resolution. Use localectl list-locales or locale -a to confirm the desired locale is installed, and run mongod --config /etc/mongod.conf --configCheck if BadValue persists—invalid entries in mongod.conf or incorrect file permissions can also surface as BadValue. For non-systemd runtimes, make sure your shell/environment scripts source the exports before launching mongod so the parameter is never empty.
DivergentArrayError: For your own good, using document.save() to update an array which was selected using an $elemMatch projection will not work
How to fix "DivergentArrayError: For your own good, using document.save() to update an array which was selected using an $elemMatch projection will not work" in MongoDB
MongoServerError: bad auth : authentication failed
How to fix "MongoServerError: bad auth : authentication failed" in MongoDB
CannotCreateIndex: Cannot create index
CannotCreateIndex: Cannot create index
StaleShardVersion: shard version mismatch
How to fix "StaleShardVersion: shard version mismatch" in MongoDB
MongoOperationTimeoutError: Operation timed out
How to fix "MongoOperationTimeoutError: Operation timed out" in MongoDB