|Sharing a database across two domains|
XML type "EAI" or something cleaner, simpler?
I have two sites, siteA and siteB. With the usual cpanel type hosting, each with a mysql database of course.
Problem is, I need to share one table in either database. Across the two sites.
I can do this in a patchy way by making my own PHP or Perl script that queries the mysql on siteA and then another PHP script on siteB reads that data in because the data is in a certain format (which I can decide).
However, I wonder if there is more intelligent way of doing this through "database replication" or something like that?
Appreciate any thoughts.
You can actually query mysql remotely. I know that in later cpanel versions you can specify which IPs are allowed to access the database. So you can do the following:
1) figure out the IP address of your siteA
2) add the above IP into Access Hosts list of siteB database
3) when you conntect to the database, instead of localhost, use IP (or domain) of the siteB.
Sample code ( from cpanel ):
$dbh = DBI->connect("DBI:mysql:db_name:localhost","db_user","<PASSWORD HERE>");
$dbh=mysql_connect ("localhost", "db_user", "<PASSWORD HERE>") or die ('I cannot connect to the database because: ' . mysql_error());
Thanks a bunch moltar, this, if possible, would be the find of the year for me! :)
My concern is that the two sites are on shared hosting so their IP address may in fact be the same. Would that be a problem?
Well, if they are on the same server, then things are much easier... You can just connect to the database like you usually do. You see, databases are not attached to a website, they are independant of the webserver/your account. All you do is just provide correct username and password.
Shucks, looks like my host has disabled access across the two domain names. It is some server-wide security setting that they're reluctant to alter. Are t hey telling me the truth? Can I suggest something to them to handle this?
A simple php/mysql script on siteB for instance gives me this error:
Warning: mysql_connect(): Access denied for user: 'siteA@localhost' (Using password: YES) in /home/siteA/public_html/mysql_test.php on line 12
Could not connect
If you can connect to the db from site A, you'll need to use the same username and password when you connect from site B, rather than the one you already use.