如何将Datalab与Google

如何将Datalab与Google

本文介绍了如何将Datalab与Google Cloud SQL连接?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

尝试从datalab笔记本与Google Cloud SQL上托管的PostgreSQL数据库进行连接.尝试直接IP和实例连接方式,但都给我们一个例外.

Trying to connect from a datalab notebook with PostgreSQL database hosted on Google Cloud SQL. Try both direct IP and instance connection ways but both give us an exception.

直接连接URI:

"{engine}://{user}:{password}@{host}:{port}/{database}"

使用gcloud sql connect

using gcloud sql connect

"{engine}://{user}:{password}@/{database}?host=/cloudsql/{instance_connection_name}"

都给我们这个例外:

OperationalError: (psycopg2.OperationalError) could not connect to
server: Connection timed out
  Is the server running on host "***.***.***.***" and accepting
  TCP/IP connections on port ****?

知道是否需要像 Collab代理连接中的云sql代理吗?如果需要的话,如何使用datalab库呢?

Any idea if it need a cloud sql proxy as in Collab proxy connection? And if it is needed how to do it with datalab libraries?

推荐答案

我终于明白了.

假设datalab VM已在Gcloud上进行身份验证,我尝试使用cloud_sql_proxy进行连接,而无需出现在 Collab代理连接,并通过创建缺少的目录来修复仍然出现的错误.我知道了这个:

Assuming that datalab VM is already authenticated on Gcloud i try to use cloud_sql_proxy to connect without auth python commands that appear on Collab proxy connection and fix the error that still appears by crating missing directory. Si i got this:

!wget https://dl.google.com/cloudsql/cloud_sql_proxy.linux.amd64 -O cloud_sql_proxy
!mkdir -p /cloudsql
!chmod +x cloud_sql_proxy

.

!./cloud_sql_proxy --instances=project-id:europe-west1:posty --dir /cloudsql

与Collab解决方案一样,我们需要让笔记本在备用窗口中运行以保持代理.在同一台计算机上的其他笔记本上,我们最终获得了对数据库的访问权限.

as with Collab solution we need to let the notebook running in alternate window to keep proxy. With other notebooks on the same machine we finally obtain access to the database.

注意:一种更好的解决方案可能是编辑datalab计算机的docker映像,使其包含这种行为,如此处.

Note: Probably a better solution could be to edit the docker image of datalab machines to include this behaviour as noted here.

这篇关于如何将Datalab与Google Cloud SQL连接?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-05 16:34