How to connect RDS mysql using php

aws-php-logo

Step 1: Login to your AWS account.

Step 2: Under the Database, select the RDS( Relational Database Service ) option.

Database RDS

Step 3: Look for the Create Instance & Click on the blue button Launch a DB Instance.

Create Instance RDS

Step 4: Now select the DB engine of your choice & click on select.

DB Engine RDS

Note-: In this tutorial we have used MySQL DB engine.

Step 5: Select the purpose ( Production or Test ) for creating RDS.

Purpose Creating RDS

Step 6: Under Instance Specifications look for the below options & make changes as per your requirement.

1. DB Engine Version
2. DB Instance Class
3. Storage Type
4. Allocated Storage

Instance Specification

Step 7: Now configure Database settings  by filling up the required information.

Username & Passwd RDSJPG

Note-: Please make note of a Username & Password which you have specified in the settings because it will be used when will connect to RDS using PHP. DB Instance Identifier is used easy identification for your DB instance from among the number of DB instance you have created.

Step 8: Configure advance settings.

1. Network & Security
One can use the default security group and can create a new security group for making your DB instance more secure.

Network Security
2. Database
Specify the Database name & port.

Advance Database Settings

3. Backup
Specify backup  retention period between 1 to 35 days.

Advance Setting Backup

Step 9: After successful creation of DB Instance it would appear like this.

Final Appearnce RDS
Note-: Endpoint is the server name which will be used when we will connect to RDS using Php. So please make a note of it.

Connecting With RDS Using Php on Debian/Ubuntu

Step 1: Install Php & Php-mysql on your machine.

# apt-get install php5 php5-mysql -y

Step 2: Create a file by using your favorite editor with .php extension.

# vim file_name.php

Note-: Instead of file_name you can use any name of your choice.

Step 3: Copy the below code into your file, Save & exit from the file.

<?php
$link = mysql_connect('localhost', 'mysql_user', 'mysql_password');
if (!$link) {
die('Could not connect: ' . mysql_error());
}
echo 'Connected successfully';
mysql_close($link);
?>

Note-: Replace ‘localhost’, with Endpoint/server_name ‘mysql_user’, with Username you have specified while creating RDS & same with the ‘mysql_password’.

Step 4: Run the code using the below command.

# php file_name.php