简体   繁体   English

node.js 中 Google Cloud mySQL 的连接字符串是什么?

[英]What is the connection string for Google Cloud mySQL, in node.js?

The scenario is as follows: I am writing an node.js app locally and want it (the mySQL modul, rather) to connect to a mySQL db running on Google Cloud Platform.场景如下:我正在本地编写一个 node.js 应用程序,并希望它(而不是 mySQL 模块)连接到在 Google Cloud Platform 上运行的 mySQL 数据库。

I can not get the connection string right and keep getting errors.我无法正确获取连接字符串并不断出错。 GCP holds two names for the database - one i picked, one generated, more complex. GCP 拥有数据库的两个名称 - 一个是我选择的,一个是生成的,更复杂。 Problably I should supply one of these into the connection string as "database", the host is likely the IP address with port (or not(?)), I am not sure what the "user" should be, or if it is necessary.可能我应该将其中一个作为“数据库”提供到连接字符串中,主机可能是带有端口的 IP 地址(或不是(?)),我不确定“用户”应该是什么,或者是否有必要. Not enough info on the GCP help pages or elsewhere. GCP 帮助页面或其他地方的信息不足。

tl;dr: I need the following to connect a node.js app with a mySQL module remotely to a GCP database: tl; dr:我需要以下内容将带有 mySQL 模块的 node.js 应用程序远程连接到 GCP 数据库:

var con = mysql.createConnection({
  host:"11.222.333.444:3306",
  user: "me"
  password:"passwo3d",
  database:"projectname-1528892969949:europe-west5:dbname"
});

Thank you!谢谢!

The raw connection string should look something like: mysql://user:password@localhost:port/db原始连接字符串应该类似于: mysql://user:password@localhost:port/db

So I think your code should look something like:所以我认为你的代码应该是这样的:

var con = mysql.createConnection({
  host:"11.222.333.444", <-- remove the port from your string
  user: "me"
  password:"passwo3d",
  database:"dbname" <--  you have it as the instance name .. its just suppose to be the DB name
});

You can also try connecting to the instance using the cloud proxy setup .您还可以尝试使用云代理设置连接到实例。 You host would just be localhost.您的主机只是本地主机。

Finally you can always go the GCP console -> go to your instance and open up a shell.最后,您始终可以转到 GCP 控制台 -> 转到您的实例并打开一个 shell。 I think they print out the gcloud cmd in there and you can copy values from the console as well.我认为他们在那里打印出gcloud cmd,您也可以从控制台复制值。

Depending on how you are pushing your code, you may have to change a few connections strings in your manifests as well.根据您推送代码的方式,您可能还需要更改清单中的一些连接字符串。 If you can share those I can help further.如果您可以分享这些,我可以进一步提供帮助。

For that to happen, you have to first enable remote access for your mysql on your GCP.为此,您必须首先在 GCP 上为您的 mysql 启用远程访问。 A step by step guide is over here , you can leave the first few steps for installing mysql.一步一步的指南到这里,你可以留下安装mysql的前几个步骤。

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

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