EN
Node.js - MySQL Create database
0 points
In this article, we would like to show you how to create a MySQL database in Node.js.
xxxxxxxxxx
1
const mysql = require('mysql');
2
3
const connection = mysql.createConnection({ // gets connection with database
4
host: 'localhost', // '127.0.0.1'
5
user: 'root',
6
password: 'password',
7
database: 'my_database',
8
});
9
10
connection.connect(error => {
11
if (error) throw error;
12
const query = 'CREATE DATABASE ??';
13
const databaseName = 'my_database';
14
15
connection.query(query, databaseName, (error, result) => { // sends queries and receives results
16
connection.end(); // closes connection
17
if (error) throw error;
18
console.log(result);
19
});
20
});
21