Before we start MongoDB for the first time, we need to create a directory in which the mongod process will write the data:
- By default, the mongod process uses the /data/db directory. To create this folder, you can use the following command:
mkdir -p /data/db
- Now we need to set permissions for the data directory:
chmod -R 777 /data
- In a new terminal (or tab) you need to run the following:
mongod
- If you didn't get an error, you could start the Mongo shell on the same host machine as mongod (in a new terminal or tab):
mongo --host 127.0.0.1:127017
If you get an error like this: Error: Port number 127017 out of range parsing HostAndPort from "127.0.0.1:127017", then just run mongo without --host flag.
- Finally, if you want to stop MongoDB, press Ctrl + C in the terminal that mongod is running.
- If everything works, you should see this in your Terminal: