iT邦幫忙

2022 iThome 鐵人賽

DAY 9
0
Software Development

ClickHouse:時序資料庫建置與運行系列 第 9

day9-ClickHouse資料庫客戶端使用方法整理

  • 分享至 

  • xImage
  •  

前言

在上一章節中,我們展示了各種情形下,啟動ClickHouse資料庫的方法,在本章節中,我們將會展示各種ClickHouse客戶端的使用方法。

指令模式

從前面章節中,我們多少都有看到可以使用clickhouse-client之客戶端指令進行連到ClickHouse資料庫,所以最基本連線到資料庫的指令如下,相關的執行指令與輸出的結果如下:

peter@ubuntu-s-4vcpu-8gb-amd-sgp1-01:~$ clickhouse-client
ClickHouse client version 22.8.4.7 (official build).
Connecting to localhost:9000 as user default.

If you have installed ClickHouse and forgot password you can reset it in the configuration file.
The password for default user is typically located at /etc/clickhouse-server/users.d/default-password.xml
and deleting this file will reset the password.
See also /etc/clickhouse-server/users.xml on the server where ClickHouse is installed.

Code: 516. DB::Exception: Received from localhost:9000. DB::Exception: default: Authentication failed: password is incorrect or there is no user with such name. (AUTHENTICATION_FAILED)

從上述的指令來看,使用這樣的指令:clickhouse-client會造成有錯誤的結果,原因是只執行clickhouse-client時候,會使用預設的參數進行連線到ClickHouse資料庫,預設參數包含了使用者名稱default、密碼是空的、主機位址是127.0.0.1,主機的埠號預設會是連到9000,因此要帶入這些參數的話,則可以使用下列的指令來做到,相關的執行指令與輸出的訊息如下:

peter@ubuntu-s-4vcpu-8gb-amd-sgp1-01:~$ clickhouse-client --host 127.0.0.1 --port 9000 --user=default --password
ClickHouse client version 22.8.4.7 (official build).
Password for user (default):
Connecting to 127.0.0.1:9000 as user default.
Connected to ClickHouse server version 22.8.4 revision 54460.

Warnings:
 * Linux is not using a fast TSC clock source. Performance can be degraded. Check /sys/devices/system/clocksource/clocksource0/current_clocksource

ubuntu-s-4vcpu-8gb-amd-sgp1-01 :)

從上述的指令來看,使用了--host--port--user--password等參數來進行連線到ClickHouse資料庫,當加上了--password的參數之後,就會顯示提示密碼,這樣就可以在連線到資料庫之前可以輸入密碼。

clickouse-client指令使用參數的時候,則會將CLICKHOUSE_HOSTCLICKHOUSE_PORTCLICKHOUSE_USERCLICKHOUSE_PASSWORD等環境變數建立,所以如果不要在執行的時候還要以互動的方式進行輸入密碼與代參數,則可以使用下列的方式進行資料庫的連線,相關的執行指令與輸出的訊息如下:

peter@ubuntu-s-4vcpu-8gb-amd-sgp1-01:~$ export CLICKHOUSE_HOST="127.0.0.1"
peter@ubuntu-s-4vcpu-8gb-amd-sgp1-01:~$ export CLICKHOUSE_PORT="9000"
peter@ubuntu-s-4vcpu-8gb-amd-sgp1-01:~$ export CLICKHOUSE_USER="default"
peter@ubuntu-s-4vcpu-8gb-amd-sgp1-01:~$ export CLICKHOUSE_PASSWORD="password"
peter@ubuntu-s-4vcpu-8gb-amd-sgp1-01:~$ clickhouse-client
ClickHouse client version 22.8.4.7 (official build).
Connecting to localhost:9000 as user default.
Connected to ClickHouse server version 22.8.4 revision 54460.

Warnings:
 * Linux is not using a fast TSC clock source. Performance can be degraded. Check /sys/devices/system/clocksource/clocksource0/current_clocksource

ubuntu-s-4vcpu-8gb-amd-sgp1-01 :)

