我尝试按照以下步骤操作:
https://cloud.google.com/sql/docs/mysql/connect-kubernetes-engine。
我在同一容器中运行了应用程序容器和cloudsql代理容器。
创建集群后,代理容器的日志似乎正确:
$kubectl logs users-app-HASH1-HASH2 cloudsql-proxy
2018/08/03 18:58:45 using credential file for authentication; [email protected]
2018/08/03 18:58:45 Listening on 127.0.0.1:3306 for tutorial-bookshelf-xxxxxx:asia-south1:it-sample-01
2018/08/03 18:58:45 Ready for new connections
但是,来自应用程序容器的日志会引发无法连接localhost的错误:
$kubectl logs users-app-HASH1-HASH2 app-container
...
19:27:38 users_app.1 | return Connection(*args, **kwargs)
19:27:38 users_app.1 | File "/usr/local/lib/python3.7/site-packages/pymysql/connections.py", line 327, in __init__
19:27:38 users_app.1 | self.connect()
19:27:38 users_app.1 | File "/usr/local/lib/python3.7/site-packages/pymysql/connections.py", line 629, in connect
19:27:38 users_app.1 | raise exc
19:27:38 users_app.1 | sqlalchemy.exc.OperationalError: (pymysql.err.OperationalError) (2003, "Can't connect to MySQL server on 'localhost' ([Errno 2] No such file or directory)") (Background on this error at: http://sqlalche.me/e/e3q8)
SQLALCHEMY_DATABASE_URI
是'mysql+pymysql://{user}:{password}@/{database}?unix_socket=/cloudsql/{cloudsql_connection_name}'
,并填充有正确的值(我使用kubectl密码设置的凭据)。我敢肯定,我在这里做的事很愚蠢,所以我希望更多的GCP经验的人可以看看并提供解决此问题的指导。
更新:
我刚刚转到GCP kubernetes引擎页面,并在应用程序容器上打开了一个外壳,并尝试连接到云SQL实例。这似乎奏效了。
$gcloud container cluster ......... -it /bin/sh
#python
>>> import pymysql
>>> connection = pymysql.connect(host='127.0.0.1', user='user', password='password', db='db')
>>> with connection.cursor() as cursor:
... cursor.execute("show databases;")
... tables = cursor.fetchall()
...
5
但是以下(当我尝试通过sqlalchemy连接时)失败:
>>> connection = pymysql.connect(host='127.0.0.1', user='u', password='p', db='d', unix_socket='/cloudsql/CONNECTION_NAME')
...
pymysql.err.OperationalError: (2003, "Can't connect to MySQL server on '127.0.0.1' ([Errno 2] No such file or directory)")
>>> from sqlalchemy import create_engine
>>> engine = create_engine('mysql://user:password@localhost/db')
>>> engine.connect()
Traceback (most recent call last):
...
sqlalchemy.exc.OperationalError: (_mysql_exceptions.OperationalError) (2002, 'Can\'t connect to local MySQL server through socket \'/run/mysqld/mysqld.sock\' (2 "No such file or directory")') (Background on this error at: http://sqlalche.me/e/e3q8)
>>> engine = create_engine('mysql+pymysql://user:password@/db?unix_socket=/cloudsql/tutorial-bookshelf-xxxx:asia-south1:test-01')
>>> engine.connect()
Traceback (most recent call last):
...
raise exc
sqlalchemy.exc.OperationalError: (pymysql.err.OperationalError) (2003, "Can't connect to MySQL server on 'localhost' ([Errno 2] No such file or directory)") (Background on this error at: http://sqlalche.me/e/e3q8)
最佳答案
通过代理连接到CloudSQL的操作可以通过unix套接字或TCP连接完成,但是您不应该尝试同时使用两者。
我没有看到有关如何配置代理的任何规范,但是如果您希望使用unix套接字,则代理实例标志应如下所示:-instances=<INSTANCE_CONNECTION_NAME>
。这将导致代理在/ cloudsql目录中创建一个UNIX套接字,以将流量转发到Cloud SQL实例。在这种情况下,您将在URL中设置unix_socket=/cloudsql/<INSTANCE_CONNECTION_NAME>
。
如果尝试通过TCP套接字进行连接,请使用如下实例标记:-instances=<INSTANCE_CONNECTION_NAME>=tcp:3306
。这将告诉代理侦听端口3306并将流量转发到您的Cloud SQL实例。在这种情况下,您将使用host ='127.0.0.1'和port = 3306。
如果您正在寻找有关在GKE上使用CloudSQL的入门知识,建议您检查一下该项目中提到的代码实验室:https://github.com/GoogleCloudPlatform/gmemegen