我正在尝试使用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)
如何正确设置?
答案 0 :(得分:3)
此问题可能有多种原因:
mysql:5.7
而不是mysql:latest
MYSQL_HOST
vairables
variables
中的凭据是否相同。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}"
作为最终输出,以下yml效果很好:
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 "DB host is "${DB_HOST}""
only:
- merge_requests
environment:
name: test
答案 1 :(得分:2)
您需要使用服务名作为数据库主机名。在这种情况下,MYSQL_HOST
应该是mysql
。
您可以在Gitlab page上查看示例,并了解有关how services are linked to the job的信息
答案 2 :(得分:0)
我看到有一个可以接受的答案,但是在 mysql 8.0 和 python3:buster 中,有些东西坏了。 Python Debian 映像随 mariadb 一起提供,设置标准的 mysql-client 包并不容易,导致:“django.db.utils.OperationalError: 2059, “Authentication plugin”
我在下面得到了一个可用的 YAML,使用 Ubuntu 作为基础映像,使用 mysql 8.0 作为服务。您可以在 .gitlab-ci 和 test_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