pymysql - google cloud - OperationalError:(2003,"无法连接到MySQL服务器

时间:2017-04-11 02:27:47

标签: python mysql google-app-engine

我的python代码非常简单:

import pymysql as pymysql
db = pymysql.connect(
            host='*.172', user="*one", passwd=CLOUDSQL_PASSWORD,
    database='my_db_name')

我在这里创建了MySQL实例: enter image description here

在此处创建了一个数据库: enter image description here

并在这里向用户提供了访问权限: enter image description here

但我一直收到以下错误:

---------------------------------------------------------------------------
OperationalError                          Traceback (most recent call last)
<ipython-input-53-1f1166ca5290> in <module>()
      1 db = pymysql.connect(
      2             host='*.172', user=CLOUDSQL_USER, passwd=CLOUDSQL_PASSWORD,
----> 3     database='*one')

C:\ProgramData\Anaconda2\lib\site-packages\pymysql\__init__.pyc in Connect(*args, **kwargs)
     88     """
     89     from .connections import Connection
---> 90     return Connection(*args, **kwargs)
     91 
     92 from . import connections as _orig_conn

C:\ProgramData\Anaconda2\lib\site-packages\pymysql\connections.pyc in __init__(self, host, user, password, database, port, unix_socket, charset, sql_mode, read_default_file, conv, use_unicode, client_flag, cursorclass, init_command, connect_timeout, ssl, read_default_group, compress, named_pipe, no_delay, autocommit, db, passwd, local_infile, max_allowed_packet, defer_connect, auth_plugin_map, read_timeout, write_timeout, bind_address)
    704             self._sock = None
    705         else:
--> 706             self.connect()
    707 
    708     def _create_ssl_ctx(self, sslp):

C:\ProgramData\Anaconda2\lib\site-packages\pymysql\connections.pyc in connect(self, sock)
    961                 exc.traceback = traceback.format_exc()
    962                 if DEBUG: print(exc.traceback)
--> 963                 raise exc
    964 
    965             # If e is neither DatabaseError or IOError, It's a bug.

OperationalError: (2003, "Can't connect to MySQL server on '*.172' (timed out)")

任何帮助都将不胜感激。

1 个答案:

答案 0 :(得分:1)

您必须在授权网络下添加您的ipv4地址。

https://cloud.google.com/sql/docs/mysql/connect-admin-ip