简体   繁体   中英

Error: Unable to connect to MySQL. Debugging error: 2002 Debugging error: Connection refused

I'm having troubles with my SQL, I can connect to SQL via Mamp and access Phpmyadmin, though when I try to display my database on browser with that code into my code editor:

<?php

$link = mysqli_connect("localhost:8000", "root", "password", 
        "Portfolio");

if (!$link) {
    echo "Error: Unable to connect to MySQL." . PHP_EOL;
    echo "Debugging error: " . mysqli_connect_errno() . PHP_EOL;
    echo "Debugging error: " . mysqli_connect_error() . PHP_EOL;
    exit;
}

echo "Success: A proper connection to MySQL was made! The Portfolio 
      database is great." . PHP_EOL;
echo "Host information: " . mysqli_get_host_info($link) . PHP_EOL;

mysqli_close($link);

?>

The browser indicates the message I wrote in my code, so a connection is made somewhere: Error: Unable to connect to MySQL. Debugging error: 2002 Debugging error: Connection refused

The url I enter everytime is : http://localhost:8888/bdd.php "bdd.php" being my file name.

This is getting me crazy, i've tried so many things (changing code with "path" on php init, reinstalling mamp, mysql..) and still have no idea of what's wrong, I began coding a few months ago and really need help on that one!

Cheers!

The default port on MAMP for mysql is 8889, but the port that php expects to use for mysql is 3306. So you need to open MAMP, go to preferences, and change the MAMP mysql port to 3306, then restart the mysql server. Now the connection should be successful with host=localhost, user=root, pass=root.

I recommend you to use PDO to prevent SQL injection.

PDO Example:

<?php
$servername = "localhost";
$username = "username";
$password = "password";

try {
    $conn = new PDO("mysql:host=$servername;dbname=myDB", $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();
    }
?>

MySQLi Object-Oriented

<?php
$servername = "localhost";
$username = "username";
$password = "password";

// Create connection
$conn = new mysqli($servername, $username, $password);

// Check connection
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
} 
echo "Connected successfully";
?>

MySQLi Procedural

<?php
$servername = "localhost";
$username = "username";
$password = "password";

// Create connection
$conn = mysqli_connect($servername, $username, $password);

// Check connection
if (!$conn) {
    die("Connection failed: " . mysqli_connect_error());
}
echo "Connected successfully";
?>

Mention Host name as "Localhost". $link = mysqli_connect("localhost", "root", "password","Portfolio");

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM