如何从本地

时间:2016-11-18 02:53:08

标签: python mysql django amazon-web-services amazon-rds

已解决:问题是使用USERNAME,而不是USER

我能够从MySQL Workbench和mysql CLI命令连接到RDS实例,因此它不是问题的凭据。我正在尝试运行python manage.py migrate,但我得到以下输出:

Traceback (most recent call last):
  File "manage.py", line 27, in <module>
    execute_from_command_line(sys.argv)
  File "C:\Users\Mike\Envs\playercomp\lib\site-packages\django\core\management\__init__.py", line 367, in execute_from_command_line
    utility.execute()
  File "C:\Users\Mike\Envs\playercomp\lib\site-packages\django\core\management\__init__.py", line 359, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "C:\Users\Mike\Envs\playercomp\lib\site-packages\django\core\management\base.py", line 294, in run_from_argv
    self.execute(*args, **cmd_options)
  File "C:\Users\Mike\Envs\playercomp\lib\site-packages\django\core\management\base.py", line 342, in execute
    self.check()
  File "C:\Users\Mike\Envs\playercomp\lib\site-packages\django\core\management\base.py", line 374, in check
    include_deployment_checks=include_deployment_checks,
  File "C:\Users\Mike\Envs\playercomp\lib\site-packages\django\core\management\commands\migrate.py", line 61, in _run_checks
    issues = run_checks(tags=[Tags.database])
  File "C:\Users\Mike\Envs\playercomp\lib\site-packages\django\core\checks\registry.py", line 81, in run_checks
    new_errors = check(app_configs=app_configs)
  File "C:\Users\Mike\Envs\playercomp\lib\site-packages\django\core\checks\database.py", line 10, in check_database_backends
    issues.extend(conn.validation.check(**kwargs))
  File "C:\Users\Mike\Envs\playercomp\lib\site-packages\django\db\backends\mysql\validation.py", line 9, in check
    issues.extend(self._check_sql_mode(**kwargs))
  File "C:\Users\Mike\Envs\playercomp\lib\site-packages\django\db\backends\mysql\validation.py", line 13, in _check_sql_mode
    with self.connection.cursor() as cursor:
  File "C:\Users\Mike\Envs\playercomp\lib\site-packages\django\db\backends\base\base.py", line 231, in cursor
    cursor = self.make_debug_cursor(self._cursor())
  File "C:\Users\Mike\Envs\playercomp\lib\site-packages\django\db\backends\base\base.py", line 204, in _cursor
    self.ensure_connection()
  File "C:\Users\Mike\Envs\playercomp\lib\site-packages\django\db\backends\base\base.py", line 199, in ensure_connection
    self.connect()
  File "C:\Users\Mike\Envs\playercomp\lib\site-packages\django\db\utils.py", line 94, in __exit__
    six.reraise(dj_exc_type, dj_exc_value, traceback)
  File "C:\Users\Mike\Envs\playercomp\lib\site-packages\django\db\backends\base\base.py", line 199, in ensure_connection
    self.connect()
  File "C:\Users\Mike\Envs\playercomp\lib\site-packages\django\db\backends\base\base.py", line 171, in connect
    self.connection = self.get_new_connection(conn_params)
  File "C:\Users\Mike\Envs\playercomp\lib\site-packages\django\db\backends\mysql\base.py", line 263, in get_new_connection
    conn = Database.connect(**conn_params)
  File "C:\Users\Mike\Envs\playercomp\lib\site-packages\pymysql\__init__.py", line 90, in Connect
    return Connection(*args, **kwargs)
  File "C:\Users\Mike\Envs\playercomp\lib\site-packages\pymysql\connections.py", line 688, in __init__
    self.connect()
  File "C:\Users\Mike\Envs\playercomp\lib\site-packages\pymysql\connections.py", line 937, in connect
    raise exc
django.db.utils.OperationalError: (2003, "Can't connect to MySQL server on 'xxx.xxx.us-west-2.rds.amazonaws.com' ([Errno 10060] A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond)")

我已经设置了AWS安全组和VPC安全组  所有交通|全部|全部| 0.0.0.0/0

我在settings.py中有以下内容

DATABASES = {
  'default': {
    'ENGINE': 'django.db.backends.mysql',
    'NAME': 'playercomp',
    'USERNAME': 'xxx',
    'PASSWORD': 'xxx',
    'HOST': 'xxx.xxx.us-west-2.rds.amazonaws.com',
    'PORT': '3306',
   }
}

这是在我的manage.py

的开头
#!/usr/bin/env python
try:
    import pymysql
    pymysql.install_as_MySQLdb()
except ImportError:
    print "Failed to import pymysql"

我很困惑,因为我可以通过mysql CLI,MySQL Workbench连接,甚至通过下面的短python脚本连接。

connection = pymysql.connect(host='xxx.xx.us-west-2.rds.amazonaws.com',
                         user='xxx',
                         password='xxx',
                         db='playercomp',
                         charset='utf8mb4',
                         cursorclass=pymysql.cursors.DictCursor)

1 个答案:

答案 0 :(得分:1)

解决方案是将USERNAME更改为USER