2022-08-01 Java中代码执行mysqldump语句,process.waitFor()返回6

项目中没有自动备份数据库的功能,需要写一个脚本,人工定时执行这个脚本备份数据库。

MySQL数据库中有mysqldump语句可以现成的备份某个数据库,语法如下:

mysqldump -uroot -proot -d dbname > dbname_20220801142508.sql
2022-08-01_143153.png

在命令行中执行这个语句,可以很容易的得到SQL文件,文件在你执行这个语句的路径。

前提是你的电脑中配置了MySQL的环境变量,某则需要在MySQL的安装目录中找到mysqldump 程序入口。

2022-08-01_143245.png

但是当我在Java代码中使用Runtime.getRuntime().exec("cmd")执行这个SQL语句时,process.waitFor()返回了6,是一个错误的结果。

private static void database() {
    System.out.println("----------------------------开始备份数据库----------------------------");
    try {
        String backupSQL = "mysqldump -uroot -proot -d dbname > dbname_" + DateUtil.format(System.currentTimeMillis(), "yyyyMMddHHmmss") + ".sql";
        System.out.println("备份数据库SQL语句:" + backupSQL);
        Process process = Runtime.getRuntime().exec(backupSQL);
        int result = process.waitFor();
        if (result == 0) {
            System.out.println("备份成功");
        } else {
            System.out.println("备份失败:" + result);
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
    System.out.println("----------------------------数据库备份结束----------------------------");
}
2022-08-01_145456.png

通过查阅process.waitFor()返回值的意义时并没有用。

process.waitFor()含义说明
OS error code 0Success操作系统错误代码0:成功
OS error code 1Operation not permitted操作系统错误代码1:操作不允许
OS error code 2No such file or directory操作系统错误代码2:没有这样的文件或目录
OS error code 3No such process操作系统错误代码3:没有这样的过程
OS error code 4Interrupted system call操作系统错误代码4:中断的系统调用
OS error code 5Input/output error操作系统错误代码5:输入/输出错误
OS error code 6No such device or address操作系统错误代码6:没有这样的设备或地址
OS error code 7Argument list too long操作系统错误代码7:参数列表太长
OS error code 8Exec format error操作系统错误代码8:执行格式错误
OS error code 9Bad file descriptor操作系统错误代码9:坏的文件描述符
OS error code 10No child processes操作系统错误代码10:无子过程
OS error code 11Resource temporarily unavailable操作系统错误代码11:资源暂时不可用
OS error code 12Cannot allocate memory操作系统错误代码12:无法分配内存
OS error code 13Permission denied操作系统错误代码13:权限被拒绝
OS error code 14Bad address操作系统错误代码14:错误的地址
OS error code 15Block device required操作系统错误代码15:需要块设备
OS error code 16Device or resource busy操作系统错误代码16:设备或资源忙
OS error code 17File exists操作系统错误代码17:文件已经存在
OS error code 18Invalid cross-device link操作系统错误代码18:无效的跨设备链接
OS error code 19No such device操作系统错误代码19:没有这样的设备
OS error code 20Not a directory操作系统错误代码20:不是一个目录
OS error code 21Is a directory操作系统错误代码21:是一个目录
OS error code 22Invalid argument操作系统错误代码22:无效参数
OS error code 23Too many open files in system操作系统错误代码23:打开太多的文件系统
OS error code 24Too many open files操作系统错误代码24:打开的文件太多
OS error code 25Inappropriate ioctl for device操作系统错误代码25:不适当的设备ioctl使用
OS error code 26Text file busy操作系统错误代码26:文本文件忙
OS error code 27File too large操作系统错误代码27:文件太大
OS error code 28No space left on device操作系统错误代码28:设备上没有空间
OS error code 29Illegal seek操作系统错误代码29:非法搜索
OS error code 30Read-only file system操作系统错误代码30:只读文件系统
OS error code 31Too many links操作系统错误代码31:链接过多
OS error code 32Broken pipe操作系统错误代码32:管道破裂
OS error code 33Numerical argument out of domain操作系统错误代码33:超出域的数值参数
OS error code 34Numerical result out of range操作系统错误代码34:数值结果超出范围
OS error code 35Resource deadlock avoided操作系统错误代码35:避免资源死锁
OS error code 37No locks available操作系统错误代码37:没有可用锁
OS error code 38Function not implemented操作系统错误代码38:功能没有实现
OS error code 39Directory not empty操作系统错误代码39:目录非空
OS error code 40Too many levels of symbolic links操作系统错误代码40:符号链接层次太多
OS error code 42No message of desired type操作系统错误代码42:没有期望类型的消息
OS error code 43Identifier removed操作系统错误代码43:标识符删除
OS error code 44Channel number out of range操作系统错误代码44:通道数目超出范围
OS error code 45Level 2 not synchronized操作系统错误代码45:2级不同步
OS error code 46Level 3 halted操作系统错误代码46:3级终止
OS error code 47Level 3 reset操作系统错误代码47:3级复位
OS error code 48Link number out of range操作系统错误代码48:链接数超出范围
OS error code 49Protocol driver not attached操作系统错误代码49:协议驱动程序没有连接
OS error code 50No CSI structure available操作系统错误代码50:没有可用的CSI结构
OS error code 51Level 2 halted操作系统错误代码51:2级中断
OS error code 52Invalid exchange操作系统错误代码52:无效的交换
OS error code 53Invalid request descriptor操作系统错误代码53:无效的请求描述符
OS error code 54Exchange full操作系统错误代码54:交换空间满
OS error code 55No anode操作系统错误代码55:阳极不存在
OS error code 56Invalid request code操作系统错误代码56:无效的请求代码
OS error code 57Invalid slot操作系统错误代码57:无效的槽
OS error code 59Bad font file format操作系统错误代码59:错误的字体文件格式
OS error code 60Device not a stream操作系统错误代码60:设备不属于流类型
OS error code 61No data available操作系统错误代码61:无可用数据
OS error code 62Timer expired操作系统错误代码62:超时
OS error code 63Out of streams resources操作系统错误代码63:超出流资源范围
OS error code 64Machine is not on the network操作系统错误代码64:主机不在网络上
OS error code 65Package not installed操作系统错误代码65:软件包没有安装
OS error code 66Object is remote操作系统错误代码66:对象是远程的
OS error code 67Link has been severed操作系统错误代码67:链接被切断
OS error code 68Advertise error操作系统错误代码68:广告错误
OS error code 69Srmount error操作系统错误代码69:srmount错误
OS error code 70Communication error on send操作系统错误代码70:发送数据时通讯错误
OS error code 71Protocol error操作系统错误代码71:协议错误
OS error code 72Multihop attempted操作系统错误代码72:企图进行多次跳转
OS error code 73RFS specific error操作系统错误代码73:RFS类型错误
OS error code 74Bad message操作系统错误代码74:坏消息
OS error code 75Value too large for defined data type操作系统错误代码75:数值超过对于给定的数据类型
OS error code 76Name not unique on network操作系统错误代码76:主机名在网络上不是唯一
OS error code 77File descriptor in bad state操作系统错误代码77:坏状态的文件描述符
OS error code 78Remote address changed操作系统错误代码78:远端地址改变
OS error code 79Can not access a needed shared library操作系统错误代码79:无法访问需要的共享库
OS error code 80Accessing a corrupted shared library操作系统错误代码80:访问了一个损坏的共享库
OS error code 81.lib section in a.out corrupted操作系统错误代码81: a. out文件中的.lib段损坏。
OS error code 82Attempting to link in too many shared libraries操作系统错误代码82:试图链接太多的共享库
OS error code 83Cannot exec a shared library directly操作系统错误代码83:不能直接执行一个共享库
OS error code 84Invalid or incomplete multibyte or wide character操作系统错误代码84:无效或不完整的多字节以及宽字符
OS error code 85Interrupted system call should be restarted操作系统错误代码85:中断的系统调用需要重新启动
OS error code 86Streams pipe error操作系统错误代码86:流管道错误
OS error code 87Too many users操作系统错误代码87:太多用户
OS error code 88Socket operation on non-socket操作系统错误代码88:在非套接字接口进行套接字操作
OS error code 89Destination address required操作系统错误代码89:需要目标地址
OS error code 90Message too long操作系统错误代码90:消息太长
OS error code 91Protocol wrong type for socket操作系统错误代码91:socket协议错误类型
OS error code 92Protocol not available操作系统错误代码92:协议不可用
OS error code 93Protocol not supported操作系统错误代码93:协议不支持
OS error code 94Socket type not supported操作系统错误代码94:socket类型不支持
OS error code 95Operation not supported操作系统错误代码95:操作不支持
OS error code 96Protocol family not supported操作系统错误代码96:协议族不支持
OS error code 97Address family not supported by protocol操作系统错误代码97:协议不支持地址族
OS error code 98Address already in use操作系统错误代码98:地址已在使用
OS error code 99Cannot assign requested address操作系统错误代码99:无法分配请求的地址
OS error code 100Network is down操作系统错误代码100:网络瘫痪
OS error code 101Network is unreachable操作系统错误代码101:网络不可达
OS error code 102Network dropped connection on reset操作系统错误代码102:网络复位时连接丢失
OS error code 103Software caused connection abort操作系统错误代码103:软件导致连接中断
OS error code 104Connection reset by peer操作系统错误代码104:连接被重置
OS error code 105No buffer space available操作系统错误代码105:没有可用的缓冲空间
OS error code 106Transport endpoint is already connected操作系统错误代码106:传输端点已连接
OS error code 107Transport endpoint is not connected操作系统错误代码107:运输端点没有连接上
OS error code 108Cannot send after transport endpoint shutdown操作系统错误代码108:运输终点关闭后无法发送数据
OS error code 109Too many references cannot splice
OS error code 110Connection timed out操作系统错误代码110:连接超时
OS error code 111Connection refused操作系统错误代码111:连接被拒绝
OS error code 112Host is down操作系统错误代码112:主机已关闭
OS error code 113No route to host操作系统错误代码113:没有路由到主机
OS error code 114Operation already in progress操作系统错误代码114:进程已运行
OS error code 115Operation now in progress操作系统错误代码115:正在进行操作
OS error code 116Stale NFS file handle操作系统错误代码116:陈旧的NFS文件句柄
OS error code 117Structure needs cleaning操作系统错误代码117:结构需要清除
OS error code 118Not a XENIX named type file操作系统错误代码118:不是一个XENIX命名类型的文件
OS error code 119No XENIX semaphores available操作系统错误代码119:没有XENIX信号量可用
OS error code 120Is a named type file操作系统错误代码120:是一个指定类型的文件
OS error code 121Remote I/O error操作系统错误代码121:远程输入/输出错误
OS error code 122Disk quota exceeded操作系统错误代码122:超出磁盘配额
OS error code 123No medium found操作系统错误代码123:没有发现介质
OS error code 124Wrong medium type操作系统错误代码124:错误的介质类型
OS error code 125Operation canceled操作系统错误代码125:操作取消
OS error code 126Required key not available操作系统错误代码126:所需的Key不可用
OS error code 127Key has expired操作系统错误代码127:Key已过期
OS error code 128Key has been revoked操作系统错误代码128:Key被撤销
OS error code 129Key was rejected by service操作系统错误代码129:Key被拒绝服务
OS error code 130Owner died操作系统错误代码130:属主死亡
OS error code 131State not recoverable操作系统错误代码131:状态不可恢复
MySQL error code 132Old database file错误代码132:旧的数据库文件
MySQL error code 133No record read before update错误代码133 :更新之前没有记录被读
MySQL error code 134Record was already deleted (or record file crashed)错误代码134 :记录已删除(或记录文件损坏)
MySQL error code 135No more room in record file错误代码135:记录文件没有更多空间
MySQL error code 136No more room in index file错误代码136:索引文件没有更多空间
MySQL error code 137No more records (read after end of file)错误代码137:没有更多的记录(已读至文件尾)
MySQL error code 138Unsupported extension used for table错误代码138 :不支持表的扩展应用
MySQL error code 139Too big row错误代码139 :行太大
MySQL error code 140Wrong create options错误代码140 :错误的创建选项
MySQL error code 141Duplicate unique key or constraint on write or update错误代码141:写和更新时,独一的键或约束发生重复
MySQL error code 142Unknown character set used错误代码142 :使用了未知字符集
MySQL error code 143Conflicting table definitions in sub-tables of MERGE table错误代码143:在合并表的子表中定义冲突
MySQL error code 144Table is crashed and last repair failed错误代码144 :表损坏,最后一次修复失败
MySQL error code 145Table was marked as crashed and should be repaired错误代码145 :表被标记为损坏,应该进行修复
MySQL error code 146Lock timed out; Retry transaction错误代码146 :锁超时,重试事务;
MySQL error code 147Lock table is full; Restart program with a larger locktable错误代码147 :锁表已满;重启程序,配置更大的locktable表
MySQL error code 148Updates are not allowed under a read only transactions错误代码148 :在一个只读事务中,不允许进行更新操作
MySQL error code 149Lock deadlock; Retry transaction错误代码149 :锁死锁;重试事务操作
MySQL error code 150Foreign key constraint is incorrectly formed错误代码150 :外键约束生成不当
MySQL error code 151Cannot add a child row错误代码151:无法添加子行
MySQL error code 152Cannot delete a parent row错误代码152:不能删除父行

我试着在语句中将MySQL的密码写一个错误的

在命令行中直接提示连接错误

2022-08-01_145313.png

在Java代码中返回的错误码是2

2022-08-01_145433.png

这个错误表看样子在这里也是没什么用

多看了网友相关的记录,有一个说到Java中使用Runtime执行命令时>符号要改成-r,所以在Java代码中执行备份的语句改成

mysqldump -uroot -proot -d dbname -r dbname_20220801150038.sql

执行后能成功的备份数据库到SQL文件。

private static void database() {
    System.out.println("----------------------------开始备份数据库----------------------------");
    try {
        String backupSQL = "mysqldump -uroot -proot -d dbname -r dbname_" + DateUtil.format(System.currentTimeMillis(), "yyyyMMddHHmmss") + ".sql";
        System.out.println("备份数据库SQL语句:" + backupSQL);
        Process process = Runtime.getRuntime().exec(backupSQL);
        int result = process.waitFor();
        if (result == 0) {
            System.out.println("备份成功");
        } else {
            System.out.println("备份失败:" + result);
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
    System.out.println("----------------------------数据库备份结束----------------------------");
}
2022-08-01_150153.png
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值