從上面的執行指令所輸出的訊息可以知道,我們先分別將各個ClickHouse之客戶端指令要帶入的參數分別使用環境變數進行宣告並事先儲存起來,接著可以直接使用clickhouse-client指令進行資料庫連線了,因為密碼還是會因為透過變數設定的方式而曝露在終端機的畫面上,為了要避免這個情況發生,可以使用設定檔並將上述這些變數分別寫入到設定檔,並透過Bash腳本的方式進行讀取設定檔案並設定相關的環境變數,接著再進行資料庫的連線,相關的執行指令與輸出的訊息如下:

peter@ubuntu-s-4vcpu-8gb-amd-sgp1-01:~$ vim setting.txt
peter@ubuntu-s-4vcpu-8gb-amd-sgp1-01:~$ cat setting.txt
127.0.0.1
9000
default
password
peter@ubuntu-s-4vcpu-8gb-amd-sgp1-01:~$ settings=$(cat setting.txt)
peter@ubuntu-s-4vcpu-8gb-amd-sgp1-01:~$ settings=($(echo $settings))
peter@ubuntu-s-4vcpu-8gb-amd-sgp1-01:~$ export CLICKHOUSE_HOST=$(echo "${settings[0]}")
peter@ubuntu-s-4vcpu-8gb-amd-sgp1-01:~$ export CLICKHOUSE_PORT=$(echo "${settings[1]}")
peter@ubuntu-s-4vcpu-8gb-amd-sgp1-01:~$ export CLICKHOUSE_USER=$(echo "${settings[2]}")
peter@ubuntu-s-4vcpu-8gb-amd-sgp1-01:~$ export CLICKHOUSE_PASSWORD=$(echo "${settings[3]}")
peter@ubuntu-s-4vcpu-8gb-amd-sgp1-01:~$ clickhouse-client
ClickHouse client version 22.8.4.7 (official build).
Connecting to localhost:9000 as user default.
Connected to ClickHouse server version 22.8.4 revision 54460.

Warnings:
 * Linux is not using a fast TSC clock source. Performance can be degraded. Check /sys/devices/system/clocksource/clocksource0/current_clocksource

ubuntu-s-4vcpu-8gb-amd-sgp1-01 :)

從上述的執行指令與輸出的訊息可以知道,首先先用vim的編輯器將settings.txt編輯好,利用cat settings.txt來輸出設定檔的內容,這一步驟是筆者要讓讀者知道設定檔的內容,在實務上這個步驟可以忽略,假設設定檔格式由上往下分別是主機位址、資料庫埠號、資料庫使用者名稱以及資料庫密碼,設定完成之後,接著可以使用cat指令將該設定檔輸出並儲存到settings的變數中,接著利用echo指令將變數內容印出並儲存到陣列裡面。

接著陣列變數是從索引0開始,因此就分別從0到3的索引開始將值取出並存放到各個ClickHouse客戶端會用到的變數,最後再執行clickhouse-client指令之後,就可以成功的連線到ClickHouse資料庫了,這樣也不會將資料庫相關的設定曝露在終端機上。

簡易測試客戶端

當透過前面的小節成功的連上ClickHouse資料庫之後,接著可以在客戶端的互動式操作介面中輸入一些命令來測試資料庫,相關的執行指令與操作的命令如下:

peter@ubuntu-s-4vcpu-8gb-amd-sgp1-01:~$ clickhouse-client
ClickHouse client version 22.8.4.7 (official build).
Connecting to localhost:9000 as user default.
Connected to ClickHouse server version 22.8.4 revision 54460.

Warnings:
 * Linux is not using a fast TSC clock source. Performance can be degraded. Check /sys/devices/system/clocksource/clocksource0/current_clocksource

ubuntu-s-4vcpu-8gb-amd-sgp1-01 :) select 1;

SELECT 1

Query id: edd026a4-7043-46f5-814c-5ee154efcc8c

┌─1─┐
│ 1 │
└───┘

1 row in set. Elapsed: 0.002 sec.

ubuntu-s-4vcpu-8gb-amd-sgp1-01 :)
ubuntu-s-4vcpu-8gb-amd-sgp1-01 :) select version();

SELECT version()

Query id: 1912d202-32d9-473c-8c26-9d0c7326e51d

┌─version()─┐
│ 22.8.4.7  │
└───────────┘

1 row in set. Elapsed: 0.002 sec.

ubuntu-s-4vcpu-8gb-amd-sgp1-01 :)
ubuntu-s-4vcpu-8gb-amd-sgp1-01 :) SELECT 1;

