<分区>
我正在编写脚本以使用 gcloud
SDK 编排 Google Cloud SQL 实例。
SDK 有一个connect 命令,它需要一个用户名,但必须在提示时输入密码 - 即它不能作为参数传递。
成功验证后允许执行查询 - 流程如下所示:
$ gcloud sql connect instance-name --user=root
Whitelisting your IP for incoming connection for 5 minutes...done.
Connecting to database with SQL user [root].Enter password:
Welcome to the MariaDB monitor. Commands end with ; or \g.
Your MySQL connection id is 8054
Server version: 5.7.14-google-log (Google)
Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
MySQL [(none)]> USE tablename;
我需要以非交互方式执行此操作。我试过:
gcloud sql connect instance-name --user=root && echo "password" && echo "USE tablename;"
但它只执行第一个命令。
如何在单个命令中连接、验证和运行查询?
更新:
根据@danlor 的评论链接到 this question ,我都试过了:
yes password | gcloud...
和
printf 'password\n' | gcloud...
但两者仍然提示输入密码。