Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix occ maintenance:install database connect failure #19303

Merged
merged 4 commits into from
Feb 6, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion core/Command/Maintenance/Install.php
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,10 @@ protected function validateInput(InputInterface $input, OutputInterface $output,
} else {
$dbHost = $input->getOption('database-host');
}
if ($dbPort) {
// Append the port to the host so it is the same as in the config (there is no dbport config)
$dbHost .= ':' . $dbPort;
}
$dbTablePrefix = 'oc_';
if ($input->hasParameterOption('--database-table-prefix')) {
$dbTablePrefix = (string) $input->getOption('database-table-prefix');
Expand Down Expand Up @@ -183,7 +187,6 @@ protected function validateInput(InputInterface $input, OutputInterface $output,
'dbpass' => $dbPass,
'dbname' => $dbName,
'dbhost' => $dbHost,
'dbport' => $dbPort,
'dbtableprefix' => $dbTablePrefix,
'adminlogin' => $adminLogin,
'adminpass' => $adminPassword,
Expand Down
14 changes: 11 additions & 3 deletions lib/private/Setup.php
Original file line number Diff line number Diff line change
Expand Up @@ -353,11 +353,9 @@ public function install($options) {

$this->config->setValues($newConfigValues);

$dbSetup->initialize($options);
try {
$dbSetup->initialize($options);
$dbSetup->setupDatabase($username);
// apply necessary migrations
$dbSetup->runMigrations();
} catch (\OC\DatabaseSetupException $e) {
$error[] = [
'error' => $e->getMessage(),
Expand All @@ -371,6 +369,16 @@ public function install($options) {
];
return $error;
}
try {
// apply necessary migrations
$dbSetup->runMigrations();
} catch (Exception $e) {
$error[] = [
'error' => 'Error while trying to initialise the database: ' . $e->getMessage(),
'hint' => '',
];
return $error;
}

//create the user and group
$user = null;
Expand Down
27 changes: 23 additions & 4 deletions lib/private/Setup/MySQL.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
use OC\DB\MySqlTools;
use OCP\IDBConnection;
use OCP\ILogger;
use Doctrine\DBAL\Platforms\MySQL80Platform;

class MySQL extends AbstractDatabase {
public $dbprettyname = 'MySQL/MariaDB';
Expand All @@ -57,6 +58,16 @@ public function setupDatabase($username) {
//fill the database if needed
$query='select count(*) from information_schema.tables where table_schema=? AND table_name = ?';
$connection->executeQuery($query, [$this->dbName, $this->tablePrefix.'users']);

$connection->close();
$connection = $this->connect();
try {
$connection->connect();
} catch (\Exception $e) {
$this->logger->logException($e);
throw new \OC\DatabaseSetupException($this->trans->t('MySQL username and/or password not valid'),
$this->trans->t('You need to enter details of an existing account.'));
}
}

/**
Expand Down Expand Up @@ -102,10 +113,18 @@ private function createDBUser($connection) {
$password = $this->dbPassword;
// we need to create 2 accounts, one for global use and one for local user. if we don't specify the local one,
// the anonymous user would take precedence when there is one.
$query = "CREATE USER '$name'@'localhost' IDENTIFIED WITH mysql_native_password BY '$password'";
$connection->executeUpdate($query);
$query = "CREATE USER '$name'@'%' IDENTIFIED WITH mysql_native_password BY '$password'";
$connection->executeUpdate($query);

if ($connection->getDatabasePlatform() instanceof Mysql80Platform) {
$query = "CREATE USER '$name'@'localhost' IDENTIFIED WITH mysql_native_password BY '$password'";
$connection->executeUpdate($query);
$query = "CREATE USER '$name'@'%' IDENTIFIED WITH mysql_native_password BY '$password'";
$connection->executeUpdate($query);
} else {
$query = "CREATE USER '$name'@'localhost' IDENTIFIED BY '$password'";
$connection->executeUpdate($query);
$query = "CREATE USER '$name'@'%' IDENTIFIED BY '$password'";
$connection->executeUpdate($query);
}
}
catch (\Exception $ex){
$this->logger->logException($ex, [
Expand Down