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

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



mysqldump -uroot -proot -d dbname > dbname_20220801142508.sql




java mysql 有多少数据 java mysqldump_mysql


2022-08-01_143153.png


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

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


java mysql 有多少数据 java mysqldump_数据库_02


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 0

Success

操作系统错误代码0:成功

OS error code 1

Operation not permitted

操作系统错误代码1:操作不允许

OS error code 2

No such file or directory

操作系统错误代码2:没有这样的文件或目录

OS error code 3

No such process

操作系统错误代码3:没有这样的过程

OS error code 4

Interrupted system call

操作系统错误代码4:中断的系统调用

OS error code 5

Input/output error

操作系统错误代码5:输入/输出错误

OS error code 6

No such device or address

操作系统错误代码6:没有这样的设备或地址

OS error code 7

Argument list too long

操作系统错误代码7:参数列表太长

OS error code 8

Exec format error

操作系统错误代码8:执行格式错误

OS error code 9

Bad file descriptor

操作系统错误代码9:坏的文件描述符

OS error code 10

No child processes

操作系统错误代码10:无子过程

OS error code 11

Resource temporarily unavailable

操作系统错误代码11:资源暂时不可用

OS error code 12

Cannot allocate memory

操作系统错误代码12:无法分配内存

OS error code 13

Permission denied

操作系统错误代码13:权限被拒绝

OS error code 14

Bad address

操作系统错误代码14:错误的地址

OS error code 15

Block device required

操作系统错误代码15:需要块设备

OS error code 16

Device or resource busy

操作系统错误代码16:设备或资源忙

OS error code 17

File exists

操作系统错误代码17:文件已经存在

OS error code 18

Invalid cross-device link

操作系统错误代码18:无效的跨设备链接

OS error code 19

No such device

操作系统错误代码19:没有这样的设备

OS error code 20

Not a directory

操作系统错误代码20:不是一个目录

OS error code 21

Is a directory

操作系统错误代码21:是一个目录

OS error code 22

Invalid argument

操作系统错误代码22:无效参数

OS error code 23

Too many open files in system

操作系统错误代码23:打开太多的文件系统

OS error code 24

Too many open files

操作系统错误代码24:打开的文件太多

OS error code 25

Inappropriate ioctl for device

操作系统错误代码25:不适当的设备ioctl使用

OS error code 26

Text file busy

操作系统错误代码26:文本文件忙

OS error code 27

File too large

操作系统错误代码27:文件太大

OS error code 28

No space left on device

操作系统错误代码28:设备上没有空间

OS error code 29

Illegal seek

操作系统错误代码29:非法搜索

OS error code 30

Read-only file system

操作系统错误代码30:只读文件系统

OS error code 31

Too many links

操作系统错误代码31:链接过多

OS error code 32

Broken pipe

操作系统错误代码32:管道破裂

OS error code 33

Numerical argument out of domain

操作系统错误代码33:超出域的数值参数

OS error code 34

Numerical result out of range

操作系统错误代码34:数值结果超出范围

OS error code 35

Resource deadlock avoided

操作系统错误代码35:避免资源死锁

OS error code 37

No locks available

操作系统错误代码37:没有可用锁

OS error code 38

Function not implemented

操作系统错误代码38:功能没有实现

OS error code 39

Directory not empty

操作系统错误代码39:目录非空

OS error code 40

Too many levels of symbolic links

操作系统错误代码40:符号链接层次太多

OS error code 42

No message of desired type

操作系统错误代码42:没有期望类型的消息

OS error code 43

Identifier removed

操作系统错误代码43:标识符删除

OS error code 44

Channel number out of range

操作系统错误代码44:通道数目超出范围

OS error code 45

Level 2 not synchronized

操作系统错误代码45:2级不同步

OS error code 46

Level 3 halted

操作系统错误代码46:3级终止

OS error code 47

Level 3 reset

操作系统错误代码47:3级复位

OS error code 48

Link number out of range

操作系统错误代码48:链接数超出范围

OS error code 49

Protocol driver not attached

操作系统错误代码49:协议驱动程序没有连接

OS error code 50

No CSI structure available

操作系统错误代码50:没有可用的CSI结构

OS error code 51

Level 2 halted

操作系统错误代码51:2级中断

OS error code 52

Invalid exchange

操作系统错误代码52:无效的交换

OS error code 53

