Installing Percona Server for MongoDB manually on Ubuntu on AWS EC2
Tags: percona, mongodb, beginners, database
Published: 2023-03-15 20:18:11

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](https://docs.percona.com/percona-server-for-mongodb/6.0/install/index.html#installation-instructions)._

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

## 1. We need to follow the procedures

[the official documentation](https://docs.percona.com/percona-server-for-mongodb/6.0/install/apt.html#procedure)

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

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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/xuf0hn6khb4iiqwl0phk.png)

## 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
```

Run
```
sudo /usr/bin/percona-server-mongodb-enable-auth.sh
```

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

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/x62pi68mrwsf6hrfuxnr.png)

## 3. Connect to the database in the console

```
mongosh --username "dba" --password "password" --authenticationDatabase admin
```

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
```
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 
```
Close and save the file

Restart MongoDB with the new parameters with the command
```
sudo systemctl restart mongod
```

That's it, you can connect from the app, you can see an example in [my other article](https://dev.to/dbazhenov/how-to-develop-a-simple-web-application-using-docker-compose-nginx-php-8-and-mongodb-6-nhi#lets-connect-to-mongodb-in-a-php-application-and-write-the-test-data)

## 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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/858um4q0mkn892qkta31.png)

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/f5xri7mex3xerb8esro1.png)