Welcome to Dream.In.Code
Getting PHP Help is Easy!

Join 117,270 PHP Programmers for FREE! Ask your question and get quick answers from experts. There are 1,805 online right now! We've got more than 500 tutorials and 2,000 snippets. Join and find out why Dream.In.Code is the #1 programming help community on the internet! Registration is fast and FREE... Join Now!



How to access MySQL on a remote server

 
Reply to this topicStart new topic

How to access MySQL on a remote server, Same as title

douffle
post 4 May, 2008 - 01:55 PM
Post #1


New D.I.C Head

*
Joined: 29 Mar, 2008
Posts: 8

Can anyone tell me if you can code a php script running on one server to access a MySQL database on remote server?

I want to have a php script compare the domain name that the script is running on to the domain name that is stored in a MySQL database on a remote server.

I am pretty sure that it can be done and if it can be, could someone show me a sample of the code that would be needed to achieve this?

I know how to get the domain name of the server that the script is running on using the $domain=$_SERVER['HTTP_HOST']; function but I need to know how to access the MySQL database on the remote server to compare the domain name to.

Thanks
User is offlineProfile CardPM

Go to the top of the page


joeyadms
post 5 May, 2008 - 12:40 AM
Post #2


D.I.C Head

Group Icon
Joined: 4 May, 2008
Posts: 145



Thanked 6 times

Dream Kudos: 600
My Contributions


This is pretty normal, you just setup the connection details for the server you want. Best way is to create a abstraction layer class, with properties that cover the basic host,username,password,database details, and change these to the remote server's info.

This post has been edited by joeyadms: 5 May, 2008 - 12:49 AM
User is offlineProfile CardPM

Go to the top of the page

douffle
post 6 May, 2008 - 02:25 PM
Post #3


New D.I.C Head

*
Joined: 29 Mar, 2008
Posts: 8

I am trying to connect to a MySQL5 database from remote.
I set up the database in my cpanel, created a user with full permissions and enabled “Allow Remote Connection”

In my script, which is running on another server I have the following code:

CODE

$host="http://www.mysite.com:/tmp/mysql5.sock";
$user="dodie";
$pass="student";
$dbname="mysite_domain";

//Connect to database
$connection = mysql_pconnect($host,$user,$pass);

      if($connection)
        echo "Connected to Database Successfully";
    else
         mysql_error();

//Select database to open
mysql_select_db($dbname);


I am getting the following error:

Warning: mysql_pconnect() [function.mysql-pconnect]: Access denied for user: dodie@server503.com' (Using password: YES) in /home/mysite/www/connect_remote.php on line 17


I have also tried replacing the

CODE


$host="http://www.mysite.com:/tmp/mysql5.sock";



with

CODE


$host="213.24.172.63:/tmp/mysql5.sock";



but I get the same error.

I have also tried:

CODE

mysql_connect ('mysql.http://www.mysite.com', $user, $pass) or die (mysql_error ());


I am obviously missing something here but I don’t know if it is an error on Server-A where the script is located or on Server-B where the database is located.

Any help much appreciated.

This post has been edited by douffle: 6 May, 2008 - 02:36 PM
User is offlineProfile CardPM

Go to the top of the page

mocker
post 6 May, 2008 - 02:35 PM
Post #4


D.I.C Head

**
Joined: 14 Oct, 2007
Posts: 219



Thanked 11 times
My Contributions


Your $host should just be the IP, or maybe IP:port . The .sock file is used when you are connecting locally .

You might also try using mysql_connect instead of pconnect, some servers have persistent connections disabled

In cPanel, it should default to blocking all remote connections. Even if you click the allow remote connection, you need to add the IP of the server that is running your script to the Access Hosts . You could also directly modify the mysql.users tables to change your user to match all hosts.. but you might not want to mess with that if you aren't sur
User is offlineProfile CardPM

Go to the top of the page

douffle
post 6 May, 2008 - 02:40 PM
Post #5


New D.I.C Head

*
Joined: 29 Mar, 2008
Posts: 8

Thanks, I'll give it a try and see how that works.
Appreciate your help.



QUOTE(mocker @ 6 May, 2008 - 02:35 PM) *

Your $host should just be the IP, or maybe IP:port . The .sock file is used when you are connecting locally .

You might also try using mysql_connect instead of pconnect, some servers have persistent connections disabled

In cPanel, it should default to blocking all remote connections. Even if you click the allow remote connection, you need to add the IP of the server that is running your script to the Access Hosts . You could also directly modify the mysql.users tables to change your user to match all hosts.. but you might not want to mess with that if you aren't sur



User is offlineProfile CardPM

Go to the top of the page

Fast ReplyReply to this topicStart new topic
Time is now: 10/6/08 10:22PM

Live PHP Help!

PHP Tutorials

Reference Sheets

PHP Snippets

Bye Bye Ads

Free DIC T-Shirt

T-Shirt Example

Related Sites

Monthly Drawing

Thumb Drive

Partners

Top Contributors

Top 10 Kudos This Month