简体   繁体   English

PHP 连接到远程 MySQL 服务器

[英]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 服务器上的本地机器的 shell 进行连接时,我可以成功连接:

> 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但是当我尝试使用 PHP 脚本连接时

$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)警告:mysql_connect():用户“myuser”@“localhost”的访问被拒绝(使用密码:YES)

The remote MySQL server version is 5.1.52 and the PHP version in the local machine is 5.3.10-1ubuntu3.4远程MySQL服务器版本为5.1.52,本机PHP版本为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我发现了一个类似的问题,但答案并没有解决我的问题: problem connected to remote mysql database using php

I'd really appreciate some help!我真的很感激一些帮助!

EDIT :编辑

The output of php -i | grep "mysql" php -i | grep "mysql"的输出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.在继续之前,确认 $serverName 变量确实是一个远程地址,主机名或 IP 地址。 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).您需要允许远程访问 mySQL(在您拥有 mySQL 数据库的服务器上)。 If you are using CPanel or Plesk, you can do it in the control panel.如果您使用的是 CPanel 或 Plesk,您可以在控制面板中进行操作。 I had this problem, apparently mySQL default config is to only allow localhost.我有这个问题,显然 mySQL 默认配置是只允许本地主机。

You need to grant access for that user to be accessible by your server's IP address.您需要为该用户授予访问权限,以便您的服务器的 IP 地址可以访问该用户。 Often times the MySQL user is locked down to the localhost.通常,MySQL 用户被锁定到本地主机。

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.一个快速的方法是查看表mysql.users并查看该用户是否被锁定到本地主机。

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.有许多不错的 GUI 可以帮助添加主机,但最可能的手动方法是向数据库添加另一行。 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.使用SHOW GRANTS查看您尝试登录的用户是否有权从 Web 服务器的 IP 或域名进行连接。

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.可以这么说,使用GRANT命令为您的Web 用户提供远程访问权限。 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 手册以了解确切的语法。 MySQL 5.1 Manual: GRANT MySQL 5.1 手册:GRANT

I really think this is not a GRANT issue since you're using the same user on the same server 我真的认为这不是GRANT问题,因为您在同一台服务器上使用相同的用户

Are you sure you are defining the good $serverName variable. 您确定要定义好的$ serverName变量吗? This has to be the REMOTE IP. 这必须是REMOTE IP。

You probably have a local mysql server which is responding also which is confusing you. 你可能有一个本地的mysql服务器,它也响应,这让你感到困惑。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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