How to configure the amazon relational database service to use in amazon cloud applications

This is new series where in I will explain step by step configuration of the amazon cloud servers to host and run php mysql applications


In this session I will first explain to use ARDS (Amazon Relational Database Service) and then binding the ARDS instance to get accessible from your app.


The main requirement is you should have AWS account.  Login with your credentials and go ahead to create your first ARDS db

First select RDS tab from the top menu

Then select launch new instance

Select available instances like mysql,oracle,MSSQL etc

Fill in the necessary details like instance class, storage, db identifier, username, password etc



Once selected all the necessary options the DB instance is now seen on RDS dashboard.

Next Step is to authorize access to your database

Select the db security group from the left hand side options

Possible select the default security group and grant access for application or the specific IP to access the same.

After authorized access you can now access your database

Note the EndPoint url username and password

With this credentials and security settings you can now connect to your db instance.


Save Image in Mysql Database

People have different opinions on saving images in database. Some says, “Why bother database if we can handle this by saving images to disk”. I am agree with this;).

Most of the requirements can be fulfilled by saving the images to disk. This reduces the unnecessary load on MySQL.

Here is the small code to save the image in MySQL with the help of PHP.

Images are saved in MySQL as BINARY data. BINARY data can not be saved in varchar or char data types, for this purpose we need a data type which can handle binary data. BLOB columns are treated as binary strings (byte strings). The following table is fulfilling our requirements for a simple test with a BLOB field.

CREATE TABLE `images` (
  `image` BLOB NOT NULL,
  PRIMARY KEY  (`id`)

Now we have a MySQL table ready to store the image. Next steps are easy i.e.

  1. Read the image
  2. Encode the image data
  3. Save binary data in DB

These 3 steps are performed with the following PHP code

$image = chunk_split(base64_encode(file_get_contents("image.jpg")));
$query = "INSERT INTO images (image) VALUES('$image')";
mysql_query($query) or die(mysql_error());
echo "Image id is ".mysql_insert_id();

Now we have saved the image in database successfully. The next step is to display the image.

Ohh not satisfied yet.. want to display in html page too.. No issues I have solution for that too…

<title>Image Test</title>
<h1>Displaying image from database</h1>
<img src="showimage.php" />
// showimage.php
header('Content-type: image/jpeg');
$query = "SELECT image from images where id=1";
$rs = mysql_fetch_array(mysql_query($query));
echo base64_decode($rs["image"]);