$ docker-compose run --rm web python manage.py test Creating test database for alias 'default'... Got an error creating the test database: (1044, "Access denied for user 'django'@'%' to database 'test_example'") ERROR: 2
テストデータベースのデフォルトの名前は、 DATABASES 設定内の各 NAME の値の前に test_ を付けたものになります。設定の DATABASES 内の TEST ディクショナリには、テストデータベースに対するいろいろな設定を書くことができます。例えば、別のデータベース名を指定したければ、 TEST ディクショナリの NAME に、 DATABASES の中から好きなデータベースを選んで指定することができます。
$ docker-compose run --rm web python manage.py dbshell MySQL[example]> show databases; +---------------------+ | Database | +---------------------+ | information_schema | | example | +---------------------+ 2 rows in set (0.001 sec)
MySQL[example]> show grants for 'django'@'%'; +-----------------------------------------------------+ | Grants for django@% | +-----------------------------------------------------+ | GRANT USAGE ON *.* TO 'django'@'%' | | GRANT ALL PRIVILEGES ON `example`.* TO 'django'@'%' | +-----------------------------------------------------+ 2 rows in set (0.001 sec)
GRANT USAGEは見慣れない権限なのですが、権限を与えないということのようです。全ての権限をなくしてからexampleデータベースにのみ全権限を与えているということのようです。
これでは現状がわからないのでrootで接続してみます。
webコンテナにログインしてからmysqlクライアントで接続します。
1 2 3 4 5 6 7 8 9 10 11 12 13 14
$ docker-compose run --rm web /bin/bash Creating example_web_run ... done root@6e114e4f73b3:/code# root@6e114e4f73b3:/code# mysql -u root -p -h db Enter password: Welcome to the MariaDB monitor. Commands end with ; or \g. Your MySQL connection id is 8 Server version: 5.7.36 MySQL Community Server (GPL)
Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
MySQL [(none)]> show databases; +--------------------------+ | Database | +--------------------------+ | example | | information_schema | | mysql | | performance_schema | | sys | | test_example | +--------------------------+ 6 rows in set (0.001 sec)
権限も付与します。
1 2 3 4 5 6 7 8 9 10 11 12 13
MySQL [(none)]> grant all privileges on `test_example`.* to 'django'@'%'; Query OK, 0 rows affected (0.003 sec)
MySQL [(none)]> MySQL [(none)]> show grants for 'django'@'%'; +----------------------------------------------------------------------+ | Grants for django@% | +----------------------------------------------------------------------+ | GRANT USAGE ON *.* TO 'django'@'%' | | GRANT ALL PRIVILEGES ON `example`.* TO 'django'@'%' | | GRANT ALL PRIVILEGES ON `test_example`.* TO 'django'@'%' | +----------------------------------------------------------------------+ 3 rows in set (0.001 sec)
権限の付与ができました。
テスト再実行
ではテストを再実行してみます。
1 2 3 4 5 6 7 8 9 10 11 12 13
$ docker-compose run --rm web python manage.py test Creating example_web_run ... done Creating test database for alias 'default'... Got an error creating the test database: (1007, "Can't create database 'test_example'; database exists") Type 'yes' if you would like to try deleting the test database 'test_example', or 'no' to cancel: yes Destroying old test database for alias 'default'... System check identified no issues (0 silenced). .. ---------------------------------------------------------------------- Ran 2 tests in 0.006s
OK Destroying test database for alias 'default'...
$ docker-compose run --rm web python manage.py test Creating example_web_run ... done Creating test database for alias 'default'... System check identified no issues (0 silenced). .. ---------------------------------------------------------------------- Ran 2 tests in 0.010s
OK Destroying test database for alias 'default'...