0

enter image description hereI need to change Nodejs MySQL database connection at runtime.

Here is my code,

var mysql=require('mysql'); 
var connection = mysql.createConnection({ host : 'localhost', user : 'root', password : '', database : 'test' }); 
var connection2 = mysql.createConnection({ host : 'localhost', user : 'root', password : '', database : 'test1' }); 
connection.connect(function(err) { if (err) throw err; console.log('You are now connected...'); }); connection2.connect(function(err) { if (err) throw err; console.log('You are now connected...'); });

Do you have any idea about it?

Please share with me.

Thanks.

5
  • run time means? Any code you have? Commented Apr 11, 2018 at 7:16
  • yes please see.. var mysql=require('mysql'); var connection = mysql.createConnection({ host : 'localhost', user : 'root', password : '', database : 'test' }); var connection2 = mysql.createConnection({ host : 'localhost', user : 'root', password : '', database : 'test1' }); connection.connect(function(err) { if (err) throw err; console.log('You are now connected...'); }); connection2.connect(function(err) { if (err) throw err; console.log('You are now connected...'); }); Commented Apr 11, 2018 at 7:22
  • run time means? Commented Apr 11, 2018 at 7:25
  • When I select the drop down option, then my DB connection should be changed. Commented Apr 11, 2018 at 7:28
  • Did you understand the problem Commented Apr 11, 2018 at 7:36

3 Answers 3

1

MySQL offers a changeUser command that allows you to alter the current user and other aspects of the connection without shutting down the underlying socket:

Since you exported connection using module exports, you can require that as below

var connection = require('./connection.js');

var connection = require('./connection.js'); // your path

function changeConnection(db)

   connection.changeUser({database : 'my_database'}, function(err) { 
     if (err) throw err; 
   });
}

Here is the documentation for the same

Sign up to request clarification or add additional context in comments.

15 Comments

Actually, I created a separate connection file and require it's in my model so I am not understanding how can I changed my connection, and require it to in my all models files
means? I didnt get you
Please see in the question I have uploaded a picture.
picture? Where is it?
okay you can just require that connection using the file name
|
0

Please try this, I hope it will serve the purpose.

var mysql=require('mysql'); 

function getConnection(db){
        var connection = mysql.createConnection({ host : 'localhost', user : 'root', password : '', database : db }); 
        connection.connect(function(err) { 
            if (err) throw err; 
            console.log('You are now connected...'); 
            return connection;
        });
}


var connection = getConnection(req.param.db); //req.param.db is a user input

// Here your code goes

Comments

0

Not Need to change db connection only change database name

connection.changeUser({database : 'my_database'}, function(err) {
  if (err) throw err;
});

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.