-
breif intro:
MongoDB cannot be started as a service (with an error:mongod: unrecognized service
) but can be started manually withsudo mongod
-
Expected results:
Mongodb starts as a service. -
Actual results (with terminal output if applicable)
sudo service mongod start
mongod: unrecognized service
-
Your Windows build number
15058 -
Steps / All commands required to reproduce the error from a brand new installation
— Install mongodb following the official ubuntu guide
— runsudo service mongod start
-
Strace of the failing command
sudo service mongod start
-
Required packages and commands to install
sudo apt-key adv --keyserver hkp://keyserver.ubuntu.com:80 --recv 0C49F3730359A14518585931BC711F9BA15703C6
sudo echo "deb [ arch=amd64,arm64 ] http://repo.mongodb.org/apt/ubuntu xenial/mongodb-org/3.4 multiverse" | sudo tee /etc/apt/sources.list.d/mongodb-org-3.4.list
sudo apt-get update
sudo apt-get install -y mongodb-org
sudo service mongod start
Published September 23, 2024
Problem: MongoDB Service Failing to Start
The «MongoDB Service Won’t Start» error stops the database from working correctly. This problem often happens when there are issues with system settings, file permissions, or service configurations, causing MongoDB to fail when it tries to start.
Diagnosing the Root Cause
Checking MongoDB Log Files
MongoDB log files are usually in the /var/log/mongodb
or /var/log/mongo
directory. These files have information about the database’s operations and errors.
Look for these error messages:
- «Unclean shutdown detected»
- «Old lock file»
- «Permission denied»
- «Couldn’t open file»
These messages often show the causes of service start failures. For example, «Unclean shutdown detected» means MongoDB didn’t shut down properly before.
Tip: Tail the Log File
To monitor the MongoDB log file in real-time, use the ‘tail’ command:
tail -f /var/log/mongodb/mongod.log
This allows you to see new log entries as they are added, which can be helpful when troubleshooting startup issues.
Examining System Permissions
Correct file ownership and permissions are important for MongoDB to work. The database files and directories should belong to the MongoDB user and group.
To check permissions, use the ls -l
command in the MongoDB data directory (often /var/lib/mongo
). The output should show that the MongoDB user and group own the files.
Common permission issues include:
- Files owned by the root user instead of MongoDB
- Wrong read/write permissions on database files
- No execute permissions on directories
These problems can stop MongoDB from accessing files and directories, causing service start failures.
Step-by-Step Solution to Fix MongoDB Service
Repairing the MongoDB Database
To repair the MongoDB database, use the mongod
command with the --repair
option:
mongod --repair --dbpath /var/lib/mongo
This process may take time, based on your database size. Back up your data before starting, as repairing can cause data loss in rare cases.
The repair aims to fix data issues and remove corrupt data. After completion, you’ll see a message indicating a successful repair.
Tip: Monitor Repair Progress
For large databases, monitor the repair progress by checking the MongoDB log file:
tail -f /var/log/mongodb/mongod.log
This allows you to track the repair status and estimate completion time.
Correcting File Ownership and Permissions
Identify the correct owner for MongoDB files, usually the ‘mongod’ user and group. Change file ownership with the chown
command:
sudo chown -R mongod:mongod /var/lib/mongo
This changes the owner and group of all files and directories in /var/lib/mongo
to ‘mongod’.
Check the changes:
ls -l /var/lib/mongo
The output should show ‘mongod’ as the owner and group for all items.
Restarting the MongoDB Service
Restart MongoDB after applying fixes:
sudo systemctl restart mongod
Monitor the restart by checking the MongoDB log file:
tail -f /var/log/mongodb/mongod.log
Look for messages indicating a successful start, like «waiting for connections» or «MongoDB starting».
To confirm the service is running:
sudo systemctl status mongod
This should show the service as «active (running)».
Скачал mongoDB для win в zip
Распоковал в D:\mongodb
Запускаю через консоль с правами админа. Сначала mongod, затем mongo и получаю ошибку:
Текст ошибки из консоли
MongoDB shell version v4.4.3
connecting to: mongodb://127.0.0.1:27017/?compressors=disabled&gs…
Error: couldn’t connect to server 127.0.0.1:27017, connection attempt failed: SocketException: Error connecting to 127.0.0.1:27017 :: caused by :: ����������� �� �����������, �.�. �������� ��������� ������ ������ �� �����������. :
connect@src/mongo/shell/mongo.js:374:17
@(connect):2:6
exception: connect failed
exiting with code 1
Смотрел через netstat -a порты, порт не занят.
При этом брандмаур windows ничего не спрашивает. Сразу вываливается ошибка. Папку для бд создал в C:\data\db
Собственно в чём проблема и может кто знает, что сделать, что бы увидеть не ����, а человеческие буквы в строке 127.0.0.1:27017 :: caused by :: ?
-
Вопрос задан
-
445 просмотров
Пригласить эксперта
Ошибку выдает клиент.
А что на сервере?
Он точно запущен?
Есть лог, со строкой что сервер запущен и listening?
Тем более, что ты говоришь что порт не занят согласно netstat.
Проверь конфиг сервера — точно ли он слушает на 27017 и точно ли слушает 127.0.0.1 а не какой-нить ipv6 под дефолту
Войдите, чтобы написать ответ
-
Показать ещё
Загружается…
Минуточку внимания
Published April 1, 2020 by
While setting up MongoDB for windows using the MSI Installer, I ran into an interesting error on attempt to start the MongoDB Server Windows Service.
To troubleshoot the problem, I went to the service entry in Services.msc and reviewed the startup parameters for the mongod.exe executable.
Looking at the Path to Executable section, I was able to
copy the command used to start the service.
“C:\Program
Files\MongoDB\Server\4.2\bin\mongod.exe” –config “C:\Program Files\MongoDB\Server\4.2\bin\mongod.cfg”
–service
Running this in a Windows command prompt shell gave me a good enough hint to continue my troubleshooting:
Since mongod.exe is reading its startup parameters from mongod.cfg, this “unrecognized” option must be somewhere in that configuration file.
And naturally enough, we found the culprit! There was an option “mp” at the end of the config file. So, I went ahead and commented out by adding a leading hash (#).
Problem Solved!
This website uses cookies to improve your experience. We’ll assume you’re ok with this, but you can opt-out if you wish. Cookie settingsACCEPT