Invalid request descriptor

操作系统错误代码53:无效的请求描述符

OS error code 54

Exchange full

操作系统错误代码54:交换空间满

OS error code 55

No anode

操作系统错误代码55:阳极不存在

OS error code 56

Invalid request code

操作系统错误代码56:无效的请求代码

OS error code 57

Invalid slot

操作系统错误代码57:无效的槽

OS error code 59

Bad font file format

操作系统错误代码59:错误的字体文件格式

OS error code 60

Device not a stream

操作系统错误代码60:设备不属于流类型

OS error code 61

No data available

操作系统错误代码61:无可用数据

OS error code 62

Timer expired

操作系统错误代码62:超时

OS error code 63

Out of streams resources

操作系统错误代码63:超出流资源范围

OS error code 64

Machine is not on the network

操作系统错误代码64:主机不在网络上

OS error code 65

Package not installed

操作系统错误代码65:软件包没有安装

OS error code 66

Object is remote

操作系统错误代码66:对象是远程的

OS error code 67

Link has been severed

操作系统错误代码67:链接被切断

OS error code 68

Advertise error

操作系统错误代码68:广告错误

OS error code 69

Srmount error

操作系统错误代码69:srmount错误

OS error code 70

Communication error on send

操作系统错误代码70:发送数据时通讯错误

OS error code 71

Protocol error

操作系统错误代码71:协议错误

OS error code 72

Multihop attempted

操作系统错误代码72:企图进行多次跳转

OS error code 73

RFS specific error

操作系统错误代码73:RFS类型错误

OS error code 74

Bad message

操作系统错误代码74:坏消息

OS error code 75

Value too large for defined data type

操作系统错误代码75:数值超过对于给定的数据类型

OS error code 76

Name not unique on network

操作系统错误代码76:主机名在网络上不是唯一

OS error code 77

File descriptor in bad state

操作系统错误代码77:坏状态的文件描述符

OS error code 78

Remote address changed

操作系统错误代码78:远端地址改变

OS error code 79

Can not access a needed shared library

操作系统错误代码79:无法访问需要的共享库

OS error code 80

Accessing a corrupted shared library

操作系统错误代码80:访问了一个损坏的共享库

OS error code 81

.lib section in a.out corrupted

操作系统错误代码81: a. out文件中的.lib段损坏。

OS error code 82

Attempting to link in too many shared libraries

操作系统错误代码82:试图链接太多的共享库

OS error code 83

Cannot exec a shared library directly

操作系统错误代码83:不能直接执行一个共享库

OS error code 84

Invalid or incomplete multibyte or wide character

操作系统错误代码84:无效或不完整的多字节以及宽字符

OS error code 85

Interrupted system call should be restarted

操作系统错误代码85:中断的系统调用需要重新启动

OS error code 86

Streams pipe error

操作系统错误代码86:流管道错误

OS error code 87

Too many users

操作系统错误代码87:太多用户

OS error code 88

Socket operation on non-socket

操作系统错误代码88:在非套接字接口进行套接字操作

OS error code 89

Destination address required

操作系统错误代码89:需要目标地址

OS error code 90

Message too long

操作系统错误代码90:消息太长

OS error code 91

Protocol wrong type for socket

操作系统错误代码91:socket协议错误类型

OS error code 92

Protocol not available

操作系统错误代码92:协议不可用

OS error code 93

Protocol not supported

操作系统错误代码93:协议不支持

OS error code 94

Socket type not supported

操作系统错误代码94:socket类型不支持

OS error code 95

Operation not supported

操作系统错误代码95:操作不支持

OS error code 96

Protocol family not supported

操作系统错误代码96:协议族不支持

OS error code 97

Address family not supported by protocol

操作系统错误代码97:协议不支持地址族

OS error code 98

Address already in use

操作系统错误代码98:地址已在使用

OS error code 99

Cannot assign requested address

操作系统错误代码99:无法分配请求的地址

OS error code 100

Network is down

操作系统错误代码100:网络瘫痪

OS error code 101

Network is unreachable

操作系统错误代码101:网络不可达

OS error code 102

Network dropped connection on reset

操作系统错误代码102:网络复位时连接丢失

OS error code 103

Software caused connection abort

操作系统错误代码103:软件导致连接中断

OS error code 104

Connection reset by peer

操作系统错误代码104:连接被重置