SELECT 1

Query id: a084df21-0315-4530-aa92-272619a2bfe3

┌─1─┐
│ 1 │
└───┘

1 row in set. Elapsed: 0.001 sec.

ubuntu-s-4vcpu-8gb-amd-sgp1-01 :) exit
Bye.
peter@ubuntu-s-4vcpu-8gb-amd-sgp1-01:~$

從上面輸出的指令與訊息來看,我們分別使用了select 1來輸出1的結果測試資料庫,還有執行了select version();來輸出資料庫的版本,接著執行SELECT 1來輸出1,由此可見,ClickHouse實做了一些SQL相關的解析器,因此我們可以利用SQL語句對ClickHouse資料庫進行操作,同時對於SQL來說,執行語句的關鍵字沒有大小寫的區分,這特性ClickHouse也有實做出來並可以透過上述的執行命令的輸出訊息可以知道。

網頁查詢介面

除了利用clickhouse-client進行連線ClickHouse資料庫之外,我們也可以透過ClickHouse客戶端內建的網頁介面方式進行資料庫連線,相關的做法如下:

這邊以Ubuntu 18.04的發行版本作業系統進行示範,假設安裝ClickHouse資料庫的機器上的作業系統完全沒有桌面環境,因此需要先利用下列的指令將Gnome桌面環境安裝起來。相關的執行指令與輸出的訊息如下:

peter@ubuntu-s-4vcpu-8gb-amd-sgp1-01:~$ sudo apt-get install -y gnome-session gdm3
[sudo] password for peter:
Reading package lists... Done
Building dependency tree
Reading state information... Done
......
peter@ubuntu-s-4vcpu-8gb-amd-sgp1-01:~$

安裝完成之後,接著執行下列的wget指令將一個Bash安裝腳本下載回來並安裝xRDP遠端桌面相關的套件,接著利用chmod指令將該腳本改成可以執行的權限,相關的執行指令與輸出的訊息如下:

peter@ubuntu-s-4vcpu-8gb-amd-sgp1-01:~$ wget https://gist.githubusercontent.com/peter279k/259f71b3a8cef3aa66e9af90e6800f2b/raw/c373ddee0d407dac850dc326c268e5df6d8c93f1/Std-Xrdp-install-0.2.sh
--2022-09-11 05:28:04--  https://gist.githubusercontent.com/peter279k/259f71b3a8cef3aa66e9af90e6800f2b/raw/c373ddee0d407dac850dc326c268e5df6d8c93f1/Std-Xrdp-install-0.2.sh
Resolving gist.githubusercontent.com (gist.githubusercontent.com)... 185.199.109.133, 185.199.108.133, 185.199.111.133, ...
Connecting to gist.githubusercontent.com (gist.githubusercontent.com)|185.199.109.133|:443... connected.
HTTP request sent, awaiting response... 200 OK
Length: 5496 (5.4K) [text/plain]
Saving to: ‘Std-Xrdp-install-0.2.sh’

Std-Xrdp-install-0.2.sh    100%[========================================>]   5.37K  --.-KB/s    in 0s

2022-09-11 05:28:04 (74.1 MB/s) - ‘Std-Xrdp-install-0.2.sh’ saved [5496/5496]
peter@ubuntu-s-4vcpu-8gb-amd-sgp1-01:~$ chmod +x Std-Xrdp-install-0.2.sh

完成上述的指令之後,接著執行該Bash腳本,相關的執行指令與輸出的訊息如下:

peter@ubuntu-s-4vcpu-8gb-amd-sgp1-01:~$ ./Std-Xrdp-install-0.2.sh

#-------------------------------------------------------------#
#   Standard XRDP Installation Script  - Ver 0.2              #
#   Written by Griffon - April 2018- www.c-nergy.be           #
#-------------------------------------------------------------#


#---------------------------------------------#
!   Detecting Ubuntu version                  #
#---------------------------------------------#


.... Ubuntu Version :   Ubuntu 18.04.6 LTS
.... Supported version detected....proceeding

#---------------------------------------------#
!   Installing XRDP Packages...Proceeding...  #
#---------------------------------------------#

......

