欢迎来到尧图网

客户服务 关于我们

您的位置:首页 > 科技 > 名人名企 > H2 Database安装部署

H2 Database安装部署

2025/2/11 16:22:47 来源:https://blog.csdn.net/qq_61920297/article/details/145358319  浏览:    关键词:H2 Database安装部署

H2 Database

H2 Database官网

H2 中文文档

安装部署H2

java版本要高于java 11

## 下载java21
wget https://download.oracle.com/java/21/latest/jdk-21_linux-x64_bin.tar.gz[root@localhost ~]# tar xf  jdk-21_linux-x64_bin.tar.gz -C /usr/local/
[root@localhost ~]# vim /etc/profileexport JAVA_HOME=/usr/local/jdk-21.0.5
export PATH=$JAVA_HOME/bin:$PATH
export CLASSPATH=.:$JAVA_HOME/lib/dt.jar:$JAVA_HOME/lib/tools.jar## 下载h2安装包
wget https://github.com/h2database/h2database/releases/download/version-2.3.232/h2-2024-08-11.zip## 创建数据目录
mkdir -p /data/h2## 启动h2
nohup java -cp h2-2.3.232.jar org.h2.tools.Server -web -webAllowOthers -tcp -tcpPort 9092 -tcpAllowOthers -pg -pgAllowOthers -pgPort 5432 -baseDir /data/h2/ &## 端口
root@PostgreSQL bin]# netstat -ntpl
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address           Foreign Address         State       PID/Program name    
tcp        0      0 0.0.0.0:22              0.0.0.0:*               LISTEN      1353/sshd: /usr/sbi 
tcp6       0      0 :::9876                 :::*                    LISTEN      9806/java           
tcp6       0      0 :::22                   :::*                    LISTEN      1353/sshd: /usr/sbi 
tcp6       0      0 :::5432                 :::*                    LISTEN      13432/java          
tcp6       0      0 :::10909                :::*                    LISTEN      10001/java          
tcp6       0      0 :::10911                :::*                    LISTEN      10001/java          
tcp6       0      0 :::10912                :::*                    LISTEN      10001/java          
tcp6       0      0 :::9092                 :::*                    LISTEN      13432/java          
tcp6       0      0 :::8080                 :::*                    LISTEN      10001/java          
tcp6       0      0 :::8081                 :::*                    LISTEN      10001/java          
tcp6       0      0 :::8082                 :::*                    LISTEN      13432/java  

h2启动参数说明

参数描述
-web启动带有 H2 控制台的 web 服务器
-webAllowOthers允许其他计算机连接到 web 服务器
-tcp启动 TCP 服务器
-tcpPort <port>设置 TCP 服务器的端口
-tcpAllowOthers允许其他计算机连接到 TCP 服务器
-pg启动 PG 服务器(PostgreSQL 兼容)
-pgAllowOthers允许其他计算机连接到 PG 服务器
-pgPort <port>设置 PG 服务器的端口
-baseDir <dir>设置 H2 数据库的基础目录,所有数据库文件将存储在这个目录下(经过测试发现该参数不起作用)

官网介绍

连接H2

使用控制台连接

[root@PostgreSQL bin]# java -cp h2-2.3.232.jar org.h2.tools.ShellWelcome to H2 Shell 2.3.232 (2024-08-11)
Exit with Ctrl+C
[Enter]   jdbc:h2:~/test
URL       
[Enter]   org.h2.Driver
Driver    
[Enter]   
User      
Password  Password  ><
Connected
Commands are case insensitive; SQL statements end with ';'
help or ?      Display this help
list           Toggle result list / stack trace mode
maxwidth       Set maximum column width (default is 100)
autocommit     Enable or disable autocommit
history        Show the last 20 statements
quit or exit   Close the connection and exitsql> 

参数说明

参数描述
-help-?打印可用选项列表
-url "<url>"数据库的 URL(例如:jdbc:h2:…)
-user <user>用户名
-password <pwd>密码
-driver <class>要使用的 JDBC 驱动类(大多数情况下不需要指定)
-sql "<statements>"执行 SQL 语句并退出
-properties "<dir>"从指定目录加载服务器属性

从日志中可以看到连接地址

TCP server running at tcp://192.168.202.159:9092 (others can connect)
PG server running at pg://192.168.202.159:5432 (others can connect)
Web Console server running at http://192.168.202.159:8082 (others can connect)
创建新一个新数据库
缺省情况下,如果 URL 指定的数据库并不存在,一个新的空的数据库将被自动的创建。创建数据库的用户自动成为这个数据库的超级管理员。自动创建新库也可以通过特殊的 URL 进行屏蔽

介绍

创建表

