laravel 如何在laravel中更改数据库名称?更改 config>database.php 不起作用
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/25713592/
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 change database name in laravel? changing config>database.php doesn't work
提问by Seyed Jalal Hosseini
I created a database "mydatabase" and I changed config>database.php to:
我创建了一个数据库“mydatabase”,并将 config>database.php 更改为:
'mysql' => array(
'driver' => 'mysql',
'host' => 'mysite.local',
'database' => 'mydatabase',
'username' => 'myusername',
'password' => 'mypassword',
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
),
now inside route.php i have:
现在在 route.php 里面我有:
Route::get('/', function()
{
$data=DB::table('user')->get();
return $data;
});
laravel sends an Exception which shows that it tries to access:
laravel 发送一个异常,表明它试图访问:
homestead.user
instead of
代替
mydatabase.user
now if i change route.php to:
现在,如果我将 route.php 更改为:
Route::get('/', function()
{
$data=DB::table('mydatabase.user')->get();
return $data;
});
it will work!
它会工作!
Also according to this questionI changed config>local>database.php to:
同样根据这个问题,我将 config>local>database.php 更改为:
'mysql' => array(
'driver' => 'mysql',
'host' => 'mysite.local',
'database' => 'mydatabase',
'username' => 'myusername',
'password' => 'mypassword',
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
),
But this time, even
但这一次,即使
$data=DB::table('mydatabase.user')->get();
doesn't work either! This time it thrown another exception :
也不行!这次它抛出了另一个异常:
PDOException (2002)
SQLSTATE[HY000] [2002] Connection refused
My question is why laravel tries to use "homestead" database instead of "mydatabase"? should I change something else?
我的问题是为什么 laravel 尝试使用“homestead”数据库而不是“mydatabase”?我应该改变别的东西吗?
EDIT:I changed the config/local/database.php to
编辑:我将 config/local/database.php 更改为
'mysql' => array(
'driver' => 'mysql',
'host' => 'localhost',
'database' => 'mydatabase',
'username' => 'myusername',
'password' => 'mypassword',
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
),
and everything works fine! (I changed mysite.local to localhost) I've not define local host in my /etc/hosts so why laravel looks for that host?
一切正常!(我将 mysite.local 更改为 localhost) 我没有在 /etc/hosts 中定义本地主机,那么为什么 laravel 会查找该主机?
采纳答案by Relaxing In Cyprus
Your host should be localhost. The term localhost means the computer which laravel is running on. mysite.local is presumably a virtual site residing on this computer. It doesn't have its own installation of Mysql. All virtual sites will share the same mysql. They will just use different databases.
您的主机应该是本地主机。术语 localhost 表示运行 laravel 的计算机。mysite.local 大概是驻留在这台计算机上的虚拟站点。它没有自己的 Mysql 安装。所有虚拟站点将共享相同的 mysql。他们只会使用不同的数据库。
Thats how my setups work anyway.
无论如何,这就是我的设置的工作方式。
回答by Muhammad Sadiq
The problem is in your config/database.php with default connection, currently default connection setting is getting from .env file as
'default' => env('DB_CONNECTION', 'mysql'),
问题出在具有默认连接的 config/database.php 中,当前默认连接设置是从 .env 文件获取的
'默认' => env('DB_CONNECTION', 'mysql'),
So change it to :
所以将其更改为:
'default' => 'mysql',