Laravel连接错误
I've eventually made my connection with mysql instead of mssql because it was not working (I'm working with laravel). But now it's still not working. I receive the error:
SQLSTATE[3D000]: Invalid catalog name: 1046 No database selected (SQL: create table `users` (`id` int unsigned not null auto_increment primary key, `username` varchar(32) not null, `email` varchar(320) not null, `password` varchar(60) not null, `created_at` timestamp default 0 not null, `updated_at` timestamp default 0 not null) default character set utf8 collate utf8_unicode_ci)
I'm connection to my localhost with a wamp server. This is my code:
> 'mysql' => [
> 'driver' => 'mysql',
> 'host' => env('localhost'),
> 'database' => env('test'),
> 'username' => env('root'),
> 'password' => env(''),
> 'charset' => 'utf8',
> 'collation' => 'utf8_unicode_ci',
> 'prefix' => '',
> 'strict' => false,
> ],
And my env :
'default' => env('mysql', 'mysql'),
EDIT
.env:
DB_HOST=localhost
DB_DATABASE=test
DB_USERNAME=root
DB_PASSWORD=
syntax env('key','default')
with:
key
is key before = in env file
default
is default value if key not exist in env file
please edit as follows:
> 'mysql' => [
> 'driver' => 'mysql',
> 'host' => env('DB_HOST', 'localhost'),
> 'database' => env('DB_DATABASE','db_name'),
> 'username' => env('DB_USERNAME','root'),
> 'password' => env('DB_PASSWORD',''),
> 'charset' => 'utf8',
> 'collation' => 'utf8_unicode_ci',
> 'prefix' => '',
> 'strict' => false,
> ],
P/s: excuse me my English.
.env file
DB_HOST=localhost
DB_DATABASE=dbname
DB_USERNAME=dbusername
DB_PASSWORD=dbpassword
config/database.php
'mysql' => [
'driver' => 'mysql',
'host' => env('DB_HOST', 'localhost'),
'database' => env('DB_DATABASE', 'databasename'),
'username' => env('DB_USERNAME', 'dbusername'),
'password' => env('DB_PASSWORD', 'dbpassword'),
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
'strict' => false,
],
As you can see in the database.php env('DB_DATABASE', 'databasename'),
means, check for DB_DATABASE
value in .env file, and if its not found or set, use databasename
that you provide as the parameter.
In your case, the syntax of env
was screwed up, which probably caused the error. Actually, if you have a .env file, you dont have to edit it at all. Or you can avoid using an .env file, and provide the datas in the database.php in the said syntax, not the one you are using at the moment.
Note : .env
file has the first priority, and if you use mention database1
in .env
file and mention database2
in the database.php
file, database1
will be used.