gpt4 book ai didi

mysql - 将 Django 项目连接到远程 Mysql 数据库

转载 作者:行者123 更新时间:2023-11-29 02:46:10 26 4
gpt4 key购买 nike

我正在我的本地主机上开发一个 Django 项目,我想使用一个远程的 MySQL 数据库

我的本​​地主机 IP 是:172.30.10.XX

我的 MySQL 远程服务器是:172.30.10.XX

在我的 Django settings.py 文件中,我写道:

DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql',
'NAME': 'DatasystemsEC',
'USER': 'root',
'PASSWORD': '*****',
'HOST': '172.30.10.XX',
'PORT': '3306',
'OPTIONS': {
'init_command': 'SET innodb_strict_mode=1',
},
}
}

我的数据库名称是:DatasystemsEC

但是,当我运行:python manage.py migrate 时,我得到了这个错误:

Traceback (most recent call last):
File "manage.py", line 22, in <module>
execute_from_command_line(sys.argv)
File "/usr/local/lib/python2.7/site-packages/django/core/management/__init__.py", line 367, in execute_from_command_line
utility.execute()
File "/usr/local/lib/python2.7/site-packages/django/core/management/__init__.py", line 359, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
File "/usr/local/lib/python2.7/site-packages/django/core/management/base.py", line 294, in run_from_argv
self.execute(*args, **cmd_options)
File "/usr/local/lib/python2.7/site-packages/django/core/management/base.py", line 342, in execute
self.check()
File "/usr/local/lib/python2.7/site-packages/django/core/management/base.py", line 374, in check
include_deployment_checks=include_deployment_checks,
File "/usr/local/lib/python2.7/site-packages/django/core/management/commands/migrate.py", line 61, in _run_checks
issues = run_checks(tags=[Tags.database])
File "/usr/local/lib/python2.7/site-packages/django/core/checks/registry.py", line 81, in run_checks
new_errors = check(app_configs=app_configs)
File "/usr/local/lib/python2.7/site-packages/django/core/checks/database.py", line 10, in check_database_backends
issues.extend(conn.validation.check(**kwargs))
File "/usr/local/lib/python2.7/site-packages/django/db/backends/mysql/validation.py", line 9, in check
issues.extend(self._check_sql_mode(**kwargs))
File "/usr/local/lib/python2.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/python2.7/site-packages/django/db/backends/base/base.py", line 231, in cursor
cursor = self.make_debug_cursor(self._cursor())
File "/usr/local/lib/python2.7/site-packages/django/db/backends/base/base.py", line 204, in _cursor
self.ensure_connection()
File "/usr/local/lib/python2.7/site-packages/django/db/backends/base/base.py", line 199, in ensure_connection
self.connect()
File "/usr/local/lib/python2.7/site-packages/django/db/utils.py", line 94, in __exit__
six.reraise(dj_exc_type, dj_exc_value, traceback)
File "/usr/local/lib/python2.7/site-packages/django/db/backends/base/base.py", line 199, in ensure_connection
self.connect()
File "/usr/local/lib/python2.7/site-packages/django/db/backends/base/base.py", line 171, in connect
self.connection = self.get_new_connection(conn_params)
File "/usr/local/lib/python2.7/site-packages/django/db/backends/mysql/base.py", line 263, in get_new_connection
conn = Database.connect(**conn_params)
File "/Library/Python/2.7/site-packages/MySQLdb/__init__.py", line 81, in Connect
return Connection(*args, **kwargs)
File "/Library/Python/2.7/site-packages/MySQLdb/connections.py", line 193, in __init__
super(Connection, self).__init__(*args, **kwargs2)
django.db.utils.OperationalError: (2003, "Can't connect to MySQL server on '172.30.10.XX' (61)")

在带有 phpmyadmin 的 MySQL 上,我有:

enter image description here

我假设我需要配置一个新用户才能注册我的 MacOSX 本地主机?

所以我创建了一个名为 osx 的新用户,并拥有所有授予的权限。但它仍然不起作用。

如果你能帮助我,谢谢你

最佳答案

我找到了一个解决方案:

在我的 my.cnf 文件中,我添加了以下行:

[mysqld]
datadir=/var/lib/mysql
socket=/var/lib/mysql/mysql.sock
user=mysql
bind-address=172.30.10.XX

然后我运行 sudo service mysql restart

它似乎可以工作(没有错误)

关于mysql - 将 Django 项目连接到远程 Mysql 数据库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41978150/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com