OS error code 105

No buffer space available

操作系统错误代码105:没有可用的缓冲空间

OS error code 106

Transport endpoint is already connected

操作系统错误代码106:传输端点已连接

OS error code 107

Transport endpoint is not connected

操作系统错误代码107:运输端点没有连接上

OS error code 108

Cannot send after transport endpoint shutdown

操作系统错误代码108:运输终点关闭后无法发送数据

OS error code 109

Too many references cannot splice

OS error code 110

Connection timed out

操作系统错误代码110:连接超时

OS error code 111

Connection refused

操作系统错误代码111:连接被拒绝

OS error code 112

Host is down

操作系统错误代码112:主机已关闭

OS error code 113

No route to host

操作系统错误代码113:没有路由到主机

OS error code 114

Operation already in progress

操作系统错误代码114:进程已运行

OS error code 115

Operation now in progress

操作系统错误代码115:正在进行操作

OS error code 116

Stale NFS file handle

操作系统错误代码116:陈旧的NFS文件句柄

OS error code 117

Structure needs cleaning

操作系统错误代码117:结构需要清除

OS error code 118

Not a XENIX named type file

操作系统错误代码118:不是一个XENIX命名类型的文件

OS error code 119

No XENIX semaphores available

操作系统错误代码119:没有XENIX信号量可用

OS error code 120

Is a named type file

操作系统错误代码120:是一个指定类型的文件

OS error code 121

Remote I/O error

操作系统错误代码121:远程输入/输出错误

OS error code 122

Disk quota exceeded

操作系统错误代码122:超出磁盘配额

OS error code 123

No medium found

操作系统错误代码123:没有发现介质

OS error code 124

Wrong medium type

操作系统错误代码124:错误的介质类型

OS error code 125

Operation canceled

操作系统错误代码125:操作取消

OS error code 126

Required key not available

操作系统错误代码126:所需的Key不可用

OS error code 127

Key has expired

操作系统错误代码127:Key已过期

OS error code 128

Key has been revoked

操作系统错误代码128:Key被撤销

OS error code 129

Key was rejected by service

操作系统错误代码129:Key被拒绝服务

OS error code 130

Owner died

操作系统错误代码130:属主死亡

OS error code 131

State not recoverable

操作系统错误代码131:状态不可恢复

MySQL error code 132

Old database file

错误代码132:旧的数据库文件

MySQL error code 133

No record read before update

错误代码133 :更新之前没有记录被读

MySQL error code 134

Record was already deleted (or record file crashed)

错误代码134 :记录已删除(或记录文件损坏)

MySQL error code 135

No more room in record file

错误代码135:记录文件没有更多空间

MySQL error code 136

No more room in index file

错误代码136:索引文件没有更多空间

MySQL error code 137

No more records (read after end of file)

错误代码137:没有更多的记录(已读至文件尾)

MySQL error code 138

Unsupported extension used for table

错误代码138 :不支持表的扩展应用

MySQL error code 139

Too big row

错误代码139 :行太大

MySQL error code 140

Wrong create options

错误代码140 :错误的创建选项

MySQL error code 141

Duplicate unique key or constraint on write or update

错误代码141:写和更新时,独一的键或约束发生重复

MySQL error code 142

Unknown character set used

错误代码142 :使用了未知字符集

MySQL error code 143

Conflicting table definitions in sub-tables of MERGE table

错误代码143:在合并表的子表中定义冲突

MySQL error code 144

Table is crashed and last repair failed

错误代码144 :表损坏,最后一次修复失败

MySQL error code 145

Table was marked as crashed and should be repaired

错误代码145 :表被标记为损坏,应该进行修复

MySQL error code 146

Lock timed out; Retry transaction

错误代码146 :锁超时,重试事务;

MySQL error code 147

Lock table is full; Restart program with a larger locktable

错误代码147 :锁表已满;重启程序,配置更大的locktable表

MySQL error code 148

Updates are not allowed under a read only transactions

错误代码148 :在一个只读事务中,不允许进行更新操作

MySQL error code 149

Lock deadlock; Retry transaction

错误代码149 :锁死锁;重试事务操作

MySQL error code 150

Foreign key constraint is incorrectly formed

错误代码150 :外键约束生成不当

MySQL error code 151

Cannot add a child row

错误代码151:无法添加子行

MySQL error code 152

Cannot 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