Skip to content

Commit 0a40648

Browse files
author
Miguel Lavalle
committed
Fix the db user for mariadb in ubuntu 22.04
In Ubuntu 22.04, mariadb version 10.6 is installed. Per [0] and [1] authentication management was changed in version 10.4. This change adapts the way the db user is created to the new rules in versions 10.4 and later. [0] https://mariadb.com/kb/en/authentication-from-mariadb-104/ [1] https://mariadb.org/authentication-in-mariadb-10-4/ Closes-Bug: #1999090 Change-Id: I77a699a9e191eb83628ad5d361282e66744b6e4a
1 parent fd502fe commit 0a40648

1 file changed

Lines changed: 21 additions & 8 deletions

File tree

lib/databases/mysql

Lines changed: 21 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -100,8 +100,13 @@ function configure_database_mysql {
100100

101101
# Set the root password - only works the first time. For Ubuntu, we already
102102
# did that with debconf before installing the package, but we still try,
103-
# because the package might have been installed already.
104-
sudo mysqladmin -u root password $DATABASE_PASSWORD || true
103+
# because the package might have been installed already. We don't do this
104+
# for Ubuntu 22.04 (jammy) because the authorization model change in
105+
# version 10.4 of mariadb. See
106+
# https://mariadb.org/authentication-in-mariadb-10-4/
107+
if ! (is_ubuntu && [[ "$DISTRO" == "jammy" ]] && [ "$MYSQL_SERVICE_NAME" == "mariadb" ]); then
108+
sudo mysqladmin -u root password $DATABASE_PASSWORD || true
109+
fi
105110

106111
# In case of Mariadb, giving hostname in arguments causes permission
107112
# problems as it expects connection through socket
@@ -115,13 +120,21 @@ function configure_database_mysql {
115120
# as root so it works only as sudo. To restore old "mysql like" behaviour,
116121
# we need to change auth plugin for root user
117122
if is_ubuntu && [[ "$DISTRO" != "bullseye" ]] && [ "$MYSQL_SERVICE_NAME" == "mariadb" ]; then
118-
sudo mysql $cmd_args -e "UPDATE mysql.user SET plugin='' WHERE user='$DATABASE_USER' AND host='localhost';"
119-
sudo mysql $cmd_args -e "FLUSH PRIVILEGES;"
123+
if [[ "$DISTRO" == "jammy" ]]; then
124+
# For Ubuntu 22.04 (jammy) we follow the model outlined in
125+
# https://mariadb.org/authentication-in-mariadb-10-4/
126+
sudo mysql -e "ALTER USER $DATABASE_USER@localhost IDENTIFIED VIA mysql_native_password USING PASSWORD('$DATABASE_PASSWORD');"
127+
else
128+
sudo mysql $cmd_args -e "UPDATE mysql.user SET plugin='' WHERE user='$DATABASE_USER' AND host='localhost';"
129+
sudo mysql $cmd_args -e "FLUSH PRIVILEGES;"
130+
fi
131+
fi
132+
if ! (is_ubuntu && [[ "$DISTRO" == "jammy" ]] && [ "$MYSQL_SERVICE_NAME" == "mariadb" ]); then
133+
# Create DB user if it does not already exist
134+
sudo mysql $cmd_args -e "CREATE USER IF NOT EXISTS '$DATABASE_USER'@'%' identified by '$DATABASE_PASSWORD';"
135+
# Update the DB to give user '$DATABASE_USER'@'%' full control of the all databases:
136+
sudo mysql $cmd_args -e "GRANT ALL PRIVILEGES ON *.* TO '$DATABASE_USER'@'%';"
120137
fi
121-
# Create DB user if it does not already exist
122-
sudo mysql $cmd_args -e "CREATE USER IF NOT EXISTS '$DATABASE_USER'@'%' identified by '$DATABASE_PASSWORD';"
123-
# Update the DB to give user '$DATABASE_USER'@'%' full control of the all databases:
124-
sudo mysql $cmd_args -e "GRANT ALL PRIVILEGES ON *.* TO '$DATABASE_USER'@'%';"
125138

126139
# Now update ``my.cnf`` for some local needs and restart the mysql service
127140

0 commit comments

Comments
 (0)