MySQL数据库自动备份

最近在做一个项目时,客户要求数据库要允许自动备份,于是百度了一下解决方法,记录下面,主要代码如下:

 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

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值