0

After reading over a number of the similar topics here and on other various internet forums, I'm still having an issue with my PHP code which is throwing me for a loop... Here's the error that the error_log file is giving me:

[30-Apr-2018 17:05:11 UTC] PHP Warning:  mysqli::mysqli(): (HY000/2002): Connection refused in /home/nicktric/public_html/index.php on line 12

And here is the current, and very simple, state of my code:

<!DOCTYPE html>
<html>
<body>

<?php
$servername = "127.0.0.1";
$username = "nicktric_admin";
$password = "########";
$dbname = "nicktric_routes";

// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
} 

$sql = "SELECT name, server, status FROM routeStatus";
$result = $conn->query($sql);

if ($result->num_rows > 0) {
    // output data of each row
    while($row = $result->fetch_assoc()) {
        echo "<br> id: ". $row["name"]. " - Name: ". $row["server"]. " " . $row["status"] . "<br>";
    }
} else {
    echo "0 results";
}

$conn->close();
?> 

</body>
</html>

I've tried the following so far:

  • Changing hostname between localhost, 127.0.0.1, and the server shared IP
  • Modifying the PHP socket to no avail, as I was unable to find the proper configuration to do so on CPanel

Additionally, some things to note are:

  • I am doing this off of CPanel web hosting
  • The database name, username, and password are all correct and linked to the database (Password covered for security)
Nick
  • 181
  • 1
  • 17
  • Firstly, I use php on cpanel accessing the local mysql instance, and I use localhost, which works fine. (And I honestly don't think you should need to change the configuration in any way - all this is core functionality in cpanel.) Secondly, does the user you have specified definitely have access to this specific database? – MandyShaw Apr 30 '18 at 18:58

2 Answers2

0

check mysqld is running and that your firewall is allowing the mysql database through.

Edit your php.ini file and make sure extension extension=php_mysqli.dll is enabled on Windows or extension=php_mysqli.so on Linux.

Try the answer here: Test Mysql

  • There are two settings you have to check for this to work (assuming you have MySQL server installed of course):
  • Check the value of mysql.default_socket in your PHP configuration.
  • Check the value of socket in your MySQL configuration file under the [mysqld] heading.
  • Those values have to be identical; if they're not, change one to match the other and restart respective service.
Diego Avila
  • 700
  • 7
  • 24
0
In /etc/mysql/my.cnf you should see this near the top of the file:

[client]
port          = 3306
socket        = /var/run/mysqld/mysqld.sock
Change socket to the location of your MemSQL socket file. By default, this is /var/lib/memsql/data/memsql.sock.

hope that will work !
rsethi
  • 35
  • 8
  • Any idea where I would find this in a Cpanel instance? I've found the .ini in the CPanel editor for it, but not the .cnf yet... – Nick Apr 30 '18 at 19:39
  • There is no internal MySQL command to trace this, it's a little too abstract. The file might be in 5 (or more?) locations, and they would all be valid because they load cascading. /etc/my.cnf /etc/mysql/my.cnf $MYSQL_HOME/my.cnf [datadir]/my.cnf ~/.my.cnf Those are the default locations MySQL looks at. If it finds more than one, it will load each of them & values override each other (in the listed order, I think). Also, the --defaults-file parameter can override the whole thing, so... But thanks to it being so confusing, there's a good chance it's just in /etc/my.cnf. – rsethi May 01 '18 at 02:51