Can't connect to database Laravel Sail

Viewed 12897

I'm trying to create a simple laravel project and I'm following a laracast to set up the project with Sail. The project is running fine and I was able to migrate with vendor/bin/sail artisan migrate.

The next step in the laracast is to connect to the database with tableplus. But i can't get past this step. I get this error in tableplus: enter image description here

This is my .env:

DB_CONNECTION=mysql
DB_HOST=mysql
DB_PORT=3306
DB_DATABASE=csv
DB_USERNAME=root
DB_PASSWORD=

I didn't change any settings in the docker-compose file, in docker desktop i can see that mysql is running on port 3306 and that there's no password set.

Any idea how i can fix this and connect to the database?

EDIT: found the issue. When I ran brew services list, is saw that an older version mysql was still running. Stopped it with brew services stop mysql and now i can connect

4 Answers

I found I had to delete my docker volume as I had the wrong username/password, which can be done with:

  1. Remove volumes with sail down -v
  2. Then back up again with sail up -d
  3. Re-cache the .env variables with sail artisan config:cache
  4. Then re-migrating the db with sail artisan migrate

I have mysql installed on my laptop outside of sail, so this is what worked for me (after you add this to your .env, you'll need to restart sail so it can take affect in docker):

.env: FORWARD_DB_PORT=3307

For TablePlus, you really just need the Name, Host, the modified port, and user/pass (sail defaults to root and no password). Enter the same database name as .env:DB_DATABASE=

enter image description here

If you look at your docker-compose.yml, you'll see the default is set to 3306 and this overrides it (nothing you need to do here, just an FYI):

mysql:
    image: 'mysql:8.0'
    ports:
        - '${FORWARD_DB_PORT:-3306}:3306'
    environment:
        MYSQL_ROOT_PASSWORD: '${DB_PASSWORD}'
        MYSQL_DATABASE: '${DB_DATABASE}'
    (...etc...)

I had mysql running through homebrew as well, which I found out was the issue. Stopping it (brew services stop mysql) and then attempting to connect solved the issue.

Related