Linux,  openGauss

CentOS安装配置使用openGauss数据库

0 背景说明

华为基于PostgreSQL搞了个opengauss数据库,基于PG版的数据库。本文简单记录在CentOS上如何安装配置,基本使用该数据库,以及基本的错误解决。

opengauss官方网站:https://opengauss.org/zh/

1 安装软件包:

[root@localhost ~]# yum install flex* bison* ncurses-devel* glibc-devel* patch* redhat-lsb-core* readline-devel* libnsl* bzip* python3*

记得安装bzip,否则后面会有类似下述错误:

[root@localhost script]# ./gs_preinstall -U omm -G dbgrp -X /opt/software/openGauss/cluster_config_template.xml 
[GAUSS-50217] : Failed to decompress version.cfg.The cmd is cd /opt/software/openGauss/script/../ && tar -xpf `head -1 version.cfg`*.tar.bz2 ./version.cfg. The output is tar (child): lbzip2:无法 exec: 没有那个文件或目录
tar (child): Error is not recoverable: exiting now
tar: Child returned status 2
tar: Error is not recoverable: exiting now.
[root@localhost script]# yum install bzip*
已加载插件:fastestmirror
Loading mirror speeds from cached hostfile
 * base: mirrors.163.com
 * extras: mirrors.163.com
 * updates: mirrors.nju.edu.cn
软件包 bzip2-libs-1.0.6-13.el7.x86_64 已安装并且是最新版本
正在解决依赖关系

以及安装Python3:

[root@localhost script]# ./gs_preinstall -U omm -G dbgrp -X /opt/software/openGauss/cluster_config_template.xml 
/usr/bin/env: python3: 没有那个文件或目录
[root@localhost script]# py
pydoc      python     python2    python2.7  
[root@localhost script]# yum install python3*
已加载插件:fastestmirror
Loading mirror speeds from cached hostfile

2 关闭SELINUX和Firewall

[root@localhost ~]# setenforce 0
[root@localhost ~]# systemctl stop firewalld
[root@localhost ~]# systemctl status firewalld

3 创建路径、下载、解压软件:

[root@localhost ~]# mkdir -p /opt/software/openGauss
[root@localhost ~]# chmod 755 -R /opt/software
[root@localhost ~]# cd /opt/software/openGauss/
[root@localhost openGauss]#  wget https://opengauss.obs.cn-south-1.myhuaweicloud.com/2.0.0/x86/openGauss-2.0.0-CentOS-64bit-all.tar.gz
[root@localhost openGauss]# tar -zxvf openGauss-2.0.0-CentOS-64bit-all.tar.gz 
....
....
再解压om压缩文件:
[root@localhost openGauss]# tar -zxvf openGauss-2.0.0-CentOS-64bit-om.tar.gz 
...

5 编辑配置文件:

[root@localhost openGauss]# cp script/gspylib/etc/conf/cluster_config_template.xml .

编辑完之后的配置文件内容:

