我在Laravel开发中使用了多个数据库连接,但它没有获得正确的信息;
/config/database.php
'connections' => [
'database' => [
'driver' => 'mysql',
'host' => env('DB_HOST', 'localhost'),
'port' => env('DB_PORT', '3306'),
'database' => env('DB_DATABASE', 'forge'),
'username' => env('DB_USERNAME', 'forge'),
'password' => env('DB_PASSWORD', ''),
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
'strict' => true,
'engine' => null,
],
'database2' => [
'driver' => 'mysql',
'host' => env('DB2_HOST', 'localhost'),
'port' => env('DB2_PORT', '3306'),
'database' => env('DB2_DATABASE', 'forge'),
'username' => env('DB2_USERNAME', 'forge'),
'password' => env('DB2_PASSWORD', ''),
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
'strict' => true,
'engine' => null,
]
],
在我的控制器中
$result = DB::connection('database2')->table('mytable')->select('*')->get();
我收到错误回复;
SQLSTATE [HY000] [1045]拒绝访问用户'user'@'000.000.000.000'(使用密码:是)
然而,即使我尝试连接到'database2',它显示的IP地址也是'数据库'
答案 0 :(得分:0)
请参阅以下配置,您必须为新数据库配置所有内容:
'database2' => [
'driver' => 'mysql',
'host' => env('DB_HOST2', 'HOST_IP'),
'port' => env('DB_PORT2', 'HOST_PORT'),
'database' => env('DB_DATABASE2', 'DATABASE_NAME'),
'username' => env('DB_USERNAME2', 'USER_NAME'),
'password' => env('DB_PASSWORD2', 'PASSWORD'),
'unix_socket' => env('DB_SOCKET2', ''),
'charset' => 'utf8mb4',
'collation' => 'utf8mb4_unicode_ci',
'prefix' => '',
'strict' => false,
'engine' => null,
],
您需要更改的 HOST_IP,HOST_PORT,DATABASE_NAME,USER_NAME,PASSWORD 。