sasi
sasi

Reputation: 99

php: SQLSTATE[HY000] [2002] No connection could be made because the target machine actively refused it

I have create register and login form using php. And i had took the source from google.

So i have added all the corresponding code, when run index.php.

It shows,

SQLSTATE[HY000] [2002] No connection could be made because the target machine actively refused it.

I tried for fixing this problem using stackoverflow and google, still can't to rectify.

I Am using xampp. In xampp control panel, apache and mysql modules are running. and apache port is 80,443 and mysql port is 3306.

May i know, any other my mistake?..

Can someone help me? Any help would be appreciated.

Thanks in advance.

This is my config.php:

<?php
ob_start();
session_start();

//set timezone
date_default_timezone_set('Europe/London');

//database credentials
define('DBHOST','localhost');
define('DBUSER','root');
define('DBPASS','');
define('DBNAME','register');

//application address
define('DIR','http://domain.com/');
define('SITEEMAIL','[email protected]');

try {

    //create PDO connection 
    $db = new PDO("mysql:host=".DBHOST.";port=8889;dbname=".DBNAME, DBUSER, DBPASS);
    $db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

} 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');
$user = new User($db); 
?>

Upvotes: 2

Views: 22942

Answers (6)

Avinash Thombre
Avinash Thombre

Reputation: 314

For Drupal Users -

This happens generally because of max_execution time for communicating server to mysql. Increase max execution time from 60 sec to upper limit you want and try refresh the page again.

It worked for me.

Upvotes: 0

KawaiKx
KawaiKx

Reputation: 9920

I suffered this error too. I did following things: 1. find out the port number used by mysql. In phpmyadmin, I ran the following sql:

show variables;

it shows a list of variables of mysql. scroll down, you will find port number.

  1. I edited the php code like this:

$dsn = 'mysql:host=127.0.0.1;port=3307; dbname=learnphpfast';

it worked like a charm!

Upvotes: 0

Nikhil Goswami
Nikhil Goswami

Reputation: 81

This error usually occurs when some other app like skype and IIS are using ports of Apache i.e. 80 and mysql server i.e. 3306

You can eighter uninstall these apps or change port numbers of the localhost to something else and make correct changes in settings.php

Upvotes: 0

Supreme
Supreme

Reputation: 109

Sasi, if your running this on a local server it may be your setup for windows the local server need to be configured to send email on.

Here you can find how to configure XAMPP to send mail from localhost.

Sorry that i answer you here, but i don't have enough reputation to make comments :D

Upvotes: -1

$db = new PDO("mysql:host=".DBHOST.";port=3306;dbname=".DBNAME, DBUSER, DBPASS);

you should change the port number to default: 3306! hope this helps

Upvotes: 2

Axel Amthor
Axel Amthor

Reputation: 11096

didn't you say your mysql port is 3306:

$db = new PDO("mysql:host=".DBHOST.";port=8889
//------------------------------------------^

this might help:

$db = new PDO("mysql:host=".DBHOST.";port=3306

Upvotes: 4

Related Questions