AWS SQLSTATE[HY000] [2002] Connection refused - php

I am new to both php and aws and I have come across an issue trying to link the aws to the simple-login php script.
(https://github.com/panique/php-login-minimal).
I'm getting the following error printed:
SQLSTATE[HY000] [2002] Connection refused
This is my code
<?php
ob_start();
session_start();
//set timezone
date_default_timezone_set('Europe/London');
//database credentials
define('DBHOST','instancename.cfci0i1rm4rl.us-east-2.rds.amazonaws.com');
define('DBUSER','name_root');
define('DBPASS','rootpassword');
define('DBNAME','login');
//application address
define('DIR','https://union.washmel.com/user/login/auth/lr/');
define('SITEEMAIL','noreply#domain.com');
try {
//create PDO connection
$db = new PDO("mysql:host=".DBHOST.";charset=utf8mb4;dbname=".DBNAME, DBUSER, DBPASS);
//$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_SILENT);//Suggested to uncomment on production websites
$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);//Suggested to comment on production websites
$db->setAttribute(PDO::ATTR_EMULATE_PREPARES, false);
} catch(PDOException $e) {
//show error
echo '<p class="bg-danger">'.$e->getMessage().'</p>';
exit;
}
//include the user class, pass in the database connection
include('classes/user.php');
include('classes/phpmailer/mail.php');
$user = new User($db);
?>
as follows is the link to the error on my site as it currently stands.
https://union.washmel.com/user/login/auth/lr/index

Related

SQLSTATE[HY000] [2002] Connection refused -> [with domain]

I tried different ports and restarting service mysql. All answerers that I found where for localhost and didn't helped me. Also tried different time zones thought it might be a problem. Don't know tho what to put under 'application address' maybe that is the problem. Thank you for answer.
<?php
ob_start();
session_start();
//set timezone
date_default_timezone_set('Europe/Amsterdam');
//database credentials
define('DBHOST','servers_ip');
define('DBUSER','root');
define('DBPASS','pass');
define('DBNAME','dbname');
//application address
define('DIR','https://domain/test/');//includes
define('SITEEMAIL','noreply#domain.com');//i leave it like this
try {
//create PDO connection
//
$db = new PDO("mysql:host=".DBHOST.";port=8889;dbname=AppDatabase".DBNAME, DBUSER, DBPASS);
//
//$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_SILENT);//Suggested to uncomment on production websites
$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);//Suggested to comment on production websites
$db->setAttribute(PDO::ATTR_EMULATE_PREPARES, false);
} catch(PDOException $e) {
//show error
echo '<p class="bg-danger">'.$e->getMessage().'</p>';
exit;
}
//include the user class, pass in the database connection
include('classes/user.php');
include('classes/phpmailer/mail.php');
$user = new User($db);
?>

I cannot connect MySQL web server

I added a database to my cPanel, but I cannot connect it.
try{
$db = new
PDO('mysql:host=markus.veridyen.com;dbname=sosyalki_pratiki1_ipss;charset=utf8','sosyalki','password');
}catch(PDOException $e){
echo 'Hata: '.$e->getMessage();
}
When I try to connect my database, it gives me this error message:
Hata: SQLSTATE[HY000] [2002] The connection could not be established because the target machine actively refused.
Notice: Undefined variable: db in C:\xampp\htdocs\ipss\survey.php on line 55
You still can use localhost as server name on a live server.
I'll assume you are using cpanel.
<?php
$servername = "localhost";
$username = "username"; //your cpanel username
$password = "password"; //your cpanel password
try {
$conn = new PDO("mysql:host=$servername;dbname=DatabaseName", $username, $password);
// set the PDO error mode to exception
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
echo "Connected successfully";
} catch(PDOException $e) {
echo "Connection failed: " . $e->getMessage();
}
?>

Php Pdo database connectivity error

I have a website hosted by united domains. It was working fine with MySQLi but I changed it to PDO. Now I cannot get the database to work. I've tried changing $host to 127.0.0.1, and to localhost. If I use localhost, I get the error SQLSTATE[HY000] [2002] No such file or directory.
If I use 127.0.0.1 I get SQLSTATE[HY000] [2002] Connection refused.
I have also tried by changing collation but it still doesn't work. It works perfectly fine on my other domain that is on Hostinger.
$host = "localhost"; // I have tried using both 127.0.0.1 and localhost here
$db = "db_name"; //db name goes here
$username = "username"; //username goes here
$password = "password"; //password goes
try {
//Creating a PDO instance
$conn = new PDO("mysql:host=$host;dbname=$db", $username, $password);
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$conn->setAttribute(PDO::ATTR_EMULATE_PREPARES, false);
$conn->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE, PDO::FETCH_OBJ);
}catch(\PDOException $e){
// if connection fails, show PDO error
echo "Connection Failed: ". $e->getMessage();
}

Connection Refused on connecting to a remote server

I'm new to web development. I bought a server on DigitalOcean, created a droplet and installed LAMP. I have a simple PHP script to connect to MySQL. Here's my PHP script(conn.php)
<?php
$servername = "xxx.xx.xx.xxx";
$username = "root";
$password = "my_password";
try {
$conn = new PDO("mysql:host=$servername;dbname=mysql", $username, $password);
// set the PDO error mode to exception
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
echo "Connected successfully";
}catch(PDOException $e) {
echo "Connection failed: " . $e->getMessage();
}
?>
Now, when I try to run on my local "xxx.xx.xx.xxx/conn.php", it's printing
Connection failed: SQLSTATE[HY000] [2002] Connection refused

SQLSTATE[HY000] [2002] Connection refused

I am trying to create a simple mailing list webpage. Using LAMP. Here is the code I have for connecting to my database:
<?php
function insert()
{
$servername = "127.0.0.1";
$username = "root";
$password = "(my password here)";
$dbname = "(my db name here)";
try
{
// preparing database handle $dbh
$dbh = new PDO("mysql:host=$servername;dbname=$dbname", $username, $password);
// set the PDO error mode to exception
$dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
}
catch(PDOException $e)
{
echo ("Could not connect to server.\n");
echo ("getMessage(): " . $e->getMessage () . "\n");
}
}
?>
The function continues after that but this connection attempt gets cung up on the catch, and throws the "SQLSTATE[HY000] [2002] Connection refused" error. I have tried:
-Changing the $servername to localhost, but this gives me a different error: "SQLSTATE[HY000] [2002] No such file or directory"
-trying to specify all different ports
-checked all my info, database name and password.
-I can log into phpmyadmin and see that my database is fine
-looked at all other questions on this topic, with no help found.

Categories