OpenSource CLI-App to install and handle stuff related to Web-Server
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

75 lines
1.8 KiB

4 years ago
  1. <?php
  2. namespace App\Commands;
  3. use Illuminate\Console\Scheduling\Schedule;
  4. use LaravelZero\Framework\Commands\Command;
  5. use Illuminate\Support\Facades\File;
  6. use App\Facades\Install;
  7. use Hackzilla\PasswordGenerator\Generator\ComputerPasswordGenerator;
  8. use Hackzilla\PasswordGenerator\RandomGenerator\Php7RandomGenerator;
  9. /**
  10. *
  11. *
  12. *
  13. */
  14. class MariadbRemoveCommand extends Command
  15. {
  16. /**
  17. * The signature of the command.
  18. *
  19. * @var string
  20. */
  21. protected $signature = 'mariadb:remove {database}';
  22. /**
  23. * The description of the command.
  24. *
  25. * @var string
  26. */
  27. protected $description = 'Remove Mariadb Database and User';
  28. /**
  29. * Execute the console command.
  30. *
  31. * @return mixed
  32. */
  33. public function handle()
  34. {
  35. $this->info('Mariadb Remove Database...');
  36. // getting root password
  37. $password = $this->secret('Enter root password');
  38. // connect database
  39. try {
  40. $mysqli = new \mysqli('127.0.0.1', 'root', $password);
  41. } catch(\ErrorException $exception) {
  42. $this->error('Failed! '.$exception->getMessage());
  43. exit();
  44. }
  45. $database = $this->argument('database');
  46. // check for user of database
  47. $result = $mysqli->query("SELECT User FROM mysql.db WHERE Db = '$database'");
  48. while ($row = $result->fetch_assoc()) {
  49. // get user from row
  50. $user = $row['User'];
  51. // remove if exists
  52. $mysqli->query("DROP USER IF EXISTS $user@localhost");
  53. $mysqli->query("DROP USER IS EXISTS $user@'%'");
  54. }
  55. // drop database
  56. $mysqli->query("DROP DATABASE IF EXISTS $database");
  57. $mysqli->close();
  58. $this->info('Success! \o/ ');
  59. }
  60. }