Reputation: 454
I am trying to migrate a Laravel 8 project from Homestead to Sail. Sail seems to be set up correctly since I can get to my project's website locally. But I can't get into mysql to look around in the database:
➜ myproject git:(master) ✗ sail mysql
ERROR 1045 (28000): Access denied for user 'myproject'@'localhost' (using password: YES)
I have tried restarting Docker, and restarting my terminal and running sail build --no-cache && sail up
to no avail.
Here is the relevant portion of my .env
file:
DB_CONNECTION=mysql
DB_HOST=localhost
DB_PORT=3306
DB_DATABASE=myproject
DB_USERNAME=myproject
DB_PASSWORD=secret
And here is the docker-compose.yml
file:
# For more information: https://laravel.com/docs/sail
version: '3'
services:
laravel.test:
build:
context: ./vendor/laravel/sail/runtimes/8.1
dockerfile: Dockerfile
args:
WWWGROUP: '${WWWGROUP}'
image: sail-8.1/app
extra_hosts:
- 'host.docker.internal:host-gateway'
ports:
- '${APP_PORT:-80}:80'
- '${HMR_PORT:-8080}:8080'
environment:
WWWUSER: '${WWWUSER}'
LARAVEL_SAIL: 1
XDEBUG_MODE: '${SAIL_XDEBUG_MODE:-off}'
XDEBUG_CONFIG: '${SAIL_XDEBUG_CONFIG:-client_host=host.docker.internal}'
volumes:
- '.:/var/www/html'
networks:
- sail
depends_on:
- mysql
mysql:
image: 'mysql/mysql-server:8.0'
ports:
- '${FORWARD_DB_PORT:-3306}:3306'
environment:
MYSQL_ROOT_PASSWORD: '${DB_PASSWORD}'
MYSQL_ROOT_HOST: "%"
MYSQL_DATABASE: '${DB_DATABASE}'
MYSQL_USER: '${DB_USERNAME}'
MYSQL_PASSWORD: '${DB_PASSWORD}'
MYSQL_ALLOW_EMPTY_PASSWORD: 1
volumes:
- 'sail-mysql:/var/lib/mysql'
- './vendor/laravel/sail/database/mysql/create-testing-database.sh:/docker-entrypoint-initdb.d/10-create-testing-database.sh'
networks:
- sail
healthcheck:
test: ["CMD", "mysqladmin", "ping", "-p${DB_PASSWORD}"]
retries: 3
timeout: 5s
networks:
sail:
driver: bridge
volumes:
sail-mysql:
driver: local
EDIT: When I change my .env to
DB_CONNECTION=mysql
DB_HOST=mysql
DB_PORT=3306
DB_DATABASE=myproject
DB_USERNAME=sail
DB_PASSWORD=password
Then the error is
➜ myproject git:(master) ✗ sail mysql
ERROR 1044 (42000): Access denied for user 'sail'@'%' to database 'myproject'
Upvotes: 2
Views: 5657
Reputation: 839
If it doesn't work try this solution
First, you need to access the MySQL container within your Sail environment.
./vendor/bin/sail exec mysql bash
Log in to MySQL as the Root User:
mysql -u root -p
Enter the password when prompted. The password is usually set in your .env file under DB_PASSWORD.
grant the necessary privileges to the sail user:
GRANT ALL PRIVILEGES ON *.* TO 'sail'@'%';
then FLUSH PRIVILEGES
FLUSH PRIVILEGES;
Upvotes: 1
Reputation: 1095
Adding the FORWARD_DB_PORT
key in my .env
file and running sail up
again seemed to work for me!
FORWARD_DB_PORT=3307
When you're connecting to the database, make sure to use the above specified port!
Upvotes: 1
Reputation: 714
I Just tried to connect to a fresh laravel 9 app and I think you only need to change a few lines in order to connect to Sail's Mysql:
DB_CONNECTION=mysql
DB_HOST=host.docker.internal
DB_PORT=3306
DB_DATABASE=myproject
DB_USERNAME=sail # <-- "sail" as default user name
DB_PASSWORD=password # <-- "password" as default password
This is needed because "mysql" is the name of the instance of the database section in the docker container that sail uses to interact with the database.
All other settings still the same.
Then as you do before sail build --no-cache && sail up
.
Upvotes: 7