parent
83d144dde1
commit
b5c5353aa5
@ -0,0 +1,90 @@
|
|||||||
|
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: undefined,
|
||||||
|
password: undefined
|
||||||
|
}
|
||||||
|
|
||||||
|
user.name = await input({
|
||||||
|
message: 'Root:',
|
||||||
|
async validate(value) {
|
||||||
|
if (!value) {
|
||||||
|
return 'Required!'
|
||||||
|
}
|
||||||
|
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
})
|
||||||
|
|
||||||
|
user.password = await password({
|
||||||
|
message: '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))
|
@ -1,6 +1,14 @@
|
|||||||
import mysql from 'mysql2/promise'
|
import mysql from 'mysql2/promise'
|
||||||
import { input, password } from '@inquirer/prompts'
|
import { input, password } from '@inquirer/prompts'
|
||||||
import generator from 'generate-password'
|
|
||||||
|
/**
|
||||||
|
* mariadb-drop.js
|
||||||
|
*
|
||||||
|
* create database and generate name, user, password
|
||||||
|
* and grant this user single priveleges
|
||||||
|
*
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
|
||||||
const user = {
|
const user = {
|
||||||
name: undefined,
|
name: undefined,
|
Loading…
Reference in new issue