pymysql - 谷歌云 - OperationalError:(2003,“无法连接到MySQL服务器上

问题描述:

我的Python代码非常简单:pymysql - 谷歌云 - OperationalError:(2003,“无法连接到MySQL服务器上

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

而且gaveaccess这里的用户: 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)") 

任何帮助,将不胜感激。

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

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