简体   繁体   English

无法使用Python连接到Google Cloud API

[英]Cannot Connect to Google Cloud API with Python

I am trying to connect to an instance of Google Cloud SQL that I created, but every attempt to connect has either resulted with an error message saying I could not connect or did not have access. 我正在尝试连接到我创建的Google Cloud SQL实例,但是每次尝试连接都导致错误消息,提示我无法连接或没有访问权限。 I have used a cloud proxy connection, a private IP connection, and a Cloud shell connection. 我使用了云代理连接,专用IP连接和云外壳连接。

I've used the cloudsql git repository as reference ( https://github.com/GoogleCloudPlatform/python-docs-samples/tree/master/appengine/standard/cloudsql ) 我已将cloudsql git存储库用作参考( https://github.com/GoogleCloudPlatform/python-docs-samples/tree/master/appengine/standard/cloudsql

I have only changed these files in regard to my specific project and instance. 我仅针对我的特定项目和实例更改了这些文件。

to gather the info about the connection: 收集有关连接的信息:

CLOUDSQL_CONNECTION_NAME = os.environ.get('CLOUDSQL_CONNECTION_NAME')
CLOUDSQL_USER = os.environ.get('CLOUDSQL_USER')
CLOUDSQL_PASSWORD = os.environ.get('CLOUDSQL_PASSWORD')

to set the connection (included on git): 设置连接(包含在git中):

if os.getenv('SERVER_SOFTWARE', '').startswith('Google App Engine/'):
    # Connect using the unix socket located at
    # /cloudsql/cloudsql-connection-name.
    cloudsql_unix_socket = os.path.join(
        '/cloudsql', CLOUDSQL_CONNECTION_NAME)

    db = MySQLdb.connect(
        unix_socket=cloudsql_unix_socket,
        user=CLOUDSQL_USER,
        passwd=CLOUDSQL_PASSWORD)
else:
   db = MySQLdb.connect(
            host='127.0.0.1', user=CLOUDSQL_USER, passwd=CLOUDSQL_PASSWORD)

The errors I am receiving: 我收到的错误:

pymysql.err.OperationalError: (1045, "Access denied for user ''@'localhost' (using password: YES)") pymysql.err.OperationalError:(1045,“用户'@'localhost'的访问被拒绝(使用密码:是)”)

and

(pymysql.err.OperationalError) (2003, "Can't connect to MySQL server on 'localhost' ([Errno 2] No such file or directory)") (pymysql.err.OperationalError)(2003,“无法连接到'localhost'上的MySQL服务器([Errno 2]没有这样的文件或目录)”)

I've tried just about everything. 我已经尝试了几乎所有东西。 This is only one example of the many routes I have taken. 这只是我走过的许多路线中的一个例子。 Any advice or guidance would be greatly appreciated. 任何建议或指导将不胜感激。

The sample you are referencing requires you to be running on App Engine Standard, or have used the proxy to create a Unix socket at /cloudsql/<INSTANCE_CONNECTION_NAME> . 您要引用的示例要求您在App Engine Standard上运行,或者已使用代理在/cloudsql/<INSTANCE_CONNECTION_NAME>上创建Unix套接字。

In your example, I would make sure that the check you are using ( SERVER_SOFTWARE env var), is working correctly. 在您的示例中,我将确保您正在使用的检查( SERVER_SOFTWARE env var)正常运行。

When you are running locally, your app will attempt to connect on 127.0.0.1. 在本地运行时,您的应用程序将尝试连接127.0.0.1。 For this to work, the proxy will need to be running and bound to a tcp port. 为此,代理将需要运行并绑定到tcp端口。 Check the logs from the proxy to see if any additional errors are thrown. 检查来自代理的日志,以查看是否引发了任何其他错误。

When you are running in App Engine, make sure you have followed the setup on this page to enable the Cloud SQL Admin API. 在App Engine中运行时,请确保已遵循此页面上的设置以启用Cloud SQL Admin API。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM