**Learn How to Connect a Django App to AWS RDS MySQL Database!**
In this tutorial, I’ll guide you through **setting up an AWS RDS MySQL database** and connecting it to your **Django application**. If you're building a **cloud-hosted Django app** and need a **scalable MySQL database**, this is the perfect solution!
---
### **What You’ll Learn:**
How to **create a MySQL database on AWS RDS**
How to **configure security groups for remote access**
How to **install and configure MySQL in Django**
How to **connect Django to AWS RDS MySQL**
How to **migrate Django models to RDS**
---
---
## **Step 1: Create a MySQL Database on AWS RDS**
1⃣ **Log in to AWS Console** → Go to **RDS Service**
2⃣ Click **Create database**
3⃣ Choose **Standard create**
4⃣ Select **MySQL** as the engine
5⃣ Set up database details:
- **DB instance identifier**: `mydb-instance`
- **Master username**: `admin`
- **Master password**: `yourpassword`
6⃣ Under Connectivity, **enable Public Access** (for development)
7⃣ Choose a security group to **allow inbound MySQL connections (port 3306)**
8⃣ Click **Create database** and wait for it to be available
---
## **Step 2: Install MySQL Client on Your System**
To connect Django to AWS RDS, install the MySQL client:
**On Ubuntu/Linux:**
```bash
sudo apt update
sudo apt install mysql-client
```
**On macOS:**
```bash
brew install mysql
```
**On Windows:**
[Download MySQL](https://dev.mysql.com/downloads/) and install it.
Verify installation with:
```bash
mysql --version
```
---
## **Step 3: Install MySQL Adapter for Django**
Inside your **Django project** folder, activate your virtual environment and install `mysqlclient`:
```bash
pip install mysqlclient
```
If you encounter errors, use:
```bash
pip install pymysql
```
Then, add this to `__init__.py` inside your Django app:
```python
import pymysql
pymysql.install_as_MySQLdb()
```
---
## **Step 4: Configure Django Settings for AWS RDS MySQL**
In your Django **settings.py**, update the `DATABASES` section:
```python
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql',
'NAME': 'your-db-name', # Replace with your AWS RDS database name
'USER': 'your-username', # AWS RDS username
'PASSWORD': 'your-password', # AWS RDS password
'HOST': 'your-db-endpoint.rds.amazonaws.com', # AWS RDS endpoint
'PORT': '3306', # Default MySQL port
'OPTIONS': {
'init_command': "SET sql_mode='STRICT_TRANS_TABLES'"
}
}
}
```
Replace **`your-db-name`**, **`your-username`**, **`your-password`**, and **`your-db-endpoint.rds.amazonaws.com`** with actual values from your RDS instance.
---
## **Step 5: Run Database Migrations**
Once the settings are updated, apply migrations to set up the database schema:
```bash
python manage.py makemigrations
python manage.py migrate
```
This will create necessary Django tables in **AWS RDS MySQL**.
---
## **Step 6: Test the Connection**
Run the Django development server:
```bash
python manage.py runserver
```
If everything is set up correctly, Django should connect to your AWS RDS MySQL database .
To confirm, open **MySQL CLI** and check if the tables are created:
```bash
mysql -h your-db-endpoint.rds.amazonaws.com -u your-username -p
```
Then, run:
```sql
SHOW DATABASES;
USE your-db-name;
SHOW TABLES;
```
---
## **Step 7: Secure Your Connection (Optional but Recommended)**
**Use environment variables** instead of hardcoding credentials in `settings.py`:
```python
import os
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql',
'NAME': os.getenv('DB_NAME'),
'USER': os.getenv('DB_USER'),
'PASSWORD': os.getenv('DB_PASSWORD'),
'HOST': os.getenv('DB_HOST'),
'PORT': '3306',
'OPTIONS': {
'init_command': "SET sql_mode='STRICT_TRANS_TABLES'"
}
}
}
```
Then, store credentials in a `.env` file:
```
DB_NAME=your-db-name
DB_USER=your-username
DB_PASSWORD=your-password
DB_HOST=your-db-endpoint.rds.amazonaws.com
```
Use **python-dotenv** to load them:
```bash
pip install python-dotenv
```
And add this to `settings.py`:
```python
from dotenv import load_dotenv
load_dotenv()
```
---
## **Common Issues & Fixes:**
**Can't connect to AWS RDS?**
Ensure **public access** is enabled in **AWS RDS settings**.
**"Host not allowed to connect to this MySQL server"?**
Edit your **RDS security group** to allow **inbound connections** from your **IP address**.
**Django migrations not working?**
Ensure `mysqlclient` is installed:
```bash
pip install mysqlclient
```
If errors persist, try:
```bash
pip install pymysql
```
#Django #AWS #MySQL #AWSRDS #DjangoAWS #CloudComputing #WebDevelopment #Python #Database #DjangoMySQL #AWSCloud #DjangoTutorial