1

I hosted a nodeJS application in google console under a paid account. When i tried to connect my nodeJS app to MySQL db in localhost server it is working but once i configured it to work in google cloud console it says can't connect to database. I successfully created a google SQL instance and sure about user name and password as i can connect to database via cloud console. i referred to many tutorials in the internet and couldn't get a way....

 var con = mysql.createConnection({
    host: 'localhost',
    user: 'root',
    password: '1234',
    database: 'test'
  }); 


  con.connect(function(error){
    if(error){
      console.log('error');
    }
    else{
      console.log('connected');
    }
  });
S.Hettz
  • 141
  • 10

2 Answers2

1

Since this question is tagged with google-app-engine, I assume it is the product you are using to deploy your application. In this case:

App Engine uses a Unix socket to connect to a Cloud SQL instance, because of that you need to pass the instance's connection name, like in the example below:

var con = mysql.createConnection({
  host: "localhost",
  socketPath: "/cloudsql/<PROJECT_ID>:<REGION>:<SQL_INSTANCE_NAME>",
  user: "root",
  password: "secret"
});

con.connect(function(err) {
  if (err) throw err;
  console.log("Connected!");
});

You can actually test that by running the cloud_sql_proxy locally and connecting through the unix socket. The Using Cloud SQL for MySQL tutorial explains how to do that.

EDIT:

In case you are using App Engine Flex, it is also important to set the correct beta_settings on your app.yaml like in the example below:

beta_settings:
  # The connection name of your instance, available by using
  # 'gcloud beta sql instances describe [INSTANCE_NAME]' or from
  # the Instance details page in the Google Cloud Platform Console.
  cloud_sql_instances: YOUR_INSTANCE_CONNECTION_NAME
pessolato
  • 1,472
  • 6
  • 14
  • I am getting this error message even tried pessolato's way `at Object.exports._errnoException (util.js:849:11) at exports._exceptionWithHostPort (util.js:872:20) at PipeConnectWrap.afterConnect [as oncomplete] (net.js:1060:14)` – S.Hettz Sep 17 '19 at 12:38
  • Have you used the correct connection name for the socketPath? Also, is you Cloud SQL instance 1st or 2nd generation? The error tells me you have a connection issue, so I suggest you take a look at the common [Connection Issues](https://cloud.google.com/sql/docs/mysql/diagnose-issues#connection), for Cloud SQL. – pessolato Sep 17 '19 at 13:30
  • its second gen, i get the Instance connection name and used it as the connection name?, should socket path start with **/cloudsql/** ?? – S.Hettz Sep 18 '19 at 05:54
  • 1
    Yes the `SocketPath` should be `/cloudsql/` **+** ``. – pessolato Sep 18 '19 at 07:46
  • same error message : `Error: connect ENOENT /cloudsql/ + at Object.exports._errnoException (util.js:849:11) at exports._exceptionWithHostPort (util.js:872:20) at PipeConnectWrap.afterConnect [as oncomplete] (net.js:1060:14) -------------------- at Protocol._enqueue (/app/node_modules/mysql/lib/protocol/Protocol.js:144:48) at Protocol.handshake (/app/node_modules/mysql/lib/protocol/Protocol.js:51:23)` – S.Hettz Sep 18 '19 at 11:56
  • Take a look at this [post](https://stackoverflow.com/q/41971154/11765092), there are a couple of things you might still wanna try: **1.** Check if you have enabled the Cloud SQL API for this project. **2.** Make sure you set the correct **beta_settings** in your **app.yaml**, I will update my answer to provide more information on that. It would also be nice if you could update your question adding this stack trace you're getting, this will make it easier for other users to find a solution. – pessolato Sep 18 '19 at 15:40
  • Yes, Its working i have missed to add beta settings in `app.yaml` . Thank you – S.Hettz Sep 27 '19 at 08:35
0

Your local machine sql server can't be connected to the google console as it is not exposed to the internet.You should host your mssql db in some platform and then you can connect with that in google console

Athish Murugan
  • 331
  • 1
  • 7