Skip to content

Commit 1a13adb

Browse files
author
Till Brehm
committed
Merge branch '5813-improve-mysql-database-plugin-code-for-mariadb-10-4-compatibility' into 'develop'
Resolve "Improve mysql database plugin code for MariaDB 10.4 compatibility" Closes #5813 See merge request ispconfig/ispconfig3!1254
2 parents a65b051 + 21b1c30 commit 1a13adb

File tree

1 file changed

+31
-20
lines changed

1 file changed

+31
-20
lines changed

server/plugins-available/mysql_clientdb_plugin.inc.php

Lines changed: 31 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -150,16 +150,21 @@ function process_host_list($action, $database_name, $database_user, $database_pa
150150
$link->query("CREATE USER '".$link->escape_string($database_user)."'@'$db_host'");
151151
$app->log("CREATE USER '".$link->escape_string($database_user)."'@'$db_host'", LOGLEVEL_DEBUG);
152152

153-
// set the password
154-
// MySQL < 5.7 and MariadB 10
155-
if(!$link->query("UPDATE mysql.user SET `Password` = '".$link->escape_string($database_password)."' WHERE `Host` = '".$db_host."' AND `User` = '".$link->escape_string($database_user)."'")) {
156-
if($this->getDatabaseType($link) == 'mysql' && $this->getDatabaseVersion($link, true) >= 8) {
157-
// for MySQL >= 8, we set authentication plugin to old mode to ensure that older additional php versions can still connect to the database
158-
if(!$link->query("UPDATE mysql.user SET `authentication_string` = '".$link->escape_string($database_password)."', `plugin` = 'mysql_native_password' WHERE `Host` = '".$db_host."' AND `User` = '".$link->escape_string($database_user)."'")) $success = false;
159-
} else {
160-
// MySQL 5.7, the Password field has been renamed to authentication_string
161-
if(!$link->query("UPDATE mysql.user SET `authentication_string` = '".$link->escape_string($database_password)."' WHERE `Host` = '".$db_host."' AND `User` = '".$link->escape_string($database_user)."'")) $success = false;
162-
}
153+
// mariadb or mysql < 5.7
154+
if($this->getDatabaseType($link) == 'mariadb' || version_compare($this->getDatabaseVersion($link), '5.7', '<')) {
155+
$query = sprintf("SET PASSWORD FOR '%s'@'%s' = '%s'",
156+
$link->escape_string($database_user),
157+
$db_host,
158+
$link->escape_string($database_password));
159+
if(!$link->query($query)) $success = false;
160+
}
161+
// mysql >= 5.7
162+
else {
163+
$query = sprintf("ALTER USER IF EXISTS '%s'@'%s' IDENTIFIED WITH mysql_native_password AS '%s'",
164+
$link->escape_string($database_user),
165+
$db_host,
166+
$link->escape_string($database_password));
167+
if(!$link->query($query)) $success = false;
163168
}
164169

165170
$app->log("PASSWORD SET FOR '".$link->escape_string($database_user)."'@'$db_host' success? " . ($success ? 'yes' : 'no'), LOGLEVEL_DEBUG);
@@ -182,15 +187,21 @@ function process_host_list($action, $database_name, $database_user, $database_pa
182187
//if(!$link->query("SET PASSWORD FOR '".$link->escape_string($database_user)."'@'$db_host' = '".$link->escape_string($database_password)."'")) $success = false;
183188
// SET PASSWORD for already hashed passwords is not supported by latest MySQL 5.7 anymore, so we have to set the hashed password directly
184189
if(trim($database_password) != '') {
185-
// MySQL < 5.7 and MariadB 10
186-
if(!$link->query("UPDATE mysql.user SET `Password` = '".$link->escape_string($database_password)."' WHERE `Host` = '".$db_host."' AND `User` = '".$link->escape_string($database_user)."'")) {
187-
if($this->getDatabaseType($link) == 'mysql' && $this->getDatabaseVersion($link, true) >= 8) {
188-
// for MySQL >= 8, we set authentication plugin to old mode to ensure that older additional php versions can still connect to the database
189-
if(!$link->query("UPDATE mysql.user SET `authentication_string` = '".$link->escape_string($database_password)."', `plugin` = 'mysql_native_password' WHERE `Host` = '".$db_host."' AND `User` = '".$link->escape_string($database_user)."'")) $success = false;
190-
} else {
191-
// MySQL 5.7, the Password field has been renamed to authentication_string
192-
if(!$link->query("UPDATE mysql.user SET `authentication_string` = '".$link->escape_string($database_password)."' WHERE `Host` = '".$db_host."' AND `User` = '".$link->escape_string($database_user)."'")) $success = false;
193-
}
190+
// mariadb or mysql < 5.7
191+
if($this->getDatabaseType($link) == 'mariadb' || version_compare($this->getDatabaseVersion($link), '5.7', '<')) {
192+
$query = sprintf("SET PASSWORD FOR '%s'@'%s' = '%s'",
193+
$link->escape_string($database_user),
194+
$db_host,
195+
$link->escape_string($database_password));
196+
if(!$link->query($query)) $success = false;
197+
}
198+
// mysql >= 5.7
199+
else {
200+
$query = sprintf("ALTER USER IF EXISTS '%s'@'%s' IDENTIFIED WITH mysql_native_password AS '%s'",
201+
$link->escape_string($database_user),
202+
$db_host,
203+
$link->escape_string($database_password));
204+
if(!$link->query($query)) $success = false;
194205
}
195206
if($success == true) $link->query("FLUSH PRIVILEGES");
196207
}
@@ -836,4 +847,4 @@ function getDatabaseVersion($link, $major_version_only = false) {
836847

837848
} // end class
838849

839-
?>
850+
?>

0 commit comments

Comments
 (0)