I am running an instance on Google Compute Engine. I installed mysql and node.js on this instance.
I have put the following in the index.js file:
const mysql = require("mysql");
var mysql_connection = mysql.createConnection(
{
host: "instance external IP",
user: "my_user_name",
password: "my_pw",
database: "my_db"
}
);
mysql_connection.connect(err =>
{
if(err) throw err;
console.log("Connected to the database.");
});
I get this error:
Error: Cannot find module 'mysql'
How do I connect node.js to mysql on compute engine?
Do I need to get a different host address instead of the external IP, if so, what is it or where do I find it?
Is the require("mysql") the issue, if so, how do I fix this to find the mysql module on the compute engine instance?
Per comments conversation above, install the mysql node module using:
npm install mysql
Related
How to connect mysql in lambda nodejs
var connection = mysql.createConnection({
host: 'localhost',
user: 'root',
password: '1304',
database: 'DemoDB'
})
connection.connect(function (err) {
if (!err) {
console.log("Database connected ... ");
}
else {
console.log("Error connecting database : " + err.message);
}
});
const sql = "CREATE TABLE MESSAGE (message VARCHAR(255))";
connection.query(sql, function (err, result) {
if (err) throw err;
console.log("Table created");
});
If this MySQL server is installed locally in your computer then here's a few troubleshooting that you can try:
Make sure your mysql services is running.
Make sure you can connect to the mysql db using that host+port+user+password combination - test connect to it using command line or sql tools like SQLyog etc.
Make sure that the mysql program and port it's using not blocked by firewall - if you have a strict firewall then you possibly need to do firewall exception for mysqld, mysqld-nt and port 3306.
To check what port is your MySQL running on, you have to look for my.ini or my.cnf file (usually located in MySQL folder or MySQL/Data folder). In that file you need to find port=XXXX and there are two of them. Make sure both values of port= are the same.
The idea here is first to make sure your MySQL server is up and running before connecting through node.js because judging from the error message that you've received, it seems like its not a problem from node.js.
AWS Lambda function runs on the cloud (some remote server) whereas you are providing localhost as the Database host which means that your MySQL server is running on your PC so that is why the connection is not establishing. You have to provide the IP of your PC instead of localhost for things to work properly.
I am testing simple code with mysql and node.js but it is saying:
Error: connect ECONNREFUSED 127.0.0.1:3306
This is my code:
require('dotenv').config();
const mysql = require('mysql2/promise');
module.exports = mysql.createConnection({
user: process.env.DB_USER,
password: process.env.DB_PASS,
database: process.env.DB_NAME
});
I just started doing some code of mysql connections. Can anyone fix this?
The way heroku works, your web dyno (virtual machine) cannot have a MySQL instance built into it. That is, you won't be able to connect to MySQL on localhost when you use a web dyno.
Heroku offer their own postgreSQL service you can use (you'll have to change your web app code from MySQL to postgreSQL, of course). Or there are a few different third-party MySQL addon offerings you can install in your Heroku app. https://elements.heroku.com/addons/
Each of these offerings will require you to connect to it using an environment variable you get from your web dyno. So if you do something like this, not debugged, you'll be able to run both locally and on your dyno.
const dburl = process.env.MYSQL_ADDON
|| 'mysql://' + process.env.DB_USER+ ':' + process.env.DB_PASS
+ '#localhost/' + process.env.DB_NAME
const conn = mysql.createConnection(dburl);
The add-on you use has documentation explaining the name and details of their environment variable.
The available third party add-ons have free tiers allowing you to handle a very small (5MiB) database. For databases bigger than that they charge.
I have a Node.js app (running on AppEngine) connecting to a GCP CloudSQL (MySQL) instance. Now I want to connect to the same database from Node.js (Knex) running on Heroku.
From AppEngine, Node.js connects via user/password and socketPath. I'm also connecting to the same MySQL DB from MySQL Workbench via host IP (over SSL).
I'm trying to use the same host, port, user and pass as Workbench from Heroku and it's not working. To try and make it easy, I've temporarily allowed all networks to connect (0.0.0.0/0) and I've allowed non-SSL connections.
Here's the error: ER_ACCESS_DENIED_ERROR: Access denied for user 'usernamehere'#'xx.xxx.xxx.xx' (using password: YES)"
The environment variables are stored in the Heroku app and they must be working because the username is correct.
It's not very helpful, but here's the code:
import Knex = require('knex');
const envConfig = require('../config/environments').get(process.env.NODE_ENV);
module.exports = knex;
The only way I found to resolve this issue was to connect to CloudSQL over SSL.
const mysql = require("mysql");
const fs = require('fs');
const knex = require('knex')({
client: 'mysql',
version: '5.7',
connection: {
host : 'xx.xx.xx.xx',
ssl: {
ca: fs.readFileSync('ca.pem'),
key: fs.readFileSync('client-key.pem'),
cert: fs.readFileSync('client-cert.pem'),
},
user : 'root',
password : 'xxxxxxxxx',
database : 'mydbname',
},
});
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');
}
});
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
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
I'm trying to connect to a MYSQL database using the npm MYSQL library(https://www.npmjs.com/package/mysql), but I'm getting a error that the server can't be connected to. When I go into the server, I can see the DB, and I validated my credentials and they're valid. My app lives on our seperate server with IP address ending in 123.75, while the DB is on 123.74. I'm using a route I setup on Express to call the DB. I've listed the Express & NPM MYSQL code below (credentials redacted), along with the error I'm receiving.
NPM MYSQL:
const config = sql.createConnection( {
host: '123.74:3306',
user: 'root',
pass: '****',
database: '****'
});
Express Route:
app.use('/t',function(req,res){
config.connect(function(err){
if (err){
res.send(err);
return;
}
res.send('connected');
});
config.end();
});
Error:
{"code":"ENOTFOUND","errno":"ENOTFOUND","syscall":"getaddrinfo","hostname":"123.74:3306","host":"123.74:3306","port":3306,"fatal":true}
Update:
I found the issue in the library, and posted a solution.
You have to correct your config object removing the port from the host and adding it as an object key.
const config = sql.createConnection( {
host: '123.74',
port: 3306,
user: 'root',
pass: '****',
database: '****'
});
The issue was the mysql library I was using. After looking into the numerous Issues raised on GitHub, it seemed like my problem was very common, so I decided to switch to another library called "mysql2", I supplied the same credentials as before and my data flowed out no problem. I put a link to the library on NPM below.
https://www.npmjs.com/package/mysql2