Eric
Eric

Reputation: 1261

Can't connect to test database with Docker and Laravel Sail

I'm trying to set up my first test database using Laravel's sail. I can see that the test database is created and a user is added:

mysql_test_1    | 2021-03-13 20:20:32+00:00 [Note] [Entrypoint]: Creating database test_database
mysql_test_1    | 2021-03-13 20:20:32+00:00 [Note] [Entrypoint]: Creating user test_root
mysql_test_1    | 2021-03-13 20:20:32+00:00 [Note] [Entrypoint]: Giving user test_root access to schema test_database

However, when I try to connect via TablePlus, I get:

Access denied for user 'test_root'@'172.24.0.1' (using password: YES)

Host: 127.0.0.1 Port: 3306 User: test_root Password: test_root Database: test_database

My .env has the following:

TEST_DB_CONNECTION=mysql
TEST_DB_HOST=mysql_test
TEST_DB_PORT=3306
TEST_DB_DATABASE=test_database
TEST_DB_USERNAME=test_root
TEST_DB_PASSWORD=test_root

and my docker-compose.yml file has:

mysql:
    image: 'mysql:8.0'
    ports:
        - '${FORWARD_DB_PORT:-3306}:3306'
    environment:
        MYSQL_ROOT_PASSWORD: '${DB_PASSWORD}'
        MYSQL_DATABASE: '${DB_DATABASE}'
        MYSQL_USER: '${DB_USERNAME}'
        MYSQL_PASSWORD: '${DB_PASSWORD}'
        MYSQL_ALLOW_EMPTY_PASSWORD: 'yes'
    volumes:
        - 'sailmysql:/var/lib/mysql'
    networks:
        - sail
    healthcheck:
      test: ["CMD", "mysqladmin", "ping"]
mysql_test:
  image: "mysql:8.0"
  environment:
    MYSQL_ROOT_PASSWORD: "${TEST_DB_PASSWORD}"
    MYSQL_DATABASE: "${TEST_DB_DATABASE}"
    MYSQL_USER: "${TEST_DB_USERNAME}"
    MYSQL_PASSWORD: "${TEST_DB_PASSWORD}"
    MYSQL_ALLOW_EMPTY_PASSWORD: "yes"
  networks:
    - sail

Upvotes: 0

Views: 2287

Answers (1)

Faeeria
Faeeria

Reputation: 826

You are launching two different mysql containers :

  • mysql is running and linking its 3306 port with your computer 3306 port. You can connect to it using the host localhost or 127.0.0.1. It is this dbms that gives you the 'Access Denied' error.
  • mysql_test is running but its 3306 port is not linked to any port on your host, meaning you cannot directly access it.

Since you seem to be running TablePlus from your host (and not from a docker container), I'd suggest to link the port 3307 (or any other port) from your host to the port 3306 of your container, using the "ports" option like you did for your first container. Then, configure TablePlus so that it connects to your first dbms on port 3306 and your second dbms on port 3307.

Upvotes: 1

Related Questions