php 如何使用 Laravel 连接到 mysql?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20208776/
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
How to connect to mysql with laravel?
提问by Michael Joseph Aubry
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)?
这是一个如此简单的基本问题,我认为没有多少人在问这个问题,但只是如何通过 localhost phpmyadmin 和 laravel 连接到 mySql(我已经搜索过,有一些东西,但我需要问,因为我是一个菜鸟,我想问一下,我看到的东西我没有完全得到 100% 或者我尝试过但没有帮助)?
I am working with backbone.jsand am brand new to laravel, I installed laravelinside C:\wamp\www\laravel-project
我正在使用backbone.js并且是laravel的新手,我在里面安装了laravelC:\wamp\www\laravel-project
I tried
我试过
c:\wamp\www\laravel-project> php artisan migrate:make create_tasks_table --table tasks --create
And
和
c:\wamp\www\laravel-project> php artisan migrate
I expect to have to define the database information, I checked C:\wamp\www\laravel-project\app\config\database.phpand it looks correct, mySQL is set on the defaults rootbeing the user name, and '' empty being the password.
我希望必须定义数据库信息,我检查了C:\wamp\www\laravel-project\app\config\database.php它看起来是正确的,mySQL 设置为默认值root是用户名,而 '' 空是密码。
The error in the command line I get after running php artisan migrateis
我在运行后得到的命令行中的错误php artisan migrate是
[PDOException]
SQLSTATE[HY000] [1049] Unknown database 'database'
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 laraveland phpmyadmin properly.
我使用的是 Windows 8,我的本地主机服务器使用 wamp,它当然包括 phpmyadmin 和 mySql。所以我确信有很多方法可以使用 mySql,但我认为我没有正确设置laravel和 phpmyadmin。
So any insight would be amazing, easy points.
因此,任何洞察力都将是惊人的、简单的点。
EDIT:
编辑:
Actually thinking this through maybe I need to create a database named databaselet me try that. be back soon. Okay I'll answer my question or someone can, just in case someone searches this topic... it is KEYto make sure the database defined in C:\wamp\www\laravel-project\app\config\database.phpmatches a database that exists inside your phpmyadmin. No brainer!
实际上仔细考虑一下,也许我需要创建一个名为database让我尝试一下的数据库。马上回来。好的,我会回答我的问题,或者有人可以回答我的问题,以防万一有人搜索这个主题……确保 中定义的数据库与 phpmyadmin 中存在的数据库相匹配是关键C:\wamp\www\laravel-project\app\config\database.php。没脑子!
采纳答案by gerobk
Laravel makes it very easy to manage your database connections through app/config/database.php.
Laravel 通过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!
正确配置后,您将能够轻松访问您的数据库!
Happy Coding!
快乐编码!
回答by Mohammed Safeer
In Laravel 5, there is a .envfile,
在 Laravel 5 中,有一个.env文件,
It looks like
看起来像
APP_ENV=local
APP_DEBUG=true
APP_KEY=YOUR_API_KEY
DB_HOST=YOUR_HOST
DB_DATABASE=YOUR_DATABASE
DB_USERNAME=YOUR_USERNAME
DB_PASSWORD=YOUR_PASSWORD
CACHE_DRIVER=file
SESSION_DRIVER=file
QUEUE_DRIVER=sync
MAIL_DRIVER=smtp
MAIL_HOST=mailtrap.io
MAIL_PORT=2525
MAIL_USERNAME=null
MAIL_PASSWORD=null
Edit that .env There is .env.sample is there , try to create from that if no such .env file found.
编辑那个 .env 那里有 .env.sample ,如果没有找到这样的 .env 文件,请尝试从中创建。
回答by Ali
It's also much more better to not modify the app/config/database.phpfile itself... otherwise modify .envfile and put your DB info there. (.envfile is available in Laravel 5, not sure if it was there in previous versions...)
最好不要修改app/config/database.php文件本身......否则修改.env文件并将您的数据库信息放在那里。(.env文件在 Laravel 5 中可用,不确定它是否在以前的版本中存在......)
NOTE:Of course you should have already set mysqlas your default database connection in the app/config/database.phpfile.
注意:当然,您应该已经mysql在app/config/database.php文件中设置为默认数据库连接。
回答by user3035039
Maybe you forgot to first create a table migrations:
也许你忘了先创建一个表迁移:
php artisan migrate:install
Also there is a very userful package Generators, which makes a lot of work for you https://github.com/JeffreyWay/Laravel-4-Generators#views
还有一个非常有用的包生成器,它为你做了很多工作https://github.com/JeffreyWay/Laravel-4-Generators#views
回答by Employee 451
I spent a lot of time trying to figure this one out. Finally, I tried shutting down my development server and booting it up again. Frustratingly, this worked for me.
I came to the conclusion, that after editing the .envfile in Laravel 5, you have to exit the server, and run php artisan serveagain.
我花了很多时间试图弄清楚这一点。最后,我尝试关闭我的开发服务器并重新启动它。令人沮丧的是,这对我有用。我得出的结论是,.env在 Laravel 5 中编辑文件后,您必须退出服务器,然后php artisan serve再次运行。
回答by Patrik Fuhrmann
You probably only forgot to create database. Enter your PHPMyAdmin and do it from there.
您可能只是忘记了创建数据库。输入您的 PHPMyAdmin 并从那里开始。
Edit: Definitely don't go with Maulik's answer. Not only it is using mysql_ extenstion (which is commonly recognized bad practice), Laravel is also taking care of your connections using PDO.
编辑:绝对不要采用 Maulik 的回答。Laravel 不仅使用 mysql_ 扩展(这是公认的不好的做法),而且还使用 PDO 处理您的连接。

