Google 云 SQL 和 sql_mode 标志

Google Cloud SQL and sql_mode flag

我正在尝试运行以下命令

gcloud beta sql instances create my_replica \
--master-instance-name=db-master01 \
--master-username=replication_user \
--master-password='replication_password' \
--master-dump-file-path=gs://path/to/dump.sql.gz \
--database-flags default_time_zone='-05:00' \
                 sql_mode='STRICT_TRANS_TABLES, NO_ZERO_IN_DATE, NO_ZERO_DATE, ERROR_FOR_DIVISION_BY_ZERO, NO_AUTO_CREATE_USER, NO_ENGINE_SUBSTITUTION' \
--region=us-central \
--tier=db-g1-small

但是我得到以下错误:

ERROR: (gcloud.beta.sql.instances.create) unrecognized arguments: sql_mode=STRICT_TRANS_TABLES, NO_ZERO_IN_DATE, NO_ZERO_DATE, ERROR_FOR_DIVISION_BY_ZERO, NO_AUTO_CREATE_USER, NO_ENGINE_SUBSTITUTION

To search the help text of gcloud commands, run:
  gcloud help -- SEARCH_TERMS

我是不是打错了什么?不支持吗?

更新: 根据 Stefan 的回复,我更新了我的命令以修复语法。我得到的新错误如下:

这是我的命令:

gcloud beta sql instances create $replica_name \
--master-instance-name=db-master01 \
--master-username=replication_user \
--master-password='replication_password' \
--master-dump-file-path=gs://path/to/dump.sql.gz \
--database-flags default_time_zone='-05:00',sql_mode='STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION' \
--region=us-central \
--tier=db-g1-small

这里是错误:

ERROR: (gcloud.beta.sql.instances.create) argument --database-flags: Bad syntax for dict arg: [NO_ZERO_IN_DATE]. Please see `gcloud topic flags-file` or `gcloud topic escaping` for information on providing list or dictionary flag values with special characters.
Usage: gcloud beta sql instances create INSTANCE [optional flags]
  optional flags may be  --activation-policy | --assign-ip | --async |
                         --authorized-gae-apps | --authorized-networks |
                         --availability-type | --backup | --backup-start-time |
                         --client-certificate-path | --client-key-path | --cpu |
                         --database-flags | --database-version |
                         --enable-bin-log | --failover-replica-name |
                         --follow-gae-app | --gce-zone | --help | --labels |
                         --maintenance-release-channel |
                         --maintenance-window-day | --maintenance-window-hour |
                         --master-ca-certificate-path |
                         --master-dump-file-path | --master-instance-name |
                         --master-password | --master-username | --memory |
                         --network | --pricing-plan |
                         --prompt-for-master-password | --region |
                         --replica-type | --replication | --require-ssl |
                         --root-password | --source-ip-address | --source-port |
                         --storage-auto-increase |
                         --storage-auto-increase-limit | --storage-size |
                         --storage-type | --tier | --zone

For detailed information on this command and its flags, run:
  gcloud beta sql instances create --help

我不明白为什么 NO_ZERO_IN_DATE 是个问题。它在抱怨是因为我给它传递了不止一个标志值吗?

--database-flags 有几个你可以传递的参数。 ","

您正在 default_time_zone='-05:00' 处剪切 --database-flags,在那里使用 ","

尝试像这样传递 --database-flags 而不是用 \

拆分它们

--database-flags default_time_zone='-05:00' , sql_mode='STRICT_TRANS_TABLES, NO_ZERO_IN_DATE, NO_ZERO_DATE, ERROR_FOR_DIVISION_BY_ZERO, NO_AUTO_CREATE_USER, NO_ENGINE_SUBSTITUTION' \

Here 你有更多关于应该如何传递数据库标志的具体信息。

告诉我。

编辑:

看看这个,挺有意思的。显然,默认的 --sql_mode 标志包括您试图通过的所有标志。

