Matthieu
Matthieu

Reputation: 235

laravel try to connect to localhost database while the database is in external server

I am working on localhost and I would like to connect my laravel app to an external database. I put all my settings in :

config/database.php :

'mysql' => [
    'driver' => 'mysql',
    'url' => env('DATABASE_URL'),
    'host' => env('DB_HOST', 'XX.XX.XXX.XXX'),
    'port' => env('DB_PORT', 'XXXX'),
    'database' => env('DB_DATABASE', 'name'),
    'username' => env('DB_USERNAME', 'username'),
    'password' => env('DB_PASSWORD', 'password'),
    'unix_socket' => env('DB_SOCKET', '/var/run/mysqld/mysqld.sock'),
    'charset' => 'latin-1',
    'collation' => 'latin1_swedish_ci',
    'prefix' => '',
    'prefix_indexes' => true,
    'strict' => true,
    'engine' => 'InnoDB',
    'options' => extension_loaded('pdo_mysql') ? array_filter([
        PDO::MYSQL_ATTR_SSL_CA => env('MYSQL_ATTR_SSL_CA'),
    ]) : [],
],

.env :

APP_NAME=Laravel
APP_ENV=local
APP_KEY=base64:X...
APP_DEBUG=true
APP_URL=http://localhost

LOG_CHANNEL=stack

DB_CONNECTION=mysql
DATABASE_URL=XX.XX.XXX.XXX:XXXX
DB_HOST=XX.XX.XXX.XXX
DB_PORT=XXXX
DB_DATABASE=name
DB_USERNAME=username
DB_PASSWORD=password

I have the same error :

SQLSTATE[HY000] [1045] access denied for user : 'username'@'@localhost'

It seems that the app try to connect to localhost database but my database is not in localhost.

Upvotes: 0

Views: 1318

Answers (1)

Stefan Yilmaz
Stefan Yilmaz

Reputation: 41

If you served your project with artisan(php artisan serve) you kill it and start it again when you change something in .env file.

Could you try it?

Upvotes: 1

Related Questions