secret('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...'); // setting password for root, repeat until do { $password = $this->secret('New Password'); $passwordRepeat = $this->secret('Repeat Password'); if ($password !== $passwordRepeat) { $this->error('Password not equal! Try again!'); } if (empty($password)) { $this->error('Password is empty! Try again!'); } } while ($password !== $passwordRepeat || empty($password)); $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]; $this->info('Database: '.$database); $this->info('Username: '.$username); $mysqli->query("CREATE DATABASE $database DEFAULT CHARACTER SET utf8 COLLATE utf8_general_ci"); // create user for remote and local access $mysqli->query("CREATE USER $username@'localhost' IDENTIFIED BY '$password'"); $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(); } // grant privleges, for remote acces require ssl $mysqli->query("GRANT ALL PRIVILEGES ON $database.* TO $username@localhost"); $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/'); } }