如何使用laravel连接到mysql? [英] How to connect to mysql with laravel?

查看:424
本文介绍了如何使用laravel连接到mysql?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是一个非常简单的基本问题,我认为并没有很多人问这个问题,而是简单地讲如何使用laravel通过localhost phpmyadmin连接到mySql(我已经搜索过,有一些东西,但是我需要问一下因为我是一个菜鸟,我想问一下,我看到的东西我不能完全达到100%,或者我尝试了却没有帮助)?

This is such a simple basic question I don't think there is much people asking this but simply how do I connect to mySql through localhost phpmyadmin with laravel (I've searched, there is some stuff, but I need to ask because I am a noob and I'd like to ask, the stuff I saw I don't quite get 100% or I tried and it didn't help)?

我正在使用 backbone.js ,并且是

I am working with backbone.js and am brand new to laravel, I installed laravel inside C:\wamp\www\laravel-project

我尝试了

c:\wamp\www\laravel-project> php artisan migrate:make create_tasks_table --table tasks --create

还有

c:\wamp\www\laravel-project> php artisan migrate

我希望必须定义数据库信息,我检查了C:\wamp\www\laravel-project\app\config\database.php,它看起来是正确的,在默认的root上将mySQL设置为用户名,而将"empty"设置为密码.

I expect to have to define the database information, I checked C:\wamp\www\laravel-project\app\config\database.php and it looks correct, mySQL is set on the defaults root being the user name, and '' empty being the password.

运行php artisan migrate后出现的命令行错误是

The error in the command line I get after running php artisan migrate is

[PDOException]
SQLSTATE[HY000] [1049] Unknown database 'database'    

我使用的是Windows 8,我的本地主机服务器为wamp,它当然包括phpmyadmin和mySql.因此,我确定有很多使用mySql的方法,但我认为我没有正确设置 laravel 和phpmyadmin

I am using windows 8, and wamp for my localhost server, it of course includes phpmyadmin and mySql. So I am sure there are tons of ways to use mySql but I don't think I am setting up laravel and phpmyadmin properly.

因此,任何见解都将是惊人的,简单的观点.

So any insight would be amazing, easy points.

实际上,我可能需要考虑创建一个名为database的数据库,让我尝试一下.马上回来.好吧,我会回答我的问题,或者有人可以回答,以防万一有人搜索此主题... KEY 是确保C:\wamp\www\laravel-project\app\config\database.php中定义的数据库与phpmyadmin内部存在的数据库匹配.没脑子!

Actually thinking this through maybe I need to create a database named database let me try that. be back soon. Okay I'll answer my question or someone can, just in case someone searches this topic... it is KEY to make sure the database defined in C:\wamp\www\laravel-project\app\config\database.php matches a database that exists inside your phpmyadmin. No brainer!

推荐答案

Laravel使通过app/config/database.php管理数据库连接非常容易.

Laravel makes it very easy to manage your database connections through app/config/database.php.

如您所述,它正在寻找一个名为数据库"的数据库.原因是这是数据库配置文件中的默认名称.

As you noted, it is looking for a database called 'database'. The reason being that this is the default name in the database configuration file.

'mysql' => array(
    'driver'    => 'mysql',
    'host'      => 'localhost',
    'database'  => 'database', <------ Default name for database
    'username'  => 'root',
    'password'  => '',
    'charset'   => 'utf8',
    'collation' => 'utf8_unicode_ci',
    'prefix'    => '',
),

将其更改为您要连接的数据库名称,如下所示:

Change this to the name of the database that you would like to connect to like this:

'mysql' => array(
    'driver'    => 'mysql',
    'host'      => 'localhost',
    'database'  => 'my_awesome_data', <------ change name for database
    'username'  => 'root',            <------ remember credentials
    'password'  => '',
    'charset'   => 'utf8',
    'collation' => 'utf8_unicode_ci',
    'prefix'    => '',
),

配置正确后,您将可以轻松访问数据库!

Once you have this configured correctly you will easily be able to access your database!

快乐编码!

这篇关于如何使用laravel连接到mysql?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