CREATE [ CACHED | MEMORY ] [ { TEMP } | [ GLOBAL | LOCAL ] TEMPORARY ]
TABLE [ IF NOT EXISTS ] [schemaName.]tableName
[ ( { columnName [columnDefinition] | tableConstraintDefinition } [,...] ) ]
[ ENGINE tableEngineName ]
[ WITH tableEngineParamName [,...] ]
[ NOT PERSISTENT ] [ TRANSACTIONAL ]
[ AS ( query ) [ WITH [ NO ] DATA ] ]
sql> CREATE TABLE TEST(ID INT PRIMARY KEY, NAME VARCHAR(255));
(Update count: 0, 6 ms)
sql> show tables;
TABLE_NAME | TABLE_SCHEMA
TEST       | PUBLIC
(1 row, 24 ms)
sql> 
  • 缓存表(普通表的默认类型)是持久化的,其行数不受主内存限制。

  • 内存表是持久化的,但索引数据存储在主内存中,这意味着内存表不应过大。

查看表的列

SHOW { SCHEMAS | TABLES [ FROM schemaName ] |
COLUMNS FROM tableName [ FROM schemaName ] }
sql> show columns from test;
FIELD | TYPE                   | NULL | KEY | DEFAULT
ID    | INTEGER                | NO   | PRI | NULL
NAME  | CHARACTER VARYING(255) | YES  |     | NULL
(2 rows, 11 ms)

插入数据

CREATE TABLE timeseries_table (id SERIAL PRIMARY KEY,timestamp TIMESTAMP,data_column  DOUBLE);INSERT INTO timeseries_table (timestamp, data_column)
SELECTDATEADD('SECOND', CAST(floor(random() * 86400) AS INT), CURRENT_TIMESTAMP),  -- 当前时间到明天同一时间的随机时间floor(random() * 100000)  -- 随机数据列值
FROM generate_series(1, 1000000);

创建用户

CREATE USER [ IF NOT EXISTS ] newUserName
{ PASSWORD string | SALT bytes HASH bytes } [ ADMIN ]
-- 创建管理员用户
CREATE USER h2 PASSWORD 'h2' ADMIN;-- 查看用户
sql> select * from information_schema.users;
USER_NAME | IS_ADMIN | REMARKS| TRUE     | null
H2        | TRUE     | null
(2 rows, 0 ms)-- 查看参数配置
sql> select * from information_schema.settings limit(3);
SETTING_NAME       | SETTING_VALUE
CREATE_BUILD       | 232
info.BUILD_ID      | 232
info.VERSION_MAJOR | 2
(3 rows, 1 ms)
sql> -- 嵌入式连接、他会自动创建一个数据库和用户,直接访问的是本地文件,有个独占排他的功能(可以不限制是否是在数据库的base目录下)
java -cp h2-2.3.232.jar org.h2.tools.Shell -url "jdbc:h2:/root/tmp/h2/data" -user h2 -password h2Welcome to H2 Shell 2.3.232 (2024-08-11)
Exit with Ctrl+C
Commands are case insensitive; SQL statements end with ';'
help or ?      Display this help
list           切换结果显示模式(在“结果列表模式”和“堆栈跟踪模式”之间切换)
maxwidth       设置最大列宽(默认值为 100)
autocommit     启用或禁用自动提交
history        显示最近执行的 20 条语句
quit or exit   关闭连接并退出

创建数据库(不能再远程创建,只能由DBA给创建好)

[root@PostgreSQL bin]# java -cp h2-*.jar org.h2.tools.ShellWelcome to H2 Shell 2.3.232 (2024-08-11)
Exit with Ctrl+C
[Enter]   jdbc:h2:~/test
URL       jdbc:h2:/data/h2/test
[Enter]   org.h2.Driver
Driver    org.h2.Driver
[Enter]   
User      sa
Password  Password  >>><
Connected
Commands are case insensitive; SQL statements end with ';'
help or ?      Display this help
list           Toggle result list / stack trace mode
maxwidth       Set maximum column width (default is 100)
autocommit     Enable or disable autocommit
history        Show the last 20 statements
quit or exit   Close the connection and exitsql> [root@PostgreSQL bin]# java -cp h2-*.jar org.h2.tools.ShellWelcome to H2 Shell 2.3.232 (2024-08-11)
Exit with Ctrl+C
[Enter]   jdbc:h2:~/test
URL       jdbc:h2:/data/h2/fbase
[Enter]   org.h2.Driver
Driver    org.h2.Driver
[Enter]   
User      sa
Password  Password  >>><
Type the same password again to confirm database creation.
Password  Password  >>><
Connected-- 使用远程连接
java -cp h2-*.jar org.h2.tools.Shell -url "jdbc:h2:tcp://192.168.202.159:9092/test" -user sa -password sajava -cp h2-*.jar org.h2.tools.Shell -url "jdbc:h2:tcp://192.168.202.159:9092/fbase" -user sa -password sa

使用web控制台的连接
在这里插入图片描述

在这里插入图片描述

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com