0

Python+Djangoの勉強をしており、makemigrationsでモデルの変更の保存を行おうとしたのですが、エラーが発生をしました。

初めての Django アプリの作成、パート 2を参考にして、モデルの変更の保存を実行するためにpython manage.py migrateを実行したのですが、エラーが発生して保存できませんでした。

・エラー

ec2-user:~/environment/mysite $ python manage.py migrate
Traceback (most recent call last):
  File "/home/ec2-user/.local/lib/python3.7/site-packages/pymysql/connections.py", line 645, in connect
    (self.host, self.port), self.connect_timeout, **kwargs
  File "/usr/lib64/python3.7/socket.py", line 728, in create_connection
    raise err
  File "/usr/lib64/python3.7/socket.py", line 716, in create_connection
    sock.connect(sa)
ConnectionRefusedError: [Errno 111] Connection refused

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/usr/local/lib/python3.7/site-packages/django/db/backends/base/base.py", line 216, in ensure_connection
    self.connect()
  File "/usr/local/lib/python3.7/site-packages/django/db/backends/base/base.py", line 194, in connect
    self.connection = self.get_new_connection(conn_params)
  File "/usr/local/lib/python3.7/site-packages/django/db/backends/mysql/base.py", line 236, in get_new_connection
    return Database.connect(**conn_params)
  File "/home/ec2-user/.local/lib/python3.7/site-packages/pymysql/connections.py", line 358, in __init__
    self.connect()
  File "/home/ec2-user/.local/lib/python3.7/site-packages/pymysql/connections.py", line 711, in connect
    raise exc
pymysql.err.OperationalError: (2003, "Can't connect to MySQL server on 'localhost' ([Errno 111] Connection refused)")

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "manage.py", line 19, in <module>
    execute_from_command_line(sys.argv)
  File "/usr/local/lib/python3.7/site-packages/django/core/management/__init__.py", line 371, in execute_from_command_line
    utility.execute()
  File "/usr/local/lib/python3.7/site-packages/django/core/management/__init__.py", line 365, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "/usr/local/lib/python3.7/site-packages/django/core/management/base.py", line 288, in run_from_argv
    self.execute(*args, **cmd_options)
  File "/usr/local/lib/python3.7/site-packages/django/core/management/base.py", line 332, in execute
    self.check()
  File "/usr/local/lib/python3.7/site-packages/django/core/management/base.py", line 364, in check
    include_deployment_checks=include_deployment_checks,
  File "/usr/local/lib/python3.7/site-packages/django/core/management/commands/migrate.py", line 57, in _run_checks
    issues = run_checks(tags=[Tags.database])
  File "/usr/local/lib/python3.7/site-packages/django/core/checks/registry.py", line 73, in run_checks
    new_errors = check(app_configs=app_configs)
  File "/usr/local/lib/python3.7/site-packages/django/core/checks/database.py", line 10, in check_database_backends
    issues.extend(conn.validation.check(**kwargs))
  File "/usr/local/lib/python3.7/site-packages/django/db/backends/mysql/validation.py", line 9, in check
    issues.extend(self._check_sql_mode(**kwargs))
  File "/usr/local/lib/python3.7/site-packages/django/db/backends/mysql/validation.py", line 13, in _check_sql_mode
    with self.connection.cursor() as cursor:
  File "/usr/local/lib/python3.7/site-packages/django/db/backends/base/base.py", line 255, in cursor
    return self._cursor()
  File "/usr/local/lib/python3.7/site-packages/django/db/backends/base/base.py", line 232, in _cursor
    self.ensure_connection()
  File "/usr/local/lib/python3.7/site-packages/django/db/backends/base/base.py", line 216, in ensure_connection
    self.connect()
  File "/usr/local/lib/python3.7/site-packages/django/db/utils.py", line 89, in __exit__
    raise dj_exc_value.with_traceback(traceback) from exc_value
  File "/usr/local/lib/python3.7/site-packages/django/db/backends/base/base.py", line 216, in ensure_connection
    self.connect()
  File "/usr/local/lib/python3.7/site-packages/django/db/backends/base/base.py", line 194, in connect
    self.connection = self.get_new_connection(conn_params)
  File "/usr/local/lib/python3.7/site-packages/django/db/backends/mysql/base.py", line 236, in get_new_connection
    return Database.connect(**conn_params)
  File "/home/ec2-user/.local/lib/python3.7/site-packages/pymysql/connections.py", line 358, in __init__
    self.connect()
  File "/home/ec2-user/.local/lib/python3.7/site-packages/pymysql/connections.py", line 711, in connect
    raise exc
django.db.utils.OperationalError: (2003, "Can't connect to MySQL server on 'localhost' ([Errno 111] Connection refused)")

データベースの設定(mysite/mysite/setting.py)

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.mysql',
        'NAME': 'dbuser',
        'PASSWORD': 'dbuser',
        'HOST': 'localhost',
        'PORT': '3306',
        'OPTIONS': {
            'init_command': "SET sql_mode='STRICT_TRANS_TABLES'",
        },
    }
}

データベース:mysql Ver 15.1 Distrib 10.2.38-MariaDB, for Linux (x86_64) using EditLine wrapper

python: Python 3.7.16

Djamgo: 2.0.2

3
  • ModuleNotFoundError: No module named 'MySQLdb'という記事に同様のエラーと対処法が記載されています。対応欄の作業をしてエラーが解消されるかご確認ください。
    – payaneco
    Commented 2023年6月29日 5:27
  • 対処したのですが、新しいエラーが発生しました。(質問に記載させて頂きました)エラーを検索して調べたのですが、役に立ちそうな物を見つけることが出来ませんでした。
    – SKY
    Commented 2023年6月30日 2:21
  • 参照しているページのDjango版数と、使っていると書かれたDjango版数が違うのと、使っていると書かれたPythonやDjangoの版数が古かったりしていて、以前の質問からの続きのようですが、Python+Djangoの勉強クラウドサービス上でPython+Djangoの環境構築のどちらが第一の目的なのかを考えた方が良いのでは? Python+Djangoの勉強の方が第一目的なのなら、PCのローカル環境で最新の版数で作業した方が問題に突き当たることが少ないと思われます。
    – kunif
    Commented 2023年6月30日 2:38

0

この質問に回答するには、ログインする必要があります。

のタグが付いた他の質問を参照する。