Reputation: 80
How can I connect to my remote database using ip address? I have this code but evetime I load my site I get an error Could not connect to MySQL: Can't connect to MySQL server on 'xxx.xx.xxx.xx:80'
define ('DB_USER', 'user');
define ('DB_PASSWORD', 'password');
define ('DB_HOST', 'xxx.xx.xxx.xx:80');
define ('DB_NAME', 'databaseName');
$dbc=mysql_connect(DB_HOST, DB_USER, DB_PASSWORD)
OR die ('Could not connect to MySQL: '.mysql_error()); @mysql_select_db (DB_NAME)
OR die ('Could not select the database" '.mysql_error());;
Upvotes: 0
Views: 9757
Reputation: 5213
If using port 3306, like the other answer says, still doesn't give you a connection, you could try two things:
Look in your /etc/mysql/my.cnf
. You might find a line like this:
bind-address = 127.0.0.1
Comment it out by putting a "#" in front. If that still didn't help, check if the user you are trying to connect with is allowed to connect from all hosts.
Oh, and another thing: Don't use PHP's mysql library. Use PDO or mysqli instead, as the mysql library is deprecated.
Upvotes: 0
Reputation: 70933
The database server must be made available for remote access. As the usual MySQL server has no encryption, and it would generally be more of a security issue to make such a server available for the whole internet, any hosted mysql server usually is only available from the local network, if at all.
Check with your hosting company if you can access the database server from outside.
After that answer is yes, you must also know that user accounts in MySQL are not only username/password, but also contain a "source IP" component (which can be a wildcard "*", but usually is not), so there is another layer of protection to allow only dedicated IP addresses to gain access. Do you have a dynamic IP? That might be another problem to be solved.
Upvotes: 2
Reputation: 218
Try using port 3306 instead:
define ('DB_HOST', 'xxx.xx.xxx.xx:3306');
Port 3306 is the default MySQL port, whereas port 80 is HTTP.
Upvotes: 2