1

Possible Duplicate:
Connecting to remote MySQL server using PHP

I can successfully connect to a database using localhost while the code is on the server where the MySQL is.

I want to connect to the same MySQL server, but from another IP:

mysql_connect("localhost","root","<password>");
mysql_select_db("db name");

But say my new IP is 12.123.123.12

I tried:

mysql_connect("12.123.123.12","root","<password>");
mysql_select_db("db name");

What am I doing wrong?

Community
  • 1
  • 1
Daniel Fein
  • 317
  • 1
  • 6
  • 18
  • Why do you think you're doing something wrong? Please don't reply with "because it doesn't work" – zerkms Oct 16 '12 at 01:06
  • "But say my new IP is 12.123.123.12" --- and what is your mysql server ip? – zerkms Oct 16 '12 at 01:09
  • I tried pinging the connection with echo mysql_ping(); and its not connecting properly. Also says, No database Selected. – Daniel Fein Oct 16 '12 at 01:09
  • 2
    Please, don't use `mysql_*` functions to write new code. They are no longer maintained and the community has begun [deprecation process](http://goo.gl/KJveJ). See the [*red box*](http://goo.gl/GPmFd)? Instead you should learn about [prepared statements](http://goo.gl/vn8zQ) and use either [PDO](http://php.net/pdo) or [MySQLi](http://php.net/mysqli). If you can't decide which, [this article](http://goo.gl/3gqF9) will help you. If you pick PDO, [here is good tutorial](http://goo.gl/vFWnC). – tereško Oct 16 '12 at 01:11

4 Answers4

3
SELECT User, Host, Password FROM mysql.user;

Sample Output:

+------+--------------------+----------+
| User | Host               | Password |
+------+--------------------+----------+
| root | localhost          |          |
| root | myhost.example.com |          |
| root | 127.0.0.1          |          |
| root | ::1                |          |
|      | localhost          |          |
|      | myhost.example.com |          |
+------+--------------------+----------+

You will see that root is only allowed to login from localhost by default. You will have to change that to allow the host you are now specifiying OR create a different account (Recommended). Using root is dangerous.

Create new user with just enough privileges as required.

(Reference)

Anirudh Ramanathan
  • 46,179
  • 22
  • 132
  • 191
1

Most of the hosting providers (at least the ones who use CPANEL) don't allow cross domain queries. If your hosting use's CPANEL, you have to go to "DATABASES" -> "REMOTE MySQL" and add the IP address from the server you're running the queries.

If your hosting, don't use CPANEL, add the host's ip address (12.123.123.12) to the user table of MySql.

One last comment, for security reasons, is a good practice that when you post a question don't use sensitive info as the ip address !! Regards Luis

Luis M. Valenzuela
  • 149
  • 1
  • 2
  • 10
0

If it is not working you can try to display the error

mysql_connect("12.123.123.12","root","<password>") or die("Error: ".mysql_error()); 

Note: Hostname is not necessarily your ip address; it is the ip or name of the mysqlserver

Replace it with your mysqlserver and it will work

Ibu
  • 42,752
  • 13
  • 76
  • 103
  • Here's the error after that, Can't connect to MySQL server on '108.166.79.24' (111) – Daniel Fein Oct 16 '12 at 01:11
  • So then I tried to make the IP, 12.123.123.12/phpmyadmin/ which is where I login to phpmyadmin, and the error is different. Unknown MySQL server host '108.166.79.24/phpmyadmin/' (11) – Daniel Fein Oct 16 '12 at 01:11
  • um i think you are not doing this right. why dont you replace the ip with you correct mysqlserver ip address. this is not necessarily your websites ip address, if you are using a hosting company they usually provide this information to you – Ibu Oct 16 '12 at 01:13
0

First of all, you need to make sure that you echo the MySQL error in the query :

 mysql_connect("12.123.123.12","root","<password>") or die(mysql_error());

Check if MySQL server has given users privilege to connect from a remote host. This is how you normally would grant privilege:

GRANT ALL PRIVILEGES ON yourdb.* to 'theuser'@'localhost' IDENTIFIED BY 'thepassword';

If the db needs to be accessed from a remote host, the following privileges statement has to be set from the MySQL server:

GRANT ALL PRIVILEGES ON yourdb.* to 'theuser'@'<yourip>' IDENTIFIED BY 'thepassword';
-- grant privilege to all remote hosts
GRANT ALL PRIVILEGES ON yourdb.* to 'theuser'@'%' IDENTIFIED BY 'thepassword';

If you have administrative access to the MySQL server, you can check the privileges by the command :

 SHOW GRANTS;

As an aside: mysql_* functions are deprecated. Use only mysqli_* functions.

B Faley
  • 17,120
  • 43
  • 133
  • 223
janenz00
  • 3,315
  • 5
  • 28
  • 37