0

Ok so I created an ubuntu instance from AWS and downloaded LAMP on it as you would in a regular Ubuntu OS. When I try to connect to the db from my php script it's rejecting the connection. saying "Connection failed: Connection refused". So I guess my question is this: Does amazon not allow you to connect to a db without using their RDS database service or am I putting something wrong here? (I've hidden some of the data for security purposes, the ... are numbers of my instance). When I put "ec2-34-...-..-64.compute-1.amazonaws.com" into the browser the apache message comes up so I don't see why this is not working as a server name?

$servername = "ec2-34-...-..-64.compute-1.amazonaws.com";

$username = "root";

$password = “hidden"; 

$dbname = "questions87";


// Create connection

$conn = mysqli_connect($servername, $username, $password, $dbname);

// Check connection

if (!$conn) {

  die("Connection failed: " . mysqli_connect_error());

}
Ashan
  • 18,898
  • 4
  • 47
  • 67
do734
  • 37
  • 9

1 Answers1

1

You need to change your EC2 Security Group Inbound Rules related with 3306, you can find that on the AWS FAQ. Then, you need to be sure that your user@YOURIP have all permissions on your MySQL database. For your Inbound Rules it is recommended to use a Custom Rule to your IP, not All Traffic.

Reference and further reading:

http://docs.aws.amazon.com/AWSEC2/latest/UserGuide/authorizing-access-to-an-instance.html

Roberto Gonçalves
  • 3,186
  • 4
  • 13
  • 27
  • Hey Thanks! I'm out right now but when I get back I'll try it. – do734 Aug 13 '17 at 20:08
  • so i've opened the ports I've created a user and password and granted all permissions and then when I try to log in to mysql I get this "ERROR 1045 (28000): Access denied for user..." I guess I just have to learn how to use RDS although I don't know why AWS would force you to do this...? – do734 Aug 13 '17 at 22:44
  • So, at least you are connecting! Now, it might be that you need to grant on user@YOURIP, and there is nothing more about AWS to worry about, it is just your RDS. Take a look at this: https://stackoverflow.com/questions/8348506/grant-remote-access-of-mysql-database-from-any-ip-address – Roberto Gonçalves Aug 13 '17 at 23:24
  • thanks. I think that's the answer but now when I try to grant access it says cant find matching row in the user table. What a headache! haha but I'm giving you the answer because you did help and I appreciate it. I'll figure it out. I don't want to use RDS because I'll have to pay. Cheers! – do734 Aug 14 '17 at 00:01
  • Remote access it is always a problem at the beginning, but then you will got it working! Good luck! – Roberto Gonçalves Aug 14 '17 at 00:18
  • I got it working on creating a user and granting permissions! the % worked! one last question. for connecting now is this right: "$servername = "ec2-34-...-..-64.compute-1.amazonaws.com"; or do I have to add anything else? – do734 Aug 14 '17 at 00:34
  • I guess you need to use your EC2 PUBLIC IP, server=, then you can connect your machine to your EC2 instance, I've never used the EC2 public DNS, just the PUBLIC IP. – Roberto Gonçalves Aug 14 '17 at 00:56
  • yea ok I'll keep messing with it. Thanks! – do734 Aug 14 '17 at 01:01