其他分享
首页 > 其他分享> > LightDB快速入门

LightDB快速入门

作者:互联网

LightDB是什么?

  LightDB是一款基于PostgreSQL的关系型数据库,99%兼容PostgreSQL,主要针对金融业务场景优化。不同于国内其他基于PostgreSQL的数据库为了修改而修改,导致无法持续向后兼容新版本的PostgreSQL,进而无法享受社区带来的改进。LightDB采用完全拥抱开源的模式,在开源PostgreSQL基础上进行增强如:支持开源PostgreSQL不支持的分区定义语法、运行时被剪除的分区不显示、实现了pg_stat_monitor&pg_profile等监控没有包含的等待事件(这是DBA优化数据库最重要的分析信息)、类似Oracle的自动参数优化、默认主机免密登录&远程md5认证等等。针对Linux平台优化(不支持也无计划支持Windows),并持续以patch模式回馈社区,采用和Percona Server、EnterpriseDB一样的运作模式。

  不同于国内要么仅提供github开源、要么必须先走销售后提供使用包的模式,LightDB选择不开源,但是无功能、容量限制180天下载试用的机制,所以开发人员无需担心被双标。

快速入门

1、安装

  用户可以从LightDB官网https://www.hs.net/lightdb下载LightDB对应版本,当前提供x86、arm两个版本(Q3末将提供麒麟OS、欧拉版本),分单机与高可用版(Q3末高可用和单机版安装包将合二为一,同时支持GUI和CLI安装),其中高可用版当前仅支持x86。下面以x86为例,先下载LightDB-X13.3-21.1-el7.x86_64.bin(注意:需要目标操作系统为redhat/centos 7.x版本,6和8可能会有依赖问题)。

 

   下载好之后,上传到linux,如下:

[root@hs-10-20-30-127 ~]# ll | grep LightDB-X13.3-21.1-el7.x86_64.bin 
-rw-r--r--   1 root root  96927077 Aug 21 17:56 LightDB-X13.3-21.1-el7.x86_64.bin

  可以使用root或lightdb用户安装LightDB数据库,如果未预创建lightdb,则安装过程中会自动创建。如下:

[root@hs-10-20-30-127 ~]# chmod a+x LightDB-X13.3-21.1-el7.x86_64.bin 
[root@hs-10-20-30-127 ~]# ./LightDB-X13.3-21.1-el7.x86_64.bin install
Do you want to install LightDB?[Y/N]:Y
Do you need to add user 'lightdb' and group 'lightdb'?[Y/N]:Y
Which directory do you want to install to? [/home/lightdb]:Y
Please enter the correct directory absolute path:
Please enter the correct directory absolute path:/home/lightdb
Install to directory '/home/lightdb'?[Y/N]:Y
lightdb install path is: '/home/lightdb/lightdb13.3-21.1'
lightdb bin directory is: '/home/lightdb/lightdb13.3-21.1/lightdb-x/bin'
uuid-1.6.2-26.el7.x86_64
Install db ...
lightdb_after_install.sh is not exists,now set environment
configuring environment variable of lightdb ... config path done
configuring lib environment variable of lightdb ... config libpath done
lightdb installed success,next step to init default instance,Do you want to create default cluster?[Y/N]:Y
Now configure instance parameters ...
 INFO  modify effective_cache_size successfully
 INFO  modify wal_buffers successfully
 INFO  modify max_wal_size successfully
 INFO  modify min_wal_size successfully
 INFO  modify random_page_cost successfully
 INFO  modify max_parallel_maintenance_workers successfully
 INFO  modify default_statistics_target successfully
 INFO  modify min_parallel_index_scan_size successfully
 INFO  modify max_worker_processes successfully
 INFO  modify auto_explain.log_min_duration successfully
 INFO  modify max_parallel_workers successfully
 INFO  modify logging_collector successfully
 INFO  modify enable_partitionwise_aggregate successfully
 INFO  modify log_min_duration_statement successfully
 INFO  modify shared_buffers successfully
 INFO  modify parallel_setup_cost successfully
 INFO  modify track_io_timing successfully
 INFO  modify log_min_messages successfully
 INFO  modify temp_buffers successfully
 INFO  modify commit_siblings successfully
 INFO  modify min_parallel_table_scan_size successfully
