mysql Can't connect to database Laravel Sail

ma8fv8wu  于 2022-12-22  发布在  Mysql
关注(0)|答案(5)|浏览(159)

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:

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

neekobn8

neekobn81#

You should define

FORWARD_DB_PORT=3306

in your .env this will expose mysql on that port for development connections. I would recommend to not expose this port on production.
https://github.com/laravel/sail/blob/e3d601598e735407070dd50e09ab6e0c68ab32ae/stubs/mysql.stub#L4

0yg35tkg

0yg35tkg2#

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
wooyq4lh

wooyq4lh3#

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=

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...)
oyxsuwqo

oyxsuwqo4#

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.

yeotifhr

yeotifhr5#

Probably, you already have a bank installed on port 3306, change the tags below in the .env to another port: (example 3307)
DB_PORT=3307
FORWARD_DB_PORT=3307
I think it works.

相关问题