django 如何使用gitlab CI/CD设置到MySql数据库的连接

l5tcr1uw  于 2023-02-14  发布在  Go
关注(0)|答案(3)|浏览(198)

我尝试使用CI/CD gitlab来设置django项目的自动测试。问题是,我无法以任何方式连接到Mysql数据库。

gitlab-ci.yml

services:
  - mysql:5.7

variables:
      MYSQL_DATABASE: "db_name"
      MYSQL_ROOT_PASSWORD: "dbpass"
      MYSQL_USER: "username"
      MYSQL_PASSWORD: "dbpass"
      

stages:
  - test

test:
  stage: test
  before_script:
  - apt update -qy && apt-get install -qqy --no-install-recommends default-mysql-client
  - mysql --user=$MYSQL_USER --password=$MYSQL_PASSWORD --database=$MYSQL_DATABASE --host=$MYSQL_HOST --execute="SHOW DATABASES; ALTER USER '$MYSQL_USER'@'%' IDENTIFIED WITH mysql_native_password BY '$MYSQL_PASSWORD'"
  script:
  - apt update -qy
  - apt install python3 python3-pip virtualenvwrapper -qy
  - virtualenv --python=python3 venv/
  - source venv/bin/activate
  - pwd
  - pip install -r requirement.txt
  - python manage.py test apps

使用此文件配置时,出现错误

ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock' (2)

我都做了些什么
添加到mysql脚本tcp连接代替套接字

mysql --protocol=TCP --user=$MYSQL_USER --password=$MYSQL_PASSWORD --database=$MYSQL_DATABASE --host=$MYSQL_HOST --execute="SHOW DATABASES; ALTER USER '$MYSQL_USER'@'%' IDENTIFIED WITH mysql_native_password BY '$MYSQL_PASSWORD'"

在这种情况下我得到了

ERROR 2002 (HY000): Can't connect to MySQL server on 'localhost' (99)

如何正确设置?

but5z9lq

but5z9lq1#

您的问题可能有多种原因:

  • MySQL版本不正确。
  • 解决方案:使用mysql:5.7代替mysql:latest
  • 缺少MySQL主机。
  • 解决方案:在variables中添加MYSQL_HOST,并使用MySQL服务器的主机名(在services键中使用mysql:5.7时,应为mysql
  • Django使用不同的DB凭证。
  • 解决方案:检查你的.gitlab-ci.ymlvariables部分中的凭证,并与Django的settings.py进行比较,它们应该是相同的。
  • 未安装MySQL客户端。
  • 解决方法:在脚本部分安装mysql-client,检查是否可以连接。

下面是一个示例script,它安装MySQL客户端并连接到基于debian的映像(或python:latest映像)中的数据库:

script:
  - apt-get update && apt-get install -y git curl libmcrypt-dev default-mysql-
  - mysql --version
  - sleep 20
  - echo "SHOW tables;"| mysql -u root -p"$MYSQL_ROOT_PASSWORD" -h "${MYSQL_HOST}" "${MYSQL_DATABASE}"

下面是一个完整有效的示例,说明如何使用MySQL 5.7作为服务,并成功连接到MySQL数据库,其中python映像安装了mysql-client

stages:
  - test

variables:
  MYSQL_DATABASE: "db_name"
  MYSQL_ROOT_PASSWORD: "dbpass"
  MYSQL_USER: "username"
  MYSQL_PASSWORD: "dbpass"
  MYSQL_HOST: mysql

test:
  image: python:latest
  stage: test
  services:
    - mysql:5.7
  script:
    - apt-get update && apt-get install -y git curl libmcrypt-dev default-mysql-client
    - mysql --version
    - sleep 20
    - echo "SHOW tables;" | mysql -u root -p"$MYSQL_ROOT_PASSWORD" -h "${MYSQL_HOST}" "${MYSQL_DATABASE}"
    - echo "Database host is '${MYSQL_HOST}'"
r1zhe5dt

r1zhe5dt2#

您需要使用服务名作为数据库主机名。在本例中,MYSQL_HOST应为mysql
您可以在Gitlab页面上查看示例并了解服务如何链接到作业

nimxete2

nimxete23#

我看到有一个可以接受的答案,但是mysql 8.0python3:buster有些东西坏了。Python Debian映像随mariadb一起提供,并且设置标准的mysql-client包并不容易,导致错误:
"django. db. utils.操作错误:2059,"身份验证插件..."
下面是一个可以工作的YAML,使用Ubuntu作为基础映像,mysql 8.0作为服务,您可以在.gitlab-citest_settings中使用root用户,也可以给MYSQL用户创建新数据库和修改现有数据库的权限。
初始变量MYSQL_DB _USER_PASS可以在Gitlab中的Settings -> CI/CD-〉Variables下设置。
.gitlab-ci.yml

variables:
    # "When using a service (e.g. mysql) in the GitLab CI that needs environtment variables
    # to run, only variables defined in .gitlab-ci.yml are passed to the service and
    # variables defined in GitLab GUI are unavailable."
    # https://gitlab.com/gitlab-org/gitlab/-/issues/30178
    # DJANGO_CONFIG: "test"
    MYSQL_DATABASE: $MYSQL_DB
    MYSQL_ROOT_PASSWORD: $MYSQL_PASS
    MYSQL_USER: $MYSQL_USER
    MYSQL_PASSWORD: $MYSQL_PASS
    # -- In your django settings file for the test environment you could put:
    # DATABASES = {
    #     'default': {
    #         'ENGINE': 'django.db.backends.mysql',
    #         'NAME': os.environ.get('MYSQL_DATABASE'),
    #       'USER':  os.environ.get('MYSQL_USER'),
    #       'PASSWORD': os.environ.get('MYSQL_PASSWORD'),
    #       'HOST': 'mysql',
    #       'PORT': '3306',
    #         'CONN_MAX_AGE':60,
    #         },
    # }

    # -- You could us '--settings' to specify a custom settings file on the command line
    # -- below or use an environment variable to trigger an include in your settings:
    #   if os.environ.get('DJANGO_CONFIG')=='test':
    #       from .settings_test import *  # or specific overrides
    #

default:
    image: ubuntu:20.04

    # -- Pick zero or more services to be used on all builds.
    # -- Only needed when using a docker container to run your tests in.
    # -- Check out: http://docs.gitlab.com/ee/ci/docker/using_docker_images.html#what-is-a-service
    services:
    - mysql:8.0

    # This folder is cached between builds
    # http://docs.gitlab.com/ee/ci/yaml/README.html#cache
    # cache:
    #   paths:
    #     - ~/.cache/pip/

    before_script:
    - echo -e "Using Database $MYSQL_DB with $MYSQL_USER"
    - apt --assume-yes update
    - apt --assume-yes install apt-utils
    - apt --assume-yes install net-tools python3.8 python3-pip mysql-client libmysqlclient-dev
    # - apt --assume-yes upgrade
    - pip3 install -r requirements.txt

djangotests:
    script:
    # -- The MYSQL user gets only permissions for MYSQL_DB and therefor cant create a test_db.
    - echo "GRANT ALL on *.* to '${MYSQL_USER}';"| mysql -u root --password="${MYSQL_ROOT_PASSWORD}" -h mysql
    # -- use python3 explicitly. see https://wiki.ubuntu.com/Python/3
    - python3 manage.py test

migrations:
    script:
    - python3 manage.py makemigrations
    - python3 manage.py makemigrations myapp
    - python3 manage.py migrate
    - python3 manage.py check

相关问题