[root@localhost openGauss]# pwd
/opt/software/openGauss
[root@localhost openGauss]# ll
总用量 298556
-rw-------.  1 omm  dbgrp      1101 4月  19 16:09 cluster_config_template.xml
drwx------. 15 root root       4096 4月  19 16:10 lib
-r--------.  1 root root  101382908 3月  31 21:31 openGauss-2.0.0-CentOS-64bit-all.tar.gz
-r--------.  1 root root         65 3月  31 21:16 openGauss-2.0.0-CentOS-64bit-om.sha256
-r--------.  1 root root   12646330 3月  31 21:16 openGauss-2.0.0-CentOS-64bit-om.tar.gz
-r--------.  1 root root         65 3月  31 21:16 openGauss-2.0.0-CentOS-64bit.sha256
-r--------.  1 root root   89573052 3月  31 21:16 openGauss-2.0.0-CentOS-64bit.tar.bz2
-r--------.  1 root root  101942608 4月  19 16:10 openGauss-Package-bak_78689da9.tar.gz
drwx------.  6 root root       4096 3月  31 21:16 script
drwxr-xr-x.  2 root root       4096 3月  31 21:16 simpleInstall
-r--------.  1 root root         65 3月  31 21:15 upgrade_sql.sha256
-r--------.  1 root root     133704 3月  31 21:15 upgrade_sql.tar.gz
-r--------.  1 root root         32 3月  31 21:14 version.cfg
[root@localhost openGauss]# cat cluster_config_template.xml 
<?xml version="1.0" encoding="utf-8"?>
<ROOT>
  <CLUSTER>
    <PARAM name="clusterName" value="Cluster_template" />
    <PARAM name="nodeNames" value="localhost.localdomain"/>
    <PARAM name="gaussdbAppPath" value="/opt/huawei/install/app" />
    <PARAM name="gaussdbLogPath" value="/var/log/omm" />
    <PARAM name="tmpMppdbPath" value="/opt/huawei/tmp"/>
    <PARAM name="gaussdbToolPath" value="/opt/huawei/install/om" />
    <PARAM name="corePath" value="/opt/huawei/corefile"/>
    <PARAM name="backIp1s" value="172.16.11.148"/>
  </CLUSTER>
  
  <DEVICELIST>
    <DEVICE sn="localhost">
      <PARAM name="name" value="localhost.localdomain"/>
      <PARAM name="azName" value="AZ1"/>
      <PARAM name="azPriority" value="1"/>
      <PARAM name="backIp1" value="172.16.11.148"/>
      <PARAM name="sshIp1" value="172.16.11.148"/>
      <!-- dn -->
      <PARAM name="dataNum" value="1"/>
      <PARAM name="dataPortBase" value="15400"/>
      <PARAM name="dataNode1" value="/opt/huawei/install/data/dn"/>
      <PARAM name="dataNode1_syncNum" value="0"/>
    </DEVICE>
  </DEVICELIST>
</ROOT>
[root@localhost openGauss]# 

6 执行安装前检查:

[root@localhost openGauss]# pwd
/opt/software/openGauss
[root@localhost openGauss]# ./script/gs_preinstall -U omm -G dbgrp -X /opt/software/openGauss/cluster_config_template.xml 
Parsing the configuration file.
Successfully parsed the configuration file.
Installing the tools on the local node.
Successfully installed the tools on the local node.
Setting pssh path
Successfully set core path.
Are you sure you want to create the user[omm] and create trust for it (yes/no)? yes
Please enter password for cluster user.
Password: 
Please enter password for cluster user again.
Password: 
Successfully created [omm] user on all nodes.
Preparing SSH service.
Successfully prepared SSH service.
Checking OS software.
Successfully check os software.
Checking OS version.
Successfully checked OS version.
Creating cluster's path.
Successfully created cluster's path.
Setting SCTP service.
Successfully set SCTP service.
Set and check OS parameter.
Setting OS parameters.
Successfully set OS parameters.
Warning: Installation environment contains some warning messages.
Please get more details by "/opt/software/openGauss/script/gs_checkos -i A -h localhost.localdomain --detail".
Set and check OS parameter completed.
Preparing CRON service.
Successfully prepared CRON service.
Setting user environmental variables.
Successfully set user environmental variables.
Setting the dynamic link library.
Successfully set the dynamic link library.
Setting Core file
Successfully set core path.
Setting pssh path
Successfully set pssh path.
Set ARM Optimization.
No need to set ARM Optimization.
Fixing server package owner.
Setting finish flag.
Successfully set finish flag.
Preinstallation succeeded.
[root@localhost openGauss]# 

7 执行安装

[root@localhost openGauss]# su - omm
上一次登录:一 4月 19 17:08:22 CST 2021pts/1 上
[omm@localhost ~]$ which gs_install
/opt/huawei/install/om/script/gs_install
[omm@localhost ~]$ gs_install -X /opt/software/openGauss/cluster_config_template.xml 
Parsing the configuration file.
Check preinstall on every node.
Successfully checked preinstall on every node.
Creating the backup directory.
Successfully created the backup directory.
begin deploy..
Installing the cluster.
begin prepare Install Cluster..
Checking the installation environment on all nodes.
begin install Cluster..
Installing applications on all nodes.
Successfully installed APP.
begin init Instance..
encrypt cipher and rand files for database.
Please enter password for database:
Please repeat for database:
[GAUSS-50322] : Failed to encrypt the password for databaseError:
 Invalid password,it must contain at least eight characters
