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

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

# 解決方法

```python: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名)を作って動くみたいで
テスト用設定を追加してみた。

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

```console
# 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",`を追記してみたらデータもコピーできた。

```python: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'@'%';`もしなくても動く。

# 参考にしたページ

* https://docs.djangoproject.com/ja/3.1/topics/testing/overview/
* https://qiita.com/HNJ/items/16ecf095009f623085fd
* https://stackoverflow.com/questions/14186055/django-test-app-error-got-an-error-creating-the-test-database-permission-deni