mysql connection with server(cpanel) error

Warning: mysqli::__construct(): (HY000/2002): A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond.

try{   
    $mysqli = new mysqli('cpanelhost','cpanel_db_user','cpanel_db_password','cpanel_db','port');
    if ($mysqli -> connect_errno) { 
        echo "Failed to connect to MySQL: " . $mysqli ->connect_error;
        exit();      
    } 
    $sql = "INSERT INTO table (column1, column2) VALUES ('value1', 'value2')";                    
    $mysqli->query($sql) ;      
    $mysqli->close();
}
catch(Exception $e){
    $mysqli->close();
    return $e;  
}

Upvotes: 0

Views: 1063

Answers (1)

user18203664
user18203664

Reputation:

I would highly recommend you to:

  1. Ask your hosting provider if they ALLOW remote MySQL connections.
  2. Ask your host if port 3306 is open.

If the above is covered, then you will need to:

  1. Add the IP address of the remote machine ( where you are trying to establish this connection ) to "Remote MySQL" in cPanel.
  2. Use your server's hostname or IP as the MySQL Host in your connection configuration.

And if you still cannot establish a successful connection, I would recommend switching to a managed hosting provider which fully supports remote MySQL connections.

Upvotes: 1

Related Questions