init default instance sucess,We will start this install by default port 5432,please remember your password
wJqnP9HlWz   -- lightdb用户默认初始密码
waiting for server to start.... done
server started
LightDB install finish,welcome to use LightDB
[root@hs-10-20-30-127 ~]# lsof -i:5432
COMMAND   PID    USER   FD   TYPE DEVICE SIZE/OFF NODE NAME
lightdb 20025 lightdb    6u  IPv4 329909      0t0  TCP *:postgres (LISTEN)

  不用一分钟,很简单,就安装好了。

  卸载同样简单,install子目录下包含了一个uninstall.sh脚本,可用于一键干净卸载。

[lightdb@hs-10-20-30-127 ~]$ ll
total 192
drwxrwxr-x 3 lightdb lightdb     28 Aug 21 18:19 data
drwxr-xr-x 5 lightdb lightdb     52 Aug 21 18:18 lightdb13.3-21.1
-rw-rw-r-- 1 lightdb lightdb 194232 Aug 21 19:40 pwr_1_10.html
[lightdb@hs-10-20-30-127 ~]$ cd lightdb13.3-21.1/
[lightdb@hs-10-20-30-127 lightdb13.3-21.1]$ ll
total 0
drwxrwxr-x 3 lightdb lightdb 106 Aug 21 18:19 install
drwxrwxr-x 7 lightdb lightdb  69 Aug 17 21:43 lightdb-x
drwxrwxr-x 7 lightdb lightdb  67 Aug  3 15:57 pgpool
[lightdb@hs-10-20-30-127 lightdb13.3-21.1]$ cd install/
[lightdb@hs-10-20-30-127 install]$ ll
total 20
-rw-rw-r-- 1 lightdb lightdb 6109 Aug 21 18:19 install_2021_08_21_181825.log
-rwxrwxr-x 1 lightdb lightdb 3246 Aug 21 18:18 lightdb-uninstall.sh
-rw-rw-r-- 1 lightdb lightdb 4671 Aug 21 18:18 README.md
drwxr-xr-x 2 lightdb lightdb   42 Aug 21 18:18 system-lib
[lightdb@hs-10-20-30-127 install]$ pwd
/home/lightdb/lightdb13.3-21.1/install

2、简单使用

  在主机端,可以使用ltsql(类似于mysql客户端或oracle sql*plus)登录LightDB数据库(一般来说,除了运维不得已以及一些特殊场景,大部分开发人员都会通过navicat或dbeaver访问LightDB,因为LightDB兼容PostgreSQL协议,因此和PostgreSQL一样的方式去连接即可)。如下:

[lightdb@hs-10-20-30-127 ~]$ ltsql
ltsql (13.3-21.1)
Type "help" for help.

[lightdb@hs-10-20-30-127 ~]$ ltsql
ltsql (13.3-21.1)
Type "help" for help.

postgres=# \l
                                List of databases
   Name    |  Owner  | Encoding |   Collate   |    Ctype    |  Access privileges  
-----------+---------+----------+-------------+-------------+---------------------
 postgres  | lightdb | UTF8     | en_US.UTF-8 | en_US.UTF-8 | 
 template0 | lightdb | UTF8     | en_US.UTF-8 | en_US.UTF-8 | =c/lightdb         +
           |         |          |             |             | lightdb=CTc/lightdb
 template1 | lightdb | UTF8     | en_US.UTF-8 | en_US.UTF-8 | =c/lightdb         +
           |         |          |             |             | lightdb=CTc/lightdb
(3 rows)

postgres=# create database my_lightdb;
CREATE DATABASE
postgres=# \c my_lightdb;
You are now connected to database "my_lightdb" as user "lightdb".
my_lightdb=# create table my_tab(id serial/*类似于mysql auto_increment,oracle序列+序列作为默认值*/,name varchar(100),desc1 varchar(1000));
CREATE TABLE
my_lightdb=# insert into my_tab(name,desc1) values('name1','desc1');
INSERT 0 1

my_lightdb=# insert into my_tab(name,desc1) select uuid_ns_oid(),uuid_ns_oid() from generate_series(1,1000);   -- 批量创建数据,类似于oracle的connect by level,比mysql方便的多
INSERT 0 1000
my_lightdb=# select * from my_tab limit 1;
 id | name  | desc1 
----+-------+-------
  1 | name1 | desc1
(1 row)

my_lightdb=# select * from my_tab limit 2;
 id |                 name                 |                desc1                 
----+--------------------------------------+--------------------------------------
  1 | name1                                | desc1
  2 | 6ba7b812-9dad-11d1-80b4-00c04fd430c8 | 6ba7b812-9dad-11d1-80b4-00c04fd430c8
