DEV Community

Daniil Bazhenov
Daniil Bazhenov

Posted on

Installing Percona Server for MongoDB manually on Ubuntu on AWS EC2

A step-by-step guide to install a free open-source alternative to MongoDB from Percona, a company focused on developing database solutions.

The guide uses the AWS t2.micro instance with Ubuntu 22.04, it has small resources but you can get it for free for a long time and that is enough for experimentation and even a small project. The instructions will work on any other server, if you have a different operating system, you can see the options in the documentation.

Suppose we already have a server and you connect to it from the console.

1. We need to follow the procedures

the official documentation

wget https://repo.percona.com/apt/percona-release_latest.$(lsb_release -sc)_all.deb`
Enter fullscreen mode Exit fullscreen mode
sudo dpkg -i percona-release_latest.$(lsb_release -sc)_all.deb
Enter fullscreen mode Exit fullscreen mode
sudo percona-release enable psmdb-60 release
Enter fullscreen mode Exit fullscreen mode
sudo apt update
Enter fullscreen mode Exit fullscreen mode
sudo apt install percona-server-mongodb
Enter fullscreen mode Exit fullscreen mode

Great, we installed Percona Server for MongoDB with the latest version, currently 6.0.4. But that's not all; it will take a few more steps.

Image description

2. Authentication and authorization

We need to create a root user and enable authentication, this is not enabled by default.

This is indicated by a message in the console, it also tells you exactly what script to run.

 ** WARNING: Access control is not enabled for the database.
 ** Read and write access to data and configuration is unrestricted.
 ** To fix this please use /usr/bin/percona-server-mongodb-enable-auth.sh
Enter fullscreen mode Exit fullscreen mode

Run

sudo /usr/bin/percona-server-mongodb-enable-auth.sh
Enter fullscreen mode Exit fullscreen mode

The result will be the username and password of the root user.

Image description

3. Connect to the database in the console

mongosh --username "dba" --password "password" --authenticationDatabase admin
Enter fullscreen mode Exit fullscreen mode

Now you can create more users or use any commands, such as show dbs.

5. Connecting to a database from an application (Optional)

If the application is hosted on the same server, just connect using 'localhost' as the URL.
If the application is hosted on another server, you need to access the database from other IPs.

You need to add the IP of the application in /etc/mongod.conf in the net.bindIp parameter

Do exit if you are in mongosh and execute on the server

sudo nano /etc/mongod.conf
Enter fullscreen mode Exit fullscreen mode

Add the IP of your application or home computer, separated by commas, to bindIp. Or set 0.0.0.0 to open to everyone on the Internet

net:
  port: 27017
  bindIp: 0.0.0.0 
Enter fullscreen mode Exit fullscreen mode

Close and save the file

Restart MongoDB with the new parameters with the command

sudo systemctl restart mongod
Enter fullscreen mode Exit fullscreen mode

That's it, you can connect from the app, you can see an example in my other article

6. Connect to the database with MongoDB Compass (Optional)

  1. If you use AWS as I do, check that you do not have restrictions in the Security and Networking Groups settings.
  2. Also in the net.bindIp parameter of /etc/mongod.conf you should add the IP of your computer or set 0.0.0.0
  3. As the host connection you must use the Public IP address of the MongoDB instance and the user/password data we created at the beginning of the article.

Image description

Image description

Top comments (0)