Skip to content

Commit

Permalink
Best guess at DB_HOST in case localhost doesn't work
Browse files Browse the repository at this point in the history
  • Loading branch information
edjeavons committed Sep 21, 2024
1 parent 0666cf8 commit 398dace
Showing 1 changed file with 12 additions and 0 deletions.
12 changes: 12 additions & 0 deletions includes/classes/DotEnv/Editor.php
Original file line number Diff line number Diff line change
Expand Up @@ -82,8 +82,20 @@ public function populate_dotenv( ConsoleIO $io ) {
$db_password = $io->ask( 'Database password: ' );
} while ( ! $db_password );

// Use localhost as a preference but switch to 127.0.0.1 if necessary
foreach ( [ 'localhost', '127.0.0.1' ] as $db_host ) {
try {
new \PDO( "mysql:dbname={$db_name};host={$db_host}", $db_user, $db_password, [ \PDO::ATTR_TIMEOUT => 1 ] );
break;
} catch ( \PDOException ) {
// Fail silently so the user can set their own special config manually in it's a niche setup
$db_host = 'localhost';
}
}

// Write settings to file
$this->write_dotenv_value( 'WP_HOME', $wp_home );
$this->write_dotenv_value( 'DB_HOST', $db_host );
$this->write_dotenv_value( 'DB_NAME', $db_name );
$this->write_dotenv_value( 'DB_USER', $db_user );
$this->write_dotenv_value( 'DB_PASSWORD', $db_password );
Expand Down

0 comments on commit 398dace

Please sign in to comment.