0

I am using more than 1 database connection in my Laravel development but it is not getting the right information;

/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,
    ]
],

in my Controller

$result = DB::connection('database2')->table('mytable')->select('*')->get();

I am getting the error response;

SQLSTATE[HY000] [1045] Access denied for user 'user'@'000.000.000.000' (using password: YES)

However the ip address it shows is for 'database' even though I am trying to connect to 'database2'

Kieran Headley
  • 647
  • 1
  • 7
  • 21
  • Looks like user 'user' do not allowed to access from that host. You could check that by query: `SHOW GRANTS user` – vstelmakh Oct 11 '17 at 18:57
  • Thanks for the comment, the reason for that is because I am trying to connect to database2 but it is using database information. database is locked and can only be accessed using ssh, database2 is open – Kieran Headley Oct 11 '17 at 18:59
  • 2
    what's your .env like? are you srure DB2 is different from DB in it? – Unamata Sanatarai Oct 11 '17 at 21:49
  • Also watch out for caching. https://stackoverflow.com/questions/44651846/conflict-database-when-multiple-laravel-projects-on-single-machine – Don't Panic Oct 12 '17 at 14:55
  • check DB2_HOST DB2* variables in your .env file also make sure you clear cache ``php artisan config:clear`` – Abdul Manan Jul 08 '19 at 07:59

1 Answers1

0

See the below configuration, you have to configure everything for your new database:

'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,
],

Where HOST_IP,HOST_PORT,DATABASE_NAME,USER_NAME,PASSWORD you need to change.

mialdi98
  • 58
  • 7