-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcreate-database.js
More file actions
38 lines (31 loc) · 1.06 KB
/
create-database.js
File metadata and controls
38 lines (31 loc) · 1.06 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
// *** User Settings *** //
// Please modify "hostname", "user", "password" to match your MySQL server connection settings.
// You can also change the "database" name if it already exists.
// Don't forget to update the same settings in /config/config.json if you make any changes here.
const { development } = require('./config/config')
const { username: user, password, database, host } = development
// *** User Settings End *** //
const mysql = require('mysql2')
// Create connection
const connection = mysql.createConnection({ host, user, password })
// Connect to MySQL server
connection.connect(err => {
if (err) throw err
console.log('Connected to MySQL server.')
// Create database
connection.query('USE ' + database, (err, result) => {
if (err) {
connection.query(
`CREATE DATABASE IF NOT EXISTS ${database}`,
(err, result) => {
if (err) throw err
console.log('Database created.')
process.exit()
}
)
} else {
console.log('Database already exists.')
process.exit()
}
})
})