search

Home  >  Q&A  >  body text

How to access MysqlServer on my PC via a PHP script on a remote hosting server

I installed MysqlServer and Mysql Workbench on my computer. The server is running well (green). Now I have a shared hosting server and I want to get data from a database on my computer via a PHP script. There is no database or any data yet as I just want to establish a connection between the two servers first. I have tried to connect many times but so far without success.

→ Using Workbench's options file, I put in bind-address the IP address provided by the hosting server. (i.e. the remote server I want to access from MysqlServer on my computer).

→ I also enabled SSL connection since my hosting website is SSL based.

→ Then, in Users and Permissions, I put % on the root user so that root can connect to MysqlServer on my computer from another host (not just localhost).

→ I have allowed the inbound rule for port 3306 on Windows Firewall.

→ The basic code I'm using is a basic PDO PHP database connection where $host is my computer's public IP, $port is 3306, $root is root, and the password is the password I created when installing MysqlServer .

The basic code I'm using is:

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

→ The error I encountered was: SQLSTATE[HY000] [2002] Unable to connect to the MySQL server "My Computer's Public IP Address".

Has anyone encountered this problem? Can you see anything weird that could help me? Any answers, suggestions, comments or questions are welcome.

P粉317679342P粉317679342480 days ago544

reply all(1)I'll reply

  • P粉511749537

    P粉5117495372023-09-07 16:22:10

    There are two ways to connect to the 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");

    reply
    0
  • Cancelreply