1 前言
H2 Database是一个优秀的数据库,又小又方便,支持内存和文件形式,经常会在测试、POC(proof of concept)或开发环境用到它。在Springboot的许多应用中,也是内置了H2数据库,很常用。接下来我们来一步步把它推上k8s,让它坐上一个不一样的位置。
建议阅读下面文章以帮助理解:
2 本地jar包运行
2.1 下载和启动
下载官网jar包如下:
$ curl http://www.h2database.com/h2-2019-03-13.zip -o h2-2019-03-13.zip
解压:
$ unzip h2-2019-03-13.zip
启动H2数据库:
$ java -cp h2/bin/h2*.jar org.h2.tools.Server -ifNotExists
TCP server running at tcp://localhost:9092 (only local connections)
PG server running at pg://localhost:5435 (only local connections)
Web Console server running at http://localhost:8082 (others can connect)
如果需要修改配置,如端口号、数据存储目录,可以在启动时添加参数:
java -cp h2/bin/h2*.jar org.h2.tools.Server -ifNotExists \
-web -webAllowOthers -webPort 8082 \
-tcp -tcpAllowOthers -tcpPort 9092 \
-baseDir ${DATA_DIR} ${H2_OPTIONS}
2.2 配置连接
成功启动后访问http://localhost:8082就能登陆控制台了。如下:
Driver Class:org.h2.Driver,驱动类;
JDBC URL:jdbc:h2:mem:pkslow,使用内存数据库,数据库名为pkslow;
账号密码设置为admin/123456。
设置完成后,点击连接即可创建数据库。
如果我们把JDBC URL改为jdbc:h2:file:~/pkslow,就是以文件形式存在,这样能把数据持久化,所以我们采取这种方式。这里就会在~目录,即${HOME}目录生成文件pkslow.mv.db以保存数据。还有文件~/.h2.server.properties。
更多URL的配置方法如下表:
Topic
URL Format and Examples
jdbc:h2:[file:][]jdbc:h2:~/test jdbc:h2:file:/data/sample jdbc:h2:file:C:/data/sample (Windows only)
jdbc:h2:mem:jdbc:h2:mem:test_mem
jdbc:h2:tcp://[:]/[]jdbc:h2:tcp://localhost/~/test j