I'm using App Engine and Cloud SQL from Google Cloud. They are both in the same project, which should be enough to be able to make connections to the SQL.
If your App Engine application is in the same project as your Cloud SQL instance, you can skip this section and go to Setting up your local environment. Otherwise, proceed to the next step.
If I spin up the Node.js on my Mac, locally - It can easily connect to the Cloud SQL, because my IP address is whitelisted.
As soon as I deploy it to App Engine, it's not working, because it can't connect to the DB. I'm using the Public IP Address for the Cloud SQL.
I have been reading a lot of tutorials, and I have also read the documentation provided by Google Cloud. And I can see there is something about the socketPath.
But I don't want to use Knex. I want to use the standard mysql.
I have been trying with the following:
var db_config = {
host: 'PUBLIC IP',
user: 'root',
password: 'PASSWORD!',
database: 'DB_NAME',
socketPath: '/cloudsql/CONNECTION_NAME'
};
Error:
error when connecting to db: { Error: connect ENOENT /cloudsql/CONNECTION_NAME-209415:europe-west1:DB_NAME
at PipeConnectWrap.afterConnect [as oncomplete] (net.js:1174:14)
--------------------
at Protocol._enqueue (/Users/root/API/Node-API/node_modules/mysql/lib/protocol/Protocol.js:145:48)
at Protocol.handshake (/Users/root/API/Node-API/node_modules/mysql/lib/protocol/Protocol.js:52:23)
at Connection.connect (/Users/root/API/Node-API/node_modules/mysql/lib/Connection.js:130:18)
at handleDisconnect (/Users/root/API/Node-API/src/App.ts:44:14)
at Object.<anonymous> (/Users/root/API/Node-API/src/App.ts:60:1)
at Module._compile (internal/modules/cjs/loader.js:654:30)
at Module.m._compile (/Users/root/API/Node-API/node_modules/ts-node/src/index.ts:435:23)
at Module._extensions..js (internal/modules/cjs/loader.js:665:10)
at Object.require.extensions.(anonymous function) [as .ts] (/Users/root/API/Node-API/node_modules/ts-node/src/index.ts:438:12)
at Module.load (internal/modules/cjs/loader.js:566:32)
errno: 'ENOENT',
code: 'ENOENT',
syscall: 'connect',
address: '/cloudsql/CONNECTION_NAME-209415:europe-west1:DB_NAME',
fatal: true }
Any suggestions, how to fix this? If I remove the socketPath from the db_config, then it works locally - But not when it's deployed.
Okay, it seems like the error is caused, when localhost AND socketPath are applied. Everything works fine without Knex, with the following db_config for mysql.
db_config = {
user: 'root',
password: 'PASSWORD!',
database: 'DB_NAME',
socketPath: '/cloudsql/CONNECTION_NAME'
};
Finally, remember to add cloud_sql_instance to app.yaml
beta_settings:
cloud_sql_instances: CONNECTION_NAME
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With