当前位置:   article > 正文

国产开源数据库openGauss的安装运行

国产开源数据库

步骤一:OpenGauss 的安装

环境

OS:openEuler 20.03 64bit with ARM

架构:arm64

部署:单机

安装过程

1、环境配置

安装依赖包:

yum install libaio-devel flex bison ncurses-devel glibc-devel patch readline-devel
2、创建 xml 配置文件

创建 cluster_config.xml 配置文件并进行配置

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <ROOT>
  3. <!-- openGauss整体信息 -->
  4. <CLUSTER>
  5. <!-- 数据库名称 -->
  6. <PARAM name="clusterName" value="GaussDB" />
  7. <!-- 数据库节点名称(hostname) -->
  8. <PARAM name="nodeNames" value="node1" />
  9. <!-- 节点IP,与nodeNames一一对应 -->
  10. <PARAM name="backIp1s" value="***.***.***.***87"/>
  11. <!-- 数据库安装目录-->
  12. <PARAM name="gaussdbAppPath" value="/opt/huawei/install/app" />
  13. <!-- 日志目录-->
  14. <PARAM name="gaussdbLogPath" value="/var/log/omm" />
  15. <!-- 临时文件目录-->
  16. <PARAM name="tmpMppdbPath" value="/opt/huawei/tmp"/>
  17. <!--数据库工具目录-->
  18. <PARAM name="gaussdbToolPath" value="/opt/huawei/install/om" />
  19. <!--数据库core文件目录-->
  20. <PARAM name="corePath" value="/opt/huawei/corefile"/>
  21. <!-- openGauss类型,此处示例为单机类型,“single-inst”表示单机一主多备部署形态-->
  22. <PARAM name="clusterType" value="single-inst"/>
  23. </CLUSTER>
  24. <!-- 每台服务器上的节点部署信息 -->
  25. <DEVICELIST>
  26. <!-- node1上的节点部署信息 -->
  27. <DEVICE sn="1000001">
  28. <!-- node1的hostname -->
  29. <PARAM name="name" value="ecs-kc1-large-2-linux-20210605213652"/>
  30. <!-- node1所在的AZ及AZ优先级 -->
  31. <PARAM name="azName" value="AZ1"/>
  32. <PARAM name="azPriority" value="1"/>
  33. <!-- 如果服务器只有一个网卡可用,将backIP1和sshIP1配置成同一个IP -->
  34. <PARAM name="backIp1" value="***.***.***.***87"/>
  35. <PARAM name="sshIp1" value="***.***.***.***87"/>
  36. <!--dbnode-->
  37. <PARAM name="dataNum" value="1"/>
  38. <!--DBnode端口号-->
  39. <PARAM name="dataPortBase" value="26000"/>
  40. <!--DBnode主节点上数据目录,及备机数据目录-->
  41. <PARAM name="dataNode1" value="node1"/>
  42. <!--DBnode节点上设定同步模式的节点数-->
  43. <PARAM name="dataNode1_syncNum" value="0"/>
  44. </DEVICE>
  45. </DEVICELIST>
  46. </ROOT>

注意节点 hostname 应与/etc/hostname 中保持一致

3、初始化安装环境

1.以 root 用户登录待安装 openGauss 的任意主机,并按规划创建存放安装包的目录。

  1. mkdir -p /opt/software/openGauss
  2. chmod 755 -R /opt/software

2.下载安装包并将配置文件“cluster_config.xml”都上传至上一步所创建的目录中。

wget https://opengauss.obs.cn-south-1.myhuaweicloud.com/2.0.1/arm/openGauss-2.0.1-openEuler-64bit-all.tar.gz

3.解压安装包。

  1. tar -zxvf openGauss-2.0.1-openEuler-64bit-all.tar.gz
  2. tar jxvf openGauss-2.0.1-openEuler-64bit.tar.bz2
  3. tar -zxvf openGauss-2.0.1-openEuler-64bit-om.tar.gz
  4. tar -zxvf upgrade_sql.tar.gz

4.进入到工具脚本存放目录下。

cd /opt/software/openGauss/script

5.如果是 openEuler 的操作系统为确保适配 python 版本,执行如下命令打开 gspylib/common/CheckPythonVersion.py 文件,将 if not pythonVersion = = (3, 6):修改为 if not pythonVersion > = (3, 6):,键入“ESC”键进入指令模式,执行:wq保存并退出修改。(我在实际操作中进入后发现无需修改)

vi gspylib/common/CheckPythonVersion.py

