Elias
Elias

Reputation: 133

Connecting to MySQL database on Google Cloud with Node.js (Error: connect ENOENT)

I'm trying to connect to my MySQL database on Google Cloud through my app hosted in the same project as the database. The app is written in node.js.

This is my code:

    mysql = require('mysql');
    var connection = mysql.createConnection({
    user: "root",
    password: "{root password}",
    database: "{database}",
    socketPath: "{project-id}:europe-west1:{database-name}",
    multipleStatements: true
});

The app is successfully deployed, but whenever I visit a page that uses MySQL, I get this error:

    { Error: connect ENOENT {project-id}:europe-west1:{database-name}      at Object._errnoException (util.js:1022:11)      at _exceptionWithHostPort (util.js:1044:20)      at PipeConnectWrap.afterConnect [as oncomplete] (net.js:1198:14)
2018-05-20 11:24:12 default[20180520t111014]      --------------------
2018-05-20 11:24:12 default[20180520t111014]      at Protocol._enqueue (/app/node_modules/mysql/lib/protocol/Protocol.js:145:48)
2018-05-20 11:24:12 default[20180520t111014]      at Protocol.handshake (/app/node_modules/mysql/lib/protocol/Protocol.js:52:23)
2018-05-20 11:24:12 default[20180520t111014]      at Connection.connect (/app/node_modules/mysql/lib/Connection.js:130:18)
2018-05-20 11:24:12 default[20180520t111014]      at Object.<anonymous> (/app/app.js:54:12)
2018-05-20 11:24:12 default[20180520t111014]      at Module._compile (module.js:652:30)
2018-05-20 11:24:12 default[20180520t111014]      at Object.Module._extensions..js (module.js:663:10)
2018-05-20 11:24:12 default[20180520t111014]      at Module.load (module.js:565:32)
2018-05-20 11:24:12 default[20180520t111014]      at tryModuleLoad (module.js:505:12)
2018-05-20 11:24:12 default[20180520t111014]      at Function.Module._load (module.js:497:3)
2018-05-20 11:24:12 default[20180520t111014]      at Function.Module.runMain (module.js:693:10)
2018-05-20 11:24:12 default[20180520t111014]    code: 'ENOENT',
2018-05-20 11:24:12 default[20180520t111014]    syscall: 'connect',
2018-05-20 11:24:12 default[20180520t111014]    address: '{project-id}:europe-west1:{database-name}',
2018-05-20 11:24:12 default[20180520t111014]    fatal: true }

And in the web browser, I get a 502 error.

This is my app.yaml

    runtime: nodejs
env: flex

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: {project-id}:europe-west1:{database-name}

Upvotes: 0

Views: 1654

Answers (2)

user2106568
user2106568

Reputation: 93

Update beta_settings in your app.yaml file.

beta_settings:
  cloud_sql_instances: {project-id}:europe-west1:{database-name}

Upvotes: 0

Elias
Elias

Reputation: 133

socketPath should be:

socketPath: "/cloudsql/{project-id}:europe-west1:{database}"

Upvotes: 1

Related Questions