let connection = mysql.createConnection({
user: 'root',
password: '1234',
database: 'data101',
port: 3306
});
I'm trying to create a Database using MySQL package for NodeJS, should I create the database name previously manually? Is it possible to do so?
Your code allows to connect to Mysql. In order to create a mysql database, you need to write your code like this.
CREATE DATABASE playlistDB; //this line creates the database
USE playlistDB;
CREATE TABLE songs(
id INT NOT NULL AUTO_INCREMENT,
title VARCHAR(45) NULL,
artist VARCHAR(45) NULL,
genre VARCHAR(45) NULL,
PRIMARY KEY (id)
);
In order to connect to the database, your code should look like the following lines.
var mysql = require("mysql");
var connection = mysql.createConnection({
host: "localhost",
port: 3306,
// Your username
user: "root",
// Your password
password: "",
database: "playlistDB"
});
connection.connect(function(err) {
if (err) throw err;
console.log("connected as id " + connection.threadId);
});