This tutorial will show you how to install a custom MongoDB instance. To do this, you need to use the WebApps platform.

If you do not have a WebApps section in your hosting Control Panel, then this tutorial is not suitable for your particular hosting environment. You can contact us for more information and assistance.

“SSH Access” and “Network Tools” must be enabled for your account in the “SSH Access” section of the Control Panel.

 Installing a custom MongoDB instance

1) Connect to your account via SSH.
2) Navigate to the location from which MongoDB will run. In this example, we use /home/myusername/.local/bin.
myusername@s501:/home/myusername$ cd .local/bin3) Download the binary files for the desired release of MongoDB. You can download the binaries from https://www.mongodb.com/download-center/community. You need to select the version, choose "Debian 10.0" as Platform, choose "shell (tgz)" as package, and click the "Copy link" button to copy the download link. In this example, the download link is "https://fastdl.mongodb.org/linux/mongodb-shell-linux-x86_64-debian10-5.0.3.tgz". Then, download the file to the server with curl by running:
myusername@s501:/home/myusername/.local/bin$ curl -O https://fastdl.mongodb.org/linux/the_name_of_the_current_package.tgz
4) Extract the files from the downloaded archive with tar. For example, through the shell, you can extract with a tar command:
myusername@s501:/home/myusername/.local/bin$ tar -zxvf the_name_of_the_current_package.tgz5) Copy the extracted binaries to the location from which MongoDB will run (e.g. /home/myusername/.local/bin).
myusername@s501:/home/myusername/.local/bin$ cp -R -n the_name_of_the_current_package/bin/* .

Attention

At the time of writing, the current package name at the mongoDB download page is mongodb-linux-x86_64-debian11-7.0.12.tgz, so the commands that need to be run are as follows::

cd ~/local/bin
curl -O https://fastdl.mongodb.org/linux/mongodb-linux-x86_64-debian11-7.0.12.tgz
tar -zxvf mongodb-linux-x86_64-debian11-7.0.12.tgz
cp -R -n mongodb-linux-x86_64-debian11-7.0.12/bin/* .

 

6) Ensure the location of the binaries is in the PATH variable. You should have the following line in your shell's rc file (/home/myusername/.bashrc):
export PATH=$HOME/.local/bin:$PATHIf you want to run the MongoDB binaries from a different location, make sure to add that location to the PATH variable:

export PATH=<mongodb-install-directory>/bin:$PATH

7) Create the deployment directory for the web app that will run your custom MongoDB instance, e.g. /home/myusername/private/mongodb. In the deployment directory, you should create the database data directory, e.g. /home/myusername/private/mongodb/data/db.


8) In the WebApps section of the hosting Control Panel, create a new app. Our example app is named MongoDB.

  • Engine: Custom
  • Name: MongoDB
  • Subdomain: www.mydomain.com
  • Web access path: /mongodb
  • Port: <assigned automatically>
  • Memory: Account default value
  • Deployment directory: /private/mongodb
  • Start Command: mongod --dbpath /home/myusername/private/mongodb/data/db --port $PORT

9) Save the changes. After you enable the app by clicking on the Enable button, refresh the page until the state becomes OK.

3. Enabling authentication

Enabling access control on a MongoDB deployment enforces authentication, requiring users to identify themselves. When accessing a MongoDB deployment that has access control enabled, users can only perform actions as determined by their roles. The following procedure first adds a user administrator to a MongoDB instance running without access control and then enables access control.

1) Through your shell, connect to the MongoDB instance.

mongosh --port $PORT

Note: The mongosh shell binary is the replacement of the mongo shell binary which has been deprecated since MongoDB v5.0.

2) Create the user administrator.

use admin
db.createUser(
{
user: "myUserAdmin",
pwd: "abc123",
roles: [ { role: "userAdminAnyDatabase", db: "admin" } ]
}
)

3) Disconnect the mongo shell.


4) Restart the MongoDB instance with access control. To do this, go to the WebApps section of the hosting Control Panel and click on the green circle to stop the app. Then update the start command of the MongoDB app by adding the "auth" parameter:

/home/myusername/.local/bin/mongod --auth --dbpath /home/myusername/private/mongodb/data/db --port $PORT

5) Click the Update button to save the changes, and then click on the red circle to restart the custom MongoDB instance with access control.

4. Connecting to the database

The standard format of the MongoDB URI connection scheme looks like this:

mongodb://[username:password@]host1[:port1][,...hostN[:portN]][/[database][?options]]

By following this example, you could access the database created in this tutorial with the following connection string:

mongodb://myUserAdmin:abc123@localhost:36375/admin