问题
I am new to laravel. I use homestead for development. I have two database connection
database.php
'cust_main_db' => [
'driver' => 'mysql',
'host' => 'localhost',
'database' => 'project_db',
'username' => 'homestead',
'password' => 'secret',
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
'strict' => false,
],
'admin_main_db' => [
'driver' => 'mysql',
'host' => 'localhost',
'database' => 'projectadmin_db',
'username' => 'homestead',
'password' => 'secret',
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
'strict' => false,
],
Homestead.yaml
databases:
- homestead
In my local mysql has both databases project_db
and projectadmin_db
When I run the project http://homestead.app:8000/
it shows SQLSTATE[HY000] [1049] Unknown database 'projectadmin_db'
What I have missed here? Correct me if anything wrong.
回答1:
When you run your app in Homestead, it looks for databases in Homestead and not your actual localhost. You can connect to Homestead's MySQL through 127.0.0.1:33060
.
回答2:
On models that use projectadmin_db
database, you need to add
protected $connection = 'admin_main_db';
回答3:
make sure you use the default Homestead port 33060 , when you are creating your databases ,
i.e: you created your databases on the guest vm not on the host machine.
来源:https://stackoverflow.com/questions/32756810/laravel-homestead-unknown-database-projectadmin-db