6.如果是 openEuler 的操作系统,执行如下命令打开 performance.sh 文件,用#注释 sysctl -w vm.min_free_kbytes=112640 &> /dev/null,键入“ESC”键进入指令模式,执行:wq保存并退出修改。

vi /etc/profile.d/performance.sh

7.为确保 openssl 版本正确,执行预安装前请加载安装包中 lib 库。执行命令如下,其中{packagePath}为用户安装包放置的路径,本示例中为/opt/software/openGauss。

export LD_LIBRARY_PATH=/opt/software/openGauss/script/gspylib/clib:$LD_LIBRARY_PATH

8.为确保成功安装,检查 hostname 与 /etc/hostname 是否一致。预安装过程中,会对 hostname 进行检查。

9.使用 gs_preinstall 准备好安装环境。若为共用环境需加入—sep-env-file=ENVFILE 参数分离环境变量,避免与其他用户相互影响,ENVFILE 为用户自行指定的环境变量分离文件的路径。

  • 采用交互模式执行前置,并在执行过程中自动创建操作系统 root 用户互信和 omm 用户互信:

    ./gs_preinstall -U omm -G dbgrp -X /opt/software/openGauss/cluster_config.xml
    

    在执行中会要求输入 omm 用户的密码。

    运行结果应类似:

    1. plat1:/opt/software/openGauss/script # ./gs_preinstall -U omm -G dbgrp -X /opt/software/openGauss/cluster_config.xml
    2. Parsing the configuration file.
    3. Successfully parsed the configuration file.
    4. Installing the tools on the local node.
    5. Successfully installed the tools on the local node.
    6. Are you sure you want to create trust for root (yes/no)? yes
    7. Please enter password for root.
    8. Password:
    9. Creating SSH trust for the root permission user.
    10. Checking network information.
    11. All nodes in the network are Normal.
    12. Successfully checked network information.
    13. Creating SSH trust.
    14. Creating the local key file.
    15. Successfully created the local key files.
    16. Appending local ID to authorized_keys.
    17. Successfully appended local ID to authorized_keys.
    18. Updating the known_hosts file.
    19. Successfully updated the known_hosts file.
    20. Appending authorized_key on the remote node.
    21. Successfully appended authorized_key on all remote node.
    22. Checking common authentication file content.
    23. Successfully checked common authentication content.
    24. Distributing SSH trust file to all node.
    25. Successfully distributed SSH trust file to all node.
    26. Verifying SSH trust on all hosts.
    27. Successfully verified SSH trust on all hosts.
    28. Successfully created SSH trust.
    29. Successfully created SSH trust for the root permission user.
    30. Setting pssh path
    31. Successfully set core path.
    32. Distributing package.
    33. Begin to distribute package to tool path.
    34. Successfully distribute package to tool path.
    35. Begin to distribute package to package path.
    36. Successfully distribute package to package path.
    37. Successfully distributed package.
    38. Are you sure you want to create the user[omm] and create trust for it (yes/no)? yes
    39. Please enter password for cluster user.
    40. Password:
    41. Please enter password for cluster user again.
    42. Password:
    43. Successfully created [omm] user on all nodes.
    44. Preparing SSH service.
    45. Successfully prepared SSH service.
    46. Installing the tools in the cluster.
    47. Successfully installed the tools in the cluster.
    48. Checking hostname mapping.
    49. Successfully checked hostname mapping.
    50. Creating SSH trust for [omm] user.
    51. Checking network information.
    52. All nodes in the network are Normal.
    53. Successfully checked network information.
    54. Creating SSH trust.
    55. Creating the local key file.
    56. Successfully created the local key files.
    57. Appending local ID to authorized_keys.
    58. Successfully appended local ID to authorized_keys.
    59. Updating the known_hosts file.
    60. Successfully updated the known_hosts file.
    61. Appending authorized_key on the remote node.
    62. Successfully appended authorized_key on all remote node.
    63. Checking common authentication file content.
    64. Successfully checked common authentication content.
    65. Distributing SSH trust file to all node.
    66. Successfully distributed SSH trust file to all node.
    67. Verifying SSH trust on all hosts.
    68. Successfully verified SSH trust on all hosts.
    69. Successfully created SSH trust.
    70. Successfully created SSH trust for [omm] user.
    71. Checking OS software.
    72. Successfully check os software.
    73. Checking OS version.
    74. Successfully checked OS version.
    75. Creating cluster's path.
    76. Successfully created cluster's path.
    77. Setting SCTP service.
    78. Successfully set SCTP service.
    79. Set and check OS parameter.
    80. Setting OS parameters.
    81. Successfully set OS parameters.
    82. Preparing CRON service.
    83. Successfully prepared CRON service.
    84. Setting user environmental variables.
    85. Successfully set user environmental variables.
    86. Setting the dynamic link library.
    87. Successfully set the dynamic link library.
    88. Setting Core file
    89. Successfully set core path.
    90. Setting pssh path
    91. Successfully set pssh path.
    92. Set ARM Optimization.
    93. No need to set ARM Optimization.
    94. Fixing server package owner.
    95. Setting finish flag.
    96. Successfully set finish flag.
    97. Preinstallation succeeded.
