Skip to content

Commit b814f3b

Browse files
committed
fix(db): use caching_sha2_password for MySQL
`caching_sha2_password` was added in 8.0.4 as the default authentication plugin. `mysql_native_password` is deprecated since then. In MySQL 8.4 it was disabled by default so a user need to manually reenable it to make it work. In MySQL 9.0 it is removed and causes the following error: > SQLSTATE[HY000] [1524] Plugin 'mysql_native_password' is not loaded Signed-off-by: Ferdinand Thiessen <[email protected]>
1 parent db8dd9f commit b814f3b

1 file changed

Lines changed: 20 additions & 7 deletions

File tree

lib/private/Setup/MySQL.php

Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
namespace OC\Setup;
99

1010
use Doctrine\DBAL\Platforms\MySQL80Platform;
11+
use Doctrine\DBAL\Platforms\MySQL84Platform;
1112
use OC\DB\ConnectionAdapter;
1213
use OC\DB\MySqlTools;
1314
use OCP\IDBConnection;
@@ -92,22 +93,29 @@ private function createDatabase($connection): void {
9293
* @throws \OC\DatabaseSetupException
9394
*/
9495
private function createDBUser($connection): void {
96+
$name = $this->dbUser;
97+
$password = $this->dbPassword;
98+
9599
try {
96-
$name = $this->dbUser;
97-
$password = $this->dbPassword;
98100
// we need to create 2 accounts, one for global use and one for local user. if we don't specify the local one,
99101
// the anonymous user would take precedence when there is one.
100102

101-
if ($connection->getDatabasePlatform() instanceof Mysql80Platform) {
103+
if ($connection->getDatabasePlatform() instanceof MySQL84Platform) {
104+
$query = "CREATE USER ?@'localhost' IDENTIFIED WITH caching_sha2_password BY ?";
105+
$connection->executeStatement($query, [$name,$password]);
106+
$query = "CREATE USER ?@'%' IDENTIFIED WITH caching_sha2_password BY ?";
107+
$connection->executeStatement($query, [$name,$password]);
108+
} elseif ($connection->getDatabasePlatform() instanceof Mysql80Platform) {
109+
// TODO: Remove this elseif section as soon as MySQL 8.0 is out-of-support (after April 2026)
102110
$query = "CREATE USER ?@'localhost' IDENTIFIED WITH mysql_native_password BY ?";
103-
$connection->executeUpdate($query, [$name,$password]);
111+
$connection->executeStatement($query, [$name,$password]);
104112
$query = "CREATE USER ?@'%' IDENTIFIED WITH mysql_native_password BY ?";
105-
$connection->executeUpdate($query, [$name,$password]);
113+
$connection->executeStatement($query, [$name,$password]);
106114
} else {
107115
$query = "CREATE USER ?@'localhost' IDENTIFIED BY ?";
108-
$connection->executeUpdate($query, [$name,$password]);
116+
$connection->executeStatement($query, [$name,$password]);
109117
$query = "CREATE USER ?@'%' IDENTIFIED BY ?";
110-
$connection->executeUpdate($query, [$name,$password]);
118+
$connection->executeStatement($query, [$name,$password]);
111119
}
112120
} catch (\Exception $ex) {
113121
$this->logger->error('Database user creation failed.', [
@@ -158,6 +166,11 @@ private function createSpecificUser($username, $connection): void {
158166
//use the admin login data for the new database user
159167
$this->dbUser = $adminUser;
160168
$this->createDBUser($connection);
169+
// if sharding is used we need to manually call this for every shard as those also need the user setup!
170+
/** @var ConnectionAdapter $connection */
171+
foreach ($connection->getInner()->getShardConnections() as $shard) {
172+
$this->createDBUser($shard);
173+
}
161174

162175
break;
163176
} else {

0 commit comments

Comments
 (0)