简体   繁体   中英

Connect to remote database on amazon server

I am working on Laravel5.4 application and it has its own database. for some business I need to connect to another remote database on amazon server to read some data and insert also some data but all tries failed. I tried:

.env.php:

DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=db1
DB_USERNAME=root
DB_PASSWORD=root

DB_EXT_CONNECTION=mysql
DB_EXT_HOST=xx.xx.xx.xx
DB_EXT_PORT=3306
DB_EXT_DATABASE=db2
DB_EXT_USERNAME=root
DB_EXT_PASSWORD=root

Config/database.php:

'mysql' => [
            'driver' => 'mysql',
            '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' => 'utf8mb4',
            'collation' => 'utf8mb4_unicode_ci',
            'prefix' => '',
            'strict' => true,
            'engine' => null,
        ],

    //External Database Credentials
    'ext_mysql' => [
        'driver' => 'mysql',
        'host' => env('DB_EXT_HOST', '127.0.0.1'),
        'port' => env('DB_EXT_PORT', '3306'),
        'database' => env('DB_EXT_DATABASE', 'forge'),
        'username' => env('DB_EXT_USERNAME', 'forge'),
        'password' => env('DB_EXT_PASSWORD', ''),
        'unix_socket' => env('DB_EXT_SOCKET', ''),
        'charset' => 'utf8mb4',
        'collation' => 'utf8mb4_unicode_ci',
        'prefix' => '',
        'strict' => true,
        'engine' => null,
    ],

The I try in my controller:

$users = DB::connection('ext_mysql')->table('users')->get();
return $users;

But, the connection on browser is take too long and finally gives 'Connection time out'

Can anybody help ?

You have defined your external database in your .env file as with prefix as DB_EXT_ and you are trying to access in config/database.php files as DB_SRD.

Change your config/database.php file

'ext_mysql' => [
    'driver' => 'mysql',
    'host' => env('DB_EXT_HOST', '127.0.0.1'),
    'port' => env('DB_EXT_PORT', '3306'),
    'database' => env('DB_EXT_DATABASE', 'forge'),
    'username' => env('DB_EXT_USERNAME', 'forge'),
    'password' => env('DB_EXT_PASSWORD', ''),
    'unix_socket' => env('DB_EXT_SOCKET', ''),
    'charset' => 'utf8mb4',
    'collation' => 'utf8mb4_unicode_ci',
    'prefix' => '',
    'strict' => true,
    'engine' => null,
]

Also check that your remote database supports connection from external IP address.

I need to connect to another remote database on amazon server to read some data

If your database resides in remote machine then, you need to use hostname or ip address of remote machine.

I'm not aware of env method, but I think, you need to change

'host' => env('DB_EXT_HOST', '127.0.0.1'),

to

'host' => env('DB_EXT_HOST', 'xxx.xxx.xxx.xx'),

'xxx.xxx.xxx.xx' would be ip address of remote machine.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM