main
HerrHase 1 year ago
parent b5c5353aa5
commit b6e43dd504

@ -15,23 +15,14 @@ const log = console.log
*/ */
const user = { const user = {
name: undefined, name: 'root',
password: undefined password: undefined
} }
user.name = await input({ log(chalk.red('Mariadb Create Database...'))
message: 'Root:',
async validate(value) {
if (!value) {
return 'Required!'
}
return true
}
})
user.password = await password({ user.password = await password({
message: 'Password:', message: 'Enter Root Password:',
mask: '*', mask: '*',
async validate(value) { async validate(value) {
if (!value) { if (!value) {

@ -1,5 +1,8 @@
import mysql from 'mysql2/promise' import mysql from 'mysql2/promise'
import { input, password } from '@inquirer/prompts' import { input, password } from '@inquirer/prompts'
import chalk from 'chalk'
const log = console.log
/** /**
* mariadb-drop.js * mariadb-drop.js
@ -11,12 +14,15 @@ import { input, password } from '@inquirer/prompts'
*/ */
const user = { const user = {
name: undefined, name: 'root',
password: undefined password: undefined
} }
user.name = await input({ log(chalk.cyan('Mariadb drop Database...'))
message: 'Root:',
user.password = await password({
message: 'Enter Root Password:',
mask: '*',
async validate(value) { async validate(value) {
if (!value) { if (!value) {
return 'Required!' return 'Required!'
@ -26,9 +32,8 @@ user.name = await input({
} }
}) })
user.password = await password({ const database = await input({
message: 'Password:', message: 'Database:',
mask: '*',
async validate(value) { async validate(value) {
if (!value) { if (!value) {
return 'Required!' return 'Required!'
@ -46,38 +51,17 @@ const connection = await mysql.createConnection({
password: user.password password: user.password
}) })
const database = { // create database
name: 'db',
user: 'u'
}
database.name += generator.generate({
length: 15,
numbers: true
})
database.user += generator.generate({ const [results ] = await connection.query("SELECT User FROM mysql.db WHERE Db = '" + database + "'")
length: 15,
numbers: true
})
database.password = generator.generate({ results.forEach(async (entity) => {
length: 30, await connection.query("DROP USER IF EXISTS '" + entity.User + "'@localhost");
numbers: true await connection.query("DROP USER IS EXISTS '" + entity.User + "'@'%'");
}) })
// create database await connection.query("DROP DATABASE IF EXISTS " + 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() connection.destroy()
// show name, user and passwort log(chalk.green('Database ' + database + ' droped!'))
console.log(database.name)
console.log(database.user)
console.log(database.password)

Loading…
Cancel
Save