Mysql 在命令行中以非交互方式执行Google Cloud SQL查询

Mysql 在命令行中以非交互方式执行Google Cloud SQL查询,mysql,bash,google-cloud-sql,non-interactive,Mysql,Bash,Google Cloud Sql,Non Interactive,我正在编写一个脚本,以使用gcloudSDK编排一个googlecloudsql实例 SDK有一个connect命令,该命令使用用户名,但密码必须在提示下输入-即不能作为参数传递 成功进行身份验证后,即可执行查询-流程如下所示: $ gcloud sql connect instance-name --user=root Whitelisting your IP for incoming connection for 5 minutes...done. Connecting to databas

我正在编写一个脚本,以使用
gcloud
SDK编排一个googlecloudsql实例

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 linking to的评论,我尝试了以下两种方法:

yes password | gcloud...


但是这两个命令都会提示输入密码。

它不像
gcloud sql connect
命令那样允许您将
execute
标志传递给MySQL命令行实用程序。我建议用这个来代替。您的脚本可能如下所示:

/cloud\u sql\u proxy-dir=/cloudsql-instances=&
PID=$!
mysql-u root--password“YOUR-password”-S/cloudsql/--execute“USE tablename;”
杀死$PID

&&
表示下一个命令将仅在前一个命令完全退出(成功)后执行。这就是为什么只执行第一个命令。谢谢@danlor-我在链接答案中尝试了
yes
printf
两种方法,但都无效
printf 'password\n' | gcloud...