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.

78 lines
1.9 KiB

4 years ago
4 years ago
4 years ago
4 years ago
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. * Remove database
  11. *
  12. * @author Björn Hase, Tentakelfabrik
  13. * @license http://opensource.org/licenses/MIT The MIT License
  14. * @link https://gitea.tentakelfabrik.de/Tentakelfabrik/mcp
  15. *
  16. */
  17. class MariadbRemoveCommand extends Command
  18. {
  19. /**
  20. * The signature of the command.
  21. *
  22. * @var string
  23. */
  24. protected $signature = 'mariadb:remove {database}';
  25. /**
  26. * The description of the command.
  27. *
  28. * @var string
  29. */
  30. protected $description = 'Remove Mariadb Database and User';
  31. /**
  32. * Execute the console command.
  33. *
  34. * @return mixed
  35. */
  36. public function handle()
  37. {
  38. $this->info('Mariadb Remove Database...');
  39. // getting root password
  40. $password = $this->secret('Enter root password');
  41. // connect database
  42. try {
  43. $mysqli = new \mysqli('127.0.0.1', 'root', $password);
  44. } catch(\ErrorException $exception) {
  45. $this->error('Failed! '.$exception->getMessage());
  46. exit();
  47. }
  48. $database = $this->argument('database');
  49. // check for user of database
  50. $result = $mysqli->query("SELECT User FROM mysql.db WHERE Db = '$database'");
  51. while ($row = $result->fetch_assoc()) {
  52. // get user from row
  53. $user = $row['User'];
  54. // remove if exists
  55. $mysqli->query("DROP USER IF EXISTS $user@localhost");
  56. $mysqli->query("DROP USER IS EXISTS $user@'%'");
  57. }
  58. // drop database
  59. $mysqli->query("DROP DATABASE IF EXISTS $database");
  60. $mysqli->close();
  61. $this->info('Success! \o/ ');
  62. }
  63. }