I use nodejs connect to mysql by knex. It's my code:
var logger = configlog.getLogger("dao-movies");
var configConnection = {
client: 'mysql',
connection: {
host: '127.0.0.1',
user: 'root',
password: '',
database: 'mobile_movie',
port: 1234
}
};
var knex = require("knex")(configConnection);
var getUser = function () {
knex("tbl_user").select()
.then(function (rows) {
logger.info(rows);
})
.catch(function (error) {
logger.error("Error select tbl_user: " + error);
});
}
But it's not working.
This is the error:
Related
What im trying to do is to connect MySQL database in my project in the config file.
config/db.js
const mysql = require('mysql')
const connection = mysql.createConnection({
host: 'localhost',
user: 'root',
password: '',
database: 'mydatabase'
})
const connectDb = async () => {
const conn = await connection.connect(function (err) {
if (err) {
return console.error("error: ", err.message);
}
console.log("Connected to MySql server");
});
};
server.js
const connectDB = require("./config/db");
connectDB();
I have done this by fare and in my terminal is shown this error: TypeError: connectDb is not a function
How can I connect MySQL in the config file?
you'll need to export the connection verb, and delete the whole "connectDb" function
config/db.js
const mysql = require('mysql')
const connection = mysql.createConnection({
host: 'localhost',
user: 'root',
password: '',
database: 'mydatabase'
})
connection.connect(function(err) {
if (err) throw err;
});
module.exports = connection;
server.js
const database = require("./config/db");
database.queryOrAnythignElse();
You'll probably want to learn again the basics of js and what is module.exports and etc...
I'm making a bot for my Discord server but I have a problem with a test command that I have created. When I run the command I receive this text : undefined. And I don't know why.
My database file :
const mysql = require('mysql')
const db = mysql.createConnection({
host: 'localhost',
port: 3306,
user: 'root',
password: '',
database: 'velocity_discord'
})
module.exports = (client) => {}
module.exports.getTicketNumber = () => {
db.query(`SELECT * FROM tickets WHERE botId = '861586919653638145'`, (err, number) => {
if (err) {
console.log(err)
} else {
if (number) {
return number[0].ticketNumber
}
}
})
}
My command code :
const database = require('#features/database.js')
module.exports = {
commands: 'testdb',
callback: (message) => {
const msg = `${database.getTicketNumber()}`
message.channel.send({
content: msg
})
},
}
I'm facing this warning/error message that pops up every minute or so after running my Node project for 2-3mins. Been searching online but couldn't find any solution for this.
pool failed to create connection ((conn=-1, no: 45009, SQLState: 08S01) socket has unexpectedly been closed)
My db.js
const mysql = require("mysql"); //^2.18.1
const mariadb = require("mariadb"); //^2.5.3
const DB_OPTIONS = {
host: "127.0.0.1",
user: "root",
password: "",
database: "test-db",
port: 3306,
minDelayValidation: 5000
};
const connection = mariadb.createPool(DB_OPTIONS);
connection.getConnection().then((conn) => {
console.log("Connected to DB!", conn.threadId);
if (conn) conn.release();
}).catch((err) => {
console.error("Error while connecting to DB", err);
});
module.exports = connection;
test.js
...
let database = require("./../middleware/db");
...
...
try
{
database.query(dbQuery, dbParameters).then((result) => {
...
}).catch((dbCatchErr) => {
...
})
}
catch (catchErr)
{
console.error(catchErr);
}
MariaDB Version: 10.3.23
**I connected to the Postgres server using Nodejs and now I want to see all the databases under the connected server. Please help me **
Please see below for mysql database:
var mysql = require('mysql');
var con = mysql.createConnection({
host: "localhost",
user: "yourusername",
password: "yourpassword",
database: "mydb"
});
con.connect(function(err) {
if (err) throw err;
con.query("show databases", function (err, result, fields) {
if (err) throw err;
console.log(result);
});
});
SQL
SELECT datname FROM pg_database
WHERE datistemplate = false;
Example
const { Pool, Client } = require('pg')
const pool = new Pool({
user: 'dbuser',
host: 'database.server.com',
database: 'mydb',
password: 'password',
port: 5432,
})
pool.query('SELECT NOW()', (err, res) => {
console.log(err, res)
pool.end()
})
const client = new Client({
user: 'dbuser',
host: 'database.server.com',
database: 'mydb',
password: 'secretpassword',
port: 3211,
})
client.connect()
client.query('SELECT datname FROM pg_database WHERE datistemplate = false;', (err, res) => { console.log(err, res) client.end() }
)
Hi I'm trying to connect to my MySQL db via sequelize but to no avail..
The connection data I giving is 100% correct and I can connect to my MySQL db via phpmyadmin.
Here is my code:
var Sequelize = require('sequelize-mysql').sequelize;
var dbConnector = function()
{
var sequelize = new Sequelize
(
'dbname',
'userName',
'password',
{
host: "ip",
port: port,
dialect: 'mysql'
}
);
var getCoutries = function ()
{
sequelize.query("SELECT * FROM `Country`")
.success
(
function(myTableRows)
{
console.log(myTableRows)
}
)
.error
(
function(error)
{
console.log(error);
// geting an error:
//[Error: connect ECONNREFUSED]
//code: 'ECONNREFUSED',
//errno: 'ECONNREFUSED',
//syscall: 'connect',
//fatal: true }
}
)
;
}
getCoutries();
};
var Sequelize = require('sequelize').Sequelize;
var dbConnector = function () {
const DB_NAME = 'DB_Name'
const DB_USER = 'root'
const DB_PASSWORD = ''
var sequelize = new Sequelize(DB_NAME, DB_USER, DB_PASSWORD,
{
host: "127.0.0.1",
port: 3306,
dialect: 'mysql'
}
);
var getCoutries = function () {
sequelize.query("SELECT * FROM `processor`").then(myTableRows => {
console.log(myTableRows)
}).catch(err => {
console.log(err)
})
}
getCoutries();
};
dbConnector()
"mysql2": "^2.2.5",
"sequelize": "^6.5.0",