I have a Powershell script that backs up my MySQL DB's each night using mysqldump. This all works fine but I would like to extend the script to update a reporting db (db1) from the backup of the prod db (db2). I have written the following test script but it does not work. I have a feeling the problem is the reading of the sql file to the CommandText but I am not sure how to debug.

我有一个Powershell脚本,每晚使用mysqldump备份MySQL DB。这一切都很好,但是我想扩展脚本,从prod db (db2)的备份中更新报告db (db1)。我已经编写了下面的测试脚本,但是它不起作用。我有一种感觉,问题是将sql文件读入命令文本,但我不知道如何调试。

[system.reflection.assembly]::LoadWithPartialName("MySql.Data")    
$mysql_server = "localhost"
$mysql_user = "root"
$mysql_password = "password"
write-host "Create coonection to db1"
# Connect to MySQL database 'db1'

$cn = New-Object -TypeName MySql.Data.MySqlClient.MySqlConnection
$cn.ConnectionString = "SERVER=$mysql_server;DATABASE=db1;UID=$mysql_user;PWD=$mysql_password"
$cn.Open()
write-host "Running backup script against db1"
# Run Update Script MySQL 
$cm = New-Object -TypeName MySql.Data.MySqlClient.MySqlCommand
$sql = Get-Content C:\db2.sql
$cm.Connection = $cn
$cm.CommandText = $sql
$cm.ExecuteReader()
write-host "Closing Connection"
$cn.Close()

Any assistance would be appreciated. Thanks.

如有任何帮助,我们将不胜感激。谢谢。

1 个解决方案

#1


3

This line:

这条线:

$sql = Get-Content C:\db2.sql 

Returns an array of strings. When that gets assigned to something expecting a string then PowerShell will concatenate the array of strings into a single string using the contents of the $OFS (output field separator) variable. If this variable isn't set, the default separator is a single space. Try this instead and see if it works:

返回字符串数组。当它被分配给某个期待字符串的对象时,PowerShell将使用$OFS(输出字段分隔符)变量的内容将字符串数组连接到单个字符串中。如果未设置此变量,则默认分隔符为单个空间。试试这个,看看是否有效:

$sql = Get-Content C:\db2.sql 
...
$OFS = "`r`n"
$cm.CommandText = "$sql"

Or if you're on PowerShell 2.0:

或者如果你使用PowerShell 2.0:

$sql = (Get-Content C:\db2.sql) -join "`r`n"

更多相关文章

  1. 由于底层异常,无法加载连接类:'java.lang.NumberFormatException:对
  2. 如何知道php脚本中的名称是指mysql表还是mysql视图
  3. 是一个mysql临时表,每个用户访问创建它的脚本是唯一的...?
  4. mysql启动脚本——指定数据目录并指定配置文件
  5. Spring Security ACL使用MySQL配置与数据库脚本
  6. MySql查询脚本,每月统计活动用户。
  7. MySQL导出和导入SQL脚本
  8. 如何在mysql数据库中找到类似的二进制字符串?
  9. MySQL在Linux下Shell脚本操作命令

随机推荐

  1. 为何总给外卖打差评?我们来数据分析一下!
  2. 动画:面试必刷之二叉树搜索第 K 大节点
  3. “没想到吧,我胡汉三又回来了!”
  4. 之所以被裁也许是少了这些东西 [每日前端
  5. 年终总结 | 小鹿给读者拜年啦!
  6. JavaScript中的执行上下文和堆栈[每日前
  7. 现代浏览器探秘(part 1):架构 [每日前端夜
  8. 讲真,Markdown有啥魅力,竟让如此多人入迷!
  9. 深入探讨 Undefined [每日前端夜话(0x0E)
  10. 《大前端吊打面试官系列》之备战面试篇!