The default SQL mode in MySQL 5.7 includes these modes: ONLY_FULL_GROUP_BY, STRICT_TRANS_TABLES, NO_ZERO_IN_DATE, NO_ZERO_DATE, ERROR_FOR_DIVISION_BY_ZERO, NO_AUTO_CREATE_USER, and NO_ENGINE_SUBSTITUTION.

Google Cloud SQL 默认使用 5.7,因此如果您设置,所有这些标志都应该应用。

这里看看here.

我说的旗帜是this一面。

部署后,您可以使用以下命令检查标志。

gcloud sql instances describe [INSTANCE_NAME]

如果您想了解更多关于 command 的信息。

编辑2:

您无法设置这些标志,因为它们无法作为 --database-flags 在 Google Cloud Platform 中传递,here you have a list of all the flags that you can pass to the --database-flags parameter. The problem is that most of them are either deprecated according to MySQL documentation or they are already implemented in the sql_mode that you establish. Have a look here.

获得你想要的所有标志的最好方法是将 sql_mode 设置为 traditional,我一直在研究严格模式,它包含你不希望的那个有。

告诉我。

所以我刚刚在 GCP 支持的帮助下解决了这个问题。结果——惊喜! - 如果 gcloud sql instances create 命令设置为逗号分隔列表,则 gcloud sql instances create 命令中有一个错误会阻止它解析 SQL_MODE。

此错误自 2016 年以来就存在,但并不是他们优先修复的问题。 <这里的愤怒表情符号> 如果他们至少在文档中记录它会很好,但这就是我们现在的生活。

解决方法如下:a flags file.

您的标志文件只是纯 YAML,但看起来有点古怪。注意第一个破折号表示列表项,然后是 space,然后是实际的标志语法 (--my-attribute: yaddayadda)。还需要注意的是,需要引用整数,以便将它们转换为字符串进行解析:

# sql-flags-file.yaml

- --availability-type: zonal
- --backup-start-time: 05:00 
- --database-flags:
    explicit_defaults_for_timestamp: on
    lower_case_table_names: '1'
    group_concat_max_len: '67108864'
    sql_mode: NO_ZERO_DATE,NO_ZERO_IN_DATE,STRICT_TRANS_TABLES,ERROR_FOR_DIVISION_BY_ZERO,NO_ENGINE_SUBSTITUTION
    default_time_zone: -07:00
    log_bin_trust_function_creators: on
    interactive_timeout: '600'
- --database-version: MYSQL_5_7

您可以将此标志文件与内联标志结合使用,如下所示:

gcloud --project=${project_name} beta sql instances create ${instance_name} \
--network="${MY_VPC_NAME}" \
--no-assign-ip \
--flags-file=sql-flags-file.yaml && echo "...done."

这允许您参数化动态变量(如上面示例中的实例名称和 VPC 网络),同时还跨实例标准化配置。

您甚至可以同时使用多个标志文件,如果您喜欢的话。我在上面提供的 link 中解决了这个问题。

真的希望这对你有帮助。没有人应该像我昨天在排除故障时那样发誓。

我知道这是老帖了,全能的yaml顶顶了, 但我想分享另一个解决方案。

都是因为分隔符'comma'的碰撞。

因此,如果您更改 --database-flags 的分隔符, 可以避免问题。

--database-flags=^||^ → changes the separator from ',' to '||'

gcloud sql instances patch ${INSTANCE_NAME} \
--database-flags=^||^\
max_connections=2000||\
log_bin_trust_function_creators=on||\
default_time_zone=+09:00||\
group_concat_max_len=10240||\
slow_query_log=on||\
log_output=TABLE||\
transaction_isolation=READ-COMMITTED||\
innodb_ft_enable_stopword=off||\
performance_schema=on||\
innodb_buffer_pool_size=60129542144||\
sql_mode='ERROR_FOR_DIVISION_BY_ZERO,NO_ENGINE_SUBSTITUTION,NO_ZERO_DATE,NO_ZERO_IN_DATE,STRICT_TRANS_TABLES'