MongoDB服务无法启动求助:执行指定命令报错(macOS High Sierra)
Hey there, let's break down why you're hitting this issue and fix it step by step. The Error reading config file: No such file or directory message points to a straightforward problem, but there are a few common culprits on your macOS High Sierra system:
1. The Config File Path You're Using Doesn't Exist
First off, let's confirm if the file /usr/local/mongodb/mongod.conf actually exists. Run this command to check:
ls /usr/local/mongodb/
If mongod.conf isn't listed here, that's the root cause—MongoDB doesn't create this file automatically unless you set it up during installation. Here's how to fix this:
a. Create Required Directories
MongoDB needs dedicated folders for data storage and logs. Make those first:
sudo mkdir -p /usr/local/mongodb/data /usr/local/mongodb/logs sudo chown -R $(whoami) /usr/local/mongodb/
The chown command ensures your user account has write permissions for these folders, which prevents future permission-related errors.
b. Create the Config File
Use a text editor like nano or vim to create the mongod.conf file:
nano /usr/local/mongodb/mongod.conf
Paste in this basic, functional configuration (adjust paths if you prefer different locations):
storage: dbPath: /usr/local/mongodb/data journal: enabled: true net: port: 27017 bindIp: 127.0.0.1 systemLog: destination: file path: /usr/local/mongodb/logs/mongod.log logAppend: true processManagement: fork: true
Save and exit (for nano: press Ctrl+O, hit Enter, then Ctrl+X).
2. You're Using the Wrong Config File Path (Homebrew Installation)
If you installed MongoDB via Homebrew (the most common method on macOS), the default config file isn't at /usr/local/mongodb/mongod.conf. It's typically located at /usr/local/etc/mongod.conf.
To confirm, run:
brew info mongodb
Look for a line like "Config file: /usr/local/etc/mongod.conf" in the output. If that's the case, use this command to start MongoDB instead:
sudo mongod --config /usr/local/etc/mongod.conf
Or use Homebrew's service manager for easier startup:
brew services start mongodb
(Note: For newer MongoDB versions, the package might be named mongodb-community—adjust the command accordingly.)
3. Typos or Path Mistakes
Even small typos (like extra slashes, misspelled directory names) can trigger this error. Double-check that you're typing /usr/local/mongodb/mongod.conf correctly—while macOS is case-insensitive, it's easy to mistype "mongodb" as "MongoDB" or mix up directory names.
Verify the Fix
After sorting out the config file path, run your original startup command again. Once it executes without errors, confirm MongoDB is running with:
ps aux | grep mongod
You should see a mongod process listed. You can also test connecting to the database with:
mongo
If you get the MongoDB shell prompt, you're all set!
内容的提问来源于stack exchange,提问作者Steven Aguilar