#---------------------------------------------#
!   Installing Gnome Tweak...Proceeding...    #
#---------------------------------------------#

......

#---------------------------------------------#
!   Granting Console Access...Proceeding...   #
#---------------------------------------------#


#---------------------------------------------#
!   Creating Polkit File...Proceeding...      #
#---------------------------------------------#


#---------------------------------------------#
!   Install Extensions Dock...Proceeding...   #
#---------------------------------------------#
......

#-----------------------------------------------------------------------#
# Installation Completed
# Please test your xRDP configuration....
# Written by Griffon - April 2018 - Ver 0.2 - Std-Xrdp-Install-0.2.sh
#-----------------------------------------------------------------------#

安裝完成之後,可以利用下列的指令確認目前的xRDP服務是否有正常的啟動,接著安裝Firefox瀏覽器的介面,相關的執行指令與輸出的訊息如下:

peter@ubuntu-s-4vcpu-8gb-amd-sgp1-01:~$ sudo systemctl status xrdp
● xrdp.service - xrdp daemon
   Loaded: loaded (/lib/systemd/system/xrdp.service; enabled; vendor preset: enabled)
   Active: active (running) since Sat 2022-09-10 18:38:54 UTC; 10h ago
     Docs: man:xrdp(8)
           man:xrdp.ini(5)
  Process: 1146 ExecStart=/usr/sbin/xrdp $XRDP_OPTIONS (code=exited, status=0/SUCCESS)
  Process: 1131 ExecStartPre=/bin/sh /usr/share/xrdp/socksetup (code=exited, status=0/SUCCESS)
 Main PID: 1150 (xrdp)
    Tasks: 1 (limit: 4915)
   CGroup: /system.slice/xrdp.service
           └─1150 /usr/sbin/xrdp
......
peter@ubuntu-s-4vcpu-8gb-amd-sgp1-01:~$ sudo apt-get install firefox
Reading package lists... Done
Building dependency tree
Reading state information... Done
The following additional packages will be installed:
  libdbus-glib-1-2 xul-ext-ubufox
Suggested packages:
  fonts-lyx
The following NEW packages will be installed:
  firefox libdbus-glib-1-2 xul-ext-ubufox
0 upgraded, 3 newly installed, 0 to remove and 0 not upgraded.
Need to get 58.8 MB of archives.
After this operation, 236 MB of additional disk space will be used.
Do you want to continue? [Y/n] Y
......
Processing triggers for desktop-file-utils (0.23-1ubuntu3.18.04.2) ...
Processing triggers for libc-bin (2.27-3ubuntu1.6) ...
Processing triggers for man-db (2.8.3-2ubuntu0.1) ...
peter@ubuntu-s-4vcpu-8gb-amd-sgp1-01:~$

接著以Windows為例,可以開啟在Windows作業系統上的遠端桌面程式並輸入該機器的外部IP位址,相關的操作如下截圖:

從上述的截圖可以知道,首先先開啟Windows作業系統上內建的遠端桌面,接著會有憑證的警告訊息出現,這邊可以按下是,接著再進到登入遠端桌面的畫面,可以輸入平常登入的使用者名稱與密碼。
接著就會進到一個藍色的桌面了,接著可以按下桌面中左上角的Activities的字,打開之後會出現應用程式的按鈕在左邊,有9個點排在一起的按鈕,按下去之後,就會出現目前作業系統上面安裝的應用程式,找到Firefox火狐的瀏覽器圖示,按下之後,就會出現瀏覽器的畫面了。

接著在瀏覽器上面輸入127.0.0.1:8123/play網址就可以進入到ClickHouse客戶端的介面,相關的截圖如下:

若有密碼需要輸入的話,則可以找到輸入網址右邊有defaultpassword的輸入文字的地方,把連線到ClickHouse資料庫的使用者名稱與密碼填上去,若沒有填上去的話則會用預設的使用者名稱與密碼,即default與空字串。

輸入好使用者名稱與密碼後,接著可以從上面的介面上輸入一些SQL的查詢語法,假設輸入SELECT 1,並按下Run按鈕之後,則會輸出下列的圖示:

如果需要知道更多ClickHouse客戶端指令的參數以及用法,可以使用--help參數來查看其他參數的用法與定義,相關的執行指令與輸出的訊息如下:

