I'm trying to create a database if it does not exist and set privledges but I'm having trouble getting it to work, I keep getting You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci' at line 1
I've included the various ways I've tried to execute the commands
Yaml:
initContainers:
- name: database-setup
image: mysql:5.7
imagePullPolicy: IfNotPresent
# command: ["rm", "-fr", "/var/lib/mysql/lost+found"]
# SQL Syntax error:
command: [sh,-c,mysql -h mysql-service -p$MYSQL_ROOT_PASSWORD -e "CREATE DATABASE IF NOT EXISTS `$WORDPRESS_DB_NAME` DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci; GRANT ALL PRIVILEGES ON $WORDPRESS_DB_NAME.* TO '$WORDPRESS_DB_USER'@'%' IDENTIFIED BY '$WORDPRESS_DB_PASSWORD'; FLUSH PRIVILEGES;"]
# Enviroment Variables arn't passed into mysql shell:
# - sh
# - -c
# - mysql -h mysql-service -p$MYSQL_ROT_PASSWORD
# - CREATE DATABASE IF NOT EXISTS $WORDPRESS_DB_NAME DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;
# - GRANT ALL PRIVILEGES ON $WORDPRESS_DB_NAME.* TO '$WORDPRESS_DB_USER'@'%' IDENTIFIED BY '$WORDPRESS_DB_PASSWORD';
# - FLUSH PRIVILEGES;
# - EXIT;
# SQL Syntax error:
# - sh
# - -c
# - mysql -h mysql-service -p$MYSQL_ROOT_PASSWORD -e "CREATE DATABASE IF NOT EXISTS `$WORDPRESS_DB_NAME` DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci; GRANT ALL PRIVILEGES ON $WORDPRESS_DB_NAME.* TO '$WORDPRESS_DB_USER'@'%' IDENTIFIED BY '$WORDPRESS_DB_PASSWORD'; FLUSH PRIVILEGES;
env:
- name: WORDPRESS_DB_NAME
value: wordpress-green
- name: WORDPRESS_DB_USER
value: wordpress-green
- name: WORDPRESS_DB_PASSWORD
valueFrom:
secretKeyRef:
name: wordpress-mysql-pass
key: wordpress-mysql-pass
- name: MYSQL_ROOT_PASSWORD
valueFrom:
secretKeyRef:
name: mysql-root-pass
key: mysql-root-pass
tried doing an echo
for the MySQL line to debug the variables returned:
mysql -h mysql-service -p\\REDACTED\\ -e CREATE DATABASE IF NOT EXISTS DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci; GRANT ALL PRIVILEGES ON wordpress-green.* TO 'wordpress-green'@'%' IDENTIFIED BY '\\REDACTED\\'; FLUSH PRIVILEGES;
I fixed the $WORDPRESS_DB_NAME not being passed to the create DB sting by removing the `` - this caused SQL Syntax error again because it does not like a dash in the database name. Instead I just used camelCase for the database name
Thanks for the help @Tim Biegeleisen