laravel SQLSTATE[HY000] [2005] 未知 MySQL 服务器主机 'localhost:3306' (2)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/27328733/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
SQLSTATE[HY000] [2005] Unknown MySQL server host 'localhost:3306' (2)
提问by Arya
currently i am facing above problem in laravel. basically this project is setup on mac OS x and currently i am working on it in Linux. so when i run it is on browser it give me that error. i have also changed the my database details in database file. but now this error comes. so can you please tell me how can i fix it.
目前我在 Laravel 中面临上述问题。基本上这个项目是在 mac OS x 上设置的,目前我正在 Linux 上工作。所以当我在浏览器上运行它时,它给了我那个错误。我还更改了数据库文件中的数据库详细信息。但现在这个错误来了。所以你能告诉我我该如何解决它。
Open: /var/www/laravel-projects/wit/vendor/laravel/framework/src/Illuminate/Database/Connectors/Connector.php
打开:/var/www/laravel-projects/wit/vendor/laravel/framework/src/Illuminate/Database/Connectors/Connector.php
*/
public function createConnection($dsn, array $config, array $options)
{
$username = array_get($config, 'username');
$password = array_get($config, 'password');
return new PDO($dsn, $username, $password, $options);
}
Thanks
谢谢
回答by johnjg12
I believe in versions of php prior to 5.5 you would use "localhost" (or the host the mysql server is running on if it's not the same host) for the 'host' parameter. If it is after version 5.5 I think you need to use "localhost:3306" (Not necessarily 3306, this is just the default mysql port, so if you changed that it will be different) . So if you are on a version of php prior to 5.5 and the code is for verion 5.5 and greater, try changing your mysql connector settings. Guessing it looks something like:
我相信在 5.5 之前的 php 版本中,您将使用“localhost”(或运行 mysql 服务器的主机,如果它不是同一主机)作为“主机”参数。如果是 5.5 版本之后,我认为您需要使用“localhost:3306”(不一定是 3306,这只是默认的 mysql 端口,因此如果您更改它会有所不同)。因此,如果您使用的是 5.5 之前的 php 版本并且代码适用于 5.5 及更高版本,请尝试更改您的 mysql 连接器设置。猜测它看起来像:
array(
'host' => "localhost:3306",
'username' => 'user',
'password' => 'pass',
'dbname' => 'database');
)
Perhaps try changing this to:
也许尝试将其更改为:
array(
'host' => "localhost",
'username' => 'user',
'password' => 'pass',
'dbname' => 'database');
)
回答by Grateful-dude
PHP 5.6.33-0+deb8u1 (cli) (built: Jan 9 2018 05:01:31) mysql Ver 14.14 Distrib 5.5.60, for debian-linux-gnu (armv8l) using readline 6.3
PHP 5.6.33-0+deb8u1 (cli) (built: Jan 9 2018 05:01:31) mysql Ver 14.14 Distrib 5.5.60, for debian-linux-gnu (armv8l) using readline 6.3
Solution for me was to remove :3306 from my config file.
我的解决方案是从我的配置文件中删除 :3306 。
<?php
define('DB_SERVER', 'localhost');
//instead of: define('DB_SERVER', 'localhost:3306');
define('DB_USERNAME', 'mysql-user');
define('DB_PASSWORD', 'password');
define('DB_DATABASE', 'database_name');
...
Thank you for your help!
感谢您的帮助!