I have been at this for days trying to get this to work. I just followed the AWS tutorial and installed a LAMP stack on an EC2 instance of AMAZON LINUX. I have been trying to just do basic connections to it. Just easy Insert statements and I continually get this error:
The server is working.. i have a test file that works.
My database is set up and ready. It will receive an id, a username, a password, and an email address. But I cannot connect to mysql. I have: 1) set up a user and granted all privelages 2) checked the my.cnf file. There is no "bind-address" listed to change, but I added one in just to check. I added my server IP. Still did not work. 3) Security groups are wide open. I have SSH, HTTP, HTTPS, and MySQL accepting inbound connects from "Anywhere".
Here is the simple task I'm trying to accomplish:
<?php
$servername = "xx.xx.xxx.xx;
$username = "ec2-user";
$password = "112233445566";
$dbname = "db";
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "INSERT INTO MyGuests (id, firstname, lastname, email) VALUES(1, Fake, User, fake3mail@gmail.com);
if ($conn->query($sql) === TRUE) {
echo "Table MyGuests created successfully";
} else {
echo "Error creating table: " . $conn->error;
}
$conn->close();
?>
Task I'm trying to accomplish: connect to MySQL database and insert a row of data.
Actual result: Error and the inability to connect.
I have scoured the internet for DAYS now trying to get this to work. What the heck am I missing here?! Are there even more configurations that must be done? I have tried every single thing that I've read and it does not work .
When I go to this page, it is working so I will assume that you fixed that issue: ec2-54-174-71-33.compute-1.amazonaws.com.
For your database, change the $servername
to "localhost"
. There is no need to do a DNS lookup to resolve your own system.
If you are having problems with your PHP code (you do from the last comment), look at your PHP.INI file. In this file will be the location for your error logs. Look for the line "error_log = "
This file will store the errors for your PHP programs (scripts). Just look at the bottom of the file for the latest error messages. If error_log is not configured, then configure it.
One problem that I do see with your code is that you are not put single quotes around your strings. Your values should look like this: VALUES(1, 'Fake;', 'User', 'fake3mail@gmail.com')
I actually figured out what's going on. apparently you cannot grant permission to ec2-user (the default user) as I was doing. I created a new separate user and granted all permissions to that user and it works fine now.