seminar-2020
seminar-2020 copied to clipboard
Github Action Django test workflow 설정 중 MySQL server에 연결할 수 없습니다.
Resolved at https://github.com/wafflestudio/rookies/issues/243#issuecomment-713555088 !
과제 3의 추가 과제, CI 진행을 해보고 있습니다. 도중 에러가 났고, 해결을 위해 이것저것 많이 구글링하고 참고했지만 잘 안되네요 ㅠ 혹시 같은 에러 겪으신 분 계실까요?
django.yml
의 MySQL configure 부분
services:
mysql:
image: mysql:latest
env:
MYSQL_ALLOW_EMPTY_PASSWORD: yes
MYSQL_DATABASE: waffle_backend_assignment_2
MYSQL_USER: waffle-backend
MYSQL_PASSWORD: seminar
MYSQL_ROOT_PASSWORD: root
ports:
- '3306:3306'
options: --health-cmd="mysqladmin ping" --health-interval=10s --health-timeout=5s --health-retries=3
에러 메시지
Creating test database for alias 'default'...
Traceback (most recent call last):
File "/opt/hostedtoolcache/Python/3.8.3/x64/lib/python3.8/site-packages/django/db/backends/base/base.py", line 219, in ensure_connection
self.connect()
File "/opt/hostedtoolcache/Python/3.8.3/x64/lib/python3.8/site-packages/django/utils/asyncio.py", line 26, in inner
return func(*args, **kwargs)
File "/opt/hostedtoolcache/Python/3.8.3/x64/lib/python3.8/site-packages/django/db/backends/base/base.py", line 200, in connect
self.connection = self.get_new_connection(conn_params)
File "/opt/hostedtoolcache/Python/3.8.3/x64/lib/python3.8/site-packages/django/utils/asyncio.py", line 26, in inner
return func(*args, **kwargs)
File "/opt/hostedtoolcache/Python/3.8.3/x64/lib/python3.8/site-packages/django/db/backends/mysql/base.py", line 234, in get_new_connection
return Database.connect(**conn_params)
File "/opt/hostedtoolcache/Python/3.8.3/x64/lib/python3.8/site-packages/MySQLdb/__init__.py", line 130, in Connect
return Connection(*args, **kwargs)
File "/opt/hostedtoolcache/Python/3.8.3/x64/lib/python3.8/site-packages/MySQLdb/connections.py", line 185, in __init__
super().__init__(*args, **kwargs2)
MySQLdb._exceptions.OperationalError: (2002, "Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock' (2)")
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
File "manage.py", line 22, in <module>
main()
File "manage.py", line 18, in main
execute_from_command_line(sys.argv)
File "/opt/hostedtoolcache/Python/3.8.3/x64/lib/python3.8/site-packages/django/core/management/__init__.py", line 401, in execute_from_command_line
utility.execute()
File "/opt/hostedtoolcache/Python/3.8.3/x64/lib/python3.8/site-packages/django/core/management/__init__.py", line 395, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
File "/opt/hostedtoolcache/Python/3.8.3/x64/lib/python3.8/site-packages/django/core/management/commands/test.py", line 23, in run_from_argv
super().run_from_argv(argv)
File "/opt/hostedtoolcache/Python/3.8.3/x64/lib/python3.8/site-packages/django/core/management/base.py", line 330, in run_from_argv
self.execute(*args, **cmd_options)
File "/opt/hostedtoolcache/Python/3.8.3/x64/lib/python3.8/site-packages/django/core/management/base.py", line 371, in execute
output = self.handle(*args, **options)
File "/opt/hostedtoolcache/Python/3.8.3/x64/lib/python3.8/site-packages/django/core/management/commands/test.py", line 53, in handle
failures = test_runner.run_tests(test_labels)
File "/opt/hostedtoolcache/Python/3.8.3/x64/lib/python3.8/site-packages/django/test/runner.py", line 695, in run_tests
old_config = self.setup_databases(aliases=databases)
File "/opt/hostedtoolcache/Python/3.8.3/x64/lib/python3.8/site-packages/django/test/runner.py", line 614, in setup_databases
return _setup_databases(
File "/opt/hostedtoolcache/Python/3.8.3/x64/lib/python3.8/site-packages/django/test/utils.py", line 170, in setup_databases
connection.creation.create_test_db(
File "/opt/hostedtoolcache/Python/3.8.3/x64/lib/python3.8/site-packages/django/db/backends/base/creation.py", line 55, in create_test_db
self._create_test_db(verbosity, autoclobber, keepdb)
File "/opt/hostedtoolcache/Python/3.8.3/x64/lib/python3.8/site-packages/django/db/backends/base/creation.py", line 176, in _create_test_db
with self._nodb_cursor() as cursor:
File "/opt/hostedtoolcache/Python/3.8.3/x64/lib/python3.8/contextlib.py", line 113, in __enter__
return next(self.gen)
File "/opt/hostedtoolcache/Python/3.8.3/x64/lib/python3.8/site-packages/django/db/backends/base/base.py", line 620, in _nodb_cursor
with conn.cursor() as cursor:
File "/opt/hostedtoolcache/Python/3.8.3/x64/lib/python3.8/site-packages/django/utils/asyncio.py", line 26, in inner
return func(*args, **kwargs)
File "/opt/hostedtoolcache/Python/3.8.3/x64/lib/python3.8/site-packages/django/db/backends/base/base.py", line 259, in cursor
return self._cursor()
File "/opt/hostedtoolcache/Python/3.8.3/x64/lib/python3.8/site-packages/django/db/backends/base/base.py", line 235, in _cursor
self.ensure_connection()
File "/opt/hostedtoolcache/Python/3.8.3/x64/lib/python3.8/site-packages/django/utils/asyncio.py", line 26, in inner
return func(*args, **kwargs)
File "/opt/hostedtoolcache/Python/3.8.3/x64/lib/python3.8/site-packages/django/db/backends/base/base.py", line 219, in ensure_connection
self.connect()
File "/opt/hostedtoolcache/Python/3.8.3/x64/lib/python3.8/site-packages/django/db/utils.py", line 90, in __exit__
raise dj_exc_value.with_traceback(traceback) from exc_value
File "/opt/hostedtoolcache/Python/3.8.3/x64/lib/python3.8/site-packages/django/db/backends/base/base.py", line 219, in ensure_connection
self.connect()
File "/opt/hostedtoolcache/Python/3.8.3/x64/lib/python3.8/site-packages/django/utils/asyncio.py", line 26, in inner
return func(*args, **kwargs)
File "/opt/hostedtoolcache/Python/3.8.3/x64/lib/python3.8/site-packages/django/db/backends/base/base.py", line 200, in connect
self.connection = self.get_new_connection(conn_params)
File "/opt/hostedtoolcache/Python/3.8.3/x64/lib/python3.8/site-packages/django/utils/asyncio.py", line 26, in inner
return func(*args, **kwargs)
File "/opt/hostedtoolcache/Python/3.8.3/x64/lib/python3.8/site-packages/django/db/backends/mysql/base.py", line 234, in get_new_connection
return Database.connect(**conn_params)
File "/opt/hostedtoolcache/Python/3.8.3/x64/lib/python3.8/site-packages/MySQLdb/__init__.py", line 130, in Connect
return Connection(*args, **kwargs)
File "/opt/hostedtoolcache/Python/3.8.3/x64/lib/python3.8/site-packages/MySQLdb/connections.py", line 185, in __init__
super().__init__(*args, **kwargs2)
django.db.utils.OperationalError: (2002, "Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock' (2)")
Error: Process completed with exit code 1.
혹시 settings.py에 workflows 관련 설정 하셨나요? https://banagale.com/running-django-tests-in-github-actions.htm 저도 이것저것 시도하는 중인데 여기 보니깐 Debug Toolbar 추가할 때처럼 무언가를 해주더라구요
해당 에러는 mysql.server start
로 mysql 서버가 연결되기 전에 뜨기도 하고 환경변수 설정 때문에 생기는 에러 같습니다.
Run Tests 단계에서 python manage.py test
이전에 mysql.server start
를 적어보아도 저에게는 똑같이 뜨기 때문에 GitHub Actions이 돌아가는 컴퓨터의 환경변수를 바꿔줘야하지 않을까 하는 추측을 해보는데 막막하네요 ㅋㅋㅋㅋㅋ
@YeonghyeonKO 넵넵 저 페이지의 글처럼 설정하진 않았지만, DATABASE field에 TEST 필드 추가해서 테스트 관련 설정을 해두긴 했습니다.
넵넵 저도 같은 상황이네요 ㅠㅠㅠㅠㅠ
아하 그렇군요 test 필드는 어떤 식으로 추가하셨나요?
https://docs.djangoproject.com/en/3.1/topics/testing/overview/#the-test-database
여기 참조해서 진행했습니다!
@dodo4114 아하 그렇군요 저는 test는 따로 안만들었는데 도현님이랑 같은 에러 단계라서 이게 다른 차원의 문제인 것 같습니당 ㅋㅋㅋ..
어차피 추가 과제니 제 코드를 올리자면 아래와 같습니다 ㅠ 영현님이 알려주신 링크대로 셋팅도 바꿔봤는데 그대로네요 ㅠ
name: Django CI
on:
push:
branches: [ master ]
pull_request:
branches: [ master ]
jobs:
test-host:
runs-on: ubuntu-latest
services:
mysql:
image: mysql:5.7
env:
MYSQL_DATABASE: 'waffle_backend_assignment_2'
MYSQL_USER: 'waffle-backend'
MYSQL_PASSWORD: 'seminar'
MYSQL_ROOT_PASSWORD: password
ports:
- 3306:3306
options: --health-cmd="mysqladmin ping" --health-interval=10s --health-timeout=5s --health-retries=3
steps:
- uses: actions/checkout@v2
- name: Verify MySQL connection
run: |
mysql --version
sudo apt-get install -y mysql-client
mysql --host 127.0.0.1 --port 3306 -uroot -ppassword -e "SHOW DATABASES"
- name: Set up Python 3.8.3
uses: actions/setup-python@v2
with:
python-version: 3.8.3
- name: Install Dependencies
run: |
python -m pip install --upgrade pip
pip install -r requirements.txt
- name: Run Tests
run: |
cd waffle_backend
coverage run manage.py test
coverage report
env:
SYSTEM_ENV: GITHUB_WORKFLOW
cd waffle_backend도 잘 하셨네요 ㅋㅋㅋㅋ 저도 저거 추가했습니다. 나머진 다 비슷한 것 같은데... 토요일 모임에서 같이 고민해봐요
-
cd waffle_backend
대신working-directory: ./waffle_backend
을 설정하는 방법도 있습니다. - 해당 에러는 waffle_backend/waffle_backend/settings.py의 설정이 문제인 것 같습니다. DATABASES 설정을 localhost 대신 127.0.0.1로 바꿔 보셨나요?
cd waffle_backend
대신working-directory: ./waffle_backend
을 설정하는 방법도 있습니다.- 해당 에러는 waffle_backend/waffle_backend/settings.py의 설정이 문제인 것 같습니다. DATABASES 설정을 localhost 대신 127.0.0.1로 바꿔 보셨나요?
와우....이거 왜 되죠..ㅋㄱ.ㅋㄱㄱㅋㅋㄱㅋㄱㄱㅋ 이젠 utf관련으로 추정되는 에러가 발생하지만 아무튼 테스트가 돌아가긴 합니다 ㅠㅠㅠ 감사합니다 ㅠㅠ
@dodo4114 팁인데 mysql verification 단계에서 다음과 같은 코드가 reading database를 엄청 빨리 하게 해준다는 걸 알았습니다.
strategy:
max-parallel: 4
matrix:
python-version: [ 3.8 ]
옹 감사합니다 ㅎㅎㅎ 버전 여러개 동시에 테스트하기 위한 코드인 줄 알았는데...그렇게 활용도 되는군요 ㅎㅎ
@dodo4114 저 위에 socket 관련 에러가 있는데, DATABASES 설정에서 localhost, 127.0.0.1 간에는 사실 차이가 있습니다. 관련해서는 #125 를 참고해보시면 좋을 듯합니다.
오 정말 감사합니다 ㅎㅎㅎ 읽어보겠습니다!