Try "gs_guc --help" for more information.
Please enter password for database:
Please repeat for database:
[GAUSS-50322] : Failed to encrypt the password for databaseError:
 Invalid password,it must contain at least three kinds of characters
Try "gs_guc --help" for more information.
Please enter password for database:
Please repeat for database:
begin to create CA cert files
The sslcert will be generated in /opt/huawei/install/app/share/sslcert/om
Cluster installation is completed.
Configuring.
Deleting instances from all nodes.
Successfully deleted instances from all nodes.
Checking node configuration on all nodes.
Initializing instances on all nodes.
[GAUSS-51400] : Failed to execute the command: source /home/omm/.bashrc;python3 '/opt/huawei/install/om/script/local/InitInstance.py' -U omm  -l /var/log/omm/omm/om/gs_local.log.Error:
Initializing instance.
[GAUSS-51615] : Failed to initialize instance. Command:/opt/huawei/install/app/bin/gs_initdb --locale=C -D /opt/huawei/install/data/dn --nodename=dn_6001  -C /opt/huawei/install/app/bin. Error:
The files belonging to this database system will be owned by user "omm".
This user must also own the server process.

The database cluster will be initialized with locale "C".
The default database encoding has accordingly been set to "SQL_ASCII".
The default text search configuration will be set to "english".

fixing permissions on existing directory /opt/huawei/install/data/dn ... ok
creating subdirectories ... ok
selecting default max_connections ... 10
selecting default shared_buffers ... 400kB
creating configuration files ... ok
creating template1 database in /opt/huawei/install/data/dn/base/1 ... gs_initdb: removing contents of data directory "/opt/huawei/install/data/dn"
[omm@localhost ~]$

8 错误GAUSS-51400和GAUSS-51615

https://gitee.com/opengauss/docs/issues/I38WUR?_from=gitee_search

当前机器内存配置比较小:

[omm@localhost ~]$ free -m
              total        used        free      shared  buff/cache   available
Mem:           3790         127        3287          90         375        3110
Swap:             0           0           0
[omm@localhost ~]$ 

9 安装成功

根据错误解决经验,更换了一台内存为8GB的新机器执行安装成功了。

[omm@localhost ~]$ free -m
               total        used        free      shared  buff/cache   available
 Mem:           7984        6336         546         720        1100          72
 Swap:          8063        3525        4538
 [omm@localhost ~]$ gs_install -X /opt/software/openGauss/cluster_config_template.xml 
 Parsing the configuration file.
 Check preinstall on every node.
 Successfully checked preinstall on every node.
 Creating the backup directory.
 Successfully created the backup directory.
 begin deploy..
 Installing the cluster.
 begin prepare Install Cluster..
 Checking the installation environment on all nodes.
 begin install Cluster..
 Installing applications on all nodes.
 Successfully installed APP.
 begin init Instance..
 encrypt cipher and rand files for database.
 Please enter password for database:
 Please repeat for database:
 begin to create CA cert files
 The sslcert will be generated in /opt/huawei/install/app/share/sslcert/om
 Cluster installation is completed.
 Configuring.
 Deleting instances from all nodes.
 Successfully deleted instances from all nodes.
 Checking node configuration on all nodes.
 Initializing instances on all nodes.
 Updating instance configuration on all nodes.
 Check consistence of memCheck and coresCheck on database nodes.
 Configuring pg_hba on all nodes.
 Configuration is completed.
 Successfully started cluster.
 Successfully installed application.
 end deploy..
 [omm@localhost ~]$

10 创建数据库

[omm@localhost ~]$ gsql -d postgres -p 15400
gsql ((openGauss 2.0.0 build 78689da9) compiled at 2021-03-31 21:04:03 commit 0 last mr  )
Non-SSL connection (SSL connection is recommended when requiring high-security)
Type "help" for help.
postgres=# \l+
                                                           List of databases
   Name    | Owner | Encoding  | Collate | Ctype | Access privileges | Size  | Tablespace |                Description                 
