在 Laravel 中拒绝使用套接字连接到 DB
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/28279664/
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
Connection to DB with socket refused in Laravel
提问by Chilion
I'm aware of a ton of hits on SO and Google about this issue, still I have a unique problem it seems.
我知道在 SO 和 Google 上有很多关于这个问题的点击,但我似乎仍然有一个独特的问题。
Error message: SQLSTATE[HY000] [2002] Connection refused
错误信息:SQLSTATE[HY000] [2002] 连接被拒绝
Configuration
配置
'mysql' => array(
'driver' => 'mysql',
"host" => "localhost:/var/run/mysqld4.sock",
'port' => '3304',
"username" => "admin",
"password" => "admin",
"database" => "frontend_generic",
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => 'pre_',
),
I confirmed that my database is running on port 3304, my prefix is correct, as are user, database and password.
我确认我的数据库在端口 3304 上运行,我的前缀是正确的,用户、数据库和密码也是如此。
For host I tried "localhost", "127.0.0.1", "http://127.0.0.1" and even the actual ip-address of the server.
对于主机,我尝试了“localhost”、“127.0.0.1”、“ http://127.0.0.1”甚至服务器的实际 IP 地址。
Still, there is no luck or change. I tried using the exact same configuration in the local database.php file, but as expected, nothing changes.
尽管如此,没有运气或变化。我尝试在本地 database.php 文件中使用完全相同的配置,但正如预期的那样,没有任何变化。
Out of options, what am I missing here?
没有选择,我在这里错过了什么?
Update:
更新:
This is code from another app that works with this configuration. This is Kohana, not Laravel, but it works.
这是来自另一个适用于此配置的应用程序的代码。这是 Kohana,而不是 Laravel,但它有效。
"general" => array
(
"type" => "mysql",
"connection" => array
(
"hostname" => "localhost:/var/run/mysqld4.sock",
"username" => "admin",
"password" => "admin",
"persistent" => FALSE,
"database" => "frontend_generic",
),
"table_prefix" => "pre_",
"charset" => "utf8",
"caching" => FALSE,
"profiling" => TRUE,
),
采纳答案by Bopp
You should change "host" to the actual server IP adres, and nothing else. So, get rid of the socket stuff.
您应该将“主机”更改为实际的服务器 IP 地址,而不是其他任何内容。所以,摆脱套接字的东西。
回答by ThievingSix
When using sockets with laravel 5, use the unix_socket
config instead of host
and port
.
在 laravel 5 中使用套接字时,请使用unix_socket
config 而不是host
and port
。
While changing to IP addresses does work, there can be advantages to using sockets when the database server is on the same machine.
虽然更改为 IP 地址确实有效,但当数据库服务器位于同一台机器上时,使用套接字可能会有好处。
Correcting the configuration originally posted:
更正最初发布的配置:
'mysql' => array(
'driver' => 'mysql',
"unix_socket" => "/var/run/mysqld4.sock",
"username" => "admin",
"password" => "admin",
"database" => "frontend_generic",
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => 'pre_',
),
回答by baao
This is what the array should look like in Laravel 5:
这是数组在 Laravel 5 中的样子:
'mysql' => [
'driver' => 'mysql',
'host' => env('DB_HOST', 'localhost'),
'database' => env('DB_DATABASE', 'forge'),
'username' => env('DB_USERNAME', 'forge'),
'password' => env('DB_PASSWORD', ''),
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
'strict' => false,
],
In your .env file, located in the project root, please change the settings you need to adjust and try it again like this. It's really hard to say what could cause this issue, so just give this a try.
在位于项目根目录的 .env 文件中,请更改您需要调整的设置,然后像这样重试。很难说是什么导致了这个问题,所以试试这个。
Example .env file
示例 .env 文件
APP_ENV=local
APP_DEBUG=true
APP_KEY=SomeRandomString
DB_HOST=localhost
DB_DATABASE=homestead
DB_USERNAME=homestead
DB_PASSWORD=secret
CACHE_DRIVER=file
SESSION_DRIVER=file
After creating the file with your credentials, as I commented, please call the file .env and put it in the root of your project. Also make sure to check todays Laravel log, located at storage/logs/laravel-todays_date.txt and report any errors here.
在使用您的凭据创建文件后,正如我所评论的,请调用文件 .env 并将其放在项目的根目录中。还要确保检查今天的 Laravel 日志,位于 storage/logs/laravel-todays_date.txt 并在此处报告任何错误。