I am currently working on a homework based on ORM. I finished my code however when I start my server, I get a access denied. I checked my credentials for my env file and its correct. I have attached the message from gitbash as well, my connection code to gain access to the database, and an env example of the credentials that are required. enter image description here
DB_NAME='ecommerce_db'
DB_USER=''
DB_PASSWORD=''
require('dotenv').config();
const Sequelize = require('sequelize');
const sequelize = process.env.JAWSDB_URL
? new Sequelize(process.env.JAWSDB_URL)
: new Sequelize(process.env.DB_NAME, process.env.DB_USER, process.env.DB_PW, {
host: 'localhost',
dialect: 'mysql',
dialectOptions: {
decimalNumbers: true,
},
});
module.exports = sequelize;
Related
I have only been working with PHP before going to Node.JS. What I was able to do in PHP when working with MYSQL was that I could include the database.php file in the files I wanted to execure queries in.
It doesn't seem to be the same in Node.Js. This is my database.js file
const mysql = require("mysql2/promise");
const db = mysql.createConnection({
host: 'localhost',
user: 'root',
password: 'XXXX',
database: 'nodelogin'
});
module.exports = db;
Then I require this in my file login.js
const db = require("../../database");
However, when I then try to run db.query(sql, [variable]) I get db.query is not a function.
Why is this? It shouldn't be that more complicated or should it?
If you use a connection pool instead, you can include the pool once, then call query on it, like so:
db-config.js
const mysql = require("mysql2/promise");
console.log("Creating connection pool...")
const pool = mysql.createPool({
host: 'localhost',
user: 'user',
database: 'test_db',
password: 'password'
})
module.exports = pool;
test.js
// Require to whereever db-config is
const pool = require('./db-config.js');
async function testQuery() {
const results = await pool.query("select * from users");
console.table(results[0]);
}
testQuery();
I am using the mysql2 package for connecting node with MySQL and I am using a .env file to store my username and password. But when I use it in my config for connecting MySql it doesn't load. It throws the following error whenever I hit an endpoint using Postman.
Error: Access denied for user 'undefined'#'localhost' (using password: YES)
Below is my code:
const express = require("express");
const router = express.Router();
const mysql = require("mysql2");
const connection = mysql.createPool({
user: `${process.env.DB_USERNAME}`,
host: "localhost",
password: `${process.env.DB_PASSWORD}`,
waitForConnections: true,
connectionLimit: 10,
queueLimit: 0,
database: "issue",
});
I am using dotenv to load env variables and initialized in my entry file as:
require('dotenv').config();
What am I doing wrong?
As a side effect of the way JavaScript coerces types, process.env.foo will result in "undefined" if it is undefined and used in a string.
There is either a typo in your dotenv config file or else some other problem with your use of dotenv.
I would recommend more defensive coding to give a better error message when this happens. (The following code is untested, but you will no doubt get the idea.)
const express = require("express");
const router = express.Router();
const mysql = require("mysql2");
const dbUsername = process.env.DB_USERNAME;
const dbPassword = process.env.DB_PASSWORD;
if (!dbUsername) {
throw new Error('DB_USERNAME environment variables must be set');
}
if (!dbPassword) {
throw new Error('DB_PASSWORD environment variables must be set');
}
const connection = mysql.createPool({
user: dbUsername,
host: "localhost",
password: dbPassword,
waitForConnections: true,
connectionLimit: 10,
queueLimit: 0,
database: "issue",
});
I have a db.js file in my node app which contains the credentials for the heroku database i'm using and the local database I'm using.
I want to a.stop having to manually comment out the credentials, as well as the ability to be able for the app to know when it's running on heroku (and therefore use the heroku credentials) Also i'm worried about security, I know I can just omit storing the db.js file from my github repo, but I'm not sure what the best practice for this is.
db.js file
var mysql = require('mysql');
var conn = {
development: {
conn : mysql.createConnection({
host: 'localhost',
user: 'root',
password: 'root',
database: 'test1'
})
},
// heroku credentials
production: {
conn : mysql.createConnection({
host: 'test.net',
user: 'test',
password: 'test',
database: 'heroku'
})
}
}
module.exports = conn;
How i'm calling it in my code
var env = process.env.NODE_ENV || 'development';
var conn = require('./db')[env];
and then using it like
let query = conn.query(sql, (err, results) => {
Which is giving me the following error
conn.query is not a function
You can set your env variables with Heroku cli
Exemple for your SQL user and password
$ heroku config:set SQL_PASSWORD=XXX
$ heroku config:set SQL_USER=John
And use it
production: {
conn : mysql.createConnection({
...
password: process.env.SQL_PASSWORD,
user: process.env.SQL_USER,
database: 'heroku'
})
Good to know: Heroku set automatically NODE_ENV=PRODUCTION
Look at this documentation: https://devcenter.heroku.com/articles/config-vars
I'm trying out db migrations with MySQL and Knex.
When I run the command knex migrate:latest, I get
ER_ACCESS_DENIED_ERROR: Access denied for user ''#'localhost' (using password: NO)
I've tried adding a password on the codebase (to '123' and 'NO'), though what confuses me most is that even as I have user: "root" in my database file, the error gives an empty string as the user...
I share what I imagine are the pertinent files:
// mysql_db.js
const knex = require('knex')({
client: 'mysql',
connection: {
host: 'localhost',
user: 'root',
password: '',
database: 'SQL_Data',
},
});
module.exports = knex;
// knexfile.js
const path = require('path');
module.exports = {
development: {
client: 'mysql',
connection: {
filename: '/server/SQL/mysql_db',
},
migrations: {
directory: path.join(__dirname, '/server/SQL/migrations'),
},
seeds: {
directory: path.join(__dirname, '/server/SQL/seeds'),
},
},
};
//knex.js
const environment = proces.env.NODE_ENV || 'development';
const config = require('../../knexfile.js')[environment];
module.exports = require(knex)('config');
// "migration definition"
exports.up = (knex, Promise) => knex.schema.createTable('sql_table', ((table) => {
table.increments();
table.string('name').notNullable();
table.string('email').notNullable();
table.string('description').notNullable();
table.string('url').otNullable();
}));
exports.down = (knex, Promise) => knex.schema.dropTable('sql_table');
As error message say you are trying to login with invalid credentials user whose name is empty string doesn't exist in DB.
This means your configuration is wrong. you have some strange segment in your node-mysql driver configuration, which tries to refer other file, which exports initialized knex instance
client: 'mysql',
connection: {
filename: '/server/SQL/mysql_db'
}
That is just plain wrong. Correct format for knexfile is pretty much the same that is used to create knex instance, except that knexfile supports also selecting the profile according to NODE_ENV environment variable.
const path = require('path');
module.exports = {
development: {
client: 'mysql',
connection: {
host: 'localhost',
user: 'root',
password: '',
database: 'SQL_Data',
},
migrations: {
directory: path.join(__dirname, '/server/SQL/migrations'),
},
seeds: {
directory: path.join(__dirname, '/server/SQL/seeds'),
},
},
};
In your mysql_db you might like to do something like this to init knex to
be able to use the same config:
const knex = require('knex')(
require('knexfile')[process.env.NODE_ENV || 'development']
);
i'm trying to connect to mysql database with meteor using nodets:mysql and i'm facing this error :
Unhandled rejection Error: No infromation can be fetched by your database, please check your permissions
this is my part of code :
Meteor.startup(function() {
//Start of changes
var connectionSettings = {
host: '127.0.0.1',
user: 'root',
password: '',
database: 'test'
};
var db = Mysql.connect(connectionSettings);
})
i need to add the port of mysql
Meteor.startup(function() {
//Start of changes
var connectionSettings = {
host: '127.0.0.1',
port : (mysqlport),
user: 'root',
password: '',
database: 'test'
};
var db = Mysql.connect(connectionSettings);
})