To connect to MySQL server, we will create a connection with mysql module. So, first include the mysql module, then call the create connection method. Here is the sample code to connect to database.
var mysql = require("mysql");
var con = mysql.createConnection({
host: "hostname",
user: "username",
password: "password",
database: "database"
});
con.connect(function(err){
if(err) {
console.log('Error connecting to Db');
return;
}
console.log('Connection Established');
})
con.end(function(err) {
});
Solution
To connect to MySQL server, we will create a connection with mysql module. So, first include the mysql module, then call the create connection method. Here is the sample code to connect to database.
need an explanation for this answer? contact us directly to get an explanation for this answer