Using PHP to connect to AWS Lightsail MYSQL Database instance - php

I hope this question falls within the scope of this site.
I have created a Lightsail instance in AWS and added a MYSQL database to it. I have been able to connect to the database with Workbench, but not with a PHP file hosted in my instance.
I have checked the servername, username, password and dbname 20 times. Every time I try to connect I get the error message: "Connection failed 6: Unknown database 'Databasename-1'". Does anybody know of a guide on troubleshooting connecting to a MYSQL database on Lightsail?
<?php
$servername = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx.rds.amazonaws.com";
$username = "xxxxxx";
$password = "xxxxxx";
$dbname = "Databasename-1";
$conn = new mysqli($servername, $username, $password, $dbname);
if ($conn->connect_error) {
die("Connection failed 6: " . $conn->connect_error);
}

So, i think it turns out I did not know how to get the database name from my lightsail instance and possibly not understand how to use Workbench.
I was using a database name that did not exist.
I just started over and created everything from scratch from the command prompt and everything works as expected.

Related

How to fix MySQL State [2002] Database "Connection Refused "

I am setting a new live server for my Laravel application its work perfectly on localhost but not on live server. Now its showing SQL State [2002] connection refused. I also try with mysqli_connect and PDO but the error remains the same. Is possible a problem with Hosting Provider?
<?php
$servername = "examrunner.com";
$database = "XXXXXXXX";
$username = "XXXXXXX";
$password = "XXXXXXXXXX";
// Create connection
$conn = new mysqli($servername, $username, $password);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
echo "Connected successfully";
?>
Use server name as localhost. Because cPanel hosts your databases locally, use localhost as the database's hostname.
$servername = "localhost";
Please check below mentioned points to resolve this issue :
Check database and user exists
Make sure that Database user and Database connected to each other and you have given sufficient privileges to user.
If site hosted on current serve user hostname = 'localhost'
Try to print detailed error.

Not able to make connection to mysql 8 in php

I am trying to connect to mysql server using php but it gives the following error
Connection failed: The server requested authentication method unknown to the client
mysql server version is 8.0.12 and php version is 7.2.9.
My code connect to mysql server
<?php
$servername = "127.0.0.1";
$username = "root";
$password = "mypassword";
// Create connection
$conn = new mysqli($servername, $username, $password);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
echo "Connected successfully";
?>
This question has been previously asked before here but the solution did not work for me so I am asking this again.
Thanks
edit: I re-installed php now it gives the following for the same code
This page isn’t working
127.0.0.1 is currently unable to handle this request.
HTTP ERROR 500
I had a similar problem and found that you can change mysql authentication by running the following code via Workbench
ALTER USER root#localhost IDENTIFIED WITH mysql_native_password BY 'new-password-here'

Connecting to RDS from PHP does not work, but works from console

I recently set up RDS server using MySQL dataset and using EC2 beanstalk as a server.
I have been able to connect to dataset from
console of local machine using endpoint url, username, password
from PHP on local machine, using following code:
$servername = "xxxxx.xxxxxxxxxxxxxx.us-east-1.rds.amazonaws.com";
$username = "xxxxx";
$password = "xxxxxxx";
$dbname = "xxxxxxx";
// Create connection
$conn = mysqli_connect($servername, $username, $password, $dbname, 3306);
// Check connection
if (! $conn) {
echo "not connecting..!!";
die("Connection failed: " . mysqli_error($conn));
}
This works correctly, inserts data into the database in the RDS.
I ssh to EC2, check connect from console (using mysql -h -u -p). This too, work correctly. I can change the database from here and it is indeed reflected.
Next, I deploy the exact same code on EC2, and hope it works via PHP as well, as that code is already tested. But it does not.
I checked SELinux status is disabled
What else could be a problem?
I should mention: I am using Amazon Linux, I installed 'httpd' on local it.

Connecting to a MySQL db created in an Ubuntu AWS instance

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());
}
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

Connect to remote MySql server with XAMPP

I have an script that i want it to run for more that 5 minutes. But my current hosting service does not allow me to modify the max_time_limit in the php.ini (That is set to 2 minutes)...
So i thought that i could run the script with XAMPP and send the data to my database but i cant connect to the remote database.
This is the code for the connection:
$servername = "217.70.186.108"; //I've also tried with the name of the webpage (metagame.gg)
$username = "the_username"; //the username is not root since I've read that root can only connect from localhost. This user has all privileges. This user was created with the permision to be connected from any server (%)
$password = "the_password";
$dbname = "the_db";
$conn = new mysqli($servername, $username, $password, $dbname);
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
It displays this error:
Connection failed: An error occurred during the attempt to connect because the connected party did not properly responded after a period of time, or failed in the established connection because connected host has failed to respond.
Any help would be highly appreciated

Categories