我有错误:SQLSTATE[HY000] [2005] Unknown MySQL server host [英] I have the error: SQLSTATE[HY000] [2005] Unknown MySQL server host
问题描述
我使用的是 mysql 数据库.我想用我写的脚本连接到它:
I'm using a mysql database. I would like to connect to it with the script i wrote :
<?php
function getDatabase() {
$host = 'localhost:3306';
$db = 'freya';
$login = 'root';
$pw = 'helloitsme';
try {
return new PDO('mysql:host='.$host.';dbname='.$db.';charset=utf8', $login, $pw);
} catch (Exception $e) {
die('Erreur : '.$e->getMessage());
}
}
$db = getDatabase();
我已经看到此错误反复出现,但没有一个解决方案适用.我检查了 my.cnf,我确定我正在使用 mysql 数据库所在的端口.我还确定 db 名称、登录名和密码是正确的,因为我正在使用它们通过 shell 访问 db.
I have seen that this error is recurent but none of the solutions worked for. I checked the my.cnf, and i'm sure that i'm using the port where the mysql db is. I'm also sure that the db name, the login and the password are correct, because i'm using them to reach the db with the shell.
可能是什么问题?
推荐答案
你不需要指定端口,因为 3306 是 mysql 的默认端口,但如果你这样做,正确的连接字符串是
You don't need to specify the port as 3306 is default for mysql, but if you do, the correct connection string is
'mysql:host=localhost;port=3306 ...'
这篇关于我有错误:SQLSTATE[HY000] [2005] Unknown MySQL server host的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!