(2 rows)

  查看SQL语句的执行时间,可以打开ltsql客户端的\timing选项,如下:

my_lightdb=# \timing on
Timing is on.
my_lightdb=# select count(1) from my_tab;
 count 
-------
  1001
(1 row)

Time: 0.420 ms
my_lightdb=# select count(1) from my_tab;
 count 
-------
  1001
(1 row)

Time: 0.151 ms

  查看当前数据库下的对象及其大小,可以执行ltsql命令\dS+,如下:

my_lightdb=# \dS+
                                             List of relations
   Schema   |              Name               |   Type   |  Owner  | Persistence |    Size    | Description 
------------+---------------------------------+----------+---------+-------------+------------+-------------
 pg_catalog | pg_aggregate                    | table    | lightdb | permanent   | 56 kB      | 
 pg_catalog | pg_am                           | table    | lightdb | permanent   | 40 kB      | 
 pg_catalog | pg_amop                         | table    | lightdb | permanent   | 80 kB      | 
 pg_catalog | pg_amproc                       | table    | lightdb | permanent   | 64 kB      | 
......
......
 public     | dual                            | view     | lightdb | permanent   | 0 bytes    | 
 public     | my_tab                          | table    | lightdb | permanent   | 152 kB     | 
 public     | my_tab_id_seq                   | sequence | lightdb | permanent   | 8192 bytes | 
 public     | pg_stat_statements              | view     | lightdb | permanent   | 0 bytes    | 
 public     | pg_wait_sampling_current        | view     | lightdb | permanent   | 0 bytes    | 
 public     | pg_wait_sampling_history        | view     | lightdb | permanent   | 0 bytes    | 
 public     | pg_wait_sampling_profile        | view     | lightdb | permanent   | 0 bytes    | 
(136 rows)

  查看特定表的定义:

my_lightdb=# \dS+ my_tab;
                                                        Table "public.my_tab"
 Column |          Type           | Collation | Nullable |              Default               | Storage  | Stats target | Description 
--------+-------------------------+-----------+----------+------------------------------------+----------+--------------+-------------
 id     | integer                 |           | not null | nextval('my_tab_id_seq'::regclass) | plain    |              | 
 name   | character varying(100)  |           |          |                                    | extended |              | 
 desc1  | character varying(1000) |           |          |                                    | extended |              | 
Access method: heap

  查看SQL语句的执行计划可以使用explain,如下:

my_lightdb=# explain select * from my_tab where id = 100;
                       QUERY PLAN                       
--------------------------------------------------------
 Seq Scan on my_tab  (cost=0.00..26.51 rows=1 width=76)
   Filter: (id = 100)
(2 rows)

Time: 0.289 ms
my_lightdb=# explain (analyze,verbose) select * from my_tab where id = 100;
                                               QUERY PLAN                                                
---------------------------------------------------------------------------------------------------------
 Seq Scan on public.my_tab  (cost=0.00..26.51 rows=1 width=76) (actual time=0.020..0.088 rows=1 loops=1)
   Output: id, name, desc1
   Filter: (my_tab.id = 100)
   Rows Removed by Filter: 1000
 Planning Time: 0.048 ms
 Execution Time: 0.110 ms
(6 rows)

Time: 0.386 ms
my_lightdb=# explain (analyze,verbose,buffers/* buffers选项能够查看每个行源rowsource的逻辑和物理读次数,使得分析更加直接*/) select * from my_tab where id = 100;
                                               QUERY PLAN                                                
---------------------------------------------------------------------------------------------------------
 Seq Scan on public.my_tab  (cost=0.00..26.51 rows=1 width=76) (actual time=0.019..0.076 rows=1 loops=1)
   Output: id, name, desc1
   Filter: (my_tab.id = 100)
   Rows Removed by Filter: 1000
   Buffers: shared hit=14
 Planning Time: 0.050 ms
 Execution Time: 0.094 ms
(7 rows)

Time: 0.385 ms

3、文件导入导出

  文本文件是日常需求之一,LightDB支持客户端和服务端导入导出,\copy(ltsql命令)是客户端导入导出,copy(SQL语句)是服务端导入导出。

  就导入而言,\copy性能一般,不适用于生产大数据量导入导出,LightDB下个版本将包含提供类似于oracle sqlloader的大数据加载工具。

  就导出而言,\copy可以开多个会话并行导出,同一个表的不同分区也可以并行导出,因此可以满足生产需求。

