Browse code

Merge "Add possibility to configure manually MYSQL_SERVICE_NAME"

Zuul authored on 2019/12/07 10:02:54
Showing 1 changed files
... ...
@@ -15,15 +15,17 @@ MYSQL_DRIVER=${MYSQL_DRIVER:-PyMySQL}
15 15
 
16 16
 register_database mysql
17 17
 
18
-MYSQL_SERVICE_NAME=mysql
19
-if is_fedora && ! is_oraclelinux; then
20
-    MYSQL_SERVICE_NAME=mariadb
21
-elif is_suse && systemctl list-unit-files | grep -q 'mariadb\.service'; then
22
-    # Older mariadb packages on SLES 12 provided mysql.service.  The
23
-    # newer ones on SLES 12 and 15 use mariadb.service; they also
24
-    # provide a mysql.service symlink for backwards-compatibility, but
25
-    # let's not rely on that.
26
-    MYSQL_SERVICE_NAME=mariadb
18
+if [[ -z "$MYSQL_SERVICE_NAME" ]]; then
19
+    MYSQL_SERVICE_NAME=mysql
20
+    if is_fedora && ! is_oraclelinux; then
21
+        MYSQL_SERVICE_NAME=mariadb
22
+    elif is_suse && systemctl list-unit-files | grep -q 'mariadb\.service'; then
23
+        # Older mariadb packages on SLES 12 provided mysql.service.  The
24
+        # newer ones on SLES 12 and 15 use mariadb.service; they also
25
+        # provide a mysql.service symlink for backwards-compatibility, but
26
+        # let's not rely on that.
27
+        MYSQL_SERVICE_NAME=mariadb
28
+    fi
27 29
 fi
28 30
 
29 31
 # Functions
... ...
@@ -92,8 +94,23 @@ function configure_database_mysql {
92 92
     # because the package might have been installed already.
93 93
     sudo mysqladmin -u root password $DATABASE_PASSWORD || true
94 94
 
95
+    # In case of Mariadb, giving hostname in arguments causes permission
96
+    # problems as it expects connection through socket
97
+    if is_ubuntu && [ "$MYSQL_SERVICE_NAME" == "mariadb" ]; then
98
+        local cmd_args="-uroot -p$DATABASE_PASSWORD "
99
+    else
100
+        local cmd_args="-uroot -p$DATABASE_PASSWORD -h127.0.0.1 "
101
+    fi
102
+
103
+    # In mariadb e.g. on Ubuntu socket plugin is used for authentication
104
+    # as root so it works only as sudo. To restore old "mysql like" behaviour,
105
+    # we need to change auth plugin for root user
106
+    if [ "$MYSQL_SERVICE_NAME" == "mariadb" ]; then
107
+        sudo mysql $cmd_args -e "UPDATE mysql.user SET plugin='' WHERE user='$DATABASE_USER' AND host='localhost';"
108
+        sudo mysql $cmd_args -e "FLUSH PRIVILEGES;"
109
+    fi
95 110
     # Update the DB to give user '$DATABASE_USER'@'%' full control of the all databases:
96
-    sudo mysql -uroot -p$DATABASE_PASSWORD -h127.0.0.1 -e "GRANT ALL PRIVILEGES ON *.* TO '$DATABASE_USER'@'%' identified by '$DATABASE_PASSWORD';"
111
+    sudo mysql $cmd_args -e "GRANT ALL PRIVILEGES ON *.* TO '$DATABASE_USER'@'%' identified by '$DATABASE_PASSWORD';"
97 112
 
98 113
     # Now update ``my.cnf`` for some local needs and restart the mysql service
99 114
 
... ...
@@ -148,8 +165,11 @@ MYSQL_PRESEED
148 148
 [client]
149 149
 user=$DATABASE_USER
150 150
 password=$DATABASE_PASSWORD
151
-host=$MYSQL_HOST
152 151
 EOF
152
+
153
+        if ! is_ubuntu || [ "$MYSQL_SERVICE_NAME" != "mariadb" ]; then
154
+            echo "host=$MYSQL_HOST" >> $HOME/.my.cnf
155
+        fi
153 156
         chmod 0600 $HOME/.my.cnf
154 157
     fi
155 158
     # Install mysql-server
... ...
@@ -159,7 +179,7 @@ EOF
159 159
         install_package mariadb-server
160 160
         sudo systemctl enable $MYSQL_SERVICE_NAME
161 161
     elif is_ubuntu; then
162
-        install_package mysql-server
162
+        install_package $MYSQL_SERVICE_NAME-server
163 163
     else
164 164
         exit_distro_not_supported "mysql installation"
165 165
     fi