@@ -121,37 +121,42 @@ case "$1" in
121121 # Data base
122122 # ----------------------------------------------------------------
123123 if mysql_available; then
124- # Take password from config if set (package update)
125- EXISTING_PASS=" $( json_get ' .AdminHelperDB.Password' ) "
124+ if mysql -u root -e " SELECT 1" > /dev/null 2>&1 ; then
125+ # Take password from config if set (package update)
126+ EXISTING_PASS=" $( json_get ' .AdminHelperDB.Password' ) "
126127
127- if [ -z " ${EXISTING_PASS} " ]; then
128- DB_PASS=" $( openssl rand -base64 16 | tr -d ' /+=' | head -c 20) "
129- echo " ==> Generated new database password."
130- else
131- DB_PASS=" ${EXISTING_PASS} "
132- echo " ==> Using existing database password from config."
133- fi
128+ if [ -z " ${EXISTING_PASS} " ]; then
129+ DB_PASS=" $( openssl rand -base64 16 | tr -d ' /+=' | head -c 20) "
130+ echo " ==> Generated new database password."
131+ else
132+ DB_PASS=" ${EXISTING_PASS} "
133+ echo " ==> Using existing database password from config."
134+ fi
134135
135- echo " ==> Configuring MySQL/MariaDB for ${DB_NAME} ..."
136+ echo " ==> Configuring MySQL/MariaDB for ${DB_NAME} ..."
136137
137- mysql -u root << SQL
138+ mysql -u root << SQL
138139CREATE DATABASE IF NOT EXISTS \` ${DB_NAME} \` CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;
139140CREATE USER IF NOT EXISTS '${DB_USER} '@'localhost' IDENTIFIED BY '${DB_PASS} ';
140141GRANT ALL PRIVILEGES ON \` ${DB_NAME} \` .* TO '${DB_USER} '@'localhost';
141142FLUSH PRIVILEGES;
142143SQL
143144
144- # Rolling out the scheme (IF NOT EXISTS — safe when updating)
145- mysql -u root " ${DB_NAME} " < " ${SCHEMA_FILE} "
145+ # Rolling out the scheme (IF NOT EXISTS — safe when updating)
146+ mysql -u root " ${DB_NAME} " < " ${SCHEMA_FILE} "
146147
147- # Write credentials to tgadmin.json
148- json_set \
149- --arg user " ${DB_USER} " \
150- --arg pass " ${DB_PASS} " \
151- --arg db " ${DB_NAME} " \
152- ' .AdminHelperDB.User = $user | .AdminHelperDB.Password = $pass | .AdminHelperDB.Database = $db'
148+ # Write credentials to tgadmin.json
149+ json_set \
150+ --arg user " ${DB_USER} " \
151+ --arg pass " ${DB_PASS} " \
152+ --arg db " ${DB_NAME} " \
153+ ' .AdminHelperDB.User = $user | .AdminHelperDB.Password = $pass | .AdminHelperDB.Database = $db'
153154
154- echo " ==> Database configured. Credentials saved to ${CONFIG_FILE} "
155+ echo " ==> Database configured. Credentials saved to ${CONFIG_FILE} "
156+ else
157+ echo " WARNING: MySQL/MariaDB is running, but root access without password is denied." >&2
158+ echo " Database bootstrap skipped. Configure DB manually and rerun: sudo dpkg-reconfigure ${PACKAGE_NAME} " >&2
159+ fi
155160 else
156161 echo " WARNING: MySQL/MariaDB not found or not running." >&2
157162 echo " Install it, then run: sudo dpkg-reconfigure ${PACKAGE_NAME} " >&2
0 commit comments