NodeJs Mysql Connection
In order use Mysql in nodeJs, we need to install packages / modules which are required for Mysql Integration with nodejs. They are many such npm modules available. We use “mysql” module to do the same.
npm install mysql
Let’s create a file with below code.
var db = require('mysql'); var con = db.createConnection({ host: "localhost", user: "root", password: "password" }); con.connect(function(err) { if (err) throw err; console.log("Connected!"); con.query("CREATE DATABASE Student", function (err, result) { if (err) throw err; console.log("Database created"); }); });
From the above code, connect method used to connect mysql with the given credentials.
Query Method is used to execute the given query. Below SS shows the output for this program