How to Connect a MariaDB Database in AWS to a Django Project



Python


In this article, we show how to connect a MariaDB database in AWS to django project.

Django comes equipped with MySQLite, which is more like a test database system rather than a production-ready database service.

For a production website, you want to use a more sophisticated database, such as postgresql, mysql, MariaDB, or oracle.

MariaDB is a MySQL-compatible database with strong support from the open source community, and extra features and performance optimizations. MariaDB supports database sizes up to 64 TiB. MariaDB is developed and supported by the MariaDB open source community.

In order to use MariaDB, we use much of the same setup as MySQL, being that MySQL serves as the backend system. This is what is meant by compatibility with MySQL.

So the first thing we will do is get our MariaDB database server set up on AWS (amazon web services).

So the first thing you must have is an account on AWS.

What you want to do is find RDS, which stands for Relational Database Services.


Amazon web services- Relational database services


This brings us to a page where we now need to click on the 'Create database' button.


AWS RDS- create database


Once we press this button, we are led to a page where we select the database that we would like to use for our website.


AWS RDS- MariaDB


As you can see, we have a variety of options.

We can select betwen Aurora (MySQL Compatible), Aurora (PostgreSQL Compatible), MySQL, MariaDB, PostgreSQL, Oracle, Microsoft SQL Server, and IBM Db2.

In this case, we select MariaDB.

It's best to choose 'Standard create' database creation method, so that you have more control of the database creation process. Easy create assumes more configuration methods and puts you less in control.

For the DB instance identifier, you can leave it as the simple name of 'database-1' (if it is the first instance database you have in the account). Otherwise, you can call it 'database-2' or whatever.

Next of important is the Master username.

By default, AWS gives the name of 'admin'. For a production environment, you of course don't want to leave it as the default name, because that would be a security risk. But since this is simply to function as an article on how to set up a MariaDB database in AWS with django, we call it 'mariadbadmin'.

Next we need a master password.

I choose to self manage this and create my own. This can be anything. I use the password, 'password123'.

The master username and password are vital to establishing connection to your AWS database instance.

Of next importance is allowing public access and creating a VPC security group.

To make the database more accessible, we assign it a public IP address.

Very important is the VPC security group, which is the Virtual Private Cloud security group. This functions as the firewall, deciding what traffic is able to access our site and what traffic the site can be sent to (in other words, who can access it and who the site's contents can be sent to).

So what I do is always create my own VPC security group and not use the one provided by default.


AWS RDS mariadb- create new VPC security group


Normally, when you are making a production website, you only want the site to be accessed by the people who maintain the website, so you would specify only the IP address that the website team uses. But, in this case, to keep accessibility at the highest level, we specify an inbound rule of 0.0.0.0/0, which means that anyone from any IP address can access the website.

When you do edit the inbound rules later, below is what this VPC security group rule would look like.


Editing inbound rules for a mariadb VPC security group


One last thing we must do that is very important is to specify a database name.

This is found in the Additional Configuration section of the Database creation.

Under the Initial database name, we specify the database name.

This can be anything such as Customerorders (if your website deals with orders of items).

This is important, because if you don't specify a database name, you won't be able to establish connection with django.

This is shown below.


AWS RDS mariadb- specifying a database name


And this is all.

After this, we click on the 'Create database' button, and our database should be up and running in a few minutes.

We then can find our endpoint, as well as all the other information necessary to connect to our AWS mariadb database with django.

We will go over this later.

Now that everything is completed on the AWS database instance side, we now go to the django side.

The first thing that we'll do is within our django project is download a module, mysqlclient if this is not already installed.

This is installed with the following line of code.



When you run 'pip freeze', you should see this module installed.

mysqlclient module installation

Next, we update our requirements.txt file. This isn't necessary if you're simply testing your program on your local computer, but if you're running it in a production environment, you would have to do this.

This should be done in the root directory of the django project.

We specify the following line shown below.



This lists out all the modules (with their versions) that are needed by the django project to work correctly.

Next, we go into our django code.

Within the root settings.py file, we must erase the following code.



We take out this code and replace it with the following code shown below.



And then after this, everything should be good to go.

We must specify in the OPTIONS dictionary that sql mode is set to STRICT_TRANS_TABLE.



Migrating the mariadb database in django


Now we know that our mariadb database works.

We can now move onto the next step of creating models in django that function as our database table information for our website.


Related Resources

How to Connect a PostgreSQL Database in AWS to a Django Project

How to Connect a MySQL Database in AWS to a Django Project

How to Connect an Oracle Database in AWS to a Django Project



HTML Comment Box is loading comments...