【问题标题】:Passing parameters into a MySQL text file将参数传递到 MySQL 文本文件
【发布时间】:2015-10-01 03:44:26
【问题描述】:

我有一个 MySQLCmds.txt 文件,其中包含一些 MySQL 命令:

drop database DB1;
create database DB1 character set=utf8 collate=utf8_bin;

drop database DB2;
create database DB2 character set=utf8 collate=utf8_bin;

执行MySQLCmds.txt的方式是

mysql -u$userName -hlocalhost < mysqlCmds_lyen.txt

我希望我可以将数据库名称作为参数传递到 MySQLCmds.txt 中,以便在执行上述 mysql 命令时指定要删除的数据库和创建的数据库。

这可能吗?

我在https://dev.mysql.com/doc/refman/5.6/en/mysql-batch-commands.html 找不到任何方法

我的意思是把 MySQLCmds.txt 修改成这样的东西

drop database $DB1;
create database $DB1 character set=utf8 collate=utf8_bin;

drop database $DB2;
create database $DB2 character set=utf8 collate=utf8_bin;

其中 $DB1 和 $DB2 是我传递给 MySQLCmds.txt 的参数。

【问题讨论】:

  • 你为什么不使用你的shell脚本文件来传递这些数据库变量名称并在那里进行查询?
  • 我想将mysql命令和shell脚本分开。我认为这看起来更有条理和优雅。

标签: mysql bash shell


【解决方案1】:

你可以用sed替换它

userName=root

sed  -e 's/DB1/DBONE/g' -e 's/DB2/DBTWO/g'  MySQLCmds.txt | mysql -u$userName -hlocalhost

【讨论】:

  • 谢谢你的回答:)
最近更新 更多