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.

102 lines
2.2 KiB

#!/usr/bin/node
import mysql from 'mysql2/promise'
import { input, password } from '@inquirer/prompts'
import generator from 'generate-password'
import chalk from 'chalk'
import { program } from 'commander'
const log = console.log
program
.option('-u, --user <name>', 'User')
.option('--ssl', 'ssl')
program.parse(process.argv)
// getting options
const options = program.opts()
/**
* mariadb-drop.js
*
*
*
*/
const user = {
name: 'root',
password: undefined
}
if (options.user) {
user.name = options.user
}
log(chalk.red('Mariadb Create Database...'))
user.password = await password({
message: 'Enter Root Password:',
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: '127.0.0.1',
user: user.name,
password: user.password,
port: 3306
})
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")
if (options.ssl) {
await connection.query("GRANT ALL PRIVILEGES ON " + database.name + ".* TO " + database.user + " require SSL")
}
await connection.query("FLUSH PRIVILEGES")
connection.destroy()
// show name, user and passwort
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))