I currently have to decide how to implement multi tenancy with my node application the best way possible.
For now, I have got my common_db_connection where I have a lookup table in which I can query the tenant database name for each specific user.
I have a resolver function which uses the users ID to get the tenant database name and finally return the my mysql connector.
My solution looks like this:
var conn_common_db = mysql.createPool({
connectionLimit : 150,
host : 'localhost',
user : 'root',
password : '',
database : 'common_db'
});
function conn_resolver(req) {
conn_common_db.query("SELECT tenant_db_name from tenants where tenant_uid = (SELECT tenant_uid from tenant_user where user_uid = ?)",
[requst.session.user_uid], function(err, result) {
if(!err) {
tenant_db_name = result[0].tenant_db_name
return mysql.createPool({
connectionLimit : 150,
host : 'localhost',
user : 'root',
password : '',
database : tenant_db_name
});
}
else{
console.log(err)
return null;
}
})
}
app.get('/', function(request, response) {
conn_resolver(req).query("SELECT * FROM user", function(err, result) {
if(!err) {
console.log(result)
}
else{
//res.send(err)
}
})
});
Is this the right way to do this and is it safe to do it like this?
Thanks in advance!
Actually no. You either need to set up a pool per tenant or just a singular connection each time.
The concept of a pool is to have a reusable connection that is already connected to the database. The next tenant will be connected to the wrong database. So you will have to recreate the pool as each tenant tries to connect to their database. You could create a pool per tenant or just do a connection on the fly.
Related
Is there a way to get data from the MySQL database (this can be done whit the Node.js back-end), but I can not create a table from that data in React.js?
Is there a good option to do that?
(I'm creating an electron application whit react and I want to list all the users from the MySQL database)
In PHP is like this https://www.w3schools.com/php/php_mysql_select.asp
var mysql = window.require('mysql');
function Test() {
var connection = mysql.createConnection({
host : 'localhost',
user : 'root',
password : 'root',
port : 8889,
database : 'loginsystem'
});
connection.connect();
var sql = 'SELECT * FROM users';
connection.query(sql, function (error, results, fields) {
if(error){
console.log(error);
}
if (!results.length){
console.log("error_fetching_data_from_database --",results.length);
}else{
console.log(result)
}
});
const listof = (
//I want print out useres here like table from the database
)
return(listof)
}
export default Test;
I'm using auth0 as my authentification services into my project. I really love it, but I have a problem when using custom database(MySql), I sure that I have configured the db.connection parameter to my remote shared hosting database in Plesk. It always show : "[Error] Script execution did not complete within 20 seconds. Are you calling the callback function?", When I trying to run "Create" script.
here the script :
function create (user, callback) {
var mysql = require('mysql');
var connection = mysql.createConnection({
host : '192.168.23.16',
user : 'user',
password : 'pass',
port : '3306',
database : 'dbname' });
connection.connect();
var query = "INSERT INTO users SET ?";
var insert = {
password: bcrypt.hashSync(user.password, 10),
email: user.email
};
connection.query(query, insert, function (err, results) {
if (err) return callback(err);
if (results.length === 0) return callback();
callback(null);
});
}
What should I'm doing right now to solve this problem? I'm new to this
Thanks..
Regards,
fxbayuanggara
You're trying to connect to a local IP address (192.168.23.16), which will always fail since database scripts and rules are executed from Auth0's servers. You'll need to make your MySQL server accessible from Auth0's IP addresses, which at the time of writing are the following:
US domains: 138.91.154.99, 54.221.228.15, 54.183.64.135, 54.67.77.38, 54.67.15.170, 54.183.204.205, 54.173.21.107, 54.85.173.28
EU domains: 52.28.56.226, 52.28.45.240, 52.16.224.164, 52.16.193.66
I cannot understand why could I connect to Mysql after release.
And is there a way to check the status of Mysql ?
Thanks for the help !
var mysqlConfig = {
host : "abcd",
port : "3306",
user : "root",
password : "root",
database : "test"
};
var pool = mysql.createPool(mysqlConfig);
pool.getConnection(function(err, conn) {
// release pool
conn.release();
// After release, Why could I connect to Mysql ????
conn.query('SELECT * FROM user_info WHERE user_id = ?', [id], function(err, rows) {
if (err) {
pushErr();
}
// ...
});
});
Based on the documentation here: https://github.com/felixge/node-mysql it looks like the connection being 'released' doesn't destroy the connection per se, it just signals the pool that it can be used by someone else if needed. If you notice the example, they release as part of the query callback. If you need to actually get rid of the connection, you should use conn.destroy().
Is there a way to import data from MySQL database on Meteor startup? I basically need just an initial data from MySQL to export it to Mongo collections for usage.
Your best bet might be to just use a mysql node package (remember to use Meteor.npmRequire(..) instead of require(..)). This one seems good:
https://github.com/felixge/node-mysql
Something like this should work:
if (Meteor.isServer) {
var mysql = Meteor.npmRequire('mysql');
Meteor.startup(function() {
var connection = mysql.createConnection({
host : 'localhost',
user : 'me',
password : 'secret'
});
connection.connect();
connection.query('SELECT * FROM table', function(err, rows, fields) {
if (err) throw err;
// create documents from rows[i] and add to your collection
});
connection.end();
});
}
i have a question regarding about node.js for mysql.
I'm trying to check for changes in the database with node.js and every time it does a change it would do something.
I have the following code:
var mysql = require('mysql');
var connection = mysql.createConnection({
host : 'localhost',
user : 'root',
password : 'root',
database : 'socialpodd',
});
connection.connect();
connection.query('SELECT * FROM personal', function(err, rows, fields) {
if (err) throw err;
console.log('Rows: ', rows[0].email);
});
connection.end();
But I want this code to be called once there is a change in the database. I know I could do a poll method, but it doesn't seem that effective and real-time. What would be a good real-time module that would allow me to do it, or is polling my only option?