MySQL is connecting to a different unknown IP when trying to connect.
Code Below:
.env
MYSQL_HOST=domain.example.com
MYSQL_USER=****
MYSQL_PASSWORD=****
MYSQL_DB=****
MYSQL_PORT=3306
connection.js
const mysql = require("mysql");
exports.pool = mysql.createPool({
connectionLimit: 10,
host: process.env.MYSQL_HOST,
port: process.env.MYSQL_PORT,
user: process.env.MYSQL_USER,
password: process.env.MYSQL_PASSWORD,
database: process.env.MYSQL_DB,
});
controller/cron
pool.getConnection((err, connection) => {
if (err) {
console.log("ERROR: ", err);
} else { ... insert data .... }
});
With this i'm getting error:
try connecting to remote mysql...
Error: Error: ER_ACCESS_DENIED_ERROR: Access denied for user '****'#'112.200.198.66' (using password: YES)
Tested in MySQL Workbench and successfully connected. I'm just curious where the IP came from when the public IP of that domain is different. Also tried using the IP instead to connect and still giving me the same error.
If you're using MySQL 8 the issue could be that the Node mysql driver uses a different authentication plugin - see Node.js can't authenticate to MySQL 8.0
Related
I'm mounting an API in Google Cloud Run that connects to an MySQL DB server using Sequelize.
This is the standard setup:
index.js file
const DB_CONFIG = require('../db/db.config');
const Sequelize = require('sequelize')
let sequelize = new Sequelize(
DB_CONFIG.NAME,
DB_CONFIG.USER,
DB_CONFIG.PASSWORD,
{ host: DB_CONFIG.HOST, dialect: DB_CONFIG.DIALECT, pool: DB_CONFIG.POOL }
)
db.config.js file
module.exports = {
NAME: process.env.DB_NAME,
HOST: process.env.DB_HOST,
USER: process.env.DB_USER,
PASSWORD: process.env.DB_PASSWORD,
DIALECT: process.env.DB_DIALECT,
POOL: {
max: 5,
min: 0,
acquire: 30000,
idle: 10000
}
};
The problem comes when I start the project, it fails with an error that shows it is trying to connect to the same IP as the local machine public IP:
{
code: 'ER_ACCESS_DENIED_ERROR',
errno: 1045,
sqlState: '28000',
sqlMessage: "Access denied for user 'DB_USER'#'LOCAL_MACHINE_IP' (using password: YES)",
sql: undefined
}
The problem happens either if I write the final values directly in the db.config file or the values are read from process.env.
It is important to not that I'm using the Google Cloud Secret Manager to inject this values to process.env.
Also, the execution logs in Google Cloud Run shows that the API is trying to connect using the local IP.
What could be happening, and how can I search the cause of this error?
There is nothing wrong with that IP address as the host name in a mysql user account indicates the client you are connecting from, not the mysql server's IP address you are connecting to.
See mysql manual on account names for details.
I'm currently learning how to setup a node server and I'm making an API that performs some requests on my MariaDB database hosted on my VPS.
The problem is that when I make a POST request which makes a SQL request to the database, the connection times out and the server shuts down.
I have tried to add new users to MariaDB with all privileges, I tried use sequelize too.
But none of those solutions work, it still times out every time I make a query to my database.
I can connect to phpmyadmin and make some request on it, so I think that my database is running fine.
Here is my code:
router.post('/login', async function(req,res) {
let conn;
try {
// establish a connection to MariaDB
conn = await pool.getConnection();
// create a new query
var query = "select * from people";
// execute the query and set the result to a new variable
var rows = await conn.query(query);
// return the results
res.send(rows);
} catch (err) {
throw err;
} finally {
if (conn) return conn.release();
}
})
The way I connect to my database in my database.js file
const pool = mariadb.createPool({
host: process.env.DATABASE_HOST,
user: process.env.DATABASE_USER,
password: process.env.DATABASE_PASSWORD,
database: process.env.DATABSE_NAME,
});
// Connect and check for errors
module.exports={
getConnection: function(){
return new Promise(function(resolve,reject){
pool.getConnection().then(function(connection){
resolve(connection);
}).catch(function(error){
reject(error);
});
});
}
}
module.exports = pool;
And my error:
Node.js v17.0.1
[nodemon] app crashed - waiting for file changes before starting...
[nodemon] restarting due to changes...
[nodemon] starting `node server.js`
Server started
/Users/alexlbr/WebstormProjects/AlloEirb/server/node_modules/mariadb/lib/misc/errors.js:61
return new SqlError(msg, sql, fatal, info, sqlState, errno, additionalStack, addHeader);
^
SqlError: retrieve connection from pool timeout after 10001ms
at Object.module.exports.createError (/Users/alexlbr/WebstormProjects/AlloEirb/server/node_modules/mariadb/lib/misc/errors.js:61:10)
at timeoutTask (/Users/alexlbr/WebstormProjects/AlloEirb/server/node_modules/mariadb/lib/pool-base.js:319:16)
at Timeout.rejectAndResetTimeout [as _onTimeout] (/Users/alexlbr/WebstormProjects/AlloEirb/server/node_modules/mariadb/lib/pool-base.js:342:5)
at listOnTimeout (node:internal/timers:559:11)
at processTimers (node:internal/timers:500:7) {
text: 'retrieve connection from pool timeout after 10001ms',```
Three possibilities come to mind:
There is a typo in database name:
database: process.env.DATABSE_NAME
database: process.env.DATABASE_NAME
Your environment variables are not being properly set. Are you using dotenv to load these from an .env file?
https://www.npmjs.com/package/dotenv
If not, how are you setting the process.env values at runtime?
If the environment values are indeed set:
verify that these environment values are correct
verify which interface your MariaDB server is listening on:
It's possible the server is using a bind-address configuration and only listening on 127.0.0.1 (which is the default on Debian/Ubuntu)
You want to make sure the server is listening on: 0.0.0.0 (all interfaces, not only localhost)
im creating a chrome extension and i keep getting this error
code: 'ER_BAD_DB_ERROR',
errno: 1049,
sqlState: '42000',
sqlMessage: "Unknown database 'blinkyblinky'",
sql: undefined
below is the code I'm using
const mysql = require('mysql2');
const connection = mysql.createConnection({
host: 'localhost',
user: 'root',
port: 3306,
database: 'blinkyblinky'
});
connection.query('SELECT * FROM userdata',
function(err, results, fields) {
console.log(err);
console.log(results);
console.log(fields);
}
);
this is the database I'm trying to connect to
so i was checking many thing i even create a db and a table and inserted values into it with javascript and it worked but when i checked locahost phpmyadmin none of the db i created was there i was like what hell so when i double check apparently it had been creating in my mysql workbench 😂😂😂
It is my first time using postgres and I am trying to use it with node. I installed postgres in Ubuntu and created the user admin
sudo -i -u postgres
createuser --interactive
in my node code I tried to connect to postgres:
let { Client } = require('pg');
conn = new Client({host:'localhost', port:5432, database:'web-viewer', user: 'admin'});
conn.connect();
but I get this error
UnhandledPromiseRejectionWarning: error: password authentication failed for user "admin"
my pg_hba.conf was
local all postgres peer
then I changed to
local all postgres md5
to try to add a password to my admin user, but when I "createuser --interactive" again, I get this error
createuser: error: could not connect to database template1: FATAL: password authentication failed for user "postgres"
how can I prepare the postgreSQL to connect to node?
You didn't parse the password in your new Client definition:
in my node code I tried to connect to postgres:
let { Client } = require('pg');
conn = new Client({host:'localhost', port:5432, database:'web-viewer', user: 'admin'});
conn.connect();
change it like the following, using the information you saved during the user creation:
let { Client } = require('pg');
conn = new Client({
host:'localhost',
port:5432,
database:'web-viewer',
user: 'admin',
password: 'yourpassword'
});
conn.connect();
Follow the doc.
btw, to avoid using sensible information (your password), consider creating environment variables.
I know the post is 8 months old, but you never know.
I consistently get a SequelizeConnectionRefusedError when trying to connect to a MySQL database on my server.
The login credentials are correct, the port is open, everything seems good (and works like a charm in the dev environment).
Sorry for the scarce background information, but I'm dumbfounded here - I really don't know what could be causing this problem.
This is my output from mysql --version
mysql Ver 14.14 Distrib 5.5.43, for debian-linux-gnu (x86_64) using readline 6.3
And this is the code I'm using to initialize Sequelize. The table I want it to use doesn't exist yet, but I'm fairly sure that hasn't got anything to do with this problem. I've tried logging in with the root user as well, but no dice - I still get the same error.
var sequelize = new Sequelize("database", username, password, {
host: "localhost",
dialect: "mysql",
port: 3306,
define: {
paranoid: true
}
});
var Model = sequelize.define("Model", {
md5: {type: Sequelize.STRING(128)},
ip: {type: Sequelize.STRING(256)},
url: {type: Sequelize.STRING(1024)}
});
sequelize.sync();
This is running on Ubuntu 14.04, where node is being run behind Passenger (although the error appears if I run the application with node directly as well). I'm running nginx and PHP on the same server, where another PHP application is connecting to the database, if that's of any relevance.
What could be causing this problem?
I tried to connect to the database directly with the MySQL module as well, but that gave me the same error. When looking for solutions to the same problem, but related to the MySQL module rather than Sequelize, I found this: connect ECONNREFUSED - node js , sql.
What I needed was to supply the mysql module with a socketPath key. Here's how I changed my code to make it work:
var sequelize = new Sequelize("database", username, password, {
host: "localhost",
dialect: "mysql",
logging: function () {},
pool: {
max: 5,
min: 0,
idle: 10000
},
dialectOptions: {
socketPath: "/var/run/mysqld/mysqld.sock"
},
define: {
paranoid: true
}
});