简体   繁体   English

远程连接拒绝 Mysql PDO 访问,但 Mysql CLI 有效

[英]Mysql PDO access denied for remote connection but Mysql CLI works

I am trying to connect to a remote Mysql server from my web server.我正在尝试从我的 Web 服务器连接到远程 Mysql 服务器。 I followed the following steps:我按照以下步骤操作:

On the remote server:在远程服务器上:

1. “CREATE USER ’newremoteuser’@‘web-server_ip' IDENTIFIED BY 'remote_user_password’;”
2. “GRANT ALL PRIVILEGES ON *.* TO 'newremoteuser'@'web_server_ip’;”
3. “FLUSH PRIVILEGES;”

On the web server when I try to access using Mysql CLI, the connection is successful.在 Web 服务器上,当我尝试使用 Mysql CLI 访问时,连接成功。

"mysql -u newremoteuser -h remote_server_ip -p"

However when I try to connect to remote server using PDO or Mysqli in PHP, I get the error但是,当我尝试在 PHP 中使用 PDO 或 Mysqli 连接到远程服务器时,出现错误

"FastCGI sent in stderr: "PHP message: PHP Fatal error: Uncaught exception 'PDOException' with message 'SQLSTATE[28000] [1045] Access denied for user 'newremoteuser'@'my-domain-name.com' (using password: YES)' “FastCGI 在标准错误中发送:”PHP 消息:PHP 致命错误:未捕获的异常 'PDOException' 带有消息 'SQLSTATE[28000] [1045] 用户 'newremoteuser'@'my-domain-name.com' 访问被拒绝(使用密码:是的)'

Below is the PDO code下面是 PDO 代码

$REMOTEPDO = new PDO("mysql:host=$remote_server_ip;port=3306;dbname=$remote_db", $newremoteuser, $remote_user_password);
$REMOTEPDO->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

I think it is denying access to 'newremoteuser'@'my-domain-name.com' as the remote user I added was 'newremoteuser'@'web-server_ip'.我认为它拒绝访问'newremoteuser'@'my-domain-name.com',因为我添加的远程用户是'newremoteuser'@'web-server_ip'。 However, I can not find a solution to this.但是,我找不到解决方案。 Can anyone please point me in the right direction?任何人都可以指出我正确的方向吗?

Note: 'my-domain-name.com' is my website name hosted on my 'web_server_ip' server.注意:“my-domain-name.com”是我在“web_server_ip”服务器上托管的网站名称。

Remote server is Ubuntu 18 and Web server is Ubuntu 14.远程服务器是 Ubuntu 18,Web 服务器是 Ubuntu 14。

As you've pointed out, changing the host for that user might fix your problem since the connection denial seems to be because of a host mismatch.正如您所指出的,更改该用户的主机可能会解决您的问题,因为连接拒绝似乎是因为主机不匹配。 Try updating the host for your existing user from the IP address to the domain name:尝试将现有用户的主机从 IP 地址更新为域名:

UPDATE mysql.user SET Host='my-domain-name.com' WHERE User='username';
FLUSH PRIVILEGES;

If it still doesn't work, it might be worth updating the host to '%' instead and seeing if it then lets you connect.如果它仍然不起作用,可能值得将主机更新为 '%' 并查看它是否可以让您连接。

I just went through this.我刚刚经历了这个。 I could connect with command line but not PDO.我可以连接命令行,但不能连接 PDO。 My webserver client wouldn't connect until I enabled httpd_can_network_connect_db on the client server's SELinux and rebooted.在我在客户端服务器的 SELinux 上启用 httpd_can_network_connect_db 并重新启动之前,我的网络服务器客户端不会连接。

sudo setsebool -P httpd_can_network_connect_db 1

I forgot the reboot and chased my tail a bit longer than necessary.我忘记了重启,追我的尾巴比必要的时间长了一点。

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

相关问题 远程MySql连接的访问​​被拒绝 - Access denied for remote MySql connection MySQL 通过 mysql cli 快速连接,但使用 PDO 速度较慢 - MySQL connection fast through mysql cli, but slow using PDO 当mysql命令行工作时,为什么PHP PDO会收到“SQLSTATE [42000] [1044]用户拒绝访问”? - Why does PHP PDO get “SQLSTATE[42000] [1044] Access denied for user” when mysql command line works? PDO 连接到外部 MySQL 数据库(来自 AWS)-“SQLSTATE[28000] [1045] 用户拒绝访问” - PDO connection to an external MySQL database (from AWS) - “SQLSTATE[28000] [1045] Access denied for user” PHP中拒绝MySQL访问,CLI将连接 - MySQL access denied in PHP, CLI will connect PHP mysql_connect 失败,到 MySQL 的 PDO 连接工作正常 - PHP mysql_connect fails, PDO connection to MySQL works fine PHP PDO与MySQL连接失败,mysql_connect工作正常 - PHP PDO connection to MySQL fails, mysql_connect works fine 用户'web'@'localhost'远程mysql的访问被拒绝 - Access denied for user 'web'@'localhost' remote mysql 通过PHP而不是通过MySQL Workbench拒绝与远程MySQL主机的连接 - Connection denied to remote MySQL host through PHP but not through MySQL Workbench PDO MySQL连接故障排除 - PDO MySQL connection troubleshooting
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM