How to Change MongoDB Compass Connection Port


Change MongoDB Compass Port

Method 1: Changing Port Using Command Line

  1. Set Environment Variables
    • Click on "Edit the system environment variables" from the Start menu.
    • Click on "Environment Variables".
    • Click "New" to add a new system variable.
    • Set Variable name to MONGO_PORT and Variable value to your desired port number, e.g., 27018.
    • Click "OK" and close the window.
  2. Change Port Using MongoDB Command
    • Type "cmd" in the Start menu and open Command Prompt.
    • Enter the following command to start MongoDB on the new port:
    • mongod --port %MONGO_PORT%
  3. Test in MongoDB Compass
    • Open MongoDB Compass and try connecting with the new port number. If you encounter errors or cannot connect, proceed to the second method.

Method 2: Changing Port Using Config File

  1. Edit Config File
    • Locate the mongod.cfg file in the directory where MongoDB is installed and open it with a text editor.
    • Find the net section in the file and adjust the port number as follows:
    • net:
        port: 27018
  2. Restart Your Computer
    • Save the changes and close the config file.
    • To restart the MongoDB service, either restart your computer or use the following commands in Command Prompt:
    • net stop MongoDB
      net start MongoDB
  3. Test in MongoDB Compass
    • Open MongoDB Compass and try connecting with the new port number. You should now be able to establish a connection.

Comments