'How do I access MysqlServer on my PC with a PHP script from a remote hosted server?

I installed MysqlServer and Mysql Workbench on my PC. The server is running well (green color). Now I have a shared hosted server and I would from this server via a PHP script to fetch data from the database on my PC. For the moment there is not database or any data because I just want to first make the connexion between the two servers. I tried a lot of times to make a connexion without any success until now.

→ Using Workbench via Option file, I put in bind-address, the IP provided by my hosted server. (= the remote server that wants to access to my MysqlServer on my PC).

→ I also enabled SSL connexions as my hosted website is on SSL.

→ Then, in users and privileges, I put % on the root user so that root can connect to the MysqlServer on my PC from another host (not only localhost).

→ I've allowed incoming rules for port 3306 on my Windows firewall.

→ The basic code I am using is a basic PDO PHP database connexion where $host is the public IP of my pc, $port is 3306, $root is root and the password is the one I created during the installation of MysqlServer.

The basic code I am using is :

try
{
    $dbh = new PDO("mysql:host=".$host.";port=".$port, $root, $root_password);
}
    catch (Exception $e)
{
    die('Erreur : ' . $e->getMessage());
}
?>

→ The error I have is : SQLSTATE[HY000] [2002] Can't connect to MySQL server on « Public Ip address of my PC ».

Does someone already have this kind of problem ? Can you see any thing strange that could helped me ? Any answers, suggestions, remarks or questions are greatly welcomed.



Solution 1:[1]

There are two types to connect to a database-

$connect = new PDO('mysql:host=localhost;port=3306;dbname=dbname','root','');
$connect->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

OR

$connect = new mysqli("localhost","root","","dbname");

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Mukesh