DEV Community

Cover image for Add Database to Application hosted in Amazon Lightsail
Harun R Rayhan for AWS Community Builders

Posted on • Originally published at blog.harun.dev

Add Database to Application hosted in Amazon Lightsail

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, 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

  3. Now you are on the database creation page:
    CleanShot 2022-05-21 at 17.23.57@2x.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

    • (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

  • (A) DB Username
  • (B) DB Password
  • (C) DB Endpoint
  • (D) DB Port

Connect to the database

Instance CLI

You can SSH into your instance.

Install MySQL using the following command:

sudo apt install mysql-server -y
sudo systemctl start mysql.service
Enter fullscreen mode Exit fullscreen mode

And then connect to MySQL:

mysql --host <db_endpoint> --user <db_username> --password
Enter fullscreen mode Exit fullscreen mode

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

SQL Client App over SSH (SSH Tunneling)

Most modern SQL Client applications (ex TablePlus, 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.

Top comments (0)