在 GitHub 工作流中使用 mysql 总是导致 SQLSTATE[HY000] [1045] 用户访问被拒绝的错误。

3

我目前正在通过GitHub Actions为我的Laravel项目配置CI管道。

这是我的build.yml文件:

# GitHub Action for Laravel with MySQL and Redis
name: API
on: [push, pull_request]
jobs:
  laravel:
    name: Laravel (PHP ${{ matrix.php-versions }})
    runs-on: ubuntu-latest
    services:
      mysql:
        image: mysql:5.7
        env:
          MYSQL_ROOT_PASSWORD: 'secret'
          MYSQL_DATABASE: 'content_information_test'
          MYSQL_USER: 'homestead'
          MYSQL_PASSWORD: 'secret'
        ports:
          - 33306: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
    strategy:
      fail-fast: false
      matrix:
        php-versions: ['7.4']
    steps:
      - name: Checkout
        uses: actions/checkout@v2
      - name: Setup PHP, with composer and extensions
        uses: shivammathur/setup-php@v2 #https://github.com/shivammathur/setup-php
        with:
          php-version: ${{ matrix.php-versions }}
          extensions: mbstring, dom, fileinfo, mysql
          coverage: xdebug #optional
      - name: Start mysql service
        run: sudo /etc/init.d/mysql start
      - name: Get composer cache directory
        id: composercache
        run: echo "::set-output name=dir::$(composer config cache-files-dir)"
      - name: Cache composer dependencies
        uses: actions/cache@v2
        with:
          path: ${{ steps.composercache.outputs.dir }}
          # Use composer.json for key, if composer.lock is not committed.
          # key: ${{ runner.os }}-composer-${{ hashFiles('**/composer.json') }}
          key: ${{ runner.os }}-composer-${{ hashFiles('**/composer.lock') }}
          restore-keys: ${{ runner.os }}-composer-
      - name: Install Composer dependencies
        run: composer install --no-progress --prefer-dist --optimize-autoloader
      - name: Copy Env File
        run: cp .env.testing .env
      - name: Setup database user
        run: mysql -u runner -e 'CREATE USER 'worker'@'localhost' IDENTIFIED BY 'secret';'
      - name: Flush privileges
        run: mysql -u worker --password=secret -e 'FLUSH PRIVILEGES;'
      - name: Create testing database
        run: mysql -u worker --password=secret -e 'CREATE DATABASE IF NOT EXISTS content_information_test;'
      - name: Migrate Test Database
        run: php artisan migrate --env=testing --seed --force
        env:
          DB_PORT: 33306:3306/tcp
          REDIS_PORT: ${{ job.services.redis.ports['6379'] }}
      - name: Change Directory Permissions
        run: chmod -R 777 storage bootstrap/cache
      - name: Static Analysis via PHPStan
        run: ./vendor/bin/phpstan analyse app/BusinessDomain app/DataDomain app/Infrastructure tests/Unit -c phpstan.neon
      - name: Execute tests (Unit and Feature tests) via PHPUnit
        run: vendor/bin/phpunit
        env:
          DB_PORT: 33306:3306/tcp
          REDIS_PORT: ${{ job.services.redis.ports['6379'] }}
      - name: Run code style fixer on app/
        run: php tools/php-cs-fixer/vendor/bin/php-cs-fixer fix app/
      - name: Run code style fixer on tests/
        run: php tools/php-cs-fixer/vendor/bin/php-cs-fixer fix tests/
      - name: Run code style fixer on database/
        run: php tools/php-cs-fixer/vendor/bin/php-cs-fixer fix database/
      - name: Run code style fixer on routes/
        run: php tools/php-cs-fixer/vendor/bin/php-cs-fixer fix routes/

问题在于,操作总是在“迁移测试数据库”步骤处失败,并出现以下错误。
Run php artisan migrate --env=testing --seed --force
  php artisan migrate --env=testing --seed --force
  shell: /usr/bin/bash -e {0}
  env:
    DB_PORT: 33306:3306/tcp
    REDIS_PORT: 49153

In Connection.php line 678:
                                                                               
  SQLSTATE[HY000] [1045] Access denied for user 'homestead'@'localhost' (usin  
  g password: NO) (SQL: select * from information_schema.tables where table_s  
  chema = content_information_test and table_name = migrations and table_type  
   = 'BASE TABLE')                                                             
                                                                               

In Connector.php line 70:
                                                                               
  SQLSTATE[HY000] [1045] Access denied for user 'homestead'@'localhost' (usin  
  g password: NO)                                                              
                                                                               

Error: Process completed with exit code 1.

不幸的是,对我来说这似乎是正确的行为,因为我从未创建过名为homestead的用户,但我仍然不知道如何创建一个可用的mysql用户,因为每当尝试通过工作流使用mysql时,总是会收到“SQLSTATE [HY000] [1045]拒绝访问用户”错误。

有人知道如何解决这个问题吗?

1个回答

0

我在使用GitHub Actions和Laravel Dusk设置CI/CD流水线时也遇到了这个问题,使用Dusk文档中的默认GitHub Action工作流程,就像其他与Dusk相关的内容一样-缺乏说明。

我通过将我的.env.dusk.testing文件中的数据库环境变量添加到GitHub Actions工作流程中来解决这个问题,以确保GitHub Actions运行器可以访问它们。我还可以将它们从“运行Dusk”步骤可选地移动到作业级别,使其对作业中的任何其他步骤都可用:

name: CI

on: [push]

jobs:
  dusk-php:
    env:
      APP_URL: "http://127.0.0.1:8000"
      DB_DATABASE: CICD_test_db
      DB_USERNAME: root
      DB_PASSWORD: root
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v2
      - name: Prepare The Environment
        run: cp .env.example .env
      - name: Create Database
        run: |
          sudo systemctl start mysql
          mysql --user="root" --password="root" -e "CREATE DATABASE CICD_test_db character set UTF8mb4 collate utf8mb4_bin;"
      - name: Install Composer Dependencies
        run: composer install --no-progress --prefer-dist --optimize-autoloader
      - name: Generate Application Key
        run: php artisan key:generate
      - name: Upgrade Chrome Driver
        run: php artisan dusk:chrome-driver `/opt/google/chrome/chrome --version | cut -d " " -f3 | cut -d "." -f1`
      - name: Start Chrome Driver
        run: ./vendor/laravel/dusk/bin/chromedriver-linux &
      - name: Run Laravel Server
        run: php artisan serve --no-reload &
      - name: Run Dusk Tests
        run: php artisan dusk --env=testing -vvv
      - name: Upload Screenshots
        if: failure()
        uses: actions/upload-artifact@v2
        with:
          name: screenshots
          path: tests/Browser/screenshots
      - name: Upload Console Logs
        if: failure()
        uses: actions/upload-artifact@v2
        with:
          name: console
          path: tests/Browser/console

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接