最近在做一个项目时,客户要求数据库要允许自动备份,于是百度了一下解决方法,记录下面,主要代码如下:
private static string RunCmd(string strPath, string strcmd)
{
Process p = new Process();
p.StartInfo.FileName = "cmd.exe";
p.StartInfo.WorkingDirectory = strPath;
p.StartInfo.UseShellExecute = false;
//p.StartInfo.Arguments="/
p.StartInfo.RedirectStandardInput = true;
p.StartInfo.RedirectStandardOutput = true;
p.StartInfo.RedirectStandardError = true;
p.StartInfo.CreateNoWindow = true;
p.Start();
p.StandardInput.WriteLine(strcmd);
p.StandardInput.WriteLine("exit");
return p.StandardError.ReadToEnd();
}
public static void DbBackup()
{
string strBackcmd = string.Format("mysqldump --host={0} --default-character-set=utf8 --lock-tables --routines --force --port=3306 --user={1} --password={2} --quick ", "主机名称", "mysql数据库用户名", "mysql数据库密码");
string strDBname = "MySqlDB";//你需要备份的数据库名称
string mysqlPath = @"D:\wamp64\bin\mysql\mysql5.7.14\bin";//mysql的安装地址
if (!string.IsNullOrEmpty(strDBname))
{
string FileName = strDBname + DateTime.Now.ToString("yyyyMMDDHHmmss") + ".sql";
string filePath = Directory.GetCurrentDirectory() + "\\" + FileName;
string cmd = strBackcmd + strDBname + " >" + filePath;
string result = RunCmd(mysqlPath, cmd);
if (result.Trim() == "")////mysqldump: [Warning] Using a password on the command line interface can be insecure.
{
Console.WriteLine("success");
}
else
{
Console.WriteLine("failed");
}
}
}
返回的result总是提示”mysqldump: [Warning] Using a password on the command line interface can be insecure.”,百度了一下解决方案,方案如下:
MySQL 5.6 警告信息 command line interface can be insecure 修复
在命令行输入密码,就会提示这些安全警告信息。
Warning: Using a password on the command line interface can be insecure.
注: mysql -u root -pPASSWORD 或 mysqldump -u root -pPASSWORD 都会输出这样的警告信息.
1、针对mysql
mysql -u root -pPASSWORD 改成mysql -u root -p 在输入密码即可.
2、mysqldump就比较麻烦了,通常都写在scripts脚本中。
解决方法:
对于 mysqldump 要如何避免出现(Warning: Using a password on the command line interface can be insecure.) 警告信息呢?
[mysqldump]
quick
max_allowed_packet = 16M
user=your_backup_user_name
password=your_backup_password
修改完配置文件后, 只需要执行mysqldump 脚本就可以了。备份脚本中不需要涉及用户名密码相关信息。
参考:http://www.lingdonge.com/coding/csharp/530.html
http://blog.51cto.com/880314/1348413
https://blog.csdn.net/u012505684/article/details/78926075