本文介绍了无法使用Docker和Laravel Sail连接到测试数据库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用Laravel的帆建立我的第一个测试数据库.我可以看到已经创建了测试数据库并添加了一个用户:

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

但是,当我尝试通过TablePlus连接时,会得到:

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

主机:127.0.0.1端口:3306用户:test_root密码:test_root数据库:test_database

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

我的.env文件如下:

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

和我的docker-compose.yml文件具有:

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

推荐答案

您要启动两个不同的mysql容器:

You are launching two different mysql containers :

  • mysql 正在运行,并将其3306端口与您的计算机3306端口链接.您可以使用主机 localhost 127.0.0.1 连接到它.正是这种dbms会给您访问被拒绝"错误.
  • mysql_test 正在运行,但其3306端口未链接到主机上的任何端口,这意味着您无法直接访问它.
  • 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.

由于您似乎正在从主机(而不是从Docker容器)运行TablePlus,因此建议您将主机中的端口3307 (或任何其他端口)链接到该端口3306,使用端口"就像您为第一个容器所做的那样.然后,配置TablePlus,使其连接到端口3306上的第一个dbms和端口3307上的第二个dbms.

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.

这篇关于无法使用Docker和Laravel Sail连接到测试数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-28 03:50