I am getting started with mysql database for my NODEJS application
const mysql = require('mysql2');
const pool = mysql.createPool({
host: 'localhost',
root: 'root',
database:'node-complete',
password: 'Pratik#123'
})
My root password is the one coded above.
I am getting this error:-
Ignoring invalid configuration option passed to Connection: root.
This is currently a warning, but in future versions of MySQL2, an error will be thrown if you pass an invalid configuration option to a Connection
Can some help me to fix this issue?
module.exports = pool.promise()
the property in connection options must be named user, not root
const pool = mysql.createPool({
host: 'localhost',
user: 'root',
database:'node-complete',
password: 'Pratik#123'
});
I was getting the same problem, i used
const pool = mysql.createPool({
host: 'localhost',
username: 'root',
database: 'node-complete',
password: '24GM#L#PM+AU',
port: '3306'
});
its not supposed to say "username" but instead just "user" like below
const pool = mysql.createPool({
host: 'localhost',
user: 'root',
database: 'node-complete',
password: '24GM#L#PM+AU',
port: '3306'
});
it works when you change username to user:
const mysql = require('mysql2');
const pool = mysql.createPool({
host: 'localhost',
user : 'root',
database : 'node-complete',
password : 'node-complete'
});
PS: I watch the same lecture as you do ( database : 'node-complete', ) gave it away :D
you have to change the connection property root as user
const pool = mysql.createPool({
host:'localhost',
user:'root',
password:'admin',
database:'node-complete'
})
Related
I have my backend with this connection pool in mysql, now I try to migrate to dabase oracle can someone help me with configuration (to connect my api to oracle DB) thank you very much.
.env
APP_PORT=3000
DB_PORT=8889
DB_HOST=localhost
DB_USER=root
DB_PASS=root
MYSQL_DB=test
**dabatase.js**
const { createPool } = require('mysql');
const pool = createPool({
port: process.env.DB_PORT,
host: process.env.DB_HOST,
user: process.env.DB_USER,
password: process.env.DB_PASS,
database: process.env.MYSQL_DB,
connectionLimit: 10
});
module.exports = pool;
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();
my config.env file
PORT=5000
DB_HOST='localhost'
DB_PORT=3306
DB_USER='root'
DB_PASSWORD='fast'
DB_NAME='hms'
my dbconnect file(in same directory as config.env file)
I cannot connect using process.env but if I directly type values like I have did in commented code then it will connect to database. Also if I console.log values of process.env.anyvariable then I will get the correct value of env variable but if i assign it to some variable like suppose const variable=process.env.DB_HOST then it will be undefined in console.log. It is throwing me this error
code: 'ER_ACCESS_DENIED_ERROR',
errno: 1045,
sqlMessage: "Access denied for user ''#'localhost' (using password: NO)",
sqlState: '28000',
fatal: true
const mysql = require("mysql")
const dotenv = require("dotenv")
dotenv.config({ path: './config.env' });
const connection = mysql.createConnection({
host: process.env.DB_HOST,
port: process.env.DB_PORT,
user: process.env.DB_USER,
password: process.env.DB_PASSWORD,
database: process.env.DB_NAME
})
// const connection = mysql.createConnection({
// host: 'localhost',
// port: 3306,
// user: 'root',
// password: 'fast',
// database: 'hms'
// })
connection.connect( (err) => {
if (err){
console.log(err)
}
else
{
console.log("Database connected!")
}
})
my /config/config.env is not in the parent directoy so instead of
dotenv.config({ path: './config.env' });
write
dotenv.config({ path: __dirname + '/config.env' });
I have started using xdevapi for Node.js as mysql connector. By following this guide (https://dev.mysql.com/doc/x-devapi-userguide/en/connecting-to-a-single-mysqld-node-setup.html):
var mysqlx = require('#mysql/xdevapi');
var dictSession = mysqlx.getSession( {
host: 'localhost', 'port': 33060,
user: 'root', password: '' } )
var db1 = dictSession.getSchema('test')
I got an error:
dictSession.getSchema is not a function
You are using synchronous JavaScript syntax that only works in the MySQL Shell (as depicted in the link you point to). Unfortunately, that guide does not seem to include a proper snippet for the Connector/Node.js version where the getSession() method is asynchronous and returns a Promise, which means you need to attach a proper then() handler, in which dictSession will eventually be resolved.
mysqlx.getSession({ host: 'localhost', 'port': 33060, user: 'root', password: '' })
.then(function (dictSession) {
var db1 = dictSession.getSchema('test')
});
or you can use the async/await flavour:
var dictSession = await mysqlx.getSession({ host: 'localhost', 'port': 33060, user: 'root', password: '' })
var db1 = dictSession.getSchema('test')
I'll make sure the guide will include a proper example.
Disclaimer: I'm the X DevAPI Node.js connector lead dev.
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);
})