I keep receiving "SQLSTATE[HY000] [2002] Connection refused" when trying to connect from a php script.
I can connect to mysql with: /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. 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
Any thoughts would be greatly appreciated.
I think your database name is wrong.
So try to enter in mysql command use myDatabase; . Also check the grants for user root for that database. you should als check for system and php errors
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.