简体   繁体   English

我无法实现到 mysql db 的 PDO 连接

[英]I can't achieve a PDO connection to mysql db

I keep receiving "SQLSTATE[HY000] [2002] Connection refused" when trying to connect from a php script.尝试从 php 脚本连接时,我不断收到“SQLSTATE[HY000] [2002] 连接被拒绝”。

I can connect to mysql with: /Applications/MAMP/Library/bin/mysql --host=localhost -uroot -proot我可以使用以下命令连接到 mysql: /Applications/MAMP/Library/bin/mysql --host=localhost -uroot -proot

setup is: Mamp Pro version: 5.5.1 PHP version: 7.3.8 MySql version: 5.7.26 Check Health reports: MySQL is running and accepting connections.设置是: Mamp Pro 版本:5.5.1 PHP 版本:7.3.8 MySql 版本:5.7.26 检查健康报告:MySQL 正在运行并接受连接。 Connections are not using encryption.连接未使用加密。

This is the script:这是脚本:

<?php
$dsn = "mysql:host=127.0.0.1;dbname=myDatabase;charset=utf8mb4";
try {
  $pdo = new PDO($dsn, "root", "root");
} catch (Exception $e) {
  error_log($e->getMessage());
  exit('Something weird happened'); //something a user can understand
}

If I use localhost instead of 127.0.0.1 in $dsn I get: SQLSTATE[HY000] [2002] No such file or directory如果我在 $dsn 中使用 localhost 而不是 127.0.0.1 我得到: SQLSTATE[HY000] [2002] No such file or directory

Any thoughts would be greatly appreciated.任何想法将不胜感激。

I think your database name is wrong.我认为您的数据库名称是错误的。

So try to enter in mysql command use myDatabase;所以尝试在mysql命令中输入使用myDatabase; . . Also check the grants for user root for that database.还要检查该数据库的用户 root 的授权。 you should als check for system and php errors您还应该检查系统和 php 错误

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

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