Path: blob/1.0-develop/app/Console/Commands/Environment/DatabaseSettingsCommand.php
7461 views
<?php12namespace Pterodactyl\Console\Commands\Environment;34use Illuminate\Console\Command;5use Illuminate\Contracts\Console\Kernel;6use Illuminate\Database\DatabaseManager;7use Pterodactyl\Traits\Commands\EnvironmentWriterTrait;89class DatabaseSettingsCommand extends Command10{11use EnvironmentWriterTrait;1213protected $description = 'Configure database settings for the Panel.';1415protected $signature = 'p:environment:database16{--host= : The connection address for the MySQL server.}17{--port= : The connection port for the MySQL server.}18{--database= : The database to use.}19{--username= : Username to use when connecting.}20{--password= : Password to use for this database.}';2122protected array $variables = [];2324/**25* DatabaseSettingsCommand constructor.26*/27public function __construct(private DatabaseManager $database, private Kernel $console)28{29parent::__construct();30}3132/**33* Handle command execution.34*35* @throws \Pterodactyl\Exceptions\PterodactylException36*/37public function handle(): int38{39$this->output->note('It is highly recommended to not use "localhost" as your database host as we have seen frequent socket connection issues. If you want to use a local connection you should be using "127.0.0.1".');40$this->variables['DB_HOST'] = $this->option('host') ?? $this->ask(41'Database Host',42config('database.connections.mysql.host', '127.0.0.1')43);4445$this->variables['DB_PORT'] = $this->option('port') ?? $this->ask(46'Database Port',47config('database.connections.mysql.port', 3306)48);4950$this->variables['DB_DATABASE'] = $this->option('database') ?? $this->ask(51'Database Name',52config('database.connections.mysql.database', 'panel')53);5455$this->output->note('Using the "root" account for MySQL connections is not only highly frowned upon, it is also not allowed by this application. You\'ll need to have created a MySQL user for this software.');56$this->variables['DB_USERNAME'] = $this->option('username') ?? $this->ask(57'Database Username',58config('database.connections.mysql.username', 'pterodactyl')59);6061$askForMySQLPassword = true;62if (!empty(config('database.connections.mysql.password')) && $this->input->isInteractive()) {63$this->variables['DB_PASSWORD'] = config('database.connections.mysql.password');64$askForMySQLPassword = $this->confirm('It appears you already have a MySQL connection password defined, would you like to change it?');65}6667if ($askForMySQLPassword) {68$this->variables['DB_PASSWORD'] = $this->option('password') ?? $this->secret('Database Password');69}7071try {72$this->testMySQLConnection();73} catch (\PDOException $exception) {74$this->output->error(sprintf('Unable to connect to the MySQL server using the provided credentials. The error returned was "%s".', $exception->getMessage()));75$this->output->error('Your connection credentials have NOT been saved. You will need to provide valid connection information before proceeding.');7677if ($this->confirm('Go back and try again?')) {78$this->database->disconnect('_pterodactyl_command_test');7980return $this->handle();81}8283return 1;84}8586$this->writeToEnvironment($this->variables);8788$this->info($this->console->output());8990return 0;91}9293/**94* Test that we can connect to the provided MySQL instance and perform a selection.95*/96private function testMySQLConnection()97{98config()->set('database.connections._pterodactyl_command_test', [99'driver' => 'mysql',100'host' => $this->variables['DB_HOST'],101'port' => $this->variables['DB_PORT'],102'database' => $this->variables['DB_DATABASE'],103'username' => $this->variables['DB_USERNAME'],104'password' => $this->variables['DB_PASSWORD'],105'charset' => 'utf8mb4',106'collation' => 'utf8mb4_unicode_ci',107'strict' => true,108]);109110$this->database->connection('_pterodactyl_command_test')->getPdo();111}112}113114115