You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

82 lines
1.8 KiB

1 year ago
#!/usr/bin/node
1 year ago
import mysql from 'mysql2/promise'
import { input, password } from '@inquirer/prompts'
import generator from 'generate-password'
1 year ago
import chalk from 'chalk'
const log = console.log
/**
1 year ago
* mariadb-drop.js
1 year ago
*
*
*
*/
1 year ago
const user = {
1 year ago
name: 'root',
1 year ago
password: undefined
}
1 year ago
log(chalk.red('Mariadb Create Database...'))
1 year ago
user.password = await password({
1 year ago
message: 'Enter Root Password:',
1 year ago
mask: '*',
async validate(value) {
if (!value) {
return 'Required!'
}
return true
}
})
// create connection, generate name for db and user, and generate password
const connection = await mysql.createConnection({
host: 'localhost',
user: user.name,
password: user.password
})
const database = {
name: 'db',
user: 'u'
}
database.name += generator.generate({
length: 15,
numbers: true
})
database.user += generator.generate({
length: 15,
numbers: true
})
database.password = generator.generate({
length: 30,
numbers: true
})
// create database
await connection.query('CREATE DATABASE ' + database.name + ' DEFAULT CHARACTER SET utf8 COLLATE utf8_general_ci')
await connection.query("CREATE USER " + database.user + "@'localhost' IDENTIFIED BY '" + database.password + "'")
await connection.query("CREATE USER " + database.user + "@'%' IDENTIFIED BY '" + database.password + "'")
await connection.query("GRANT ALL PRIVILEGES ON " + database.name + ".* TO " + database.user + "@localhost")
await connection.query("FLUSH PRIVILEGES")
connection.destroy()
// show name, user and passwort
1 year ago
log(' ')
log(chalk.green('Database created!'))
log(' ')
log(chalk.green('name:') + ' ' + chalk.cyan(database.name))
log(chalk.green('user:') + ' ' + chalk.cyan(database.user))
log(chalk.green('password:') + ' ' + chalk.cyan(database.password))