Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Use PHP to connect to MySQL database from a different host

Status
Not open for further replies.

darryncooke

Technical User
May 6, 2009
308
0
0
US
So i have site A (Wordpress blog as CMS) on one shared host and Site B (wordpress blog) on another shared host.

I am trying to connect from A to B MySQL using php but my script wont connect.

Script works fine if I run it from B. So I am guessing its a permissions issue with MySQL. How do I allow domain A access to B through PHPMYADMIN as that's all Godaddy affords me.

BTW A is on one host and B is on Godaddy, not that it matters.

Darryn Cooke
| Marketing and Creative Services
 
you can do it in plain mysql but you may consider that to be insecure.

on box B insert this sql to the panel in phpmyadmin on Box B

Code:
GRANT ALL ON {DBNAME} TO {USERNAME}@{IP ADDRESS OF BOX A}

This assumes you want box a to have full rights. if only limited rights are required then change the ALL statement as required.

of course you need to make sure that port 3306 (the mysql default port) is on Box B.

the, more secure, alternative is to ssh from box B to box A and do a port redirect over the tunnel.

for example, run this code on Box A

Code:
ssh -N -f -L 16789:BOX_B.com:3306 {username}@BOX_B.com
[type password]

you can reference Box B by IP address or domain name.

then attach to the mysql database as if it were on localhost.

Code:
mysql_connect ('127.0.0.1:16789', $user, $password);

If the connection is flaky or the boxes go frequently down then you will want to set up key based ssh connection and write a cron job that tests for the presence of the link every few seconds and re-establishes it if there is a problem. or perhaps autossh. That is beyond the scope of this forum.
 
thanks ill give both a go - security is not a huge deal. its just one website wanting to pull blog feed for their respective author.

Darryn Cooke
| Marketing and Creative Services
 
BTW A is on one host and B is on Godaddy, not that it matters."

It might, especially for the part about making sure port 3306 is available.
 
agreed

but a rackspace US virtual server is only a dollar or two a month more expensive. and for that you have full root access. no brainer.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top