View Full Version : Connecting to MySQL server from another server
tarsius 07-06-2005, 06:28 AM Hi!
I have these 2 servers, one for the PHP scripts and the other for the MySQL database. I want to connect to my MySQL server using the PHP scripts from the other server. Here's the PHP code from the other server:
$Host="ipaddress"; //IP address of the MySQL server
$User="my_user"; //user of the database in MySQL server
$Password="my_pword"; //password of the database in MySQL server
$DBName="my_db";
$Link=mysql_connect($Host, $User, $Password) or die("Cannot connect to server!!");
mysql_select_db($DBName, $Link) or die("SQL-Errors: ". mysql_error());
The problem is the program won't run. I already tried the program run on the server with the MySQL and the $Host equal to 'localhost' and it run properly.
Is there any initializations to be done on MySQL so that it can be accessed from other servers? I'm sure this is possible.:)
Please help. Thanks.
cipherus 07-06-2005, 07:38 AM it's my understanding that mysql has no restrictions like that straight out of the box. try checking your firewall rules and make sure the right ports are forwarded.
tarsius 07-06-2005, 08:53 AM I'm just not sure if the $Host must be equal to the IP address of the MySQL server. I never tried this before since all my PHP scripts had to be run on the same server where MySQL resides.
I'm sure this can be done, I just don't know what to configure.:)
recipher 07-06-2005, 03:14 PM If you have a domain on the server with the mysql database, just try your domain name. If the domain is http://www.example.com, try example.com
maxymizer 07-06-2005, 05:09 PM Addition to what rechiper said - you MUST create an account that's allowed to connect from IP different than 127.0.0.1 (localhost).
MySQL has got that restriction by default.
Or you can just modify the existing MySQL account and allow it to connect from IP of the website that resides on your 2nd server.
And it's actually somewhat better to specify the IP of MySQL server. Using domain names my be tricky since DNS might be down for some reason and the IP of your MySQL server won't be resolved, thus connection wont occur.
Omega-Mark 07-06-2005, 06:40 PM go in cpanel of the host where the database is
mysql settings and then add mysql host at bottom of page
(providing you have cpanel on it)
if not thats all i can offer.
tarsius 07-06-2005, 07:22 PM I'm actually running both servers on a shared host so I'm sure there are configurations made by the host provider so that other databases on the MYsql servers will not be compormised.
But I'm sure connecting to the mysql server from the other server can still be done since the db name, username and password are all correct. I tried using also '%' instead of IP address but still no result.
I already added the IP address of the server where PHP script resides and '%' in cpanel.
azizny 07-06-2005, 10:02 PM Two things you will need to do:
Connect to: $Host="ipaddress", where the ipaddress is the domain name "domainname.com" format.
You will also need to add the IP of where you are connecting from
to your MYSQL allow list (located in the MYSQL Bin). you will need to enter the EXACT IP address (No % or * will work).
Peace,
michael-lane 07-07-2005, 08:59 AM why not just host the database on localhost?
DA Master 07-09-2005, 04:41 AM Failing that, if you have root access you can alter the permissions table for your account to have access from all (%) or just your IP if it is static.
Lebanon 07-09-2005, 12:13 PM whats the whole confusion about ? i believe the problem is exactly from the access list itself azizny mentioned.
inshort : Where you mysql db is hosted
And whatever panel ur using most of them have that (u dont need root or anything ) there will be an option to allow an IP address to access that DB
Just add the IP of the host your FILEs are on .
Should work as your code seems fine.
tarsius 07-10-2005, 09:40 AM Hi!
Thanks for all your replies.:) Unfortunately, it still doesn't work.:(
There must be something to be done with the configuration of the MySQL server. And since I'm on a shared host, I don't have the rights.
I already added the IP address of my PHP server in the Access Hosts of the MySQL server but the output is just 'Page cannot be displayed'. I also run my code in the MySQL server with 'localhost' as Access Host and it was fine.
Burhan 07-10-2005, 10:08 AM Are you sure there is no firewall, or something else blocking your requests? Can you access the MySQL server remotely from the shell client?
mysql -u username -h hostname databasename
Marble 07-10-2005, 11:18 AM Originally posted by tarsius
Hi!
Thanks for all your replies.:) Unfortunately, it still doesn't work.:(
There must be something to be done with the configuration of the MySQL server. And since I'm on a shared host, I don't have the rights.
I already added the IP address of my PHP server in the Access Hosts of the MySQL server but the output is just 'Page cannot be displayed'. I also run my code in the MySQL server with 'localhost' as Access Host and it was fine.
If you are on a shared host, have you checked whether they allow you to connect to a db in this manner? and why haven't you contacted your hosting provider to see if they can help you out? Its probably on their end.
VolkNet 07-10-2005, 11:47 AM Originally posted by fyrestrtr
mysql -u username -h hostname databasename
I was just about to say this.
|