1

I've set up an RDS and an EC2 on amazon web services. The RDS is in one security group, and the EC2 in another. The EC2 security group accepts http, https and ssh traffic (though I have tried setting it to all traffic) from any location, while the RDS security group accepts all inbound traffic from itself, and the EC2 security group.

I have some php files on the EC2, with

$server = $_SERVER['DB_SERVER'];
$username = $_SERVER['DB_USERNAME'];
$password = $_SERVER['DB_PASSWORD'];
$database = $_SERVER['DB_DATABASE'];

$connection = new mysqli($server, $username, $password, $database);
echo $connection->connect_error;

where DB_SERVER etc are listed in dbinfo.inc in /var/www/inc/dbinfo.inc, as per this tutorial: http://docs.aws.amazon.com/AmazonRDS/latest/UserGuide/CHAP_Tutorials.WebServerDB.CreateWebServer.html

This gives the response "No such file or directory", which I am told is because the mysql.sock file is not being found.

In /etc/my.cnf I have:

[mysqld]
datadir=/var/lib/mysql
socket=/var/lib/mysql/mysql.sock
*bunch of comments*

[mysqld_safe]
log-error=/var/log/mysqld.log
pid-file=/var/run/mysqld/mysqld.pid

but no mysql.sock exists in /var/lib/mysql (or /var/lib/mysql/mysql)

Running

find . mysql.sock 

and

find . mysqld.sock 

both give "No such file or directory"

Thanks for your help

w1nter
  • 337
  • 4
  • 23
  • Print the values of `$server` and the other variables before calling `connect()`. I'll bet they are empty. It is completely invalid to try to use a socket file like `mysql.sock` when connecting to RDS, so your problem is **not** that the file is missing. The problem is that your system is trying to look for it -- it isn't needed and your configuration should not be checking for its existence and should not be throwing that error... but it probably would try that by default if `$server` is not actually getting set. – Michael - sqlbot Nov 06 '16 at 20:36
  • ...and since your example code does not show the necessary ``, it may be that the `dbinfo.inc` file isn't actually getting loaded and the values would therefore most likely be undefined... – Michael - sqlbot Nov 06 '16 at 20:42
  • good to know I don't need a socket file! I added include "../inc/dbinfo.inc" into the php file, and also made it echo $server etc, but with and without the include, it does not echo anything before it shows a 500 internal server error. – w1nter Nov 07 '16 at 10:55
  • Well... that's progress. If `$server` is empty then you definitely have a PHP issue, here, not an RDS issue. – Michael - sqlbot Nov 07 '16 at 14:33
  • It's not that $server is empty, well it could well be, but I can't see its contents regardless. I cannot echo a fixed string before I get the 500 internal server error flashing up on the console and then disappearing. – w1nter Nov 08 '16 at 11:27
  • Read the error log to find the reason for the 500 error. – Michael - sqlbot Nov 08 '16 at 11:42
  • It says PHP Fatal error: Call to a member function fetch_assoc() on boolean, which is because I am performing fetch_assoc() on the result of the query of the database, which I assume is returning False - fair enough. However, if I echo the query that it is making, and pass this exact same query into the database directly, it returns the result just fine. – w1nter Nov 08 '16 at 18:44
  • i would suggest testing the network connection from your EC2 host to your RDS host first, outside of this program to make sure your security group settings are correct. when i set up mysql, for example, i'll 'telnet 3306' from my ec2 instance to make sure the mysql client will be able to connect ok. ensure your connection between the two is good, then work on your program. HTH – matias elgart Nov 13 '16 at 13:13

1 Answers1

0

I had faced same problem while I was trying to deploy php application on aws ec2 and hosting database on rds

First of all, do not follow the amazon documentation for connecting rds database. reason $_server['anything'] returns empty value. So
$server = $_SERVER['DB_SERVER']; in here $server holds nothing but a empty value

Use following code instead:

<?php
$dbhost = 'your rds instance endpoint';
$dbport = 'port numberdefault 3306 ';
$dbname = 'name of data base';
$charset = 'utf8' ;
$dsn = "mysql:host={$dbhost};port={$dbport};dbname={$dbname};charset=   {$charset}";
$username = 'username ';
$password = 'password';

try {

$pdo = new PDO($dsn, $username, $password);

$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

$sql = "your sql query";

$pdo->exec($sql);
echo "successfully connected"; 
}
catch(PDOException $e)
{
t;    
echo "Connection failed: <br>  ". $e->getMessage(); 

}

$conn = null;

?>
Aniruddh Parihar
  • 3,072
  • 3
  • 21
  • 39