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;
Related
Future<MySqlConnection> dbConnectSystem() async {
final conn = await MySqlConnection.connect(ConnectionSettings(
host: 'my_ip_adress',
port: 3306,
user: 'my_user_name',
password: 'my_password',
db: 'my_db'));
return conn;
}
I rented a hosting via natro and I'm trying to add and remove my data.
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'
})
Attempting to connect database from within the microservice, failing to the connect the database
Error: connect ECONNREFUSED 172.18.0.2:3306
service/index.js
var http = require('http');
//create a server object:
http.createServer(function (req, res) {
res.write('Hello World!'); //write a response to the client
res.end(); //end the response
}).listen(8080); //the server object listens on port 8080
console.log("Listening at 8080");
var mysql = require('mysql');
var con = mysql.createConnection({
host: "database",
user: "root",
password: "password"
});
con.connect(function(err) {
if (err) throw err;
console.log("Database Connected!");
});
docker-compose.yml
version: '3'
services:
database:
build:
./database
ports:
- "6603:3306"
image: "test-mysql"
container_name: "test-mysql"
service:
build:
./service
ports:
- "8080:8080"
depends_on:
- database
image: "test-nodejs"
container_name: "test-nodejs"
restart: on-failure
I've attempted connecting to database with different settings.
1) Without port
var con = mysql.createConnection({
host: "database",
user: "root",
password: "password"
});
2) specified port 3306
var con = mysql.createConnection({
host: "database",
user: "root",
password: "password"
port: 3306
});
3) specified port 6603
var con = mysql.createConnection({
host: "database",
user: "root",
password: "password",
port: 6603
});
database/Dockerfile
FROM mysql
ENV MYSQL_DATABASE=test
ENV MYSQL_ROOT_PASSWORD=password
EXPOSE 6603:3306
COPY ./schema.sql /docker-entrypoint-initdb.d/
Basically how my node.js microservice can discover the database service?
Edit
I suspected that database wasn't ready by the time nodejs kicks in, so I added some delay before connecting to database and error changed
Updated Code
setTimeout(function(){
var mysql = require('mysql');
var con = mysql.createConnection({
host: "database",
user: "root",
password: "password"
});
con.connect(function(err) {
if (err) throw err;
console.log("Database Connected!");
});
}, 20 * 1000);
output
Error: ER_NOT_SUPPORTED_AUTH_MODE: Client does not support authentication protocol requested by server; consider upgrading MySQL client
Probably you are using a version of MySQL that doesnt support the login you are trying. Try with mysql v5.7:
docker run -d -p 6603:3306 --name mysql-container -e MYSQL_ROOT_PASSWORD=password mysql:5.7
You miss order:
First connect to database, then listen the port.
var http = require('http');
var mysql = require('mysql');
var con = mysql.createConnection({
host: "database",
user: "root",
password: "password"
} );
con.connect(function(err) {
if (err) throw err;
console.log("Database Connected!");
});
//create a server object:
http.createServer(function (req, res) {
res.write('Hello World!'); //write a response to the client
res.end(); //end the response
}).listen(8080); //the server object listens on port 8080
console.log("Listening at 8080");
the database port does not get translated as it's within the container. just use 3306 in your app
I've a working script to connect and work with Sequelize on Node.js
But now i'm trying to connect this to my MySQL database on XAMPP
MySQL Port on XAMPP: 3306
When i run node.js after i have configured the app.listen and the config of sequealize i get the following error
ERROR: listen EADDRINUSE :::3306
I've looked for but i didn't find much information about that, i don't know what i'm doing bad.
Thanks you for every answer!
app.js
const express = require('express')
const bodyParser = require('body-parser')
const cors = require('cors')
const morgan = require('morgan')
const { sequelize } = require('./models')
const config = require('./config/config')
const app = express()
app.use(morgan('combined'))
app.use(bodyParser.json())
app.use(cors())
require('./routes')(app)
sequelize.sync()
.then(() => {
app.listen(config.db.options.port || 3306) // 8081 original
console.log(`Server iniciado en puerto: ${config.db.options.port}`)
})
config.js
module.exports = {
db: {
database: process.env.DB_NAME || 'intraenvios',
user: process.env.DB_USER || 'root',
password: process.env.DB_PASS || '',
options: {
dialect: process.env.DIALECT || 'mysql', // sqlite original
host: process.env.HOST || 'localhost',
storage: './intraenvios.sqlite',
port: process.env.PORT || 3306 // 8081 original
}
}
}
EDIT:
For to connect by xampp simply, i made this:
const sequelize = new Sequelize('test', 'root', '', {
host: "127.0.0.1",
dialect : 'mysql',
operatorsAliases: false
});
sequelize.authenticate().then(function(){
console.log("sucess");
}).catch(function(error){
console.log("error: "+error);
});
Obs: operatorsAliases: false - To fix deprecated message of sequelize
Good Fun :)
I have started node-js recently and i was trying to connect my nodejs server with mysql.
The problem is i am getting an error, i really don't know why, i am using phpmyadmin.
Phpmyadmin details
user: root
host: localhost
password is not set
This is the image of my phpmyadmin database
This is the settings of my phpmyadmin console
This is the terminal where it is showing error connecting to DB
index.js
var express = require("express");
var app = express();
var mysql = require('mysql');
var port = process.env.PORT || 3000;
var connection = mysql.createConnection({
host: "localhost",
user: "root",
database: "learning",
});
connection.connect(function(err){
if(err) {
console.log('Error connecting to Db');
return;
}
console.log('Connection established');
console.log('3');
connection.end(function(err) {
console.log('Connection closed');
console.log('4');
process.exit();
});
});
app.listen(port,function(err1){
console.log("Listening on the port 3000");
});
connection to mysql from node js, you are use to mysql from node js connection
/config
/database.js
/server.js
/.env
const http = require('http');
const app = require('express')();
require('./config/database.js');
const bodyParser = require('body-parser');
const server = http.createServer(app);
server.listen(process.env.ServerPort, '0.0.0.0', () => {
logger.info(`Express server listening on port ${process.env.ServerPort}`);
});
When you run this:
node server.js
database.js file
const My = require('jm-ez-mysql');
// Init DB Connection
const connection = My.init({
host: process.env.DBHOST,
user: process.env.DBUSER,
password: process.env.DBPASSWORD,
database: process.env.DATABASE,
dateStrings: true,
charset: 'utf8mb4',
timezone: 'utc',
multipleStatements: true,
connectTimeout: 100 * 60 * 1000,
acquireTimeout: 100 * 60 * 1000,
timeout: 100 * 60 * 1000,
});
module.exports = {
connection,
};
I had changed the port from default 3306 of phpmyadmin mysql to 3308
therefore i added port: 3308 and it started working.
var connection = mysql.createConnection({
host: "localhost",
user: "root",
database: "learning",
port: 3308
});