peter@ubuntu-s-4vcpu-8gb-amd-sgp1-01:~$ clickhouse-client --help
Main options:
  --help                                                            produce help message
  -V [ --version ]                                                  print version information and exit
  --version-clean                                                   print version in machine-readable
                                                                    format and exit
  -C [ --config-file ] arg                                          config-file path
  --queries-file arg                                                file path with queries to execute;
                                                                    multiple files can be specified
......
External tables options:
  --file arg                   data file or - for stdin
  --name arg (=_data)          name of the table
  --format arg (=TabSeparated) data format
  --structure arg              structure
  --types arg                  types

Hosts and ports options:
  -h [ --host ] arg (=localhost) Server hostname. Multiple hosts can be passed via multiple
                                 argumentsExample of usage: '--host host1 --host host2 --port port2 --host
                                 host3 ...'Each '--port port' will be attached to the last seen host that
                                 doesn't have a port yet,if there is no such host, the port will be
                                 attached to the next first host or to default host.
  --port arg                     server ports

In addition, --param_name=value can be specified for substitution of parameters for parametrized queries.
peter@ubuntu-s-4vcpu-8gb-amd-sgp1-01:~$

遠端網站體驗ClickHouse客戶端

上述的作法都是需要現在機器上安裝ClickHouse的伺服器與客戶端之後,才可以進行操作,ClickHouse官方則提供了一個網站可以讓我們體驗ClickHouse客戶端上面的一些功能,也就是所謂的Playground。

體驗的資料庫相關的資訊如下:

  1. HTTPS連線的網址為:https://play.clickhouse.com,客戶端網站介面為:https://play.clickhouse.com/play,用一般的網頁瀏覽器輸入網址即可進行體驗。
  2. 原生TCP連線的網址為:play.clickhouse.com:9440,這個是給ClickHouse客戶端指令連線所使用。
  3. 體驗資料庫的使用者為play或是explorer
  4. 體驗資料庫的使用者密碼皆為空字串。

我們可以直接打開瀏覽器並輸入https://play.clickhouse.com/play網址之後就可以進入到網站與操作等相關的截圖如下所示:

同時,我們也可以使用clickhouse-client的指令的方式連線到官方提供的測試資料庫,相關執行指令與輸出的訊息如下:

peter@ubuntu-s-4vcpu-8gb-amd-sgp1-01:~$ clickhouse-client --secure --host play.clickhouse.com --port 9440 --user play
ClickHouse client version 22.8.4.7 (official build).
Connecting to play.clickhouse.com:9440 as user play.
Connected to ClickHouse server version 22.9.1 revision 54460.

ClickHouse client version is older than ClickHouse server. It may lack support for new features.

play-eu :) select 1;

SELECT 1

Query id: 7dd4b463-08a3-401f-a3bf-45ee52d33a2e

┌─1─┐
│ 1 │
└───┘

1 row in set. Elapsed: 0.001 sec.

play-eu :) exit
Bye.
peter@ubuntu-s-4vcpu-8gb-amd-sgp1-01:~$

從上面的指令來看,我們可以使用的使用者有playexplorer且這兩個使用者密碼皆為空字串,加入--secure參數表示使用TLS的連線方式連線到體驗的ClickHouse資料庫。

在體驗的ClickHouse資料庫上,仍有一些操作的限制,相關的限制如下列表:

  1. DDL(資料定義語言)的SQL是不被允許的,像是建立資料庫與資料表等這些有關建立的操作SQL都是無法使用的。
  2. INSERT的語法也是不被允許的,即寫入資料是不能的。

從上面的限制來看,在體驗資料庫上只能進行查詢的操作來進行體驗,如執行SELECT的SQL查詢語句等。

結論

在本章節中,我們學會了各種ClickHouse客戶端連線的方式,包含了指令與網頁操作介面,同時也可以利用ClickHouse官方提供的體驗資料庫(Playground)進行資料庫靠戶端連線的操作、測試與體驗,在下一章節中,我們將會介紹操作ClickHouse資料庫的方法與介紹。

參考資料


上一篇
day8-ClickHouse資料庫伺服器啟動方法整理
下一篇
day10-SQL使用與操作方法介紹(一)
系列文
ClickHouse:時序資料庫建置與運行30
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言