Moodle 4.4 on AWS LightSail EC2 and Database

Moodle 4.4 on AWS LightSail EC2 and Database

by Nick Shri -
Number of replies: 3

Note: I tried to search the forum for something similar and didn't find any results.

I am using Lightsail EC2 and Database (two separate instances, both in same aws region). 

I am updating my config.php on the ec2 instance as below (I have removed the lines that are default to not clutter here.):

$CFG->dbtype    = 'auroramysql';      // 'pgsql', 'mariadb', 'mysqli', 'auroramysql', 'sqlsrv' or 'oci'
$CFG->dbhost    = '<my end point>';  // eg 'localhost' or 'db.isp.com' or IP
$CFG->dbname    = 'moodle-db';     // database name, eg moodle
$CFG->dbuser    = '<my db username>';   // your database username
$CFG->dbpass    = '<my db password>';   // your database password
$CFG->dboptions = array(
    'dbport'    => '3306',          // the TCP port number to use when connecting

When I try to connect via browser, I get: Error: Database connection failed

My question - is my config looking right or do I need to make any changes? And if it is right, what could be going wrong?

Average of ratings: -
In reply to Nick Shri

Re: Moodle 4.4 on AWS LightSail EC2 and Database

by Hallan Franco -

Hello Nick!

Looking to the message, you EC2 didn't hited the aurora database, your configuration at moodle is right but using cloud you need check other kind of security configuration.

Follow my recommendations:

  •  if you are using VPN in your environment, you need check if you have the security group configured to accept the connection from your EC2 to the database, you need provide the internal IP of your EC2 in the security group of the aurora database
  • Check if your aurora database are in the same region, if yes It supposed to work, and don't need to have configuration at the security group.
  • Try to make your database public and check if you can connect by some tool, like DBHeaver or MySQL Workbrench before use to moodle, will help you to check if is really something the security configuration.
  • Like this is MYSQL not aurora, maybe the library it's different, check that, I can't create aurora database in my lightsail

In reply to Nick Shri

Moodle 4.4 on AWS LightSail EC2 and Database

by Eduardo Kraus -
Sary ny Particularly helpful Moodlers Sary ny Plugin developers Sary ny Testers

Hello, Nick Shri! 😄

I see you're using auroramysql. Make sure you're actually using Aurora MySQL and not regular MySQL or MariaDB. If it's MySQL, you can switch to 'mysqli'.

The value you entered for dbhost needs to be correct. Make sure the endpoint you provided is complete and includes the public DNS (or IP) of the database in AWS.

Network Security:

  • Check if the Security Group of your database in AWS LightSail allows inbound connections on port 3306 from your EC2 server.
  • Verify that your EC2 allows outbound traffic on port 3306.
  • If your database is in a private subnet, make sure the EC2 instance can access that subnet. This could be an issue with peering or permissions in the VPC.

Make sure the database credentials (dbuser and dbpass) are correct.

Tip:

Try accessing the database directly via MySQL Workbench or command line to verify the external connection. Try running a simple test directly on your EC2 server to ensure it can connect to the database:

mysql -h <your-db-endpoint> -u <your-username> -p

This should open a connection to the database. If it fails, the connection between EC2 and the database might be blocked.

After checking these points, I believe you'll be one step closer to solving this problem and getting your Moodle running smoothly on AWS LightSail! 🚀

Eduardo Kraus
Teacher and Developer

Average of ratings:Useful (1)