-----------+-------+-----------+---------+-------+-------------------+-------+------------+--------------------------------------------
 postgres  | omm   | SQL_ASCII | C       | C     |                   | 11 MB | pg_default | default administrative connection database
 template0 | omm   | SQL_ASCII | C       | C     | =c/omm           +| 10 MB | pg_default | default template for new databases
           |       |           |         |       | omm=CTc/omm       |       |            | 
 template1 | omm   | SQL_ASCII | C       | C     | =c/omm           +| 10 MB | pg_default | unmodifiable empty database
           |       |           |         |       | omm=CTc/omm       |       |            | 
(3 rows)

postgres=# CREATE DATABASE mydb WITH ENCODING 'GBK' template = template0;
CREATE DATABASE
postgres=# \l+     
                                                           List of databases
   Name    | Owner | Encoding  | Collate | Ctype | Access privileges | Size  | Tablespace |                Description                 
-----------+-------+-----------+---------+-------+-------------------+-------+------------+--------------------------------------------
 mydb      | omm   | GBK       | C       | C     |                   | 10 MB | pg_default | 
 postgres  | omm   | SQL_ASCII | C       | C     |                   | 11 MB | pg_default | default administrative connection database
 template0 | omm   | SQL_ASCII | C       | C     | =c/omm           +| 10 MB | pg_default | default template for new databases
           |       |           |         |       | omm=CTc/omm       |       |            | 
 template1 | omm   | SQL_ASCII | C       | C     | =c/omm           +| 10 MB | pg_default | unmodifiable empty database
           |       |           |         |       | omm=CTc/omm       |       |            | 
(4 rows)

postgres=# 

11 gsql远程连接数据库

前提条件:

服务端启动数据库服务;

配置白名单

客户端安装gsql

https://opengauss.org/zh/docs/2.0.0/docs/Quickstart/gsql%E5%AE%A2%E6%88%B7%E7%AB%AF%E8%BF%9E%E6%8E%A5.html

注意:客户端配置时的,PATH和LD_LIBRARY_PATH环境变量中,一个是指向/opt/software/bin,一个是指向了/opt/software/lib。不可粗心大意!!!!我就是不注意细节,导致,客户端通过gsql访问远程的数据库,一直报错。自己还傻呵呵的去手动创建链接:

[root@localhost ~]# gsql -d fdp -h 172.16.11.106 -U fdp -p 15400 -W Aa123456
gsql: error while loading shared libraries: libssl.so.1.1: cannot open shared object file: No such file or directory
[root@localhost ~]# openssl version
OpenSSL 1.1.1g  21 Apr 2020
[root@localhost ~]# find / -name libssl.so.1.1
/root/gauss/script/gspylib/clib/libssl.so.1.1
/root/gauss_om/omm/script/gspylib/clib/libssl.so.1.1
/opt/software/lib/libssl.so.1.1
/opt/software/bin/script/gspylib/clib/libssl.so.1.1
[root@localhost ~]# 
​
​
[root@localhost ~]# ln -s  /opt/software/lib/libssl.so.1.1 /usr/local/lib/libssl.so.1.1 
[root@localhost ~]# ln -s  /opt/software/lib/libssl.so.1.1 /usr/lib64/libssl.so.1.1 

12 Java程序通过JDBC访问数据库

安装opengauss官方的说法是,凡是之前可以运行在PostgreSQL数据库上的系统,都可以直接运行在opengauss的数据库上。

https://opengauss.org/zh/docs/2.0.0/docs/Quickstart/%E9%80%9A%E8%BF%87JDBC%E6%89%A7%E8%A1%8CSQL.html

JDBC包与驱动类

  • JDBC包在linux服务器端源代码目录下执行build.sh,获得驱动jar包postgresql.jar,包位置在源代码目录下。从发布包中获取, 包名为openGauss-1.0.1-操作系统版本号-64bit-Jdbc.tar.gz。驱动包与PostgreSQL保持兼容,其中类名、类结构与PostgreSQL驱动完全一致,曾经运行于PostgreSQL的应用程序可以直接移植到当前系统使用。
  • 驱动类在创建数据库连接之前,需要加载数据库驱动类“org.postgresql.Driver”。
