Add Database to Application hosted in Amazon Lightsail
Tags: aws, cloud, lightsail, laravel
Published: 2022-05-23 14:04:33

Amazon Lightsail offers dedicated Databases. You can make use of it instead of an on-instance database. It's easy, managed, secure, and most importantly it's not tied to the instance. So, your database will be safe even if instance crashes.

In "**[Deploy WordPress App to Amazon Lightsail](https://blog.harun.dev/deploy-wordpress-app-to-amazon-lightsail)**, the database is installed in the instance. In this article, we are to cover the dedicated databases.

> ⚠️ Warning: Dedicated Lightsail Databases are going to cost $$$. It has a 3-month free tier at this time of writing. Check your plan before creating a database.

So, what are you waiting for? Let's dive right in 🏃‍♂️

## Create a Database Instance

1. Go to your Amazon Lightsail home page. And then click on the **Databases** tab.

2. Now click on the **Create database** button
![CleanShot 2022-05-21 at 17.17.16@2x.png](https://cdn.hashnode.com/res/hashnode/image/upload/v1653131872710/gEcI9vj3t.png)

3. Now you are on the database creation page: 
![CleanShot 2022-05-21 at 17.23.57@2x.png](https://cdn.hashnode.com/res/hashnode/image/upload/v1653132301734/u04toFGII.png)
  - (A) Select AWS Region and Availablity Zone (AZ). for me Virginia and Zone A are perfect.
  - (B) Choose the Database engine and version. I selected MySQL 8.
  - (C) Click on **Specify login credentials**, and
  - (D) Click on **Specify the master database name**.

4. Scroll down a little and configure this: 
![CleanShot 2022-05-21 at 18.03.04@2x.png](https://cdn.hashnode.com/res/hashnode/image/upload/v1653134742780/DRvaNLKt4.png)
  - (A) Choose Database username
  - (B) Password. I'm okay with the generated password, but you can choose yours.
  - (C) The first/master database. You can create more later.
  - (D & E) Choose your plan type and the actual plan. You can choose the free-tier option like me.
  - (F) Give it a name, and
  - (G) Hit the **Create database** button
Your database should be created within a few minutes. Please wait for it until it's **Running**.

## Database Credentials
You already know your database username and master database. For everything else, go to your database and click on the **Connect** tab. You should see your credentials like this:
![CleanShot 2022-05-21 at 18.24.30@2x.png](https://cdn.hashnode.com/res/hashnode/image/upload/v1653135919053/o9bP5UDl4.png)
  - (A) DB Username
  - (B) DB Password
  - (C) DB Endpoint
  - (D) DB Port

## Connect to the database

### Instance CLI
You can [SSH into your instance](https://blog.harun.dev/deploy-laravel-application-to-amazon-lightsail#heading-connect-to-the-instance-using-ssh). 

Install MySQL using the following command:
```bash
sudo apt install mysql-server -y
sudo systemctl start mysql.service
```

And then connect to MySQL:
```bash
mysql --host <db_endpoint> --user <db_username> --password
```
You will be asked for your DB password. If successful, you will see this screen:
![CleanShot 2022-05-21 at 18.35.21@2x.png](https://cdn.hashnode.com/res/hashnode/image/upload/v1653136540334/gIFUYhIHz.png)

### SQL Client App over SSH (SSH Tunneling)

Most modern SQL Client applications (ex [TablePlus](https://docs.tableplus.com/gui-tools/manage-connections#ssh-tunneling), MySQL Workbench) have the option to connect Database via SSH. You can connect using that option. 

## Add Database to your application

### Laravel and NodeJS
You can update your application's `.env` file with Database credentials.

### WordPress
Update your `wp-config.php` file with the database credentials.

### Any other app
Update your database credential in your application's configuration and/or environment file. Check your application's documentation for that. Or it's custom, you know where to put it.

## Conclusion
Thanks for reading the article. Adding dedicated databases to Amazon Lightsail instance is not that hard. You can follow me to get instant updates on my posts. You can also subscribe to [my newsletter](https://blog.harun.dev).