老问题,但是,很多人可能需要这个,所以添加对我有用的
在 5.6+ 中,也将在出租版本上工作
DB_CONNECTION_REMOTE=mysql
DB_HOST_REMOTE=remotehost
DB_PORT_REMOTE=3306
DB_DATABASE_REMOTE=database_name
DB_USERNAME_REMOTE=database_user
DB_PASSWORD_REMOTE=database_password
并在 config/database.php
'connections' => [
'mysqlRemote' => [
'driver' => 'mysql',
'host' => env('DB_HOST_REMOTE', '127.0.0.1'),
'port' => env('DB_PORT_REMOTE', '3306'),
'database' => env('DB_DATABASE_REMOTE', 'forge'),
'username' => env('DB_USERNAME_REMOTE', 'forge'),
'password' => env('DB_PASSWORD_REMOTE', ''),
'unix_socket' => env('DB_SOCKET', ''),
'charset' => 'utf8mb4',
'collation' => 'utf8mb4_unicode_ci',
'prefix' => '',
'strict' => false,
'engine' => null,
],
然后在你的控制器中
$records = DB::connection('mysqlRemote')->select( DB::raw('SELECT `col1`, `col2` FROM `table`') );
如果你的 IP 被列入白名单,它应该可以正常工作,在 Laravel 5.6 中测试