【问题标题】:Error to connect to mysql using github actions + mysql + python使用 github 操作 + mysql + python 连接到 mysql 时出错
【发布时间】:2020-06-26 07:26:59
【问题描述】:

我正在尝试使用 github 操作设置 python 应用程序和 mysql 服务器。这是我的行动:

name: CI

on:
  push:
    branches:
      - master
  pull_request:
    branches:
      - master

jobs:
  # Label of the container job
  container-job:
    # Containers must run in Linux based operating systems
    runs-on: ubuntu-latest
    # Docker Hub image that `container-job` executes in
    container: python:3.7

    # Service containers to run with `container-job`
    services:
      mysql:
        image: mysql:8.0
        env:
          MYSQL_ALLOW_EMPTY_PASSWORD: yes
          MYSQL_DATABASE: cloudia_test
          MYSQL_USER: root
        ports:
          - 3306:3306
        options: --health-cmd="mysqladmin ping" --health-interval=10s --health-timeout=5s --health-retries=3

      # Label used to access the service container
      redis:
        # Docker Hub image
        image: redis
        ports:
          - 6379/tcp
        # Set health checks to wait until redis has started
        options: >-
          --health-cmd "redis-cli ping"
          --health-interval 10s
          --health-timeout 5s
          --health-retries 5
    steps:
      # Downloads a copy of the code in your repository before running CI tests
      - uses: actions/checkout@v2
      - name: Setup Python
        uses: actions/setup-python@v2
        with:
          python-version: '3.7'

      - name: pip cache
        uses: actions/cache@v2
        id: cache
        with:
          # This path is specific to Ubuntu
          path: ~/.cache/pip
          # Look to see if there is a cache hit for the corresponding requirements file
          key: ${{ runner.os }}-pip-${{ hashFiles('**/requirements.txt') }}
          restore-keys: |
            ${{ runner.os }}-pip-
            ${{ runner.os }}-
      # Performs a clean installation of all dependencies in the `package.json` file
      # For more information, see https://docs.npmjs.com/cli/ci.html
      - name: Install dependencies
        if: steps.cache.outputs.cache-hit != 'true'
        run: |
          pip install --upgrade pip
          pip install -r requirements-dev.txt
          
          
      - name: Run tests
        env:
          PYTHON_ENV: test
          REDIS_URL: redis://redis
          DATABASE_HOST: mysql
          DATABASE_PORT: ${{ job.services.mysql.ports[3306] }}
        run: |
          pytest

但是我收到了这个错误:

value = OperationalError(2002, "Can't connect to MySQL server on 'mysql' (115)")

我尝试将 DATABASE_HOST 用作 127.0.0.1 和 0.0.0.0,但我也收到消息“无法连接到 '127.0.0.1' 上的 MySQL 服务器”或“无法连接到 '0.0 上的 MySQL 服务器” .0.0'"

【问题讨论】:

    标签: python mysql github-actions


    【解决方案1】:

    这里有几个运行 MySQL 的示例。希望对您有所帮助。

    工作:
    测试主机:
    运行:ubuntu 最新服务:
    mysql:
    图片:mysql:5.7env:
    MYSQL\_ROOT\_PASSWORD:密码端口:
            -3306options:--health-cmd="mysqladmin ping" --health-interval=10s --health-timeout=5s --health-retries=3steps:
        -name:验证来自主机运行的 MySQL 连接:| sudo apt-get install -y mysql-client mysql --host 127.0.0.1 --port ${{ job.services.mysql.ports['3306'] }} -uroot -ppassword -e "SHOW DATABASES"test-container :
    运行:ubuntu-latestcontainer:ubuntuservices:
    mysql:
    图片:mysql:5.7env:
    MYSQL\_ROOT\_PASSWORD:passwordoptions:--health-cmd="mysqladmin ping" --health-interval=10s --health-timeout=5s --health-retries=5steps:
        -name:从容器运行验证 MySQL 连接:| apt-get update apt-get install -y mysql-client mysql --host mysql -uroot -ppassword -e "SHOW DATABASES"

    【讨论】:

      猜你喜欢
      • 2013-12-03
      • 2019-07-25
      • 1970-01-01
      • 1970-01-01
      • 2019-06-05
      • 1970-01-01
      • 1970-01-01
      • 2018-08-19
      • 1970-01-01
      相关资源
      最近更新 更多