Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to start mysql database at localhost for node.js server?

Tags:

node.js

mysql

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')
);
like image 934
user3239600 Avatar asked Aug 01 '26 16:08

user3239600


1 Answers

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.

like image 146
Naved Alam Avatar answered Aug 03 '26 05:08

Naved Alam



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!