Access mysql and website which are on two different hosts

+1 Harsha Naidu · October 3, 2014
I am trying to place website source code and mysql in two different servers.
I am previously using the below code. What should I change in order to implement on other host.
<?php
try
{
  $pdo = new PDO('mysql:host=localhost;dbname=databasename','XXXX','XXXX');
}
catch(PDOException $e)
{
  exit('Database error.');
}

I have replaced the above code as below:
<?php
try
{
  $pdo = new PDO('mysql:host=23.229.177.23;dbname=databasename','XXXX','XXXX');
}
catch(PDOException $e)
{
  exit('Database error.');
}


Can you please help me.

Post a Reply

Replies

Oldest  Newest  Rating
-1 Eugene Botma · October 4, 2014
Assuming the IP is correct, this should work. Have you allowed MySQL to accept connections from hosts other than localhost? Also, the firewall on your sql servers computer might be causing the problem. Try opening the mysql port on the firewall.

If you set up mysql to run on a different port than the default, you would need to add that to the address in your connection string. so where you now have PDO('mysql:host=23.229.177.23;dbname=databasename','XXXX','XXXX'); you should have PDO('mysql:host=23.229.177.23:portnumber;dbname=databasename','XXXX','XXXX');
  • 1

SQL & Databases

106,992 followers
About

Everything SQL and Databases related in here!

Links
Moderators
Bucky Roberts Administrator