import mysql from 'mysql2/promise' import { input, password } from '@inquirer/prompts' import generator from 'generate-password' import chalk from 'chalk' const log = console.log /** * mariadb-create.js * * create database and generate name, user, password * and grant this user single priveleges * * */ const user = { name: 'root', password: undefined } 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: '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 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))