1,在.env文件中添加下列代码
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=demo1
DB_USERNAME=root
DB_PASSWORD=root123456
2,在config/database.php文件中添加
'mysql_web' => [
'driver' => 'mysql',
'url' => env('DATABASE_URL'),
'host' => env('DB_HOST', '127.0.0.1'),
'port' => env('DB_PORT', '3306'),
'database' => env('DB_DATABASE', 'forge'),
'username' => env('DB_USERNAME', 'forge'),
'password' => env('DB_PASSWORD', ''),
'unix_socket' => env('DB_SOCKET', ''),
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
'prefix_indexes' => true,
'strict' => true,
'engine' => null,
'options' => extension_loaded('pdo_mysql') ? array_filter([
PDO::MYSQL_ATTR_SSL_CA => env('MYSQL_ATTR_SSL_CA'),
]) : [],
],
3,创建模型,在模型中指定数据库链接
protected $connection = ‘mysql_web’; //指定链接的数据库