Laravel 无法连接到数据库

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20334627/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 08:43:26  来源:igfitidea点击:

Laravel can't connect to DB

phpmysqllaravellaravel-4

提问by Jonathan Bender

I'm following through the Quick Start Guide, and got to the section actually connecting to the DB to fetch the users table, and am getting the following error:

我正在按照快速入门指南进行操作,并进入了实际连接到数据库以获取用户表的部分,并且出现以下错误:

SQLSTATE[28000] [1045] Access denied for user 'root'@'localhost' (using password: NO)

SQLSTATE[28000] [1045] 用户 'root'@'localhost' 的访问被拒绝(使用密码:NO)

My database.phpincludes the following setup (default set to mysql):

我的database.php包括以下设置(默认设置为 mysql):

'mysql' => array(
    'driver'    => 'mysql',
    'host'      => 'localhost',
    'database'  => 'bt',
    'username'  => 'root',
    'password'  => '',
    'charset'   => 'utf8',
    'collation' => 'utf8_unicode_ci',
    'prefix'    => '',
),

When I go to the terminal window, however, I'm able to log in using mysql -h localhost -u root -pthen just hit enter to go in without a password, which I thought would be equivalent (the 'bt' table shows up when I run show databases();). What am I missing here?

但是,当我转到终端窗口时,我可以使用登录,mysql -h localhost -u root -p然后只需按 Enter 即可进入,而无需输入密码,我认为这等效(我运行时会显示“bt”表show databases();)。我在这里错过了什么?

回答by

I think this is the solution :)

我认为这是解决方案:)

'mysql' => array(
    'driver'    => 'mysql',
    'host'      => '127.0.0.1',
    'database'  => 'bt',
    'username'  => 'root',
    'password'  => '',
    'charset'   => 'utf8',
    'collation' => 'utf8_unicode_ci',
    'prefi

回答by just_some_guy

It's also possible that pdo is not installed on your machine. You can check if it's installed from the phpinfo() page or a php script

也有可能你的机器上没有安装 pdo。您可以从 phpinfo() 页面或 php 脚本检查它是否已安装

if ( extension_loaded('pdo') ) {
    .......
}

On linux you can install it by

在linux上,您可以通过以下方式安装它

sudo apt-get install php5-mysql