Sometimes you may need to change the port of mongod instance running in your system. The default port is 27017 and it is used by default if you are running mongod. This port is used by both mongod and mongos instance.
But, for security reasons or for any other reason, you may want to change the port to something else! This is actually easier than you think. You can change this port to any other number (available port) in your local system or in your server if you are hosting MongoDB without using any third party services.
This option is useful if you want to run MongoDB directly with the terminal. Normally, for local development, we use terminal to run the mongod service.
Open one terminal, enter the below command and hit enter :
If you have done the setup correctly in your system, then it will start the mongod service with this command. You will see a list of logs on your terminal. Check the last line, it should be something like below :
I NETWORK [initandlisten] waiting for connections on port 27017
i.e. it is running on port 27017. Our goal is to change it to another port. Write the below command on a terminal and hit enter :
mongod --port 27080
It will start mongod on port 27080. Before using this port, make sure that it is available. You can use Ctrl + C to stop the process.
The above process is useful for development i.e. if you don’t want to run mongod as a service. But for production level deployments, you can use one configuration file to add all settings you want to add to mongod.
By default, one configuration file is added in the system whenever you install MongoDB. The location of this file is different for different operating systems.
- On Linux, you will find it on /etc/mongod.conf, if you are installing MongoDB using any package manager.
- On Mac, it is /usr/local/etc/mongod.conf, if you have installed MongoDB from its official website.
- On windows, it in inside /bin/mongod.cfg in the MongoDB installation directory.
So, based on your operating system, you can find out where the default configuration file is placed.
But, the best part is that we can also use a different configuration file instead of editing the main system file. Create one folder with the below two files :
The first .conf file is for entering the configuration details and the .log file is for capturing the logs. Open the mongod.conf file and put the below code :
systemLog: destination: file path: "mongolog.log" logAppend: true storage: journal: enabled: true processManagement: fork: true net: bindIp: 127.0.0.1 port: 27021 setParameter: enableLocalhostAuthBypass: false
This is a basic configuration file for running a mongod instance. Note that we have added the port number as 27021. Run the below command to use this configuration file :
mongod --config mongod.conf
It will start mongod as a service on 27021 port with the settings we have defined in the configuration file. You will see the logs inside mongolog.log.
That’s it. If you want to stop it, you need to stop the process. You can do this by finding out the process id.
Try to run mongod as a process on your system and drop one comment below if you have any queries.