Gedaym
Gedaym

Reputation: 31

Laravel Migration is working but cannot access with Eloquent

I have some issues with my Laravel.

Migrations work fine but when I try to insert or access data with Eloquent I have an error:

"SQLSTATE[HY000] [2002] Connection refused"

.env file:

DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=myddb
DB_USERNAME=root
DB_PASSWORD=password

database.php file:

'driver' => 'mysql',
'host' =>'127.0.0.1',
'port' => '3306',
'database' =>'myddb',
'username' => 'root',
'password' => 'password',

I use Docker.

Upvotes: 1

Views: 667

Answers (2)

Moritz Schmid
Moritz Schmid

Reputation: 153

Your DB should be your docker container so it should look something like this:

DB_CONNECTION=mysql
DB_HOST=database <== needs to be named after your database container name
DB_PORT=3306
DB_DATABASE=myddb
DB_USERNAME=root
DB_PASSWORD=password

Upvotes: 1

Unamata Sanatarai
Unamata Sanatarai

Reputation: 6637

Assuming your docker-compose.yml shows the folowing:

version: '2'
services:

  thedatabase:
    build:...

  web:
    ...

Your .env file should read:

DB_HOST=thedatabase

That way your docker images will communicate. And always when you have to run any artisan command, simply enter to image and run the command. For example:

my@computer $ docker-compose exec web bash
docker:web $ php artisan migrate

Or command a image to run a command like so:

docker-compose exec web php artisan migrate

Upvotes: 2

Related Questions