H2 目前支持三个服务器:web 服务器(用于 H2 控制台)、TCP 服务器(用于客户端/服务器连接)和 PG 服务器(用于 PostgreSQL 客户端)。
请注意,只有 web 服务器支持浏览器连接。启动服务器有多种方式,其中一种是使用服务器工具。启动服务器并不会打开数据库 —— 数据库会在客户端连接后立即打开。
要使用默认设置从命令行启动服务器工具,请运行:
java -cp h2*.jar org.h2.tools.Server这将以默认选项启动工具。更多示例:
// 把数据库文件存储在指定的 /var/data/h2 目录中
java -cp h2*.jar org.h2.tools.Server -baseDir /var/data/h2
// 在最后一个连接关闭后 30 秒自动关闭数据库
java -cp h2*.jar org.h2.tools.Server -dbCloseDelay 30
// 指定 TCP 连接的端口为 9092,Web 控制台端口为 9093
java -cp h2*.jar org.h2.tools.Server -tcpPort 9092 -webPort 9093
// 允许其他计算机通过 TCP 连接到数据库服务器
// 允许其他计算机通过 Web 控制台访问数据库服务器
java -cp h2*.jar org.h2.tools.Server -tcpAllowOthers -webAllowOthers要获取更多的选项列表和默认值,请运行:
java -cp h2*.jar org.h2.tools.Server -?示例:
C:\Users\Administrator\Desktop\h2\bin>java -cp h2-2.3.232.jar org.h2.tools.Server -?
Starts the H2 Console (web-) server, TCP, and PG server.
Usage: java org.h2.tools.Server <options>
When running without options, -tcp, -web, -browser and -pg are started.
Options are case sensitive.
Supported options[-help] or [-?]Print the list of options
[-web] 使用 H2 控制台启动 web 服务器
[-webAllowOthers] 允许其他计算机连接
[-webExternalNames] 以逗号分隔的服务器外部名称和 IP 地址列表,与 -webAllowOthers 一起使用
[-webDaemon] 使用守护进程线程
[-webPort <port>] 端口(默认:8082)
[-webSSL] 使用加密(HTTPS)连接
[-webAdminPassword] DB 控制台管理员密码
[-browser] 启动浏览器连接 web 服务器
[-tcp] 启动 TCP 服务器
[-tcpAllowOthers] 允许其他计算机连接
[-tcpDaemon] 使用守护进程线程
[-tcpPort <port>] 端口(默认值:9092)
[-tcpSSL] 使用加密(SSL)连接
[-tcpPassword <pwd>] 关闭 TCP 服务器的密码
[-tcpShutdown "<url>"] 停止 TCP 服务器;例如:tcp://localhost
[-tcpShutdownForce] 不等待关闭所有连接
[-pg] 启动 PG 服务器
[-pgAllowOthers] 允许其他计算机连接
[-pgDaemon] 使用守护进程线程
[-pgPort <port>] 端口(默认:5435)
[-properties "<dir>"] 服务器属性(默认:~,禁用:空)
[-baseDir <dir>] H2 数据库的基本目录(所有服务器)
[-ifExists] 只能打开现有数据库(所有服务器)
[-ifNotExists] 访问时创建数据库
[-trace] 打印额外的跟踪信息(所有服务器)
[-key <from> <to>] 允许将一个数据库名称映射到另一个数据库名称(所有服务器)
The options -xAllowOthers are potentially risky.
For details, see Advanced Topics / Protection against Remote Access.
See also https://h2database.com/javadoc/org/h2/tools/Server.html如果我们要使用 JDBC 连接到远程 H2 数据库,请使用以下格式的数据库 URL:
jdbc:h2:tcp://localhost/~/test其中,~ 表示用户主目录,tcp://localhost/~/test 表示连接远程主机 localhost 上,用户主目录下的 test 数据库文件。
请注意,您不能使用 web 浏览器连接此 URL,只能使用 H2 客户端(通过 JDBC)连接。
示例:
Connection connection = DriverManager.getConnection("jdbc:h2:tcp://localhost/~/test");
Statement statement = connection.createStatement();
statement.execute("CREATE TABLE IF NOT EXISTS users (id INT PRIMARY KEY, name VARCHAR(255))");
statement.execute("TRUNCATE TABLE users"); // 清空表
statement.execute("INSERT INTO users VALUES (1, '张三')"); // 插入一条数据
statement.close();
connection.close();除了命令行启动 H2 服务器,也可以在应用程序中启动和停止服务器。示例代码:
import org.h2.tools.Server;
...
// 启动 TCP 服务器
Server server = Server.createTcpServer(args).start();
...
// 停止 TCP 服务器
server.stop();例如:
public static void main(String[] args) {
try {
// 启动 tcp 和 web 服务,如果数据库不存在,则自动创建
Server.createTcpServer("-tcp","-web","-ifNotExists").start();
} catch (SQLException e) {
e.printStackTrace();
}
}H2 允许从另一个进程停止 TCP 服务器,要从命令行停止服务器,请运行:
java org.h2.tools.Server -tcpShutdown tcp://localhost:9092 -tcpPassword password要从用户应用程序停止服务器,请使用以下代码:
org.h2.tools.Server.shutdownTcpServer("tcp://localhost:9092", "password", false, false);此函数只会停止 TCP 服务器。如果在同一进程中启动了其他服务器,它们将继续运行。为避免下次打开数据库时出现未知问题,应在调用此方法前关闭与数据库的所有连接。
注意、要停止远程服务器,必须在服务器上启用远程连接。并且可使用选项 -tcpPassword 保护关闭 TCP 服务器(启动和停止 TCP 服务器必须使用相同的密码)。
使用 Java 安全套接字扩展(SSLServerSocket、SSLSocket)支持远程 TLS 连接。
要使用自己的密钥存储,请在启动 H2 服务器和客户端之前设置系统属性 javax.net.ssl.keyStore 和 javax.net.ssl.keyStorePassword。
注意:点击查看“自定义默认密钥和信任存储,存储类型和存储密码”相关信息。
示例:
jdbc:h2:ssl://<server>[:<port>]/[<path>]<databaseName>
jdbc:h2:ssl://localhost:8085/~/sample;上述 URL 中,除了 ssl,其他字段没有什么变化。