问题描述
我正在尝试遵循Google提供的示例来连接云使用Python和MySQL实例的Cloud SQL函数脚本.
I am trying to follow along with the example provided by Google to connect a Cloud Functions script to Cloud SQL with Python and a MySQL instance.
我在项目中创建了一个Cloud SQL实例,然后创建了一个Cloud Function,在其中我将链接中的整个脚本粘贴到了内联编辑器中.我将环境变量设置为等于连接到Cloud SQL所需的环境变量,但出现错误.
I have created a Cloud SQL instance in the project and then created a Cloud Function in which I pasted the entire script from the link into the inline editor. I set the environment variables equal to what they need to be in order to connect to Cloud SQL but I am getting an error.
为了使这项工作有效,我是否缺少某些明显的东西?
Is there something obvious I am missing in order to make this work?
跟踪(最近一次通话最近):文件"/env/local/lib/python3.7/site-packages/pymysql/connections.py",第582行,在连接** kwargs中)文件"/opt/python3 .7/lib/python3.7/socket.py,行727,在create_connection中引发err文件"/opt/python3.7/lib/python3.7/socket.py,行716,在create_connection sock.connect( sa)ConnectionRefusedError:[Errno 111]连接被拒绝在处理上述异常期间,发生了另一个异常:追溯(最近一次调用为上):文件"/user_code/main.py",第49行,位于mysql_demo mysql_conn = pymysql.connect( ** mysql_config)在连接返回Connection(* args,** kwargs)中的第94行,文件"/env/local/lib/python3.7/site-packages/pymysql/init.py" init self.connect()文件中的文件"/env/local/lib/python3.7/site-packages/pymysql/connections.py",第327行,文件"/env/local/lib/python3.7/site-packages/pymysql/connections.py"中的第629行,在连接中引发了pymysql.err.OperationalError:(2003年,无法连接至MySQL服务器或n'本地主机'([Errno 111]连接被拒绝))在处理上述异常期间,发生了另一个异常:追溯(最近一次调用最近):文件"/env/local/lib/python3.7/site-packages/pymysql/connections.py,连接sock.connect(self.unix_socket)中的第570行,ConnectionRefusedError:[Errno 111]连接被拒绝在上述异常的处理过程中,发生了另一个异常:追溯(最近一次调用是最近的):文件"/env/local/lib/python3.7/site-packages/google/cloud/functions_v1beta2/worker.py,第297行,位于run_http_function结果= _function_handler.invoke_user_function(flask.request)文件"/env/local/lib/python3 .7/site-packages/google/cloud/functions_v1beta2/worker.py,第199行,在invoke_user_function中返回call_user_function(request_or_event)文件"/env/local/lib/python3.7/site-packages/google/cloud/functions_v1beta2 /worker.py,第192行,在call_user_function中返回self._user_function(request_or_event)文件"/user_code/main.py,第53行,在mysql_demo中,mysql _conn = pymysql.connect(** mysql_config)文件"/env/local/lib/python3.7/site-packages/pymysql/init.py",连接返回第94行,Connection(* args,** kwargs)文件"/env/local/lib/python3.7/site-packages/pymysql/connections.py",第327行,位于 init self.connect()文件中,"/env/local/lib/python3.7/site-packages/pymysql/connections.py,第629行,在connect中,请举起pymysql.err.OperationalError:(2003年,无法连接到'localhost'上的MySQL服务器( [Errno 111]拒绝连接))
Traceback (most recent call last): File "/env/local/lib/python3.7/site-packages/pymysql/connections.py", line 582, in connect **kwargs) File "/opt/python3.7/lib/python3.7/socket.py", line 727, in create_connection raise err File "/opt/python3.7/lib/python3.7/socket.py", line 716, in create_connection sock.connect(sa) ConnectionRefusedError: [Errno 111] Connection refused During handling of the above exception, another exception occurred: Traceback (most recent call last): File "/user_code/main.py", line 49, in mysql_demo mysql_conn = pymysql.connect(**mysql_config) File "/env/local/lib/python3.7/site-packages/pymysql/init.py", line 94, in Connect return Connection(*args, **kwargs) File "/env/local/lib/python3.7/site-packages/pymysql/connections.py", line 327, in init self.connect() File "/env/local/lib/python3.7/site-packages/pymysql/connections.py", line 629, in connect raise exc pymysql.err.OperationalError: (2003, "Can't connect to MySQL server on 'localhost' ([Errno 111] Connection refused)") During handling of the above exception, another exception occurred: Traceback (most recent call last): File "/env/local/lib/python3.7/site-packages/pymysql/connections.py", line 570, in connect sock.connect(self.unix_socket) ConnectionRefusedError: [Errno 111] Connection refused During handling of the above exception, another exception occurred: Traceback (most recent call last): File "/env/local/lib/python3.7/site-packages/google/cloud/functions_v1beta2/worker.py", line 297, in run_http_function result = _function_handler.invoke_user_function(flask.request) File "/env/local/lib/python3.7/site-packages/google/cloud/functions_v1beta2/worker.py", line 199, in invoke_user_function return call_user_function(request_or_event) File "/env/local/lib/python3.7/site-packages/google/cloud/functions_v1beta2/worker.py", line 192, in call_user_function return self._user_function(request_or_event) File "/user_code/main.py", line 53, in mysql_demo mysql_conn = pymysql.connect(**mysql_config) File "/env/local/lib/python3.7/site-packages/pymysql/init.py", line 94, in Connect return Connection(*args, **kwargs) File "/env/local/lib/python3.7/site-packages/pymysql/connections.py", line 327, in init self.connect() File "/env/local/lib/python3.7/site-packages/pymysql/connections.py", line 629, in connect raise exc pymysql.err.OperationalError: (2003, "Can't connect to MySQL server on 'localhost' ([Errno 111] Connection refused)")
推荐答案
非常重要的信息:
开始之前,您需要先执行GCP IAM,将Cloud SQL管理员角色添加到Cloud Function服务帐户中.服务帐户信息可在云功能的常规选项卡中找到.完成此操作后,您应该会很好.如果没有,请尝试向您的云功能服务帐户(如项目编辑器)添加更多角色.
Before you can get started, you need to go the GCP IAM to add the cloud SQL admin role to the cloud function service account. The service account information can be found in the general tab of the cloud function. After you complete this action, you should be good to go. If not, try to add more roles to your cloud function service account like project editor.
供代码参考,(仅要更改的部分)
For code reference,( only part to change)
# TODO(developer): specify SQL connection details
CONNECTION_NAME = getenv(
'INSTANCE_CONNECTION_NAME',
'proj-chatbot-og:us-central1:your connection name')
# Please don't change the name on the left like MYSQL_USER
DB_USER = getenv('MYSQL_USER', 'your user name')
DB_PASSWORD = getenv('MYSQL_PASSWORD', 'your password')
DB_NAME = getenv('MYSQL_DATABASE', 'your sql database')
mysql_config = {
'user': DB_USER,
'password': DB_PASSWORD,
'db': DB_NAME,
'charset': 'utf8mb4',
'cursorclass': pymysql.cursors.DictCursor,
'autocommit': True
}
完整文档可在以下 https://cloud.google.com/functions中找到/docs/sql
如果发现解决方案可以解决您的问题,请对解决方案进行投票.
Please vote the solution if you found the solution solved your issue.
这篇关于云功能“拒绝连接";连接到Cloud SQL时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!