LoginSignup
6
5

More than 3 years have passed since last update.

【django自動テスト】接続エラー Got an error creating the test database

Last updated at Posted at 2021-03-23

djangoの自動テストでDB接続のエラーが出た。

> python manage.py test
Creating test database for alias 'default'...
Got an error creating the test database: (1044, "Access denied for user 'myuser'@'%' to database 'test_mydb'")

環境

  • Python 3.8.2
  • Django 3.0.6
  • MySQL 8.0.20

解決方法

settings.py
DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.mysql',
        'NAME': 'mydb',
        'USER': 'myuser',
        'PASSWORD': 'mypassword',
        'HOST': 'localhost',
        'PORT': '3306',
        # ↓ここを追記した
        'TEST': {
            'MIRROR': "default",
        },
    }
}
> python manage.py test
System check identified no issues (0 silenced).
..
----------------------------------------------------------------------
Ran 2 tests in 0.040s

OK

試したこと

自動的にテスト用のdb(test_ + db名)を作って動くみたいで
テスト用設定を追加してみた。

settings.py
DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.mysql',
        'NAME': 'mydb',
        'USER': 'myuser',
        'PASSWORD': 'mypassword',
        'HOST': 'localhost',
        'PORT': '3306',
        # ↓ここを追記した
        'TEST': {
            'NAME': "test_mydb",
        },
    }
}
# mysql -u root -p
Enter password:
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 114
Server version: 8.0.20 MySQL Community Server - GPL

Copyright (c) 2000, 2020, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql> GRANT ALL PRIVILEGES ON test_mydb.* TO 'myuser'@'%';
Query OK, 0 rows affected (0.01 sec)

動いたがデータがなくてテストで失敗した。

>python manage.py test
Creating test database for alias 'default'...
System check identified no issues (0 silenced).
.F
======================================================================
FAIL: test_something (tests.test_sample.SampleTestCase)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "D:\work\app\sample\tests\test_test_sample.py", line 10, in test_something
    self.assertEqual(28, len(list))
AssertionError: 28 != 0

----------------------------------------------------------------------
Ran 2 tests in 0.015s

'MIRROR': "default",を追記してみたらデータもコピーできた。

settings.py
DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.mysql',
        'NAME': 'mydb',
        'USER': 'myuser',
        'PASSWORD': 'mypassword',
        'HOST': 'localhost',
        'PORT': '3306',
        'TEST': {
            'NAME': "test_mydb",
            # ↓ここを追記した
            'MIRROR': "default",
        },
    }
}

最終的に'NAME': "test_mydb",を削っても動いた。
GRANT ALL PRIVILEGES ON test_mydb.* TO 'myuser'@'%';もしなくても動く。

参考にしたページ

6
5
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
6
5