Home > database >  Try to connect localhost laravel project to another database from another server
Try to connect localhost laravel project to another database from another server

Time:12-02

I want to use database from another server to my local-host laravel project. I configure .env file and config\database.php file like below. I am able to access my local database but unable to access outside database. I get error: "SQLSTATE[HY000] [1045] Access denied for user 'ExecUser'@'localhost' (using password: YES) (SQL: select * from value_list) "

DB_CONNECTION_SECOND=mysql
DB_HOST_SECOND=180.48.10.222
DB_PORT_SECOND=3306
DB_DATABASE_SECOND=nsu
DB_USERNAME_SECOND=ExeUs
DB_PASSWORD_SECOND=******


'mysql2' => [
            'driver' => 'mysql',
            'url' => env('DATABASE_URL'),
            'host' => env('DB_HOST', '180.48.10.222'),
            'port' => env('DB_PORT', '3306'),
            'database' => env('DB_DATABASE_SECOND', 'forge'),
            'username' => env('DB_USERNAME_SECOND', 'forge'),
            'password' => env('DB_PASSWORD_SECOND', ''),
            'unix_socket' => env('DB_SOCKET', ''),
            'charset' => 'utf8mb4',
            'collation' => 'utf8mb4_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'),
            ]) : [],
        ],  

CodePudding user response:

Since you are using a second connection you will also have to change the host and url configs for the second connection. At the moment the problem is that the url and host point to the localhost DB_HOSTand DB_PORT .env variables.
They should point to DB_HOST_SECOND and DB_PORT_SECOND, respectively.

CodePudding user response:

The root cause: You forgot to change the host, port to host another.

Please update the code same below:

'mysql2' => [
        'driver' => 'mysql',
        'url' => env('DATABASE_URL'),
        'host' => env('DB_HOST_SECOND'),
        'port' => env('DB_PORT_SECOND'),
        'database' => env('DB_DATABASE_SECOND'),
        'username' => env('DB_USERNAME_SECOND'),
        'password' => env('DB_PASSWORD_SECOND'),
        'unix_socket' => env('DB_SOCKET', ''),
        'charset' => 'utf8mb4',
        'collation' => 'utf8mb4_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'),
        ]) : [],
    ],  
  • Related