0

I'm trying to connect to my website's mysql from my VDS, the following code does not work, what is the secret?

<?php

$db_host = "WEBSITE.COM";
$db_user = "username";
$db_pass = "pass";

mysql_connect("".$db_host."", "".$db_user."", "".$db_pass."") or die(mysql_error());
echo "Connected!<br />";

?>
aynber
  • 22,380
  • 8
  • 50
  • 63
user1804119
  • 142
  • 3
  • 12

3 Answers3

2

Without more details on the error it's hard to guess, but if I have to guess I'd say your username is only allowed to connect from localhost. Mysql users have both a username and a host, and both must match for the connection to be allowed.

You could create another user allowed to connect remotely with something like CREATE USER username@'your.ip.address' IDENTIFIED BY 'some_password'

And then granting him access to the databases you want (see http://dev.mysql.com/doc/refman/5.1/en/grant.html).

Another thing to check would be that the port mysql is using (3306 by default) is open to the outside world and that the mysql server is listening on all interfaces (see your my.cnf config file).

djfm
  • 2,317
  • 1
  • 18
  • 34
  • But what I'm wanting to do is to grab information from VDS.Mysql ant insert it to Website.Mysql, so if I'd creat another user, it wouldn't have my database. – user1804119 Apr 05 '14 at 20:39
  • Yes it would, if you grant him access, using the GRANT command I pointed you out to :) – djfm Apr 05 '14 at 20:43
  • It says "#1227 - Access denied; you need (at least one of) the CREATE USER privilege(s) for this operation " – user1804119 Apr 05 '14 at 20:46
2

As you posted what error code is, i recomend reading this topic. I believe, that answer is there ;)

Community
  • 1
  • 1
T.G
  • 1,913
  • 1
  • 16
  • 29
  • The problem is that I'm not the owner of the server I'm trying to connect. I only have a hosting plan. So I can't access that file. – user1804119 Apr 05 '14 at 20:42
  • 1
    Why exactly are you trying to connect from the outside? Where is your .php file hosted? – djfm Apr 05 '14 at 20:47
  • Well I have a server where I upload files and I have a website where I post links to those files, I want to create program that does it all automatically, so I should get the name, the link and some other props from my VDS mysql and insert it to my website's database. – user1804119 Apr 05 '14 at 20:54
2

Probably MySQL at your VDS Hoster is configured to only accpet requests from particular hosts. You can tell MySQL to only accept packets from particular IP addresses. So you have to use such an IP address instead of your domain name.

If you don't know that IP address The only solution is to contact your hoster, IMHO.

citizen404
  • 1,485
  • 1
  • 10
  • 19
  • Well, I guess then I have to do it the other way, upload the script to my website and make a connection to VDS that I own. – user1804119 Apr 05 '14 at 21:01