I'm running mariadb locally and want to query for a few rows using the Node driver.
I have the following code below that comes straight from the mariadb node docs. I've confirmed my server is up and running and that I can query from the CLI itself.
The weird thing is that the script doesn't fail or throw error. It just gets stuck at the await pool.getConnection() line. I even waited for like 5 minutes.
My server version is Server version: 10.7.3-MariaDB Homebrew and I'm using the 3.0.0 package version for Node https://www.npmjs.com/package/mariadb
const mariadb = require('mariadb');
const pool = mariadb.createPool({
host: 'localhost',
user: 'node',
password: 'PWD',
connectionLimit: 5,
port: 3306,
database: 'DB',
acquireTimeout: 5000
});
(async () => {
console.log('in iife');
let conn;
try {
console.log('awaiting conn');
//This never gets reached..just gets stuck even after 5s
conn = await pool.getConnection();
console.log(conn);
console.log('awaiting q');
const rows = await conn.query("SELECT * FROM foo where id=1");
console.log(rows);
} catch (err) {
throw err;
} finally {
if (conn) return conn.end();
}
})();
I've actually tried this on two separate devices and have the same exact issue.
Any ideas?
It was a red herring. Using dev tools to debug led me to realize there was a mundane runtime error in undeclared var. Red herring was that error was being caught and thrown. But bc this was being called as ad-hoc script, I was never aware of what the error was. Replacing the throw with a console.error led me to see error
Related
I didn't expect that I'll stuck with this simple (at first glance) question, but I really haven't find anything helpful about the problem. I use the following code in my connect.js file:
`let mysql = require('mysql');
let connection = mysql.createConnection({
host: 'localhost',
user: 'xxxxx',
password: 'xxxxxxxxxxxxxxx',
database: 'xxxxx'
});
connection.connect(function(err) {
if (err) {
return console.error('error: ' + err.message);
}
console.log('Connected to the MySQL server.');
});
`
then I run the following command, and recieve this response:
` $ node connect.js
file:///home/timothy/Desktop/Backend/connect.js:1
let mysql = require('mysql');
^
ReferenceError: require is not defined
at file:///home/timothy/Desktop/Backend/connect.js:1:13
at ModuleJob.run (internal/modules/esm/module_job.js:145:37)
at async Loader.import (internal/modules/esm/loader.js:182:24)
at async Object.loadESM (internal/process/esm_loader.js:68:5)`
I tried to remove 'type': 'module' from my package.json file, then it works as intended, I recieve console.error (but that is not the point, it's related to my database). But if I remove 'type': 'module' my main file index.js doesn't work, because it uses express js dependency. So now I wondering how to make both files work properly. I tried to install requirejs via
`$ npm i requirejs
`
but it doesn't solve my problem
UPDATE:
mysql npm package is installed of course
and I see the same problem here:
How can I get all the filesnames from a directory on the server in javascript?
Many people says that this error happens when you try running code in a browser and not in a nodejs server. I don't know if I have to specify that this file is for node server. I just run
node filename.js
As you can see, may be I miss something?
Can you try to change to this
import * as mysql from 'mysql'
or
import mysql from 'mysql'
And set type to module
I think your mysql npm package is not installed or an unwanted character put before
let mysql = require('mysql'); line, so at first run npm i mysql command on your project directory. then use the following code...
let mysql = require('mysql');
let connection = mysql.createConnection({
host: 'localhost',
user: 'root',
password: '',
database: 'ecomm'
});
connection.connect(function(err) {
if (err) {
return console.error('error: ' + err.message);
}
console.log('Connected to the MySQL server.');
});
I have updated my node version, and now I can't access to my DB with my app. I tried many things:
-Change rout to 127.0.0.1.
-add my port number 3306
-add socketPath: '/Applications/MAMP/tmp/mysql/mysql.sock'
-kill node running
And probably others, but I always have an error. Actually, I have this one while running this code.
Error:
disconnected
error: connect ECONNREFUSED ::1:3306
My code who was working before:
const mysql = require('mysql');
const db = mysql.createConnection({
host: "localhost",
user: "root",
password: "",
database: "discord"
});
if (db.state === 'disconnected') {
db.connect(function (err) {
if (err) { return console.error('error: ' + err.message); }
console.log('Connected to the MySQL server.');
});
}
console.log(db.state);
module.exports = db
My issue was the version of discord.js because I have updated it as well. I've changed it in my package.json for the one I used before and everything is working well. It will be hard to update all my app to the new version. If you have any recommendation to update it easily without running into multiple issues, I'm here !
I'm trying to connect to a Postgres database from my Heroku node app, which works when running locally, both through node and by running the heroku local web command, but when running it on Heroku, it times out while waiting for pool.connect
I'm running the following code snippet through the Heroku console (I've also tried using this code in my app directly, but this is more efficient than redeploying each time):
node -e "
const { Pool } = require('pg');
const pool = new Pool({
connectionTimeoutMillis: 15000,
connectionString: process.env.DATABASE_URL + '?sslmode=require',
ssl: {
rejectUnauthorized: true
}
});
console.log('pool created');
(async() => {
try {
console.log('connecting');
const client = await pool.connect(); // this never resolves
console.log('querying');
const { rows } = await client.query('SELECT * FROM test_table LIMIT 1;');
console.log('query success', rows);
client.release()
} catch (error) {
console.log('query error', error);
}
})()
"
Things I've tried so far:
Using the pg Clientinstead of Pool
Using ssl: true instead of ssl: { rejectUnauthorized: true }
Using client.query without using pool.connect
Increased and omitted connectionTimeoutMillis (it resolves quickly when running locally since I'm querying a database that has just one row)
I've also tried using callbacks and promises instead of async / await
I've tried setting the connectionString both with the ?sslmode=require parameter and without it
I have tried using pg versions ^7.4.1 and ^7.18.2 so far
I have been working with nodejs google cloud functions for a while. I have been facing a weird issue where I can't connect to database servers and there is no error logged not even timeout. I am using node v14.2.0 with pg as postgres library. My nodejs code is
const { Client } = require('pg');
let connectionSetting = {
host: "host",
user: "user",
database: "db_name",
password: "password",
};
const client = new Client(connectionSetting);
console.log(connectionSetting);
client.connect(err => {
if (err) {
console.error(`Error connecting to db -> ${err}`);
}
console.log("Connection established with PgSql DB ");
});
There are no console logs or whatever.
This same code is working on other systems. The database is remote database hosted on gcp and I'm able to connect to it using tablePlus as GUI client.
Any help appreciated.
I found the issue. It has to do with the node version. I was using node current 14.2.0 so I installed node lts 12 and then everything works fine.
Thanks for all help
I'm starting at Node.js and i'm trying to make a simple connection with Sequelize based on its documentation (http://docs.sequelizejs.com/manual/installation/getting-started.html#installation).
Here my db.js file :
const Sequelize = require('sequelize')
const db = new Sequelize('chat','root','root',{
host: 'localhost',
port: 3306,
dialect: 'mysql'
});
db
.authenticate()
.then(() => {
console.log('Connection has been established successfully.');
})
.catch(err => {
console.error('Unable to connect to the database:', err);
});
When executing this code, I have no success nor error message, just a message indicating 'String base operators deprecated' but nothing important i think.
I tried changing localhost to 127.0.0.1, remove port number and multiples thread but i'm stucked here...
Tested your code and all seemed to work fine, it connected successfully to the database instance I have running.
You might want to ensure mysql2 package is installed. Also check that the database chat has been created in MySQL workbench, and ensure the password is correct.