mysql - 在 shell 脚本中提供 mysql 命令

标签 mysql shell automation sh

我正在尝试自动化 mysql docker 容器的一些任务,因为我正在使用 shell 脚本。但启动 mysql 服务器后我无法传递/提供 mysql 命令。

下面是我的 shell 脚本。

#!/bin/bash
mysql.server start
mysql -u root
show databases;

所有步骤均按预期进行,否则最后一个“显示数据库;”我不知道如何传递“显示数据库;” mysql服务器启动后的命令。我在 MAC 机器上,同样的行为发生在我的 ubuntu 14.04 容器上。

下面是我的控制台上的输出。

enter image description here

最佳答案

请尝试:

mysql -uUSER -pPASSWORD DBNAME -e 'show databases;'

USER是你的用户 PASSWORD 是您的密码 -e 开关用于通过 shell 向 mysql 发送命令

示例输出:

User@Host:~> mysql -uUSER -pPASSWORD DBNAME -e 'show databases ;'
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mdpdb              |
| mdpdb6             |
| mysql              |
+--------------------+

关于mysql - 在 shell 脚本中提供 mysql 命令,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42222260/

相关文章:

php - 如何在 Codeigniter 中使收件人电子邮件动态化

php - 查询构建器不插入时间戳

linux - 如何在 bash 的每个子目录中创建相同的目录?

Powershell 网页自动化适用于 Internet,而非 Intranet

javascript - 如何从 JavaScript 函数调用 Java 方法?

php - 在 html/php 表中显示 mysql 数据库的结果

MySQL 问题 "Error Code: 1093. You can' t 在 FROM 子句中指定目标表 'product' 进行更新”

linux - 从shell脚本中的目录获取文件名

linux - 使用Openssl读取多个证书

linux - Linux 中的空文件