简体   繁体   中英

PHP connection to remote MySQL server

When I try to connect through the shell of the local machine at the remote MySQL server I can successfully connect:

> mysql -h remotehost -u myuser -p
Enter password:
Welcome to the MySQL monitor.  Commands end with ; or \g.

But when I try to connect through a PHP script using

$serverName = "remotehost"; // here I put the actual IP address
$userName = "myuser";
$passCode = "actualpassword";
mysql_connect($serverName,$userName,$passCode);

I get the following error

Warning: mysql_connect(): Access denied for user 'myuser'@'localhost' (using password: YES)

The remote MySQL server version is 5.1.52 and the PHP version in the local machine is 5.3.10-1ubuntu3.4

I found a similar question but the answer does not solve my problem: problem connecting to remote mysql database using php

I'd really appreciate some help!

EDIT :

The output of php -i | grep "mysql" php -i | grep "mysql"

/etc/php5/cli/conf.d/mysql.ini,

/etc/php5/cli/conf.d/mysqli.ini,

/etc/php5/cli/conf.d/pdo_mysql.ini

mysql

MYSQL_SOCKET => /var/run/mysqld/mysqld.sock

MYSQL_INCLUDE => -I/usr/include/mysql

MYSQL_LIBS => -L/usr/lib/i386-linux-gnu -lmysqlclient_r

mysql.allow_local_infile => On => On

mysql.allow_persistent => On => On

mysql.connect_timeout => 60 => 60

mysql.default_host => no value => no value

mysql.default_password => no value => no value

mysql.default_port => no value => no value

mysql.default_socket => /var/run/mysqld/mysqld.sock => /var/run/mysqld/mysqld.sock

mysql.default_user => no value => no value

mysql.max_links => Unlimited => Unlimited

mysql.max_persistent => Unlimited => Unlimited

mysql.trace_mode => Off => Off

mysqli

MYSQLI_SOCKET => /var/run/mysqld/mysqld.sock

mysqli.allow_local_infile => On => On

mysqli.allow_persistent => On => On

mysqli.default_host => no value => no value

mysqli.default_port => 3306 => 3306

mysqli.default_pw => no value => no value

mysqli.default_socket => /var/run/mysqld/mysqld.sock => /var/run/mysqld/mysqld.sock

mysqli.default_user => no value => no value

mysqli.max_links => Unlimited => Unlimited

mysqli.max_persistent => Unlimited => Unlimited

mysqli.reconnect => Off => Off

PDO drivers => mysql

pdo_mysql

pdo_mysql.default_socket => /var/run/mysqld/mysqld.sock => /var/run/mysqld/mysqld.sock

Before proceeding, confirm the $serverName variable really is a remote address, either a hostname or an IP address. Then...

Did you post actual code? If so, you forgot to enclose the strings in quotes:

$serverName = "remotehost"; // here I put the actual IP address
$userName   = "myuser";
$passCode   = "actualpassword";
mysql_connect($serverName,$userName,$passCode);

You need to allow remote access to the mySQL(on the server where you have mySQL db). If you are using CPanel or Plesk, you can do it in the control panel. I had this problem, apparently mySQL default config is to only allow localhost.

You need to grant access for that user to be accessible by your server's IP address. Often times the MySQL user is locked down to the localhost.

A quick way to do this would be to look at the table mysql.users and see if that user is locked down to the localhost.

There are many nice GUIs that can help with adding hosts, but the most manual way possible would be to add another row to the DB. You could change the host to '%' to test but that opens things up and is less secure.

Use SHOW GRANTS to see if the user you are trying to login with has permission to connect from the web server's IP or domain name.

mysql> SHOW GRANTS;
+----------------------------------------------------------------------------------------------------------------------------------------+
| Grants for root@localhost                                                                                                              |
+----------------------------------------------------------------------------------------------------------------------------------------+
| GRANT ALL PRIVILEGES ON *.* TO 'root'@'localhost' IDENTIFIED BY PASSWORD '*6381FFD48B21F7ED17AE12AF4F8BE4458F4B0AC7' WITH GRANT OPTION |
+----------------------------------------------------------------------------------------------------------------------------------------+
1 row in set (0.00 sec)

Use the GRANT command to give remote access to your web user , so to speak. Something like this.

mysql> GRANT SELECT ON database.table TO 'myuser'@'web_server_IP_or_domain_name' IDENTIFIED BY 'actualPassword';

Reference the MySQL 5.1 manual for exact syntax. MySQL 5.1 Manual: GRANT

I really think this is not a GRANT issue since you're using the same user on the same server

Are you sure you are defining the good $serverName variable. This has to be the REMOTE IP.

You probably have a local mysql server which is responding also which is confusing you.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM