laravel Github操作未连接到MySQL示例

ct3nt3jp  于 2023-06-24  发布在  Git
关注(0)|答案(1)|浏览(103)

我试图为Laravel项目设置Github操作,但我似乎无法连接到MySQL示例...它总是给我一个如下错误;

SQLSTATE[HY000] [2002] Connection refused

我拉入一个容器,在ubuntu-latest之上运行我的操作,除了运行测试套件,其他一切都正常;
当启动MySQL容器时,它似乎都能工作,并输出以下内容;

mysql service is starting
mysql service is healthy.

我的行动文件如下;

name: Doing some tests n stuff

on: push

jobs:
    tests:
        runs-on: ubuntu-latest
        container: existenz/builder:8.0
        env:
            APP_ENV: testing
            DB_CONNECTION: mysql
            DB_USERNAME: laravel
            DB_DATABASE: laravel
            DB_HOST: 127.0.0.1
            DB_PORT: 3306
            BROADCAST_DRIVER: log
            CACHE_DRIVER: redis
            QUEUE_CONNECTION: redis
            SESSION_DRIVER: redis

        services:
            mysql:
                image: mysql:latest
                env:
                    MYSQL_ALLOW_EMPTY_PASSWORD: yes
                    MYSQL_USER: laravel
                    MYSQL_PASSWORD: password
                    MYSQL_DATABASE: laravel
                ports:
                    - 3306/tcp
                options: --health-cmd="mysqladmin ping" --health-interval=10s --health-timeout=5s --health-retries=3

            redis:
                image: redis
                ports:
                    - 6379/tcp
                options: --health-cmd="redis-cli ping" --health-interval=10s --health-timeout=5s --health-retries=3

        steps:
            - name: Checkout code
              uses: actions/checkout@v3

            - name: Install extensions
              run: apk -U --no-cache add php8-sodium php8-pdo_mysql

            - name: Install Composer dependencies
              run: composer install --prefer-dist --no-interaction

            - name: Create APP_KEY
              run: echo "APP_KEY=" > .env

            - name: Generate APP_KEY
              run: php artisan key:generate

            - name: Clear Config
              run: php artisan config:clear

            - name: Run Testsuite
              run: vendor/bin/phpunit
              env:
                DB_PORT: ${{ job.services.mysql.ports['3306'] }}
                REDIS_PORT: ${{ job.services.redis.ports['6379'] }}

            - name: Upload artifacts
              uses: actions/upload-artifact@master
              if: failure()
              with:
                name: Logs
                path: ./storage/logs
bmp9r5qi

bmp9r5qi1#

您的Laravel应用程序似乎无法连接到MySQL示例。
1.* * Docker网络问题。**
1.* * MySQL not ready yet:**“Connection refused”错误也可能意味着测试运行时MySQL服务尚未就绪。您可以通过在运行测试之前添加延迟或使用等待脚本来缓解这种情况。
1.* * 暴露端口:除非您需要从作业主机外部连接到服务(通常情况下并非如此),否则无需在操作的服务部分中暴露端口。服务可以在其默认端口上相互连接,而不会暴露它们。
尝试在
Clean configRun Test Suite**之间增加以下步骤:

- name: Wait for MySQL to initialize
    run: sleep 15 # Adding a delay

请注意,使用sleep 15添加延迟并不是最健壮的解决方案,最好使用等待脚本来确保MySQL在继续之前完全初始化。但对于快速测试,延迟可能有助于确定问题是否与时间有关。

相关问题