Reputation: 321
I've found a bunch of tutorials which tell me how to use mysql for node.js, but since i am a newbie - i don't know how to create localhost mysql database. Can anyone explain me how to or just send link to tutorial?
I am using this code in my node.js server:
app.use(
connection(mysql,{
host : 'localhost',
user : 'root',
password : '',
database : 'test',
debug : false //set true if you wanna see debug logger
},'request')
);
Upvotes: 3
Views: 10108
Reputation: 141
First, you'll need to install the javascript client which connects Node to MySQL. You can do this by npm install MySQL
.
To create a connection, use
var mysql = require('mysql');
var connection = mysql.createConnection({
host : 'localhost',
user : 'root',
password : 'secret',
database : 'test'
});
To connect to the DB, run connection.connect();
Following this, you can execute a query by
connection.query('SELECT now()', function(err, rows) {
if (err) throw err;
console.log('The current time is: ', rows[0].solution);
});
For more you can visit this and this.
Upvotes: 2