secret('Enter root password'); // connect database try { $mysqli = new \mysqli('127.0.0.1', 'root', $password); } catch(\ErrorException $exception) { $this->error('Failed! '.$exception->getMessage()); exit(); } $this->info('Mariadb Create Database...'); // generate password $generator = new ComputerPasswordGenerator(); $generator->setRandomGenerator(new Php7RandomGenerator()) ->setUppercase() ->setLowercase() ->setUppercase() ->setNumbers() ->setSymbols(false) ->setLength(self::PASSWORD_LENGTH); // getting password $password = $generator->generatePasswords(1)[0]; $generator = new ComputerPasswordGenerator(); $generator->setRandomGenerator(new Php7RandomGenerator()) ->setUppercase(false) ->setLowercase(false) ->setUppercase(false) ->setNumbers() ->setSymbols(false) ->setLength(self::NAME_LENGTH); // getting names $names = $generator->generatePasswords(2); $database = 'db'.$names[0]; $username = 'u'.$names[1]; // getting option for ssl $ssl = $this->option('ssl'); $mysqli->query("CREATE DATABASE $database DEFAULT CHARACTER SET utf8 COLLATE utf8_general_ci"); if ($ssl === false) { $mysqli->query("CREATE USER $username@localhost IDENTIFIED BY '$password'"); } else { $mysqli->query("CREATE USER $username@'%' IDENTIFIED BY '$password'"); } if ($mysqli->error) { $this->error('Failed! '.$mysqli->error); $mysqli->query("DROP DATABASE IF EXISTS $database"); $mysqli->query("DROP USER $username@localhost"); $mysqli->query("DROP USER $username@'%'"); exit(); } $mysqli->query("GRANT ALL PRIVILEGES ON $database.* TO $username@localhost"); if ($ssl === true) { $mysqli->query("GRANT ALL PRIVILEGES ON $database.* TO $username@'%' require ssl"); } if ($mysqli->error) { $this->error('Failed! '.$mysqli->error); $mysqli->query("DROP DATABASE IF EXISTS $database"); $mysqli->query("DROP USER $username@localhost"); $mysqli->query("DROP USER $username@'%'"); exit(); } $mysqli->query("FLUSH PRIVILEGES"); $mysqli->close(); $this->info('Success! \o/ Check /root/mcp.log'); file_put_contents(self::MCP_LOG_FILE, "Mariadb $database created\nuser: $username\npassword: $password\n--\n", FILE_APPEND); } }