PHP connecting to MYSQL

0

Hi all,

Total noob with web services. I'm trying to connect a php script to a AWS MYSQL server.

I can get a connection via MySQL workbench using the same settings I'm trying to use with a php script. The PHP script is failing.

This is the error I'm getting:
Connection failed: Can't connect to MySQL server on 'sayings.cfb0xnn82xyg.ap-southeast-2.rds.amazonaws.com' (111 "Connection refused")

I've set the public accessibility to yes.

For the subnet and security, I've left it as default, but I have no options in the Network & Security drop down menu to change.

I changed the security group using this website. https://docs.aws.amazon.com/vpc/latest/userguide/VPC_SecurityGroups.html#AddRemoveRules
The default is set to 0:0:0:0/0 and ::/0 which from my understanding should allow anyone to access the database.

I'm guessing my PHP script is okay because is copied verbatim from another site.

Any help would be appreciated. I just want a really simple connection to a database to pull data from to go to C#(Unity). Or if someone else has a simpler way than using a PHP script I would be really appreciative.

Web connections are not my strength if you can keep it low level as possible it would be appreciated.

cheers,

Steve

<?php $servername = "********.0xnn86xyg.ap-southeast-2.rds.amazonaws.com"; $username = "Shendo"; $password = "*******"; $dbname = "exams"; // Create connection $conn = new mysqli($servername, $username, $password, $dbname); // Check connection if ($conn->connect_error) { die("Connection failed: " . $conn->connect_error); } ?>

Edited by: shendo on Feb 18, 2020 3:14 PM

Edited by: shendo on Feb 18, 2020 3:54 PM

shendo
asked 4 years ago1191 views
1 Answer
1

To solve this issue:

if you're getting a fatal error
eg: "mysqli_connect not defined"
first make sure you have php-mysql installed
if you don't, you can install it using

sudo apt-get install php-mysql -y
or
yum install php-mysql

Note: Connect to hostname "localhost" not your amazon server url
Also make sure that you have port 3306 opened on your inbound rules

to add new rule on your instance

  1. goto security groups

  2. click create security group

  3. add inbound rule

  4. select mysql/ (default port 3306 will be selected on port field)

with all that done, make your connection in this way;

<?php $servername = "localhost"; $username = "Shendo"; $password = "*******"; $dbname = "exams"; // Create connection $conn = new mysqli_connect($servername, $username, $password, $dbname); // Check connection if (!$conn) { die(mysqli_error()); } else{ echo"connected!"; //---continue to do other things } ?>
tryHard
answered 3 years ago

You are not logged in. Log in to post an answer.

A good answer clearly answers the question and provides constructive feedback and encourages professional growth in the question asker.

Guidelines for Answering Questions