gpt4 book ai didi

mysql - 使用环境变量执行 MySQL init 命令来创建数据库

转载 作者:行者123 更新时间:2023-11-29 03:18:22 24 4
gpt4 key购买 nike

我正在尝试创建一个数据库(如果它不存在)并设置权限,但我无法让它工作,我不断收到 你的 SQL 语法有错误;检查与您的 MySQL 服务器版本对应的手册,了解在第 1 行的“DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci”附近使用的正确语法

我已经包含了我尝试执行命令的各种方式

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

最佳答案

尝试为 MySQL 行做一个 echo 来调试变量返回:

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;

我通过删除 `` 修复了 $WORDPRESS_DB_NAME 没有被传递到创建数据库字符串的问题 - 这再次导致 SQL 语法错误,因为它不喜欢数据库名称中的破折号。相反,我只是使用 camelCase 作为数据库名称

感谢@Tim Biegeleisen 的帮助

关于mysql - 使用环境变量执行 MySQL init 命令来创建数据库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50402654/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com