前提・実現したいこと
[開発環境]
Ubuntu: 18.04 LTS
Python: 3.6.6
Django: 2.1
MySQL: 5.7
[実行環境]
Docker: 18.06.1-ce
Ubuntu上でDjangoプロジェクトの開発をしています。
DockerからUbuntu上のMySQLを利用するにはどうしたらよいでしょうか?
django.db.utils.OperationalError: (2003, "Can't connect to MySQL server on '127.0.0.1' (111)")
というエラーがでてしまっています。
まず、Dockerからubuntu上のMySQLに接続する方法が検索しても出てこないのですが、
このようなことはできないのでしょうか?
発生している問題・エラーメッセージ
root@4a7c4f70c69d:~/mysite2/mysite2# python3 manage.py runserver 0.0.0.0:8000 Performing system checks... System check identified no issues (0 silenced). Unhandled exception in thread started by <function check_errors.<locals>.wrapper at 0x7fa7497250d0> Traceback (most recent call last): File "/usr/local/lib/python3.6/site-packages/django/db/backends/base/base.py", line 216, in ensure_connection self.connect() File "/usr/local/lib/python3.6/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.6/site-packages/django/db/backends/mysql/base.py", line 227, in get_new_connection return Database.connect(**conn_params) File "/usr/local/lib/python3.6/site-packages/MySQLdb/__init__.py", line 85, in Connect return Connection(*args, **kwargs) File "/usr/local/lib/python3.6/site-packages/MySQLdb/connections.py", line 204, in __init__ super(Connection, self).__init__(*args, **kwargs2) _mysql_exceptions.OperationalError: (2003, "Can't connect to MySQL server on '127.0.0.1' (111)") The above exception was the direct cause of the following exception: Traceback (most recent call last): File "/usr/local/lib/python3.6/site-packages/django/utils/autoreload.py", line 225, in wrapper fn(*args, **kwargs) File "/usr/local/lib/python3.6/site-packages/django/core/management/commands/runserver.py", line 120, in inner_run self.check_migrations() File "/usr/local/lib/python3.6/site-packages/django/core/management/base.py", line 442, in check_migrations executor = MigrationExecutor(connections[DEFAULT_DB_ALIAS]) File "/usr/local/lib/python3.6/site-packages/django/db/migrations/executor.py", line 18, in __init__ self.loader = MigrationLoader(self.connection) File "/usr/local/lib/python3.6/site-packages/django/db/migrations/loader.py", line 49, in __init__ self.build_graph() File "/usr/local/lib/python3.6/site-packages/django/db/migrations/loader.py", line 209, in build_graph self.applied_migrations = recorder.applied_migrations() File "/usr/local/lib/python3.6/site-packages/django/db/migrations/recorder.py", line 61, in applied_migrations if self.has_table(): File "/usr/local/lib/python3.6/site-packages/django/db/migrations/recorder.py", line 44, in has_table return self.Migration._meta.db_table in self.connection.introspection.table_names(self.connection.cursor()) File "/usr/local/lib/python3.6/site-packages/django/db/backends/base/base.py", line 255, in cursor return self._cursor() File "/usr/local/lib/python3.6/site-packages/django/db/backends/base/base.py", line 232, in _cursor self.ensure_connection() File "/usr/local/lib/python3.6/site-packages/django/db/backends/base/base.py", line 216, in ensure_connection self.connect() File "/usr/local/lib/python3.6/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.6/site-packages/django/db/backends/base/base.py", line 216, in ensure_connection self.connect() File "/usr/local/lib/python3.6/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.6/site-packages/django/db/backends/mysql/base.py", line 227, in get_new_connection return Database.connect(**conn_params) File "/usr/local/lib/python3.6/site-packages/MySQLdb/__init__.py", line 85, in Connect return Connection(*args, **kwargs) File "/usr/local/lib/python3.6/site-packages/MySQLdb/connections.py", line 204, in __init__ super(Connection, self).__init__(*args, **kwargs2) django.db.utils.OperationalError: (2003, "Can't connect to MySQL server on '127.0.0.1' (111)")
settings.py
DATABASES = { 'default': { 'ENGINE': 'django.db.backends.mysql', 'NAME': 'mysite2', 'USER': 'ユーザ名', 'PASSWORD': 'パスワード', 'HOST': '127.0.0.1', 'PORT': '3306', 'ATOMIC_REQUESTS': True, 'OPTIONS': { 'sql_mode': 'TRADITIONAL,NO_AUTO_VALUE_ON_ZERO', }, } }
試したこと①
Ubuntuのターミナルから、Djangoプロジェクトのモデルクラスを通して、データを取得する操作は成功しています。
試したこと②
MySQL は間違いなく起動しています。
VirtualBox:~/PycharmProjects/mysite2/mysite2$ sudo netstat -plutn | grep -i sql tcp 0 0 127.0.0.1:3306 0.0.0.0:* LISTEN 1438/mysqld
どうぞよろしくお願い致します。
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2018/10/03 09:02
2018/10/03 09:05
2018/10/04 00:30
2018/10/04 04:13