my_lightdb=# \copy (select * from ora_datatype) to '/home/lightdb/t.csv' delimiter '|' csv header;
COPY 5
my_lightdb=# exit
[lightdb@hs-10-20-30-127 install]$ cd
[lightdb@hs-10-20-30-127 ~]$ more t.csv 
id|name|create_date
1|ff|2021-08-21
2|should b|2021-08-21
3|xx|2021-08-21
4|baba|2021-08-21
5|baba|2020-12-21
[lightdb@hs-10-20-30-127 ~]$ ltsql
ltsql (13.3-21.1)
Type "help" for help.

postgres=# \c my_lightdb 
You are now connected to database "my_lightdb" as user "lightdb".

my_lightdb=# create table ora_datatype_2 (like ora_datatype);  -- 默认值、序列、索引都不包含
CREATE TABLE
my_lightdb=# create table ora_datatype_3 (like ora_datatype INCLUDING INDEXES INCLUDING COMMENTS);  -- 不包含序列定义
CREATE TABLE
my_lightdb=# create table ora_datatype_4 (like ora_datatype INCLUDING DEFAULTS);  -- 包含了序列定义,不合适
CREATE TABLE

my_lightdb=# \copy ora_datatype_2 from '/home/lightdb/t.csv' with (format csv,delimiter '|',header true);
COPY 5
my_lightdb=# select * from ora_datatype_2
my_lightdb-# ;
 id |   name   | create_date 
----+----------+-------------
  1 | ff       | 2021-08-21
  2 | should b | 2021-08-21
  3 | xx       | 2021-08-21
  4 | baba     | 2021-08-21
  5 | baba     | 2020-12-21
(5 rows)

   在后续系列,我们将陆续推出从Oracle、MySQL到LightDB的迁移示例。心急的小伙伴也可以现在就访问https://www.hs.net/lightdb,右下角包含了pdf版本的迁移指南。

  注:对于非text/varchar类型的字段,如果CSV文件中有潜在的null值,需要在with选项中通过FORCE_NULL(col1,col2)列出可能为NULL的字段,如下:

postgres=# copy tsys_user_right from '/home/zjh/test_data/tsys_user_right.csv' with (FORMAT csv, DELIMITER ',',HEADER false,FORCE_NULL(create_date));

  否则会提示“ERROR:  invalid input syntax for type bigint: ""”,如下:

postgres=# copy tsys_user_right from '/home/zjh/test_data/tsys_user_right.csv' with (FORMAT csv, DELIMITER ',',HEADER false);
ERROR:  invalid input syntax for type bigint: ""
CONTEXT:  COPY tsys_user_right, line 280, column create_date: ""

  除了copy导入导出外,LightDB还支持外部表的该表,其通过标准的FDW实现(不过其效率较低,不适合生产使用)。

4、监控

  对于任何数据库来说,监控是很重要的基础工具,LightDB也不例外,自带pgcenter用来实时监控LightDB实例的整体运行情况以及top sql,如下:

# 在下个版本,不带地址选项的pgcenter默认将访问本地LightDB实例
[lightdb@hs-10-20-30-127 ~]$ pgcenter top -h 127.0.0.1 -p 5432 postgres

 

  相比pgAdmin,pgcenter更加直观。除了top命令,pgcenter还包含profile命令用于跟踪某个特定PID,record命令用来跟踪LightDB实例一段时间的运行情况。用户可输入pgcenter --help查看完整的功能。

  当前除了使用pgcenter监控外,用户也可以通过promethous监控。后续,LightDB将提供数据库领域监控平台LightDB-EM。

5、PWR报告

  笔者以前做架构时,但凡谁来推荐数据库,首先问的是有没有类似一样开箱即用的AWR报告。所以,在LightDB中,自然不能错过,LightDB在pg_profile中补充了最重要的等待事件部分(下个版本将完全重构为PWR和PSH),且默认开箱即用的10分钟自动执行一次快照。如下:

postgres=# select show_samples();   -- 查询当前快照
           show_samples            
-----------------------------------
 (1,"2021-08-21 18:20:00+08",t,,,)
 (2,"2021-08-21 18:30:00+08",t,,,)
 (3,"2021-08-21 18:40:00+08",t,,,)
 (4,"2021-08-21 18:50:00+08",t,,,)
 (5,"2021-08-21 19:00:00+08",t,,,)
 (6,"2021-08-21 19:10:00+08",t,,,)
 (7,"2021-08-21 19:20:00+08",t,,,)
