---
title: "How to Connect to AWS RDS MySQL Database to EC2 Instance With PHP By Using PDO"
lang: "en"
author: "Mohammad Abu Mattar"
canonical: https://mkabumattar.com/post/how-to-connect-to-aws-rds-mysql-database-to-ec2-instance-with-php-by-using-pdo
---

![Blog post image for How to Connect to AWS RDS MySQL Database to EC2 Instance With PHP By Using PDO - In this post, we will learn how to connect to AWS RDS MySQL Database to EC2 Instance With PHP By Using PDO.](/_astro/hero.BbyjrsxP_YmgOR.webp)

[Home](/)›[Blog](/blog)›[All Categories](/blog/categories)›[AWS](/blog/categories/aws)

Blog

[Prev in AWSHow To Connect A Two EC2 Instances Database and Files Transfer Using AWS CLI](/blog/post/how-to-connect-a-two-ec2-instances-database-and-files-transfer-using-aws-cli)[Next in AWSHow to Create a AWS RDS MySQL Database and Connect to it using MySQL Workbench](/blog/post/how-to-create-a-aws-rds-mysql-database-and-connect-to-it-using-mysql-workbench)

[AWS](/blog/categories/aws)[RDS](/blog/categories/rds)[EC2](/blog/categories/ec2)[PHP](/blog/categories/php)[MySQL](/blog/categories/mysql)[Database](/blog/categories/database)

# How to Connect to AWS RDS MySQL Database to EC2 Instance With PHP By Using PDO

[Mohammad Abu Mattar](/authors/mohammad-abu-mattar)Published: 07 Nov 202208 Mins read07 Mins listen

[Markdown for AI(opens in a new tab)](/post/how-to-connect-to-aws-rds-mysql-database-to-ec2-instance-with-php-by-using-pdo/index.md "Open the plain-Markdown version of this page, for pasting into an AI tool")

TL;DR

In this post, we will learn how to connect to AWS RDS MySQL Database to EC2 Instance With PHP By Using PDO.

Series

[AWS Automation](/series/aws-automation)4/6

[PreviousHow to Install and Configure Node.js on EC2 Instance Amazon Linux 2](/blog/post/how-to-install-and-configure-nodejs-on-ec2-instance-amazon-linux-2)[NextHow To Connect A EBS Volume To An Windows EC2 Instance Using Powershell/GUI](/blog/post/how-to-connect-a-ebs-volume-to-an-windows-ec2-instance-using-powershell-gui)

All posts in this series (6)

Blog6

1.  [How to Run an Apache Web Server Using Docker on an AWS EC2 Instance](/blog/post/how-to-run-an-apache-web-server-using-docker-on-an-aws-ec2-instance)
2.  [How to Create a AWS RDS MySQL Database and Connect to it using MySQL Workbench](/blog/post/how-to-create-a-aws-rds-mysql-database-and-connect-to-it-using-mysql-workbench)
3.  [How to Install and Configure Node.js on EC2 Instance Amazon Linux 2](/blog/post/how-to-install-and-configure-nodejs-on-ec2-instance-amazon-linux-2)
4.  [How to Connect to AWS RDS MySQL Database to EC2 Instance With PHP By Using PDOYou are here](/blog/post/how-to-connect-to-aws-rds-mysql-database-to-ec2-instance-with-php-by-using-pdo)
5.  [How To Connect A EBS Volume To An Windows EC2 Instance Using Powershell/GUI](/blog/post/how-to-connect-a-ebs-volume-to-an-windows-ec2-instance-using-powershell-gui)
6.  [Scaling Up, Staying Strong: Hands-On AWS CloudFormation Techniques for Building Resilient and Scalable Systems](/blog/post/scaling-up-staying-strong-hands-on-aws-cloudformation-techniques-for-building-resilient-and-scalable-systems)

### How to Connect to AWS RDS MySQL Database to EC2 Instance With PHP By Using PDO

Contents