a idea创建普通的maven工程
b 引入postgresql的驱动
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>org.example</groupId>
    <artifactId>gauss</artifactId>
    <version>1.0-SNAPSHOT</version>
<dependencies>
    <dependency>
        <groupId>org.postgresql</groupId>
        <artifactId>postgresql</artifactId>
        <version>42.2.20.jre7</version>
    </dependency>
</dependencies>

</project>

c 创建测试类DBTest

/**
 * @Author:asher
 * @Date:2021/6/3 15:36
 * @Description:PACKAGE_NAME
 * @Version:1.0
 */
//DBtest.java
//演示基于JDBC开发的主要步骤,会涉及创建数据库、创建表、插入数据等。

import java.sql.*;

public class DBTest {

    //创建数据库连接。
    public static Connection GetConnection(String username, String passwd) {
        String driver = "org.postgresql.Driver";
        String sourceURL = "jdbc:postgresql://172.16.11.106:15400/es_sam_dev";
//        String sourceURL = "jdbc:postgresql://172.16.11.35:5433/es_sam_dev";
        Connection conn = null;
        try {
            //加载数据库驱动。
            Class.forName(driver).newInstance();
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }

        try {
            //创建数据库连接。
            conn = DriverManager.getConnection(sourceURL, username, passwd);
            System.out.println("Connection succeed!");
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }

        return conn;
    };

    //执行普通SQL语句,创建customer_t1表。
    public static void CreateTable(Connection conn) {
        Statement stmt = null;
        try {
            stmt = conn.createStatement();

            //执行普通SQL语句。
            int rc = stmt
                    .executeUpdate("CREATE TABLE customer_t1(c_customer_sk INTEGER, c_customer_name VARCHAR(32));");

            stmt.close();
        } catch (SQLException e) {
            if (stmt != null) {
                try {
                    stmt.close();
                } catch (SQLException e1) {
                    e1.printStackTrace();
                }
            }
            e.printStackTrace();
        }
    }

    //执行预处理语句,批量插入数据。
    public static void BatchInsertData(Connection conn) {
        PreparedStatement pst = null;

        try {
            //生成预处理语句。
            pst = conn.prepareStatement("INSERT INTO customer_t1 VALUES (?,?)");
            for (int i = 0; i < 3; i++) {
                //添加参数。
                pst.setInt(1, i);
                pst.setString(2, "data " + i);
                pst.addBatch();
            }
            //执行批处理。
            pst.executeBatch();
            pst.close();
        } catch (SQLException e) {
            if (pst != null) {
                try {
                    pst.close();
                } catch (SQLException e1) {
                    e1.printStackTrace();
                }
            }
            e.printStackTrace();
        }
    }

    //执行预编译语句,更新数据。
    public static void ExecPreparedSQL(Connection conn) {
        PreparedStatement pstmt = null;
        try {
            pstmt = conn
                    .prepareStatement("UPDATE customer_t1 SET c_customer_name = ? WHERE c_customer_sk = 1");
            pstmt.setString(1, "new Data");
            int rowcount = pstmt.executeUpdate();
            pstmt.close();
        } catch (SQLException e) {
            if (pstmt != null) {
                try {
                    pstmt.close();
                } catch (SQLException e1) {
                    e1.printStackTrace();
                }
            }
            e.printStackTrace();
        }
    }


    //执行存储过程。
    public static void ExecCallableSQL(Connection conn) {
        CallableStatement cstmt = null;
        try {

            cstmt=conn.prepareCall("{? = CALL TESTPROC(?,?,?)}");
            cstmt.setInt(2, 50);
            cstmt.setInt(1, 20);
            cstmt.setInt(3, 90);
            cstmt.registerOutParameter(4, Types.INTEGER);  //注册out类型的参数,类型为整型。
            cstmt.execute();
            int out = cstmt.getInt(4);  //获取out参数
            System.out.println("The CallableStatment TESTPROC returns:"+out);
            cstmt.close();
        } catch (SQLException e) {
            if (cstmt != null) {
                try {
                    cstmt.close();
                } catch (SQLException e1) {
                    e1.printStackTrace();
                }
            }
            e.printStackTrace();
        }
    }


