🛠️Configuring and setting up MongoDB servers

How to setup and configure your own personal MongoDB.

MongoDB should work straight away out of the box. As soon as your server is configured, it should automatically be setup by defaults and easily bootable straight away. However, we'd recommend a few adjustments for both security & personal purposes.

Setup security (authentication), and configure server port

By default, your server can be easily logged into without a user or password. This is a massive security flaw and needs to be fixed right away.

So, go onto your server > File Manager, and find the mongod conf file. Find the security section as shown below and change it so it says the following.

security:
    authorization: "enabled"

This enables security on your MongoDB server.

Then find the following and change port to the port assigned to your server. In my case, my port is 17525.

net:
  port: 17525
  bindIp: 127.0.0.1

This allows your server to actually be connectable. Leave 127.0.0.1 as it is.

Then, save the file and restart your server to apply the changes.

Creating a database & MongoDB user

Because authentication has just been enabled, you may want to create your own MongoDB user to login to your own database. Depending on your use-case, you may want to create a user bound to a specific container, or a specific area of your MongoDB database. Of course, I'll leave you to it to do this research yourself, if you want to create an admin user for all containers.

Below we're going to create a container and create a user to that container.

In the console, type the following commands.

use data

This sets the container we're going to do stuff on. We've called ours data. If you haven't created one yet or don't know how to create one, use creates one if it already doesn't exist!

db.createUser({
  user: "janeberru",
  pwd: "o2v2hvZzhHqSG3pLV7VJ",
  roles: [
    { role: "readWrite", db: "data" }
  ]
})

Made a fatal mistake? Need to delete the user? No problem.

Do "use [database name]" in the console, and then straight away type:

db.dropUser("myuser")

So for me specifically I'd write db.dropUser('janeberru') to delete the user 'janeberru'.

It should return 'true' if successful, and 'false' if perhaps the user wasn't found.

This creates a user called 'janeberru' with password 'o2v2hvZzhHqSG3pLV7VJ' tied to database 'data' (database/container we just made). 'janeberru' will only be allowed the "readWrite" permissions, which specifically allow only read/write to only this database only.

So if any other databases are created, janeberru won't have access to it. If you want to make a user with different roles or perhaps a user with administrator access (access to all databases, all permissions), then I'd encourage you to do your own research.

Other than that, that's how you setup your MongoDB database.

Adding a user or making a new database doesn't require a server restart, so you should be able to connect right away.

Connecting to your MongoDB database

For this, testing the database can be a nightmare if your not fully confident with using MongoDB CLI or via a programming language module right away. If you want to fiddle with it, I'd recommend installing a program called 'MongoDB Compass' which we'll be using now to demonstrate how to login to your database, fiddle around, and see data be updated in real-time, with an interactive UI.

Open MongoDB Compass and expand the 'Advanced Connection Options'. Enter your 'Host' as specified in the Game Panel. For us, we've entered the host and port.

Then, go onto the 'Authentication' tab and put in the details we created earlier.

Username: Username we created earlier
Password: Password we created earlier
Authentication Database: Database that we did 'use [database name]' earlier
Authentication Mechanism: Default

For us, it'd be the following as per mentioned above.

That's all that needs changing. If you scroll up, you should see that the 'URI' has got much bigger compared to earlier. That's because entering those details automatically creates the URI for you, which can be used if your programming, or in MC development for example.

Hit the 'Connect' button and straight away, you should be connected and see just the database that you gave it access to. In my case, it can only connect to the database 'data'.

If you have access to the 'admin', 'local' and other databases then something is definitely wrong with your authentication. Try logging in without a user (put 'None' for Authentication to test it). If you can login without authentication (on it being 'None') and the "admin", and other databases come up, and you can create databases, stop your server immediately and try editing your config file, & testing it until authentication works.

If you still can't get authentication to work, contact us and we'll edit your startup command so authentication is enabled instead of via your config file. This will enable authentication permanently, no matter what is done via the config file, so if you ever want to turn it off, you have to contact us to do that also. We'll definitely do this for you and the limits are just whenever we're available to help!

For us, we had to add the --auth argument in the startup command because the configuration file was not working correctly.

We gave our user 'readWrite' permissions, exclusively only to access the 'data' database. If you try hitting the '+' icon in MongoDB Compass and try creating another database, you should get an error saying that the user doesn't have permissions to do this action.

If a database gets created and you didn't give the user this role, please make sure to check the security as per mentioned in our last prompt and contact us if you feel your database has authentication disabled if you want it enabled and you can't do anything to enable it.

That's how you use MongoDB with SkyVillage! You can also use your database in Discord Bots, Minecraft Servers, etc, etc. If you want to make a user with more privileges, I'd suggest doing some additional research and looking at the MongoDB Docs, looking at what roles you can apply to users. On top of that, there are some useful community-asked questions via StackOverflow that has some commands that people have already written which can be used.

Last updated