Add Database to Application hosted in Amazon Lightsail
3 min read
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
Go to your Amazon Lightsail home page. And then click on the Databases tab.
Now click on the Create database button
Now you are on the database creation page:
- (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.
Scroll down a little and configure this:
- (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.
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:
- (A) DB Username
- (B) DB Password
- (C) DB Endpoint
- (D) DB Port
Connect to the database
You can SSH into your instance.
Install MySQL using the following command:
sudo apt install mysql-server -y sudo systemctl start mysql.service
And then connect to MySQL:
mysql --host <db_endpoint> --user <db_username> --password
You will be asked for your DB password. If successful, you will see this screen:
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.
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.
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.
Did you find this article valuable?
Support Harun R Rayhan by becoming a sponsor. Any amount is appreciated!