[SAILORS](#sailors)[BOATS](#boats)[RESERVES](#reserves)[Find all information of sailors who have reserved boat number 101.](#find-all-information-of-sailors-who-have-reserved-boat-number-101)[Find the names of sailors who have reserved a red boat, and list in the order of age.](#find-the-names-of-sailors-who-have-reserved-a-red-boat-and-list-in-the-order-of-age)[Find the names of sailors who have reserved at least one boat.](#find-the-names-of-sailors-who-have-reserved-at-least-one-boat)[Find the ids and names of sailors who have reserved two different boats on the same day.](#find-the-ids-and-names-of-sailors-who-have-reserved-two-different-boats-on-the-same-day)[Find the ids of sailors who have reserved a red boat or a green boat.](#find-the-ids-of-sailors-who-have-reserved-a-red-boat-or-a-green-boat)[Insert data into database](#insert-data-into-database)

## [Introduction](#introduction)

In this post, we will learn how to connect an AWS RDS MySQL database to an EC2 instance with PHP, using PDO.

## [Prerequisites](#prerequisites)

-   AWS Account
-   EC2 Instance Amazon Linux 2
-   RDS MySQL Database
-   MySQL Workbench
-   SSH Client

Note

In previous posts, we have learned how to create an AWS RDS MySQL database and connect to it using MySQL Workbench. If you have not read that post yet, you can read it here: [How to Create a AWS RDS MySQL Database and Connect to it Using MySQL Workbench](/blog/post/how-to-create-a-aws-rds-mysql-database-and-connect-to-it-using-mysql-workbench)

* * *

Note

In previous posts, we have learned how to create an AWS EC2 instance running Amazon Linux 2. If you have not read that post yet, you can read it here: [How To Create An AWS EC2 Instance Using AWS CLI](/blog/post/how-to-create-an-aws-ec2-instance-using-aws-cli)

## [Create the PHP file structure](#create-the-php-file-structure)

```
1/var/www/html2├── connect.php3├── insert.php4├── index.php5└── insert-boats-form.php
```

## [Connect the RDS MySQL database to the EC2 instance with PHP using PDO](#connect-the-rds-mysql-database-to-the-ec2-instance-with-php-using-pdo)

### [Create a connect.php file](#create-a-connectphp-file)

We will create a `connect.php` file in the `/var/www/html` directory.

```
1<?php2    $servername = "localhost";3    $username = "root";4    $password = "password";5    $dbname = "SAI";6    $port = "3306";7
8    $dsn = "mysql:host=$servername;port=$port;dbname=$dbname";9
10    try {11        $conn = new PDO($dsn, $username, $password);12        $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);13        echo "Connected successfully";14    } catch(PDOException $e) {15        echo "Connection failed: " . $e->getMessage();16    }17
18?>
```

Explanation:

Click to expand the explanation

-   `$servername` - The server name of the RDS MySQL Database.
-   `$username` - The username of the RDS MySQL Database.
-   `$password` - The password of the RDS MySQL Database.
-   `$dbname` - The database name of the RDS MySQL Database.
-   `$port` - The port of the RDS MySQL Database.
-   `$dsn` - The Data Source Name, or DSN, contains the information required to connect to the database.
-   `$conn` - The connection object.
-   `PDO::ATTR_ERRMODE` - The error reporting attribute.
-   `PDO::ERRMODE_EXCEPTION` - Throw exceptions for errors.

### [Create an insert.php file](#create-an-insertphp-file)

We will create an `insert.php` file in the `/var/www/html` directory.

```
1<?php2    try {3        include_once 'connect.php';4
5        $bid = $_POST['bid'];6        $bname = $_POST['bname'];7        $color = $_POST['color'];8
9        $search = $conn->prepare(10            "INSERT INTO BOATS (BID, BName, BColor) VALUES (:bid, :bname, :color)"11        );12
13        $search->bindParam(':bid', $bid);14        $search->bindParam(':bname', $bname);15        $search->bindParam(':color', $color);16
17        $search->execute();18
19        $conn = null;20    } catch(PDOException $e) {21        echo "Connection failed: " . $e->getMessage();22        $conn = null;23    }24?>
```

Explanation:

Click to expand the explanation

-   `include_once 'connect.php'` - Include the `connect.php` file.
-   `$_POST['bid']` - Get the `bid` value from the `POST` request.
-   `$_POST['bname']` - Get the `bname` value from the `POST` request.
-   `$_POST['color']` - Get the `color` value from the `POST` request.
-   `$search = $conn->prepare()` - Prepare the SQL statement.
-   `$search->bindParam(':bid', $bid)` - Bind the `bid` value to the `:bid` parameter.
-   `$search->bindParam(':bname', $bname)` - Bind the `bname` value to the `:bname` parameter.
-   `$search->bindParam(':color', $color)` - Bind the `color` value to the `:color` parameter.
-   `$search->execute()` - Execute the SQL statement.
-   `$conn = null` - Close the connection.
-   `echo "Connection failed: " . $e->getMessage()` - Print the error message.

### [Create an index.php file](#create-an-indexphp-file)

We will create an `index.php` file in the `/var/www/html` directory, and connect to the database using the `connect.php` file.

```
1<?php2    include_once 'connect.php';3
4    $query_SAILORS = "SELECT * FROM SAILORS";5    $query_BOATS = "SELECT * FROM BOATS";6    $query_RESERVES = "SELECT * FROM RESERVES";7    $query_1 = "SELECT * FROM SAILORS WHERE SID in (select SID from RESERVES WHERE BID = 101)";8    $query_2 = "SELECT SName FROM SAILORS WHERE SID in (select SID from RESERVES WHERE BID in (select BID from BOATS WHERE BColor = 'red')) order by SAge";9    $query_3 = "SELECT SName FROM SAILORS WHERE SID in (select SID from RESERVES)";10    $query_4 = "SELECT SID, SName FROM SAILORS WHERE SID in (select SID from RESERVES group by SID, Day having count(*) >= 2)";11    $query_5 = "SELECT SID FROM SAILORS WHERE SID in (select SID from RESERVES WHERE BID in (select BID from BOATS WHERE BColor = 'red')) or SID in (select SID from RESERVES WHERE BID in (select BID from BOATS WHERE BColor = 'green'))";12
13    $result_SAILORS = $conn->query($query_SAILORS);14    $result_BOATS = $conn->query($query_BOATS);15    $result_RESERVES = $conn->query($query_RESERVES);16    $result_1 = $conn->query($query_1);17    $result_2 = $conn->query($query_2);18    $result_3 = $conn->query($query_3);19    $result_4 = $conn->query($query_4);20    $result_5 = $conn->query($query_5);21
22    $SAILORS = $result_SAILORS->fetchAll();23    $BOATS = $result_BOATS->fetchAll();24    $RESERVES = $result_RESERVES->fetchAll();25    $result_1 = $result_1->fetchAll();26    $result_2 = $result_2->fetchAll();27    $result_3 = $result_3->fetchAll();28    $result_4 = $result_4->fetchAll();29    $result_5 = $result_5->fetchAll();30
31  $conn = null;32?>33
34<!DOCTYPE html>35<html>36<head>37    <title>SAI</title>38    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/tailwindcss/1.4.6/tailwind.min.css">39</head>40<body>41    <div class="container mx-auto">42        <div class="flex flex-wrap">43            <div class="w-full">44                <h1 class="text-4xl text-center">SAI</h1>45            </div>46        </div>47        <div class="flex flex-wrap">48            <div class="w-full">49                <h2 class="text-2xl text-center">SAILORS</h2>50            </div>51        </div>52        <div class="flex flex-wrap">53            <div class="w-full">54                <table class="table-auto">55                    <thead>56                        <tr>57                            <th class="px-4 py-2">SID</th>58                            <th class="px-4 py-2">SName</th>59                            <th class="px-4 py-2">SRating</th>60                            <th class="px-4 py-2">SAge</th>61                        </tr>62                    </thead>63                    <tbody>64                        <?php foreach ($SAILORS as $SAILOR): ?>65                            <tr>66                                <td class="border px-4 py-2"><?php echo $SAILOR['SID'] ?></td>67                                <td class="border px-4 py-2"><?php echo $SAILOR['SName'] ?></td>68                                <td class="border px-4 py-2"><?php echo $SAILOR['SRating'] ?></td>69                                <td class="border px-4 py-2"><?php echo $SAILOR['SAge'] ?></td>70                            </tr>71                        <?php endforeach; ?>72                    </tbody>73                </table>74            </div>75        </div>76        <div class="flex flex-wrap">77            <div class="w-full">78                <h2 class="text-2xl text-center">BOATS</h2>79            </div>80        </div>81        <div class="flex flex-wrap">82            <div class="w-full">83                <table class="table-auto">84                    <thead>85                        <tr>86                            <th class="px-4 py-2">BID</th>87                            <th class="px-4 py-2">BName</th>88                            <th class="px-4 py-2">BColor</th>89                        </tr>90                    </thead>91                    <tbody>92                        <?php foreach ($BOATS as $BOAT): ?>93                            <tr>94                                <td class="border px-4 py-2"><?php echo $BOAT['BID'] ?></td>95                                <td class="border px-4 py-2"><?php echo $BOAT['BName'] ?></td>96                                <td class="border px-4 py-2"><?php echo $BOAT['BColor'] ?></td>97                            </tr>98                        <?php endforeach; ?>99                    </tbody>100                </table>101            </div>102        </div>103        <div class="flex flex-wrap">104            <div class="w-full">105                <h2 class="text-2xl text-center">RESERVES</h2>106            </div>107        </div>108        <div class="flex flex-wrap">109            <div class="w-full">110                <table class="table-auto">111                    <thead>112                        <tr>113                            <th class="px-4 py-2">SID</th>114                            <th class="px-4 py-2">BID</th>115                            <th class="px-4 py-2">Day</th>116                        </tr>117                    </thead>118                    <tbody>119                        <?php foreach ($RESERVES as $RESERVE): ?>120                            <tr>121                                <td class="border px-4 py-2"><?php echo $RESERVE['SID'] ?></td>122                                <td class="border px-4 py-2"><?php echo $RESERVE['BID'] ?></td>123                                <td class="border px-4 py-2"><?php echo $RESERVE['Day'] ?></td>124                            </tr>125                        <?php endforeach; ?>126                    </tbody>127                </table>128            </div>129        </div>130        <div class="flex flex-wrap">131            <div class="w-full">132                <h2 class="text-2xl text-center">Find all information of sailors who have reserved boat number 101.</h2>133            </div>134        </div>135        <div class="flex flex-wrap">136            <div class="w-full">137                <table class="table-auto">138                    <thead>139                        <tr>140                            <th class="px-4 py-2">SID</th>141                            <th class="px-4 py-2">SName</th>142                            <th class="px-4 py-2">SRating</th>143                            <th class="px-4 py-2">SAge</th>144                        </tr>145                    </thead>146                    <tbody>147                        <?php foreach ($result_1 as $row): ?>148                            <tr>149                                <td class="border px-4 py-2"><?php echo $row['SID'] ?></td>150                                <td class="border px-4 py-2"><?php echo $row['SName'] ?></td>151                                <td class="border px-4 py-2"><?php echo $row['SRating'] ?></td>152                                <td class="border px-4 py-2"><?php echo $row['SAge'] ?></td>153                            </tr>154                        <?php endforeach; ?>155                    </tbody>156                </table>157            </div>158        </div>159        <div class="flex flex-wrap">160            <div class="w-full">161                <h2 class="text-2xl text-center">Find the names of sailors who have reserved a red boat, and list in the order of age.</h2>162            </div>163        </div>164        <div class="flex flex-wrap">165            <div class="w-full">166                <table class="table-auto">167                    <thead>168                        <tr>169                            <th class="px-4 py-2">SName</th>170                        </tr>171                    </thead>172                    <tbody>173                        <?php foreach ($result_2 as $row): ?>174                            <tr>175                                <td class="border px-4 py-2"><?php echo $row['SName'] ?></td>176                            </tr>177                        <?php endforeach; ?>178                    </tbody>179                </table>180            </div>181        </div>182        <div class="flex flex-wrap">183            <div class="w-full">184                <h2 class="text-2xl text-center">Find the names of sailors who have reserved at least one boat. </h2>185            </div>186        </div>187        <div class="flex flex-wrap">188            <div class="w-full">189                <table class="table-auto">190                    <thead>191                        <tr>192                            <th class="px-4 py-2">SName</th>193                        </tr>194                    </thead>195                    <tbody>196                        <?php foreach ($result_3 as $row): ?>197                            <tr>198                                <td class="border px-4 py-2"><?php echo $row['SName'] ?></td>199                            </tr>200                        <?php endforeach; ?>201                    </tbody>202                </table>203            </div>204        </div>205        <div class="flex flex-wrap">206            <div class="w-full">207                <h2 class="text-2xl text-center">Find the ids and names of sailors who have reserved two different boats on the same day.</h2>208            </div>209        </div>210        <div class="flex flex-wrap">211            <div class="w-full">212                <table class="table-auto">213                    <thead>214                        <tr>215                            <th class="px-4 py-2">SID</th>216                            <th class="px-4 py-2">SName</th>217                        </tr>218                    </thead>219                    <tbody>220                        <?php foreach ($result_4 as $row): ?>221                            <tr>222                                <td class="border px-4 py-2"><?php echo $row['SID'] ?></td>223                                <td class="border px-4 py-2"><?php echo $row['SName'] ?></td>224                            </tr>225                        <?php endforeach; ?>226                    </tbody>227                </table>228            </div>229        </div>230        <div class="flex flex-wrap">231            <div class="w-full">232                <h2 class="text-2xl text-center">Find the ids of sailors who have reserved a red boat or a green boat.</h2>233            </div>234        </div>235        <div class="flex flex-wrap">236            <div class="w-full">237                <table class="table-auto">238                    <thead>239                        <tr>240                            <th class="px-4 py-2">SID</th>241                        </tr>242                    </thead>243                    <tbody>244                        <?php foreach ($result_5 as $row): ?>245                            <tr>246                                <td class="border px-4 py-2"><?php echo $row['SID'] ?></td>247                            </tr>248                        <?php endforeach; ?>249                    </tbody>250                </table>251            </div>252        </div>253    </div>254</body>255</html>
```

Click to expand the explanation

-   Select all columns from the `SAILORS` table

```
1SELECT * FROM SAILORS
```

-   Select all columns from the `BOATS` table

```
1SELECT * FROM BOATS
```

-   Select all columns from the `RESERVES` table

```
1SELECT * FROM RESERVES
```

-   Select all columns from the `SAILORS` table where the `SID` is in the `SID` column of the `RESERVES` table where the `BID` is equal to `101`

```
1select * from `SAI`.`SAILORS`2WHERE SID in (3    select SID from `SAI`.`RESERVES`4    WHERE BID = 1015);
```

-   Select the `SName` column from the `SAILORS` table where the `SID` is in the `SID` column of the `RESERVES` table where the `BID` is in the `BID` column of the `BOATS` table where the `BColor` is equal to `'red'` and order the results by the `SAge` column

```
1select SName from `SAI`.`SAILORS`2WHERE SID in (3    select SID from `SAI`.`RESERVES`4    WHERE BID in (5        select BID from `SAI`.`BOATS`6        WHERE BColor = 'red'7    )8)9order by SAge;
```

-   Select the `SName` column from the `SAILORS` table where the `SID` is in the `SID` column of the `RESERVES` table

```
1select SName from `SAI`.`SAILORS`2WHERE SID in (3    select SID from `SAI`.`RESERVES`4);
```

-   Select the `SID` and `SName` columns from the `SAILORS` table where the `SID` is in the `SID` column of the `RESERVES` table grouped by the `SID` and `Day` columns having a count of `*` greater than or equal to `2`

```
1select SID, SName from `SAI`.`SAILORS`2WHERE SID in (3    select SID from `SAI`.`RESERVES`4    group by SID, Day5    having count(*) >= 26);
```

-   Select the `SID` column from the `SAILORS` table where the `SID` is in the `SID` column of the `RESERVES` table where the `BID` is in the `BID` column of the `BOATS` table where the `BColor` is equal to `'red'` or the `SID` is in the `SID` column of the `RESERVES` table where the `BID` is in the `BID` column of the `BOATS` table where the `BColor` is equal to `'green'`

```
1select SID from `SAI`.`SAILORS`2WHERE SID in (3    select SID from `SAI`.`RESERVES`4    WHERE BID in (5        select BID from `SAI`.`BOATS`6        WHERE BColor = 'red'7    )8)9or SID in (10    select SID from `SAI`.`RESERVES`11    WHERE BID in (12        select BID from `SAI`.`BOATS`13        WHERE BColor = 'green'14    )15);
```

-   `$result = $conn->query($query)` executes the query and returns the result as a `PDOStatement` object, which can be used to fetch the results as an associative array using the `fetchAll()` method
    
-   `$result->fetchAll();` fetches all the results as an associative array and returns it
    
-   `<?php foreach ($results as $row): ?>` loops through the results and assigns each row to the `$row` variable
    
-   `<?php echo $row['column_name'] ?>` prints the value of the column with the name `column_name` in the current row
    
-   `<?php endforeach; ?>` ends the loop
    

## [Create a form to insert a new row into the `BOATS` table](#create-a-form-to-insert-a-new-row-into-the-boats-table)

We will create a new file `insert-boats-form.php` in the `/var/www/html` directory and add the following code to it.

```
1<!DOCTYPE html>2<html>3<head>4    <title>SAI</title>5    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/tailwindcss/1.4.6/tailwind.min.css">6</head>7<body>8    <div class="container mx-auto">9        <div class="flex flex-wrap">10            <div class="w-full">11                <h1 class="text-4xl text-center">SAI</h1>12            </div>13        </div>14        <div class="flex flex-wrap">15            <div class="w-full">16                <h2 class="text-2xl text-center">Insert data into database</h2>17            </div>18        </div>19        <div class="flex flex-wrap">20            <div class="w-full">21                <form action="insert.boats.php" method="post">22                    <div class="flex flex-wrap">23                        <div class="w-full">24                            <label class="block text-gray-700 text-sm font-bold mb-2" for="bid">25                                bid26                            </label>27                            <input class="shadow appearance-none border rounded w-full py-2 px-3 text-gray-700 leading-tight focus:outline-none focus:shadow-outline" id="bid" name="bid" type="text" placeholder="bid">28                        </div>29                    </div>30                    <div class="flex flex-wrap">31                        <div class="w-full">32                            <label class="block text-gray-700 text-sm font-bold mb-2" for="bname">33                                bname34                            </label>35                            <input class="shadow appearance-none border rounded w-full py-2 px-3 text-gray-700 leading-tight focus:outline-none focus:shadow-outline" id="bname" name="bname" type="text" placeholder="bname">36                        </div>37                    </div>38                    <div class="flex flex-wrap">39                        <div class="w-full">40                            <label class="block text-gray-700 text-sm font-bold mb-2" for="color">41                                color42                            </label>43                            <input class="shadow appearance-none border rounded w-full py-2 px-3 text-gray-700 leading-tight focus:outline-none focus:shadow-outline" id="color" name="color" type="text" placeholder="color">44                        </div>45                    </div>46                    <div class="flex flex-wrap">47                        <div class="w-full">48                            <button class="bg-blue-500 hover:bg-blue-700 text-white font-bold py-2 px-4 rounded focus:outline-none focus:shadow-outline w-full" type="submit">49                                Submit50                            </button>51                        </div>52                    </div>53                </form>54            </div>55        </div>56    </div>57</body>58</html>
```

Explanation:

Click to expand the explanation

-   The `action` attribute of the form is set to `insert.boats.php`, the file that will handle the form submission
-   The `method` attribute of the form is set to `post`, so the form data will be sent to the server using the `POST` method
-   The `id` attributes of the input elements are set to the column names of the `BOATS` table
-   The `name` attributes of the input elements are set to the column names of the `BOATS` table
-   The `type` attributes of the input elements are set to `text`, so each input is a plain text field
-   The `placeholder` attributes of the input elements are set to the column names of the `BOATS` table
-   The `type` attribute of the submit button is set to `submit`, so clicking it submits the form

## [Conclusion](#conclusion)

In this tutorial, we learned how to connect to a MySQL database using PHP. We also learned how to create a form to insert data into a MySQL database using PHP.

## [References](#references)

-   [PHP Manual - PDO::\_\_construct](https://www.php.net/manual/en/pdo.construct.php)
-   [PHP Manual - PDO::prepare](https://www.php.net/manual/en/pdo.prepare.php)
-   [PHP Manual - PDOStatement::bindParam](https://www.php.net/manual/en/pdostatement.bindparam.php)
-   [PHP Manual - PDOStatement::execute](https://www.php.net/manual/en/pdostatement.execute.php)
-   [PHP Manual - PDOStatement::fetchAll](https://www.php.net/manual/en/pdostatement.fetchall.php)
-   [PHP Manual - PDO::setAttribute](https://www.php.net/manual/en/pdo.setattribute.php)
-   [Amazon RDS User Guide](https://docs.aws.amazon.com/AmazonRDS/latest/UserGuide/Welcome.html)
-   [Connecting to an Amazon RDS DB instance](https://docs.aws.amazon.com/AmazonRDS/latest/UserGuide/USER_ConnectToInstance.html)
-   [Amazon EC2 User Guide for Linux Instances](https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/concepts.html)
-   [Tutorial: Installing a LAMP Web Server on Amazon Linux 2](https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/ec2-lamp-amazon-linux-2.html)
-   [Connecting to your Linux instance using SSH](https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/AccessingInstancesLinux.html)
-   [Security groups for your VPC](https://docs.aws.amazon.com/vpc/latest/userguide/VPC_SecurityGroups.html) (Relevant for RDS and EC2 access)
-   [MySQL Documentation](https://dev.mysql.com/doc/)
-   [Tailwind CSS Documentation (for styling the example)](https://tailwindcss.com/docs)

Was this useful?

## Tags

[#AWS RDS MySQL](/blog/tags/aws-rds-mysql)[#EC2 PHP PDO](/blog/tags/ec2-php-pdo)[#Database Connection](/blog/tags/database-connection)[#PHP PDO](/blog/tags/php-pdo)[#MySQL PHP](/blog/tags/mysql-php)[#AWS EC2](/blog/tags/aws-ec2)[#AWS RDS](/blog/tags/aws-rds)[#LAMP Stack](/blog/tags/lamp-stack)

## Share

[Facebook](https://facebook.com/sharer/sharer.php?u=https%3A%2F%2Fmkabumattar.com%2Fblog%2Fpost%2Fhow-to-connect-to-aws-rds-mysql-database-to-ec2-instance-with-php-by-using-pdo "Share on Facebook")[Twitter](https://twitter.com/intent/tweet/?text=How%20to%20Connect%20to%20AWS%20RDS%20MySQL%20Database%20to%20EC2%20Instance%20With%20PHP%20By%20Using%20PDO&url=https%3A%2F%2Fmkabumattar.com%2Fblog%2Fpost%2Fhow-to-connect-to-aws-rds-mysql-database-to-ec2-instance-with-php-by-using-pdo "Share on Twitter")[LinkedIn](https://www.linkedin.com/shareArticle?mini=true&url=https%3A%2F%2Fmkabumattar.com%2Fblog%2Fpost%2Fhow-to-connect-to-aws-rds-mysql-database-to-ec2-instance-with-php-by-using-pdo&title=How%20to%20Connect%20to%20AWS%20RDS%20MySQL%20Database%20to%20EC2%20Instance%20With%20PHP%20By%20Using%20PDO&summary=In%20this%20post%2C%20we%20will%20learn%20how%20to%20connect%20to%20AWS%20RDS%20MySQL%20Database%20to%20EC2%20Instance%20With%20PHP%20By%20Using%20PDO.&source=https://mkabumattar.com "Share on LinkedIn")[WhatsApp](https://wa.me/?text=How%20to%20Connect%20to%20AWS%20RDS%20MySQL%20Database%20to%20EC2%20Instance%20With%20PHP%20By%20Using%20PDO%20https%3A%2F%2Fmkabumattar.com%2Fblog%2Fpost%2Fhow-to-connect-to-aws-rds-mysql-database-to-ec2-instance-with-php-by-using-pdo "Share on WhatsApp")[Telegram](https://t.me/share/url?url=https%3A%2F%2Fmkabumattar.com%2Fblog%2Fpost%2Fhow-to-connect-to-aws-rds-mysql-database-to-ec2-instance-with-php-by-using-pdo&text=How%20to%20Connect%20to%20AWS%20RDS%20MySQL%20Database%20to%20EC2%20Instance%20With%20PHP%20By%20Using%20PDO "Share on Telegram")[Reddit](https://www.reddit.com/submit?url=https%3A%2F%2Fmkabumattar.com%2Fblog%2Fpost%2Fhow-to-connect-to-aws-rds-mysql-database-to-ec2-instance-with-php-by-using-pdo&title=How%20to%20Connect%20to%20AWS%20RDS%20MySQL%20Database%20to%20EC2%20Instance%20With%20PHP%20By%20Using%20PDO "Share on Reddit")[Hacker News](http://news.ycombinator.com/submitlink?u=https%3A%2F%2Fmkabumattar.com%2Fblog%2Fpost%2Fhow-to-connect-to-aws-rds-mysql-database-to-ec2-instance-with-php-by-using-pdo&t=How%20to%20Connect%20to%20AWS%20RDS%20MySQL%20Database%20to%20EC2%20Instance%20With%20PHP%20By%20Using%20PDO "Share on Hacker News")[Pinterest](https://pinterest.com/pin/create/button/?url=https%3A%2F%2Fmkabumattar.com%2Fblog%2Fpost%2Fhow-to-connect-to-aws-rds-mysql-database-to-ec2-instance-with-php-by-using-pdo&media=&description=In%20this%20post%2C%20we%20will%20learn%20how%20to%20connect%20to%20AWS%20RDS%20MySQL%20Database%20to%20EC2%20Instance%20With%20PHP%20By%20Using%20PDO. "Share on Pinterest")[Email](<mailto:?subject=How%20to%20Connect%20to%20AWS%20RDS%20MySQL%20Database%20to%20EC2%20Instance%20With%20PHP%20By%20Using%20PDO&body=Check out this article: https%3A%2F%2Fmkabumattar.com%2Fblog%2Fpost%2Fhow-to-connect-to-aws-rds-mysql-database-to-ec2-instance-with-php-by-using-pdo>)

## Comments

## You might also enjoy

More posts on similar topics

[![How to Create a AWS RDS MySQL Database and Connect to it using MySQL Workbench](/_astro/hero.BalpSU3D_jRzSY.webp)](/blog/post/how-to-create-a-aws-rds-mysql-database-and-connect-to-it-using-mysql-workbench)

## [How to Create a AWS RDS MySQL Database and Connect to it using MySQL Workbench](/blog/post/how-to-create-a-aws-rds-mysql-database-and-connect-to-it-using-mysql-workbench)

-   [Mohammad Abu Mattar](/authors/mohammad-abu-mattar)
-   [AWS](/blog/categories/aws)
-   [RDS](/blog/categories/rds)
-   [MySQL](/blog/categories/mysql)
-   [Database Management](/blog/categories/database-management)
-   [Cloud Computing](/blog/categories/cloud-computing)

Introduction RDS is a managed service that makes it easy to set up, operate, and scale a relational database in the cloud. It provides cost-efficient and resizable capacity while automating time-c

[#AWS RDS Setup](/blog/tags/aws-rds-setup)[#MySQL Workbench Connection](/blog/tags/mysql-workbench-connection)[#Relational Database](/blog/tags/relational-database)+4 tags

[read more](/blog/post/how-to-create-a-aws-rds-mysql-database-and-connect-to-it-using-mysql-workbench)

[![How To Connect A EBS Volume To An Windows EC2 Instance Using Powershell/GUI](/_astro/hero.CPGBv147_1yKKK8.webp)](/blog/post/how-to-connect-a-ebs-volume-to-an-windows-ec2-instance-using-powershell-gui)

## [How To Connect A EBS Volume To An Windows EC2 Instance Using Powershell/GUI](/blog/post/how-to-connect-a-ebs-volume-to-an-windows-ec2-instance-using-powershell-gui)

-   [Mohammad Abu Mattar](/authors/mohammad-abu-mattar)
-   [AWS](/blog/categories/aws)
-   [Windows Server](/blog/categories/windows-server)
-   [EC2](/blog/categories/ec2)
-   [EBS](/blog/categories/ebs)
-   [PowerShell](/blog/categories/powershell)

Introduction In this post, we will learn how to connect an EBS volume to a Windows EC2 instance using PowerShell or the Disk Management GUI, and how to mount the volume on the instance. Prereq

[#AWS EBS](/blog/tags/aws-ebs)[#Windows EC2](/blog/tags/windows-ec2)[#Attach EBS Volume](/blog/tags/attach-ebs-volume)+5 tags

[read more](/blog/post/how-to-connect-a-ebs-volume-to-an-windows-ec2-instance-using-powershell-gui)

[![How to Run an Apache Web Server Using Docker on an AWS EC2 Instance](/_astro/hero.WA3H4xsW_KuMrf.webp)](/blog/post/how-to-run-an-apache-web-server-using-docker-on-an-aws-ec2-instance)

## [How to Run an Apache Web Server Using Docker on an AWS EC2 Instance](/blog/post/how-to-run-an-apache-web-server-using-docker-on-an-aws-ec2-instance)

-   [Mohammad Abu Mattar](/authors/mohammad-abu-mattar)
-   [AWS](/blog/categories/aws)
-   [EC2](/blog/categories/ec2)
-   [Docker](/blog/categories/docker)
-   [Apache](/blog/categories/apache)
-   [Web Server](/blog/categories/web-server)

Introduction In this post, we will learn how to run an Apache web server using Docker on an AWS EC2 instance. We will use the following tools:AWS EC2 \[Docker\](

[#AWS EC2 Docker](/blog/tags/aws-ec2-docker)[#Apache Docker](/blog/tags/apache-docker)[#Containerization](/blog/tags/containerization)+4 tags

[read more](/blog/post/how-to-run-an-apache-web-server-using-docker-on-an-aws-ec2-instance)

[![How to Install and Configure Node.js on EC2 Instance Amazon Linux 2](/_astro/hero.CIYv_zEL_2aFueX.webp)](/blog/post/how-to-install-and-configure-nodejs-on-ec2-instance-amazon-linux-2)

## [How to Install and Configure Node.js on EC2 Instance Amazon Linux 2](/blog/post/how-to-install-and-configure-nodejs-on-ec2-instance-amazon-linux-2)

-   [Mohammad Abu Mattar](/authors/mohammad-abu-mattar)
-   [AWS](/blog/categories/aws)
-   [EC2](/blog/categories/ec2)
-   [Node.js](/blog/categories/nodejs)
-   [Linux](/blog/categories/linux)

Introduction Node.js does not exist in the default Amazon Linux 2 repository. So, we need to add the Node.js repository to the system. In this post, we will learn how to install and configure Node

[#Node.js Installation](/blog/tags/nodejs-installation)[#Amazon Linux 2](/blog/tags/amazon-linux-2)[#AWS EC2 Setup](/blog/tags/aws-ec2-setup)+3 tags

[read more](/blog/post/how-to-install-and-configure-nodejs-on-ec2-instance-amazon-linux-2)

[![How To Connect A Two EC2 Instances Database and Files Transfer Using AWS CLI](/_astro/hero.839lq2GB_1XSap0.webp)](/blog/post/how-to-connect-a-two-ec2-instances-database-and-files-transfer-using-aws-cli)

## [How To Connect A Two EC2 Instances Database and Files Transfer Using AWS CLI](/blog/post/how-to-connect-a-two-ec2-instances-database-and-files-transfer-using-aws-cli)

-   [Mohammad Abu Mattar](/authors/mohammad-abu-mattar)
-   [AWS](/blog/categories/aws)
-   [EC2](/blog/categories/ec2)
-   [EBS](/blog/categories/ebs)
-   [EFS](/blog/categories/efs)
-   [Database](/blog/categories/database)
-   [Networking](/blog/categories/networking)

Introduction In this post, I will show you how to share a database and files between two EC2 instances using AWS CLI. I will use AWS CLI to create a VPC, EC2 instances, EBS, EFS, and security grou

[#AWS CLI](/blog/tags/aws-cli)[#EC2 Instance Communication](/blog/tags/ec2-instance-communication)[#EBS Volume](/blog/tags/ebs-volume)+7 tags

[read more](/blog/post/how-to-connect-a-two-ec2-instances-database-and-files-transfer-using-aws-cli)

[![Scaling Up, Staying Strong: Hands-On AWS CloudFormation Techniques for Building Resilient and Scalable Systems](/_astro/hero.CoOFXLCa_Z2wwlfS.webp)](/blog/post/scaling-up-staying-strong-hands-on-aws-cloudformation-techniques-for-building-resilient-and-scalable-systems)

## [Scaling Up, Staying Strong: Hands-On AWS CloudFormation Techniques for Building Resilient and Scalable Systems](/blog/post/scaling-up-staying-strong-hands-on-aws-cloudformation-techniques-for-building-resilient-and-scalable-systems)

-   [Mohammad Abu Mattar](/authors/mohammad-abu-mattar)
-   [AWS](/blog/categories/aws)
-   [Cloud Computing](/blog/categories/cloud-computing)
-   [DevOps](/blog/categories/devops)
-   [Infrastructure as Code](/blog/categories/infrastructure-as-code)
-   [Scalability](/blog/categories/scalability)
-   [Resilience](/blog/categories/resilience)

Introduction Building resilient and scalable systems matters for businesses that need to meet growing demand and maintain high availability. Cloud native architecture, combined with the capabiliti

[#AWS CloudFormation](/blog/tags/aws-cloudformation)[#IaC](/blog/tags/iac)[#Scalable Systems](/blog/tags/scalable-systems)+7 tags

[read more](/blog/post/scaling-up-staying-strong-hands-on-aws-cloudformation-techniques-for-building-resilient-and-scalable-systems)

6 related posts
