Skip to content

Commit 976db32

Browse files
authored
Merge pull request #19326 from nextcloud/backport/19303/stable18
[stable18] Fix occ maintenance:install database connect failure
2 parents 8e35ea0 + 686686b commit 976db32

File tree

3 files changed

+38
-8
lines changed

3 files changed

+38
-8
lines changed

core/Command/Maintenance/Install.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,10 @@ protected function validateInput(InputInterface $input, OutputInterface $output,
134134
} else {
135135
$dbHost = $input->getOption('database-host');
136136
}
137+
if ($dbPort) {
138+
// Append the port to the host so it is the same as in the config (there is no dbport config)
139+
$dbHost .= ':' . $dbPort;
140+
}
137141
$dbTablePrefix = 'oc_';
138142
if ($input->hasParameterOption('--database-table-prefix')) {
139143
$dbTablePrefix = (string) $input->getOption('database-table-prefix');
@@ -183,7 +187,6 @@ protected function validateInput(InputInterface $input, OutputInterface $output,
183187
'dbpass' => $dbPass,
184188
'dbname' => $dbName,
185189
'dbhost' => $dbHost,
186-
'dbport' => $dbPort,
187190
'dbtableprefix' => $dbTablePrefix,
188191
'adminlogin' => $adminLogin,
189192
'adminpass' => $adminPassword,

lib/private/Setup.php

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -353,11 +353,9 @@ public function install($options) {
353353

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

356+
$dbSetup->initialize($options);
356357
try {
357-
$dbSetup->initialize($options);
358358
$dbSetup->setupDatabase($username);
359-
// apply necessary migrations
360-
$dbSetup->runMigrations();
361359
} catch (\OC\DatabaseSetupException $e) {
362360
$error[] = [
363361
'error' => $e->getMessage(),
@@ -371,6 +369,16 @@ public function install($options) {
371369
];
372370
return $error;
373371
}
372+
try {
373+
// apply necessary migrations
374+
$dbSetup->runMigrations();
375+
} catch (Exception $e) {
376+
$error[] = [
377+
'error' => 'Error while trying to initialise the database: ' . $e->getMessage(),
378+
'hint' => '',
379+
];
380+
return $error;
381+
}
374382

375383
//create the user and group
376384
$user = null;

lib/private/Setup/MySQL.php

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
use OC\DB\MySqlTools;
3535
use OCP\IDBConnection;
3636
use OCP\ILogger;
37+
use Doctrine\DBAL\Platforms\MySQL80Platform;
3738

3839
class MySQL extends AbstractDatabase {
3940
public $dbprettyname = 'MySQL/MariaDB';
@@ -57,6 +58,16 @@ public function setupDatabase($username) {
5758
//fill the database if needed
5859
$query='select count(*) from information_schema.tables where table_schema=? AND table_name = ?';
5960
$connection->executeQuery($query, [$this->dbName, $this->tablePrefix.'users']);
61+
62+
$connection->close();
63+
$connection = $this->connect();
64+
try {
65+
$connection->connect();
66+
} catch (\Exception $e) {
67+
$this->logger->logException($e);
68+
throw new \OC\DatabaseSetupException($this->trans->t('MySQL username and/or password not valid'),
69+
$this->trans->t('You need to enter details of an existing account.'));
70+
}
6071
}
6172

6273
/**
@@ -102,10 +113,18 @@ private function createDBUser($connection) {
102113
$password = $this->dbPassword;
103114
// we need to create 2 accounts, one for global use and one for local user. if we don't specify the local one,
104115
// the anonymous user would take precedence when there is one.
105-
$query = "CREATE USER '$name'@'localhost' IDENTIFIED WITH mysql_native_password BY '$password'";
106-
$connection->executeUpdate($query);
107-
$query = "CREATE USER '$name'@'%' IDENTIFIED WITH mysql_native_password BY '$password'";
108-
$connection->executeUpdate($query);
116+
117+
if ($connection->getDatabasePlatform() instanceof Mysql80Platform) {
118+
$query = "CREATE USER '$name'@'localhost' IDENTIFIED WITH mysql_native_password BY '$password'";
119+
$connection->executeUpdate($query);
120+
$query = "CREATE USER '$name'@'%' IDENTIFIED WITH mysql_native_password BY '$password'";
121+
$connection->executeUpdate($query);
122+
} else {
123+
$query = "CREATE USER '$name'@'localhost' IDENTIFIED BY '$password'";
124+
$connection->executeUpdate($query);
125+
$query = "CREATE USER '$name'@'%' IDENTIFIED BY '$password'";
126+
$connection->executeUpdate($query);
127+
}
109128
}
110129
catch (\Exception $ex){
111130
$this->logger->logException($ex, [

0 commit comments

Comments
 (0)