4、执行安装

内存小于安装要求的 32G 应该做一些配置修改:

  1. # vim /etc/sysctl.conf
  2. kernel.shmall = 1125899906842624
  3. kernel.shmmax = 1351079888211149
  4. # vim /opt/huawei/install/data/db1/postgresql.conf
  5. cstore_buffers=16MB
  6. bulk_write_ring_size=128MB
  7. shared_buffers=128MB
  8. max_process_memory=2GB
  9. max_connections=10

切换到 omm 用户下执行安装:

  1. su - omm
  2. gs_install -X /opt/software/openGauss/cluster_config.xml

步骤二 数据库的简单链接与使用

1、创建新用户,新数据库并赋予权限

使用 gsql 用 omm 管理账号登陆,创建新用户 jack,创建新数据库 testjack,赋予权限,执行

  1. CREATE USER jack PASSWORD 'Gaussdba@Mpp';
  2. CREATE DATABASE testjack OWNER jack;
  3. GRANT SELECT ON pg_catalog.pg_roles to jack;
  4. GRANT SELECT ON pg_catalog.pg_user_status to jack;
  5. GRANT ALL PRIVILEGES on TABLESPACE pg_default,pg_global TO jack;

然后退出,使用 jack 用户登录 gsql

gsql -U jack -d testjack -p "Gaussdba@Mpp"

创建 SCHEMA

CREATE SCHEMA jack AUTHORIZATION jack;

退出 gsql,赋予 jack 权限,这里 client_address 是客户端的地址

gs_guc set -N all -I all -h "host all jack client_address/32 sha256

或者也可以修改 pg_hba.conf,添加

host    all             jack            client_address/32         sha256

2、允许客户端访问数据库

执行,这里的 client_address 是要客户端的地址, listen_addresses 是参数名。

gs_guc set -I all -c "listen_addresses='client_address'"

或在使用 omm 账号在 gsql 中

ALTER SYSTEM SET listen_addresses TO "client_address";

之后重启数据库

3、关闭防火墙,打开端口

4、使用 Data Studio 访问数据库

可在 opengauss 官网下载 DATA STUDIO 应用

<img src='./img/image-20210913111340859.png'>

填入对应参数,注意这里应去掉启用 SSL 的选项,因为 SSL 还需要配置证书或密钥。

连接后的界面

<img src='./img/image-20210913111551057.png'>

5、使用 JDBC 访问数据库

我这里使用 windows 系统作为客户端连接数据库。

在 opengauss 网站下载对应的 JDBC 包,并解压。