    /**
     * 主程序,逐步调用各静态方法。
     * @param args
     */
    public static void main(String[] args) {
        //创建数据库连接。
        Connection conn = GetConnection("es_sam_dev", "Es_sam_dev2");
//        Connection conn = GetConnection("es_sam_dev", "es_sam_dev");

        //创建表。
        CreateTable(conn);

        //批插数据。
        BatchInsertData(conn);

        //执行预编译语句,更新数据。
        ExecPreparedSQL(conn);

        //执行存储过程。
//        ExecCallableSQL(conn);

        //关闭数据库连接。
        try {
            conn.close();
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
}

13 报错解决

a SSL error , Could not generate DH keypair
org.postgresql.util.PSQLException: SSL error: java.lang.RuntimeException: Could not generate DH keypair
	at org.postgresql.ssl.MakeSSL.convert(MakeSSL.java:43)
	at org.postgresql.core.v3.ConnectionFactoryImpl.enableSSL(ConnectionFactoryImpl.java:534)
	at org.postgresql.core.v3.ConnectionFactoryImpl.tryConnect(ConnectionFactoryImpl.java:149)
	at org.postgresql.core.v3.ConnectionFactoryImpl.openConnectionImpl(ConnectionFactoryImpl.java:213)
	at org.postgresql.core.ConnectionFactory.openConnection(ConnectionFactory.java:51)
	at org.postgresql.jdbc.PgConnection.<init>(PgConnection.java:223)
	at org.postgresql.Driver.makeConnection(Driver.java:465)
	at org.postgresql.Driver.connect(Driver.java:264)
	at java.sql.DriverManager.getConnection(DriverManager.java:664)
	at java.sql.DriverManager.getConnection(DriverManager.java:247)
	at DBTest.GetConnection(DBTest.java:30)
	at DBTest.main(DBTest.java:146)
Caused by: javax.net.ssl.SSLException: java.lang.RuntimeException: Could not generate DH keypair

解决办法:opengauss数据库上关闭ssl验证。默认情况下,opengauss开启了ssl。

78 #ssl = on                               # (change requires restart)
79 ssl = off                               # (change requires restart)
b 用户口令加密方式错误Invalid or unsupported by client SCRAM mechanisms
org.postgresql.util.PSQLException: Invalid or unsupported by client SCRAM mechanisms
	at org.postgresql.jre7.sasl.ScramAuthenticator.processServerMechanismsAndInit(ScramAuthenticator.java:82)
	at org.postgresql.core.v3.ConnectionFactoryImpl.doAuthentication(ConnectionFactoryImpl.java:760)
	at org.postgresql.core.v3.ConnectionFactoryImpl.tryConnect(ConnectionFactoryImpl.java:161)
	at org.postgresql.core.v3.ConnectionFactoryImpl.openConnectionImpl(ConnectionFactoryImpl.java:213)
	at org.postgresql.core.ConnectionFactory.openConnection(ConnectionFactory.java:51)
	at org.postgresql.jdbc.PgConnection.<init>(PgConnection.java:223)
	at org.postgresql.Driver.makeConnection(Driver.java:465)
	at org.postgresql.Driver.connect(Driver.java:264)
	at java.sql.DriverManager.getConnection(DriverManager.java:664)
	at java.sql.DriverManager.getConnection(DriverManager.java:247)
	at DBTest.GetConnection(DBTest.java:30)
	at DBTest.main(DBTest.java:146)
Exception in thread "main" java.lang.NullPointerException
	at DBTest.CreateTable(DBTest.java:44)
	at DBTest.main(DBTest.java:150)

解决方法:

修改配置文件:

password_encryption_type = 0            #Password storage type, 0 is md5 for PG, 1 is sha256 + md5, 2 is sha256 only

然后,重置数据库用户的密码。

postgres=# alter user es_sam_dev password 'Es_sam_dev1';
ERROR:  The password cannot be reused.
postgres=# alter user es_sam_dev password 'Es_sam_dev3';
NOTICE:  The encrypted password contains MD5 ciphertext, which is not secure.
ALTER ROLE
postgres=# 

成功:

Connection succeed!

留言