opengauss数据库快速开始

opengauss数据库是一款基于postgres开发的国产数据库。

快速开始安装

下载安装包并解压

1
2
3
wget https://opengauss.obs.cn-south-1.myhuaweicloud.com/6.0.2/CentOS7/x86/openGauss-Server-6.0.2-CentOS7-x86_64.tar.bz2

mkdir -p /usr/local/opengauss && tar -jxvf openGauss-Server-6.0.2-CentOS7-x86_64.tar.bz2 -C /usr/local/opengauss

创建数据库用户

1
2
3
groupadd -g 1001 dbgrp
useradd -u 2001 -g dbgrp omm
chown -R omm:dbgrp /usr/local/opengauss

修改系统配置

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
cat >> /etc/sysctl.conf << EOF
net.ipv4.tcp_max_tw_buckets = 10000
net.ipv4.tcp_tw_reuse = 1
net.ipv4.tcp_tw_recycle = 1
net.ipv4.tcp_keepalive_time = 30
net.ipv4.tcp_keepalive_probes = 9
net.ipv4.tcp_keepalive_intvl = 30
net.ipv4.tcp_retries1 = 5
net.ipv4.tcp_syn_retries = 5
net.ipv4.tcp_synack_retries = 5
net.ipv4.tcp_retries2 = 12
vm.overcommit_memory = 0
net.ipv4.tcp_rmem = 8192 250000 16777216
net.ipv4.tcp_wmem = 8192 250000 16777216
net.core.wmem_max = 21299200
net.core.rmem_max = 21299200
net.core.wmem_default = 21299200
net.core.rmem_default = 21299200
net.ipv4.ip_local_port_range = 26000 65535
kernel.sem = 250 6400000 1000 25600
vm.min_free_kbytes = 102400 ##suggest to set as physical memory * 5%
net.core.somaxconn = 65535
net.ipv4.tcp_syncookies = 1
net.core.netdev_max_backlog = 65535
net.ipv4.tcp_max_syn_backlog = 65535
net.ipv4.tcp_fin_timeout = 60
kernel.shmall = 1152921504606846720
kernel.shmmax = 18446744073709551615
net.ipv4.tcp_sack = 1
net.ipv4.tcp_timestamps = 1
vm.extfrag_threshold = 500
vm.overcommit_ratio = 90
EOF

sysctl -p

开始安装

1
2
3
4
5
6
7
su - omm

cd /usr/local/opengauss/simpleInstall

sh install.sh -w "xxx" -p 54320

source ~/.bashrc

登录数据库

1
gsql -d postgres -p 54320

创建postgres用户并授权

1
2
3
4
CREATE USER postgres WITH PASSWORD "xxx";
GRANT ALL PRIVILEGES TO postgres;

\q # 退出sql命令行

授权postgres用户远程登录

1
gs_guc set -N all -I all -h "host all postgres 0.0.0.0/0 sha256"

开放端口

1
2
3
4
5
6
7
8
9
cd /usr/local/opengauss/data/single_node
vim pg_hba.conf

#host all all 127.0.0.1/32 trust
host all all 0.0.0.0/0 sha256

vim postgresql.conf

listen_addresses = '*'

JDBC使用

1
2
3
4
5
6
<!-- 添加依赖 -->
<dependency>
<groupId>org.opengauss</groupId>
<artifactId>opengauss-jdbc</artifactId>
<version>6.0.2</version>
</dependency>

代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
try (Connection conn = DriverManager.getConnection("jdbc:postgresql://ip:port/school", "postgres", "xxx");
PreparedStatement statement = conn.prepareStatement("select * from student");
ResultSet resultSet = statement.executeQuery()){
while (resultSet.next()) {
ResultSetMetaData metaData = resultSet.getMetaData();
for (int i = 0; i < metaData.getColumnCount(); i++) {
String columnName = metaData.getColumnName(i + 1);
System.out.println(columnName + ": " + resultSet.getString(columnName));
}
System.out.println("---------------");
}
} catch (SQLException e) {
throw new RuntimeException(e);
}