创建 Gauss.java 文件

  1. import java.sql.Connection;
  2. import java.sql.DriverManager;
  3. import java.sql.PreparedStatement;
  4. import java.sql.ResultSet;
  5. import java.sql.ResultSetMetaData;
  6. import java.sql.SQLException;
  7. import java.sql.Statement;
  8. import java.sql.CallableStatement;
  9. import java.sql.Types;
  10. import java.util.Collections;
  11. import java.util.Properties;
  12. public class Gauss {
  13. public static void main(String[] args) {
  14. Connection connection;
  15. ResultSet resultSet;
  16. String url = "jdbc:postgresql://address:port/testjack"; //address 地址 port 端口 testjack 数据库名
  17. String user = "××××"; // 数据库用户名
  18. String password = "××××"; // 对应密码
  19. String sql;
  20. if(args.length > 0)
  21. {
  22. sql = args[0];
  23. }
  24. else
  25. {
  26. System.out.println("输入一条sql语句");
  27. return;
  28. }
  29. if ((connection = getConnect(user, password, url)) != null) {
  30. System.out.println(connection.toString());
  31. }
  32. if ((resultSet = execSql(connection, sql)) != null)
  33. {
  34. }
  35. }
  36. // 以下代码将获取数据库连接操作封装为一个接口,可通过给定用户名和密码来连接数据库。
  37. public static Connection getConnect(String username, String passwd, String url) {
  38. // 驱动类。
  39. String driver = "org.postgresql.Driver";
  40. // 数据库连接描述符。
  41. String sourceURL = url;
  42. Connection conn = null;
  43. try {
  44. // 加载驱动。
  45. Class.forName(driver);
  46. } catch (Exception e) {
  47. e.printStackTrace();
  48. return null;
  49. }
  50. try {
  51. // 创建连接。
  52. conn = DriverManager.getConnection(sourceURL, username, passwd);
  53. System.out.println("Connection succeed!");
  54. } catch (Exception e) {
  55. e.printStackTrace();
  56. return null;
  57. }
  58. return conn;
  59. };
  60. // 以下代码将使用Properties对象作为参数建立连接
  61. public static Connection getConnectUseProp(String username, String passwd, String url) {
  62. // 驱动类。
  63. String driver = "org.postgresql.Driver";
  64. // 数据库连接描述符。
  65. String sourceURL = url + "?";
  66. Connection conn = null;
  67. Properties info = new Properties();
  68. try {
  69. // 加载驱动。
  70. Class.forName(driver);
  71. } catch (Exception e) {
  72. e.printStackTrace();
  73. return null;
  74. }
  75. try {
  76. info.setProperty("user", username);
  77. info.setProperty("password", passwd);
  78. // 创建连接。
  79. conn = DriverManager.getConnection(sourceURL, info);
  80. System.out.println("Connection succeed!");
  81. } catch (Exception e) {
  82. e.printStackTrace();
  83. return null;
  84. }
  85. return conn;
  86. };
  87. public static ResultSet execSql(Connection conn, String sql) {
  88. Statement stmt = null;
  89. ResultSet rs = null;
  90. SQLWarning sqlw = null;
  91. try {
  92. stmt = conn.createStatement();
  93. // 执行普通SQL语句。
  94. stmt.execute(sql);
  95. if((sqlw = stmt.getWarnings()) != null)
  96. System.out.println(sqlw.toString());
  97. if((rs = stmt.getResultSet()) != null)
  98. printResultSet(rs);
  99. stmt.close();
  100. } catch (SQLException e) {
  101. if (stmt != null) {
  102. try {
  103. stmt.close();
  104. } catch (SQLException e1) {
  105. e1.printStackTrace();
  106. }
  107. }
  108. e.printStackTrace();
  109. }
  110. return rs;
  111. }
  112. private static void printResultSet(ResultSet rs)
  113. {
  114. String line = "";
  115. try {
  116. ResultSetMetaData rsmd = rs.getMetaData();
  117. for(int i = 1; i <= rsmd.getColumnCount(); i ++)
  118. {
  119. String label = rsmd.getColumnLabel(i).toString();
  120. System.out.print(label + "\t");
  121. line += String.join("", Collections.nCopies(label.length(), "-")) + "\t";
  122. }
  123. System.out.println("\n" + line);
  124. while(rs.next())
  125. {
  126. for(int i = 1; i <= rsmd.getColumnCount(); i ++)
  127. {
  128. System.out.print(rs.getObject(i).toString() + "\t");
  129. }
  130. System.out.println("");
  131. }
  132. } catch (Exception e) {
  133. e.printStackTrace();
  134. }
  135. }
  136. }

编译

javac .\Gauss.java -encoding "utf-8"

运行,我这里将 postgresql.jar 放在同一目录下,创建一个表 nt 作为测试

  1. java -cp ".;postgresql.jar" Gauss "CREATE TABLE nt(id INTEGER, name VARCHAR(20))"
  2. java -cp ".;postgresql.jar" Gauss "INSERT into nt(id, name) VALUES (1,'n1'),(2,'n2'),(3,'n3');"
  3. java -cp ".;postgresql.jar" Gauss "SELECT * FROM nt;"

最后一句输出结果为,可以看到成功进行了连接和操作。

  1. org.postgresql.core.v3.ConnectionFactoryImpl openConnectionImpl
  2. 信息: [75000bb7-1475-4579-94cb-f53a01bec9eb] Try to connect. IP: ***.***.***.***:****
  3. org.postgresql.core.v3.ConnectionFactoryImpl openConnectionImpl
  4. 信息: [***.***.***.***:****/***.***.***.***:****] Connection is established. ID: 75000bb7-1475-4579-94cb-f53a01bec9eb
  5. org.postgresql.core.v3.ConnectionFactoryImpl openConnectionImpl
  6. 信息: Connect complete. ID: 75000bb7-1475-4579-94cb-f53a01bec9eb
  7. Connection succeed!
  8. id name
  9. -- ----
  10. 1 n1
  11. 2 n2
  12. 3 n3
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/小蓝xlanll/article/detail/504845
推荐阅读
相关标签
  

闽ICP备14008679号