2

I've set up a laravel project with sail and I'm trying to connect to my database with a db management tool. Yet I can't figure out what address and what username I should use.

In my .env file :

DB_CONNECTION=mysql
DB_HOST=mysql
DB_PORT=3306
DB_DATABASE=app-name
DB_USERNAME=sail
DB_PASSWORD=password

The set-up is the default one. I've tried to connect through localhost:3306, but it doesn't work. Do you have any Idea on how I should proceed?

note that my project is running correctly in localhost.

L.DZ
  • 111
  • 3
  • 12
  • Does this answer your question? [Can't connect to database Laravel Sail](https://stackoverflow.com/questions/65761142/cant-connect-to-database-laravel-sail) – online Thomas Feb 22 '22 at 09:32

3 Answers3

1

As Laravel sail is basically just a docker-compose wrapper, you should check your docker-compose.yml file in the root directory for port forwardings of the mysql container. There should be something like this:

...
mysql:
  image: 'mysql/mysql-server:8.0'
  ports:
    - '${FORWARD_DB_PORT:-3306}:3306'
...

You could also check docker after putting sail up with sail up -d by running docker ps -a | grep mysql. If your output contains 0.0.0.0:3306->3306/tcp, your mysql server should be reachable at localhost:3306.

MPK1
  • 336
  • 1
  • 12
  • I have that but it still doesn't work. It seems that it is because of v8 of mysql that change something in the password management. I saw some fix by editing some permision. To do that I need to enter sql command, [link](https://hackthestuff.com/article/error-solved-client-does-not-support-authentication-protocol-requested-by-server-consider-upgrading-mysql-client) do you know where I shall enter those command? – L.DZ Feb 21 '22 at 15:07
  • 2
    You can enter the mysql console by executing `sail exec mysql mysql -u root -p` in your root directory. The password is in your `.env` file. – MPK1 Feb 21 '22 at 15:42
0

You need to connect to 127.0.0.1, not localhost. The username and password are the same as in the .env file.

TimoSolo
  • 7,068
  • 5
  • 34
  • 50
0

First, DB_HOST=mysql because the web container is looking for the host it defined as 'mysql' rather than itself (127.0.0.1).

Secondly, in my case, I was forwarding to port 33062 so I could keep my already running mysql service, and also access the sail mysql instance from the command line.

However, in the .env file, within the container environment, that web server would still be accessing 3306, so DB_PORT=3306 rather than DB_PORT=33062 was correct.

iateadonut
  • 1,951
  • 21
  • 32