我有一个错误:SQLSTATE[HY000][2005]未知的MySQL服务器主机

时间:2022-09-19 21:29:45

I'm using a mysql database. I would like to connect to it with the script i wrote :

我使用的是mysql数据库。我想把它和我写的剧本联系起来:

<?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();

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.

我已经看到这个错误是递归的,但是没有一个解决方案是有效的。我检查了my.cnf,我确定我正在使用mysql db所在的端口。我还确信db名称、登录名和密码都是正确的,因为我使用它们来访问shell的db。

What could be the problem ?

有什么问题呢?

1 个解决方案

#1


4  

You don't need to specify the port as 3306 is default for mysql, but if you do, the correct connection string is

您不需要为mysql指定端口3306是默认的,但是如果您这样做,正确的连接字符串是

'mysql:host=localhost;port=3306 ...'

#1


4  

You don't need to specify the port as 3306 is default for mysql, but if you do, the correct connection string is

您不需要为mysql指定端口3306是默认的,但是如果您这样做,正确的连接字符串是

'mysql:host=localhost;port=3306 ...'