(7 rows)
postgres=# select take_sample();  -- 创建快照
      take_sample       
------------------------
 (local,OK,00:00:00.17)
(1 row)
[lightdb@hs-10-20-30-127 ~]$ ltsql -Aqtc "SELECT get_report(1,10)" -o pwr_1_10.html
[lightdb@hs-10-20-30-127 ~]$ ll | grep pwr_1_10.html 
-rw-rw-r-- 1 lightdb lightdb 194232 Aug 21 19:40 pwr_1_10.html

  

 

   

  

  在下个版本,PWR中将包含主机的信息,TOP SQL语句中包含SQL语句的前200个字符提高易读性等,等待事件进一步细分为Idle和非Idle。

  关于PWR每部分的解释可以参见https://www.hs.net/r/cms/www/itn/forPrd/html/pgprofile.html#id-1.11.7.37.10

6、Oracle兼容

  LightDB兼容部分Oracle函数、特性及数据类型,例如to_number,to_char,dual,decode,varchar2,date。

postgres=# select to_number(x) from (select '123.323' x) x;
 to_number 
-----------
   123.323
(1 row)
postgres=# select to_char(123.45) from dual;
 to_char 
---------
 123.45
(1 row)

postgres=# select to_number('123.45') from dual;
 to_number 
-----------
    123.45
(1 row)
my_lightdb=# create table ora_datatype(id serial primary key,name varchar2(100),create_date date /*注:date部分不包含时间*/);
CREATE TABLE

my_lightdb=# insert into ora_datatype(name,create_date) values('ff',current_date);
INSERT 0 1
my_lightdb=# select * from ora_datatype;
 id | name | create_date 
----+------+-------------
  1 | ff   | 2021-08-21
(1 row)

my_lightdb=# insert into ora_datatype(name,create_date) values(decode('abc'::text,'a','a','should b'),current_date);
INSERT 0 1
my_lightdb=# select * from ora_datatype;
 id |   name   | create_date 
----+----------+-------------
  1 | ff       | 2021-08-21
  2 | should b | 2021-08-21
(2 rows)

my_lightdb=# select nvl(decode(name,'ff',null,name),'cast to null') from ora_datatype;
     nvl      
--------------
 cast to null
 should b
(2 rows)

  listagg聚合函数:

my_lightdb=# select * from ora_datatype;
 id |   name   | create_date 
----+----------+-------------
  1 | ff       | 2021-08-21
  2 | should b | 2021-08-21
  3 | xx       | 2021-08-21
  4 | baba     | 2021-08-21
  5 | baba     | 2020-12-21
(5 rows)

my_lightdb=# select create_date,listagg(name,',') from ora_datatype group by create_date;
 create_date |       listagg       
-------------+---------------------
 2020-12-21  | baba
 2021-08-21  | ff,should b,xx,baba
(2 rows)
my_lightdb=# select add_months(date'2016/05/01',2) from dual;
 add_months 
------------
 2016-07-01
(1 row)

my_lightdb=# select add_months(date'2016/05/01',-2) from dual;
 add_months 
------------
 2016-03-01
(1 row)

my_lightdb=# select last_day(current_timestamp::date) from dual;
  last_day  
------------
 2021-08-31
(1 row)

my_lightdb=# SELECT MONTHS_BETWEEN(DATE'2016/03/15', DATE'2015/11/15') FROM DUAL;
 months_between 
----------------
              4
(1 row)

my_lightdb=# select to_date('2021/12/12','yyyy/mm/dd')
my_lightdb-# ;
  to_date   
------------
 2021-12-12
(1 row)

  详细兼容明细可以参考https://www.hs.net/r/cms/www/itn/forPrd/html/orafce.html#id-1.11.7.30.4,后续版本将兼容rownum,rowid,sysdate。

===========================

  入门系列就到此为止,足以带用户上路了。

  除了上面这些功能外,LightDB还包含了很多其它开箱即用功能,有兴趣的小伙伴和用户可以下载体验。

   最后,如果用户希望部署高可用的话,可参考LightDB官网的《LightDB数据库安装手册》,其安装在单机基础上增加了ssh互通和一个文本格式的主机列表配置,除此之外,无需额外配置即可完成高可用配置,应用通过VIP访问LightDB。下一篇我们介绍LightDB高可用的自动切换使用。

  

 

标签:lightdb,快速,08,入门,21,2021,my,LightDB
来源: https://www.cnblogs.com/zhjh256/p/15170255.html