TDengine-安装集群(CENTOS)

1 服务端

三台虚拟机搭建集群,再有一台虚拟机做连接器

1.1 修改HOSTNAME和HOSTS,PING

1.1.1 HOSTNAME

第零步:规划集群所有物理节点的FQDN,将规划好的FQDN分别添加到每个物理节点的/etc/hostname;修改每个物理节点的/etc/hosts,将所有集群物理节点的IP与FQDN的对应添加好。

[root@pyrk-taos1 ~]# hostnamectl set-hostname pyrk-taos1
[root@pyrk-taos1 ~]# hostname
pyrk-taos1

[root@pyrk-taos2 ~]# hostnamectl set-hostname pyrk-taos2
[root@pyrk-taos2 ~]# hostname
pyrk-taos2

[root@pyrk-taos3 ~]# hostnamectl set-hostname pyrk-taos3
[root@pyrk-taos3 ~]# hostname
pyrk-taos3

1.1.2 /ETC/HOSTS

[root@pyrk-taos1 ~]# cat /etc/hosts
127.0.0.1   localhost localhost.localdomain localhost4 localhost4.localdomain4
::1         localhost localhost.localdomain localhost6 localhost6.localdomain6
192.168.0.71 pyrk-taos1
192.168.0.72 pyrk-taos2
192.168.0.73 pyrk-taos3

[root@pyrk-taos2 ~]# cat /etc/hosts
127.0.0.1   localhost localhost.localdomain localhost4 localhost4.localdomain4
::1         localhost localhost.localdomain localhost6 localhost6.localdomain6
192.168.0.71 pyrk-taos1
192.168.0.72 pyrk-taos2
192.168.0.73 pyrk-taos3

[root@pyrk-taos3 ~]# cat /etc/hosts
127.0.0.1   localhost localhost.localdomain localhost4 localhost4.localdomain4
::1         localhost localhost.localdomain localhost6 localhost6.localdomain6
192.168.0.71 pyrk-taos1
192.168.0.72 pyrk-taos2
192.168.0.73 pyrk-taos3

1.1.3 PING

[root@pyrk-taos1 ~]# ping pyrk-taos1
PING pyrk-taos1 (192.168.0.71) 56(84) bytes of data.
64 bytes from pyrk-taos1 (192.168.0.71): icmp_seq=1 ttl=64 time=0.020 ms
64 bytes from pyrk-taos1 (192.168.0.71): icmp_seq=2 ttl=64 time=0.033 ms
64 bytes from pyrk-taos1 (192.168.0.71): icmp_seq=3 ttl=64 time=0.051 ms
64 bytes from pyrk-taos1 (192.168.0.71): icmp_seq=4 ttl=64 time=0.033 ms
^C
--- pyrk-taos1 ping statistics ---
4 packets transmitted, 4 received, 0% packet loss, time 3002ms
rtt min/avg/max/mdev = 0.020/0.034/0.051/0.011 ms


 [root@pyrk-taos1 ~]# ping pyrk-taos2
PING pyrk-taos2 (192.168.0.72) 56(84) bytes of data.
64 bytes from pyrk-taos2 (192.168.0.72): icmp_seq=1 ttl=64 time=0.877 ms
64 bytes from pyrk-taos2 (192.168.0.72): icmp_seq=2 ttl=64 time=0.743 ms
64 bytes from pyrk-taos2 (192.168.0.72): icmp_seq=3 ttl=64 time=0.392 ms
64 bytes from pyrk-taos2 (192.168.0.72): icmp_seq=4 ttl=64 time=0.337 ms
64 bytes from pyrk-taos2 (192.168.0.72): icmp_seq=5 ttl=64 time=0.371 ms
^C
--- pyrk-taos2 ping statistics ---
5 packets transmitted, 5 received, 0% packet loss, time 4007ms
rtt min/avg/max/mdev = 0.337/0.544/0.877/0.221 ms


[root@pyrk-taos2 ~]# ping pyrk-taos3
PING pyrk-taos3 (192.168.0.73) 56(84) bytes of data.
64 bytes from pyrk-taos3 (192.168.0.73): icmp_seq=1 ttl=64 time=0.607 ms
64 bytes from pyrk-taos3 (192.168.0.73): icmp_seq=2 ttl=64 time=1.31 ms
^C
--- pyrk-taos3 ping statistics ---
2 packets transmitted, 2 received, 0% packet loss, time 1011ms
rtt min/avg/max/mdev = 0.607/0.961/1.315/0.354 ms

1.2 基础配置

1.2.1 防火墙

关闭防火墙,及自启动

[root@pyrk-taos1 ~]# systemctl stop firewalld
[root@pyrk-taos1 ~]# systemctl status firewalld
● firewalld.service - firewalld - dynamic firewall daemon
   Loaded: loaded (/usr/lib/systemd/system/firewalld.service; disabled; vendor preset: enabled)
   Active: inactive (dead)
     Docs: man:firewalld(1)
[root@pyrk-taos1 ~]# systemctl disable firewalld

[root@pyrk-taos2 ~]# systemctl stop firewalld
[root@pyrk-taos2 ~]# systemctl status firewalld
● firewalld.service - firewalld - dynamic firewall daemon
   Loaded: loaded (/usr/lib/systemd/system/firewalld.service; disabled; vendor preset: enabled)
   Active: inactive (dead)
     Docs: man:firewalld(1)
[root@pyrk-taos2 ~]# systemctl disable firewalld

[root@pyrk-taos3 ~]# systemctl stop firewalld
[root@pyrk-taos3 ~]# systemctl status firewalld
● firewalld.service - firewalld - dynamic firewall daemon
   Loaded: loaded (/usr/lib/systemd/system/firewalld.service; disabled; vendor preset: enabled)
   Active: inactive (dead)
     Docs: man:firewalld(1)
[root@pyrk-taos3 ~]# systemctl disable firewalld

1.2.2 SELINUX

设置为DISABLED

[root@pyrk-taos1 ~]# cat /etc/selinux/config 
# This file controls the state of SELinux on the system.
# SELINUX= can take one of these three values:
#     enforcing - SELinux security policy is enforced.
#     permissive - SELinux prints warnings instead of enforcing.
#     disabled - No SELinux policy is loaded.
SELINUX=disabled
# SELINUXTYPE= can take one of three two values:
#     targeted - Targeted processes are protected,
#     minimum - Modification of targeted policy. Only selected processes are protected. 
#     mls - Multi Level Security protection.
SELINUXTYPE=targeted


[root@pyrk-taos2 ~]# cat /etc/selinux/config 
# This file controls the state of SELinux on the system.
# SELINUX= can take one of these three values:
#     enforcing - SELinux security policy is enforced.
#     permissive - SELinux prints warnings instead of enforcing.
#     disabled - No SELinux policy is loaded.
SELINUX=disabled
# SELINUXTYPE= can take one of three two values:
#     targeted - Targeted processes are protected,
#     minimum - Modification of targeted policy. Only selected processes are protected. 
#     mls - Multi Level Security protection.
SELINUXTYPE=targeted


[root@pyrk-taos3 ~]# cat /etc/selinux/config 
# This file controls the state of SELinux on the system.
# SELINUX= can take one of these three values:
#     enforcing - SELinux security policy is enforced.
#     permissive - SELinux prints warnings instead of enforcing.
#     disabled - No SELinux policy is loaded.
SELINUX=disabled
# SELINUXTYPE= can take one of three two values:
#     targeted - Targeted processes are protected,
#     minimum - Modification of targeted policy. Only selected processes are protected. 
#     mls - Multi Level Security protection.
SELINUXTYPE=targeted

重启三台虚拟机reboot

1.3 安装TAOS

1.3.1 下载并上传

https://www.taosdata.com/cn/getting-started/

文件:TDengine-server-2.0.20.13-Linux-x64.rpm

[root@pyrk-taos1 ~]# ll
total 9036
-rw-------. 1 root root    1259 May 21 01:39 anaconda-ks.cfg
-rw-r--r--  1 root root 9246235 Sep  9 22:46 TDengine-server-2.0.20.13-Linux-x64.rpm


[root@pyrk-taos2 ~]# ll
total 9036
-rw-------. 1 root root    1259 May 21 01:39 anaconda-ks.cfg
-rw-r--r--  1 root root 9246235 Sep  9 22:46 TDengine-server-2.0.20.13-Linux-x64.rpm

[root@pyrk-taos3 ~]# ll
total 9036
-rw-------. 1 root root    1259 May 21 01:39 anaconda-ks.cfg
-rw-r--r--  1 root root 9246235 Sep  9 22:46 TDengine-server-2.0.20.13-Linux-x64.rpm

1.3.2 安装

第一台:

第二台:需要输入第一台的HOSTNAME

第三台:需要输入第一台的HOSTNAME

2 集群配置、启动

2.1 节点1

2.1.1 配置

[root@pyrk-taos1 ~]# vi /etc/taos/taos.cfg 
[root@pyrk-taos1 ~]# cat -n /etc/taos/taos.cfg
     8	# first fully qualified domain name (FQDN) for TDengine system
     9	firstEp                   pyrk-taos1:6030
    10	secondEp                  pyrk-taos2:6030
    11	
    12	# local fully qualified domain name (FQDN)
    13	fqdn                      pyrk-taos1
    14	
    15	# first port number for the connection (12 continuous UDP/TCP port number are used) 
    16	serverPort                6030
    17	
    46	# number of management nodes in the system
    47	numOfMnodes               3
    54	
    55	# enable/disable load balancing
    56	balance                   1

设置主管理节点,备管理节点,副本数,启动负载均衡等。

还可以修改端口号,日志路径等

2.1.2 启动

[root@pyrk-taos1 ~]# systemctl start taosd
[root@pyrk-taos1 ~]# systemctl status taosd
● taosd.service - TDengine server service
   Loaded: loaded (/etc/systemd/system/taosd.service; enabled; vendor preset: disabled)
   Active: active (running) since Fri 2021-09-10 16:31:19 CST; 21min ago
  Process: 855 ExecStartPre=/usr/local/taos/bin/startPre.sh (code=exited, status=0/SUCCESS)
 Main PID: 865 (taosd)
   CGroup: /system.slice/taosd.service
           └─865 /usr/bin/taosd

Sep 10 16:31:19 pyrk-taos1 systemd[1]: Starting TDengine server service...
Sep 10 16:31:19 pyrk-taos1 systemd[1]: Started TDengine server service.
Sep 10 16:31:19 pyrk-taos1 TDengine:[865]: Starting TDengine service...
Sep 10 16:31:20 pyrk-taos1 TDengine:[865]: Started TDengine service successfully.

2.1.3 查看数据节点和管理节点

[root@pyrk-taos1 ~]# taos

Welcome to the TDengine shell from Linux, Client Version:2.0.20.13
Copyright (c) 2020 by TAOS Data, Inc. All rights reserved.

taos> show dnodes;
   id   |           end_point            | vnodes | cores  |   status   | role  |       create_time       |      offline reason      |
======================================================================================================================================
      1 | pyrk-taos1:6030                |      1 |      1 | ready      | any   | 2021-09-10 16:31:19.944 |                          |
Query OK, 1 row(s) in set (0.000918s)

taos> show mnodes;
   id   |           end_point            |     role     |       create_time       |
===================================================================================
      1 | pyrk-taos1:6030                | master       | 2021-09-10 16:31:19.944 |
Query OK, 1 row(s) in set (0.000679s)

2.2 节点2

2.2.1 配置

[root@pyrk-taos2 ~]# vi /etc/taos/taos.cfg 
[root@pyrk-taos2 ~]# cat -n /etc/taos/taos.cfg 
     8	# first fully qualified domain name (FQDN) for TDengine system
     9	firstEp                   pyrk-taos1
    10	secondEp                  pyrk-taos2
    11	
    12	# local fully qualified domain name (FQDN)
    13	fqdn                      pyrk-taos2
    14	
    15	# first port number for the connection (12 continuous UDP/TCP port number are used) 
    16	serverPort                6030
    17	
    46	# number of management nodes in the system
    47	numOfMnodes               3
    48	
    55	# enable/disable load balancing
    56	balance                   1

2.2.2 启动

[root@pyrk-taos2 ~]# systemctl start taosd
[root@pyrk-taos2 ~]# systemctl status taosd
● taosd.service - TDengine server service
   Loaded: loaded (/etc/systemd/system/taosd.service; enabled; vendor preset: disabled)
   Active: active (running) since Fri 2021-09-10 16:31:37 CST; 25min ago
  Process: 859 ExecStartPre=/usr/local/taos/bin/startPre.sh (code=exited, status=0/SUCCESS)
 Main PID: 870 (taosd)
   CGroup: /system.slice/taosd.service
           └─870 /usr/bin/taosd

Sep 10 16:31:37 pyrk-taos2 systemd[1]: Starting TDengine server service...
Sep 10 16:31:37 pyrk-taos2 systemd[1]: Started TDengine server service.
Sep 10 16:31:38 pyrk-taos2 TDengine:[870]: Starting TDengine service...
Sep 10 16:31:38 pyrk-taos2 TDengine:[870]: Started TDengine service successfully.

2.2.3 查看数据节点和管理节点

[root@pyrk-taos1 ~]# taos

taos> create dnode "pyrk-taos2";
Query OK, 0 of 0 row(s) in database (0.001028s)

taos> show dnodes;
   id   |           end_point            | vnodes | cores  |   status   | role  |       create_time       |      offline reason      |
======================================================================================================================================
      1 | pyrk-taos1:6030                |      1 |      1 | ready      | any   | 2021-09-10 16:31:19.944 |                          |
      2 | pyrk-taos2:6030                |      0 |      1 | ready      | any   | 2021-09-10 16:58:53.428 |                          |
Query OK, 2 row(s) in set (0.000838s)

taos> show mnodes;
   id   |           end_point            |     role     |       create_time       |
===================================================================================
      1 | pyrk-taos1:6030                | master       | 2021-09-10 16:31:19.944 |
Query OK, 1 row(s) in set (0.000641s)

2.3 节点3

2.3.1 配置

[root@pyrk-taos3 ~]# vi /etc/taos/taos.cfg 
[root@pyrk-taos3 ~]# cat -n /etc/taos/taos.cfg 
     8	# first fully qualified domain name (FQDN) for TDengine system
     9	firstEp                   pyrk-taos1
    10	secondEp                  pyrk-taos2
    11	
    12	# local fully qualified domain name (FQDN)
    13	fqdn                      pyrk-taos3
    14	
    15	# first port number for the connection (12 continuous UDP/TCP port number are used) 
    16	serverPort                6030
    17	
    46	# number of management nodes in the system
    47	numOfMnodes               3
    48	
    55	# enable/disable load balancing
    56	balance                   1

2.3.2 启动

[root@pyrk-taos3 ~]# systemctl start taosd
[root@pyrk-taos3 ~]# systemctl status taosd
● taosd.service - TDengine server service
   Loaded: loaded (/etc/systemd/system/taosd.service; enabled; vendor preset: disabled)
   Active: active (running) since Fri 2021-09-10 17:03:53 CST; 3s ago
  Process: 1362 ExecStartPre=/usr/local/taos/bin/startPre.sh (code=exited, status=0/SUCCESS)
 Main PID: 1367 (taosd)
   CGroup: /system.slice/taosd.service
           └─1367 /usr/bin/taosd

Sep 10 17:03:53 pyrk-taos3 systemd[1]: Starting TDengine server service...
Sep 10 17:03:53 pyrk-taos3 systemd[1]: Started TDengine server service.
Sep 10 17:03:53 pyrk-taos3 TDengine:[1367]: Starting TDengine service...
Sep 10 17:03:53 pyrk-taos3 TDengine:[1367]: Started TDengine service successfully.

2.3.3 查看数据节点和管理节点

[root@pyrk-taos1 taos]# taos

Welcome to the TDengine shell from Linux, Client Version:2.0.20.13
Copyright (c) 2020 by TAOS Data, Inc. All rights reserved.

taos> create dnode "pyrk-taos3:6030";
Query OK, 0 of 0 row(s) in database (0.001415s)

taos> show dnodes;
   id   |           end_point            | vnodes | cores  |   status   | role  |       create_time       |      offline reason      |
======================================================================================================================================
      1 | pyrk-taos1:6030                |      0 |      1 | ready      | any   | 2021-09-10 16:31:19.944 |                          |
      2 | pyrk-taos2:6030                |      1 |      1 | ready      | any   | 2021-09-10 16:58:53.428 |                          |
      3 | pyrk-taos3:6030                |      0 |      1 | ready      | any   | 2021-09-10 17:10:02.387 |                          |
Query OK, 3 row(s) in set (0.000969s)

taos> show mnodes;
   id   |           end_point            |     role     |       create_time       |
===================================================================================
      1 | pyrk-taos1:6030                | master       | 2021-09-10 16:31:19.944 |
      2 | pyrk-taos2:6030                | slave        | 2021-09-10 17:09:41.406 |
      3 | pyrk-taos3:6030                | slave        | 2021-09-10 17:10:03.834 |
Query OK, 3 row(s) in set (0.000720s)

三个节点均已加上,而且主备设备完成

  • 任何已经加入集群在线的数据节点,都可以作为后续待加入节点的 firstEp。
  • firstEp 这个参数仅仅在该数据节点首次加入集群时有作用,加入集群后,该数据节点会保存最新的 mnode 的 End Point 列表,不再依赖这个参数。
  • 两个没有配置 firstEp 参数的数据节点 dnode 启动后,会独立运行起来。这个时候,无法将其中一个数据节点加入到另外一个数据节点,形成集群。无法将两个独立的集群合并成为新的集群。

2.4 节点管理

2.4.1 添加节点

CREATE DNODE "fqdn:port"; 

将新数据节点的End Point添加进集群的EP列表。”fqdn:port”需要用双引号引起来,否则出错。一个数据节点对外服务的fqdn和port可以通过配置文件taos.cfg进行配置,缺省是自动获取。

2.4.2 删除数据节点

DROP DNODE "fqdn:port | dnodeID";

通过”fqdn:port”或”dnodeID”来指定一个具体的节点都是可以的。其中fqdn是被删除的节点的FQDN,port是其对外服务器的端口号;dnodeID可以通过SHOW DNODES获得。

【注意】

  • 一个数据节点一旦被drop之后,不能重新加入集群。需要将此节点重新部署(清空数据文件夹)。集群在完成drop dnode操作之前,会将该dnode的数据迁移走。
  • 请注意 drop dnode 和 停止taosd进程是两个不同的概念,不要混淆:因为删除dnode之前要执行迁移数据的操作,因此被删除的dnode必须保持在线状态。待删除操作结束之后,才能停止taosd进程。
  • 一个数据节点被drop之后,其他节点都会感知到这个dnodeID的删除操作,任何集群中的节点都不会再接收此dnodeID的请求。
  • dnodeID是集群自动分配的,不得人工指定。它在生成时是递增的,不会重复。

2.4.3 查看数据节点

SHOW DNODES;

它将列出集群中所有的dnode,每个dnode的ID,end_point(fqdn:port),状态(ready, offline等),vnode数目,还未使用的vnode数目等信息。在添加或删除一个数据节点后,可以使用该命令查看。

2.5 负载均衡

修改配置文件

    55	# enable/disable load balancing
    56	balance                   1

有三种情况,将触发负载均衡,而且都无需人工干预。

  • 当一个新数据节点添加进集群时,系统将自动触发负载均衡,一些节点上的数据将被自动转移到新数据节点上,无需任何人工干预。
  • 当一个数据节点从集群中移除时,系统将自动把该数据节点上的数据转移到其他数据节点,无需任何人工干预。
  • 如果一个数据节点过热(数据量过大),系统将自动进行负载均衡,将该数据节点的一些vnode自动挪到其他节点。

当上述三种情况发生时,系统将启动各个数据节点的负载计算,从而决定如何挪动。

2.6 主备、副本数

配置文件修改

    46	# number of management nodes in the system
    47	numOfMnodes               3

TDengine集群是由mnode (taosd的一个模块,管理节点) 负责管理的,为保证mnode的高可用,可以配置多个mnode副本,副本数由系统配置参数numOfMnodes决定,有效范围为1-3。为保证元数据的强一致性,mnode副本之间是通过同步的方式进行数据复制的。

SHOW MNODES

注意副本数必须小于或者等于集群服务器数,否则会报错。

主备切换

10	secondEp                  pyrk-taos2:6030

2.7 负载均衡

BALANCE

有三种情况,将触发负载均衡,而且都无需人工干预。

  • 当一个新数据节点添加进集群时,系统将自动触发负载均衡,一些节点上的数据将被自动转移到新数据节点上,无需任何人工干预。
  • 当一个数据节点从集群中移除时,系统将自动把该数据节点上的数据转移到其他数据节点,无需任何人工干预。
  • 如果一个数据节点过热(数据量过大),系统将自动进行负载均衡,将该数据节点的一些vnode自动挪到其他节点。

当上述三种情况发生时,系统将启动各个数据节点的负载计算,从而决定如何挪动。

3 客户端

参考:

http://tjei-tju.club/index.php/2021/09/07/tao_install_centos/
[root@pyrk-taos ~]# cat /etc/hosts
127.0.0.1   localhost localhost.localdomain localhost4 localhost4.localdomain4
::1         localhost localhost.localdomain localhost6 localhost6.localdomain6
192.168.0.71 pyrk-taos1
192.168.0.72 pyrk-taos2
192.168.0.73 pyrk-taos3

网络测试:

[root@pyrk-taos ~]# ping pyrk-taos1
PING pyrk-taos1 (192.168.0.71) 56(84) bytes of data.
64 bytes from pyrk-taos1 (192.168.0.71): icmp_seq=1 ttl=64 time=0.589 ms
64 bytes from pyrk-taos1 (192.168.0.71): icmp_seq=2 ttl=64 time=0.281 ms
64 bytes from pyrk-taos1 (192.168.0.71): icmp_seq=3 ttl=64 time=0.286 ms
64 bytes from pyrk-taos1 (192.168.0.71): icmp_seq=4 ttl=64 time=0.278 ms
^C
--- pyrk-taos1 ping statistics ---
4 packets transmitted, 4 received, 0% packet loss, time 3001ms
rtt min/avg/max/mdev = 0.278/0.358/0.589/0.134 ms
[root@pyrk-taos ~]# 
[root@pyrk-taos ~]# 
[root@pyrk-taos ~]# ping pyrk-taos2
PING pyrk-taos2 (192.168.0.72) 56(84) bytes of data.
64 bytes from pyrk-taos2 (192.168.0.72): icmp_seq=1 ttl=64 time=0.817 ms
64 bytes from pyrk-taos2 (192.168.0.72): icmp_seq=2 ttl=64 time=0.305 ms
64 bytes from pyrk-taos2 (192.168.0.72): icmp_seq=3 ttl=64 time=0.240 ms
64 bytes from pyrk-taos2 (192.168.0.72): icmp_seq=4 ttl=64 time=0.281 ms
^C
--- pyrk-taos2 ping statistics ---
4 packets transmitted, 4 received, 0% packet loss, time 3001ms
rtt min/avg/max/mdev = 0.240/0.410/0.817/0.237 ms
[root@pyrk-taos ~]# ping pyrk-taos3
PING pyrk-taos3 (192.168.0.73) 56(84) bytes of data.
64 bytes from pyrk-taos3 (192.168.0.73): icmp_seq=1 ttl=64 time=0.457 ms
64 bytes from pyrk-taos3 (192.168.0.73): icmp_seq=2 ttl=64 time=0.268 ms
64 bytes from pyrk-taos3 (192.168.0.73): icmp_seq=3 ttl=64 time=0.277 ms
64 bytes from pyrk-taos3 (192.168.0.73): icmp_seq=4 ttl=64 time=0.191 ms
^C
--- pyrk-taos3 ping statistics ---
4 packets transmitted, 4 received, 0% packet loss, time 3002ms
rtt min/avg/max/mdev = 0.191/0.298/0.457/0.098 ms

TAOS主备配置

[root@pyrk-taos ~]# cat -n /etc/taos/taos.cfg 
     8	# first fully qualified domain name (FQDN) for TDengine system
     9	firstEp                   pyrk-taos1:6030
    10	secondEP                  pyrk-taos2:6030

4 测试

测试集群是,就不需要指定HOST,通过/ETC/HOSTS自动检测主备

conn = taos.connect(host="127.0.0.1", user="root", password="taosdata", config="/etc/taos")

4.1 测试文件

"""
This is the sample code for TDengine python2 client.
"""
import taos
import sys
import datetime
import random

def exitProgram(conn):
    conn.close()
    sys.exit()

if __name__ == '__main__':
    start_time = datetime.datetime(2019, 7, 1)
    time_interval = datetime.timedelta(seconds=60)

    # Connect to TDengine server.
    # 
    # parameters:
    # @host     : TDengine server IP address 
    # @user     : Username used to connect to TDengine server
    # @password : Password 
    # @database : Database to use when connecting to TDengine server
    # @config   : Configuration directory
    if len(sys.argv)>1:
        hostname=sys.argv[1]
        conn = taos.connect(host=hostname, user="root", password="taosdata", config="/etc/taos")
    else:
        conn = taos.connect(user="root", password="taosdata", config="/etc/taos")
   
    # Generate a cursor object to run SQL commands
    c1 = conn.cursor()
    # Create a database named db
    try:
        c1.execute('create database if not exists db replica 3')
    except Exception as err:
        conn.close()
        raise(err)
        
    # use database
    try:
        c1.execute('use db')
    except Exception as err:
        conn.close()
        raise(err)


    # create table
    try:
        c1.execute('create table if not exists t (ts timestamp, a int, b float, c binary(20))')
    except Exception as err:
        conn.close()
        raise(err)

    # insert data 
    for i in range(10):
        try:
           value = c1.execute("insert into t values ('%s', %d, %f, '%s')" % (start_time, random.randint(1,10), random.randint(1,10)/10.0, 'hello'))
           #if insert, value is the affected rows
           print(value)
        except Exception as err:
            conn.close()
            raise(err)
        start_time += time_interval

    # query data and return data in the form of list
    try:
        c1.execute('select * from db.t')
    except Exception as err:
        conn.close()
        raise(err)

    # Column names are in c1.description list
    cols = c1.description
    # Use fetchall to fetch data in a list
    data = c1.fetchall()

    for col in data:
        print(col)

    print('Another query method ')

    try:
        c1.execute('select * from db.t')
    except Exception as err:
        conn.close()
        raise(err)

    # Use iterator to go through the retreived data
    for col in c1:
        print(col)

    conn.close()

4.2 测试1

4.2.1.1 执行结果

[root@pyrk-taos python]# python3 read_example.py 
1
1
1
1
1
1
1
1
1
1
(datetime.datetime(2019, 7, 1, 0, 0), 2, 0.8999999761581421, 'hello')
(datetime.datetime(2019, 7, 1, 0, 1), 2, 1.0, 'hello')
(datetime.datetime(2019, 7, 1, 0, 2), 8, 1.0, 'hello')
(datetime.datetime(2019, 7, 1, 0, 3), 3, 0.5, 'hello')
(datetime.datetime(2019, 7, 1, 0, 4), 7, 0.800000011920929, 'hello')
(datetime.datetime(2019, 7, 1, 0, 5), 5, 0.699999988079071, 'hello')
(datetime.datetime(2019, 7, 1, 0, 6), 3, 1.0, 'hello')
(datetime.datetime(2019, 7, 1, 0, 7), 8, 1.0, 'hello')
(datetime.datetime(2019, 7, 1, 0, 8), 4, 0.4000000059604645, 'hello')
(datetime.datetime(2019, 7, 1, 0, 9), 2, 0.10000000149011612, 'hello')
Another query method 
(datetime.datetime(2019, 7, 1, 0, 0), 2, 0.8999999761581421, 'hello')
(datetime.datetime(2019, 7, 1, 0, 1), 2, 1.0, 'hello')
(datetime.datetime(2019, 7, 1, 0, 2), 8, 1.0, 'hello')
(datetime.datetime(2019, 7, 1, 0, 3), 3, 0.5, 'hello')
(datetime.datetime(2019, 7, 1, 0, 4), 7, 0.800000011920929, 'hello')
(datetime.datetime(2019, 7, 1, 0, 5), 5, 0.699999988079071, 'hello')
(datetime.datetime(2019, 7, 1, 0, 6), 3, 1.0, 'hello')
(datetime.datetime(2019, 7, 1, 0, 7), 8, 1.0, 'hello')
(datetime.datetime(2019, 7, 1, 0, 8), 4, 0.4000000059604645, 'hello')
(datetime.datetime(2019, 7, 1, 0, 9), 2, 0.10000000149011612, 'hello')
taos> show databases;
              name              |      created_time       |   ntables   |   vgroups   | replica | quorum |  days  |   keep0,keep1,keep(D)    |  cache(MB)  |   blocks    |   minrows   |   maxrows   | wallevel |    fsync    | comp | cachelast | precision | update |   status   |
====================================================================================================================================================================================================================================================================================
 log                            | 2021-09-10 16:31:21.010 |           6 |           1 |       1 |      1 |     10 | 30,30,30                 |           1 |           3 |         100 |        4096 |        1 |        3000 |    2 |         0 | us        |      0 | ready      |
 db                             | 2021-09-10 18:47:10.694 |           1 |           1 |       3 |      1 |     10 | 3650,3650,3650           |          16 |           6 |         100 |        4096 |        1 |        3000 |    2 |         0 | ms        |      0 | ready      |
Query OK, 2 row(s) in set (0.000930s)

taos> use db;
Database changed.

taos> show tables;
           table_name           |      created_time       | columns |          stable_name           |          uid          |     tid     |    vgId     |
==========================================================================================================================================================
 t                              | 2021-09-10 18:47:10.764 |       4 |                                |      1407374900340666 |           1 |           5 |
Query OK, 1 row(s) in set (0.001523s)

taos> select * from t;
           ts            |      a      |          b           |          c           |
======================================================================================
 2019-07-01 00:00:00.000 |           5 |              0.20000 | hello                |
 2019-07-01 00:01:00.000 |           1 |              0.70000 | hello                |
 2019-07-01 00:02:00.000 |           3 |              0.70000 | hello                |
 2019-07-01 00:03:00.000 |           2 |              0.30000 | hello                |
 2019-07-01 00:04:00.000 |           5 |              0.50000 | hello                |
 2019-07-01 00:05:00.000 |           5 |              0.80000 | hello                |
 2019-07-01 00:06:00.000 |           9 |              0.20000 | hello                |
 2019-07-01 00:07:00.000 |           4 |              1.00000 | hello                |
 2019-07-01 00:08:00.000 |           2 |              0.40000 | hello                |
 2019-07-01 00:09:00.000 |           7 |              0.50000 | hello                |
Query OK, 10 row(s) in set (0.003964s)

服务器2:

taos> show databases;
              name              |      created_time       |   ntables   |   vgroups   | replica | quorum |  days  |   keep0,keep1,keep(D)    |  cache(MB)  |   blocks    |   minrows   |   maxrows   | wallevel |    fsync    | comp | cachelast | precision | update |   status   |
====================================================================================================================================================================================================================================================================================
 log                            | 2021-09-10 16:31:21.010 |           6 |           1 |       1 |      1 |     10 | 30,30,30                 |           1 |           3 |         100 |        4096 |        1 |        3000 |    2 |         0 | us        |      0 | ready      |
 db                             | 2021-09-10 18:47:10.694 |           1 |           1 |       3 |      1 |     10 | 3650,3650,3650           |          16 |           6 |         100 |        4096 |        1 |        3000 |    2 |         0 | ms        |      0 | ready      |
Query OK, 2 row(s) in set (0.000922s)

服务器3:

taos> show databases;
              name              |      created_time       |   ntables   |   vgroups   | replica | quorum |  days  |   keep0,keep1,keep(D)    |  cache(MB)  |   blocks    |   minrows   |   maxrows   | wallevel |    fsync    | comp | cachelast | precision | update |   status   |
====================================================================================================================================================================================================================================================================================
 log                            | 2021-09-10 16:31:21.010 |           6 |           1 |       1 |      1 |     10 | 30,30,30                 |           1 |           3 |         100 |        4096 |        1 |        3000 |    2 |         0 | us        |      0 | ready      |
 db                             | 2021-09-10 18:47:10.694 |           1 |           1 |       3 |      1 |     10 | 3650,3650,3650           |          16 |           6 |         100 |        4096 |        1 |        3000 |    2 |         0 | ms        |      0 | ready      |
Query OK, 2 row(s) in set (0.001778s)

三台服务器对应三副本REPLICA 为3

4.3 测试2:暂停一台服务器

4.3.1 关闭服务器pyrk-taos1

[root@pyrk-taos1 ~]# systemctl stop taosd
[root@pyrk-taos1 ~]# systemctl status taosd
● taosd.service - TDengine server service
   Loaded: loaded (/etc/systemd/system/taosd.service; enabled; vendor preset: disabled)
   Active: inactive (dead) since Fri 2021-09-10 18:31:41 CST; 8s ago
  Process: 1410 ExecStart=/usr/bin/taosd (code=exited, status=0/SUCCESS)
  Process: 1403 ExecStartPre=/usr/local/taos/bin/startPre.sh (code=exited, status=0/SUCCESS)
 Main PID: 1410 (code=exited, status=0/SUCCESS)

Sep 10 17:07:23 pyrk-taos1 systemd[1]: Starting TDengine server service...
Sep 10 17:07:23 pyrk-taos1 systemd[1]: Started TDengine server service.
Sep 10 17:07:23 pyrk-taos1 TDengine:[1410]: Starting TDengine service...
Sep 10 17:07:23 pyrk-taos1 TDengine:[1410]: Started TDengine service successfully.
Sep 10 18:31:37 pyrk-taos1 TDengine:[1410]: Shut down signal is 15
Sep 10 18:31:37 pyrk-taos1 systemd[1]: Stopping TDengine server service...
Sep 10 18:31:37 pyrk-taos1 TDengine:[1410]: Shutting down TDengine service...
Sep 10 18:31:41 pyrk-taos1 systemd[1]: Stopped TDengine server service.
[root@pyrk-taos2 ~]# taos

Welcome to the TDengine shell from Linux, Client Version:2.0.20.13
Copyright (c) 2020 by TAOS Data, Inc. All rights reserved.

taos> show dnodes;
   id   |           end_point            | vnodes | cores  |   status   | role  |       create_time       |      offline reason      |
======================================================================================================================================
      1 | pyrk-taos1:6030                |      0 |      0 | offline    | any   | 2021-09-10 16:31:19.944 | status not received      |
      2 | pyrk-taos2:6030                |      1 |      1 | ready      | any   | 2021-09-10 16:58:53.428 |                          |
      3 | pyrk-taos3:6030                |      1 |      1 | ready      | any   | 2021-09-10 17:10:02.387 |                          |
Query OK, 3 row(s) in set (0.000911s)

taos> show mnodes;
   id   |           end_point            |     role     |       create_time       |
===================================================================================
      1 | pyrk-taos1:6030                | offline      | 2021-09-10 16:31:19.944 |
      2 | pyrk-taos2:6030                | master       | 2021-09-10 17:09:41.406 |
      3 | pyrk-taos3:6030                | slave        | 2021-09-10 17:10:03.834 |
Query OK, 3 row(s) in set (0.000865s)

可以看到PYRK-TAOS2变成了主,PYRK-TAOS1离线

4.3.2 执行结果

将执行文件的数据库db改为db1

[root@pyrk-taos python]# python3 read_example.py 
Traceback (most recent call last):
  File "read_example.py", line 53, in <module>
    raise(err)
  File "read_example.py", line 50, in <module>
    c1.execute('create table if not exists t (ts timestamp, a int, b float, c binary(20))')
  File "/usr/local/python3/lib/python3.6/site-packages/taos/cursor.py", line 148, in execute
    self._result), errno)
taos.error.ProgrammingError: Out of DNodes

报错,超除了节点数,三副本却只有2台服务器做集群

taos> show databases;
              name              |      created_time       |   ntables   |   vgroups   | replica | quorum |  days  |   keep0,keep1,keep(D)    |  cache(MB)  |   blocks    |   minrows   |   maxrows   | wallevel |    fsync    | comp | cachelast | precision | update |   status   |
====================================================================================================================================================================================================================================================================================
 log                            | 2021-09-10 16:31:21.010 |           6 |           1 |       1 |      1 |     10 | 30,30,30                 |           1 |           3 |         100 |        4096 |        1 |        3000 |    2 |         0 | us        |      0 | ready      |
 db                             | 2021-09-10 18:47:10.694 |           1 |           1 |       3 |      1 |     10 | 3650,3650,3650           |          16 |           6 |         100 |        4096 |        1 |        3000 |    2 |         0 | ms        |      0 | ready      |
 db1                            | 2021-09-10 18:50:15.324 |           0 |           0 |       3 |      1 |     10 | 3650,3650,3650           |          16 |           6 |         100 |        4096 |        1 |        3000 |    2 |         0 | ms        |      0 | ready      |
Query OK, 3 row(s) in set (0.000902s)

taos> use db1;
Database changed.

taos> show tables;
Query OK, 0 row(s) in set (0.000654s)

将3副本改为2副本,同时数据库改为db3

c1.execute('create database if not exists db1  replica 3') 

改为

c1.execute('create database if not exists db1  replica 2')

重新执行如下:

[root@pyrk-taos python]# python3 read_example.py 
1
1
1
1
1
1
1
1
1
1
(datetime.datetime(2019, 7, 1, 0, 0), 6, 0.8999999761581421, 'hello')
(datetime.datetime(2019, 7, 1, 0, 1), 5, 0.699999988079071, 'hello')
(datetime.datetime(2019, 7, 1, 0, 2), 2, 0.4000000059604645, 'hello')
(datetime.datetime(2019, 7, 1, 0, 3), 3, 0.4000000059604645, 'hello')
(datetime.datetime(2019, 7, 1, 0, 4), 10, 0.10000000149011612, 'hello')
(datetime.datetime(2019, 7, 1, 0, 5), 1, 0.800000011920929, 'hello')
(datetime.datetime(2019, 7, 1, 0, 6), 1, 0.699999988079071, 'hello')
(datetime.datetime(2019, 7, 1, 0, 7), 2, 0.800000011920929, 'hello')
(datetime.datetime(2019, 7, 1, 0, 8), 7, 1.0, 'hello')
(datetime.datetime(2019, 7, 1, 0, 9), 6, 0.6000000238418579, 'hello')
Another query method 
(datetime.datetime(2019, 7, 1, 0, 0), 6, 0.8999999761581421, 'hello')
(datetime.datetime(2019, 7, 1, 0, 1), 5, 0.699999988079071, 'hello')
(datetime.datetime(2019, 7, 1, 0, 2), 2, 0.4000000059604645, 'hello')
(datetime.datetime(2019, 7, 1, 0, 3), 3, 0.4000000059604645, 'hello')
(datetime.datetime(2019, 7, 1, 0, 4), 10, 0.10000000149011612, 'hello')
(datetime.datetime(2019, 7, 1, 0, 5), 1, 0.800000011920929, 'hello')
(datetime.datetime(2019, 7, 1, 0, 6), 1, 0.699999988079071, 'hello')
(datetime.datetime(2019, 7, 1, 0, 7), 2, 0.800000011920929, 'hello')
(datetime.datetime(2019, 7, 1, 0, 8), 7, 1.0, 'hello')
(datetime.datetime(2019, 7, 1, 0, 9), 6, 0.6000000238418579, 'hello')
taos> show databases;
              name              |      created_time       |   ntables   |   vgroups   | replica | quorum |  days  |   keep0,keep1,keep(D)    |  cache(MB)  |   blocks    |   minrows   |   maxrows   | wallevel |    fsync    | comp | cachelast | precision | update |   status   |
====================================================================================================================================================================================================================================================================================
 log                            | 2021-09-10 16:31:21.010 |           6 |           1 |       1 |      1 |     10 | 30,30,30                 |           1 |           3 |         100 |        4096 |        1 |        3000 |    2 |         0 | us        |      0 | ready      |
 db                             | 2021-09-10 18:47:10.694 |           1 |           1 |       3 |      1 |     10 | 3650,3650,3650           |          16 |           6 |         100 |        4096 |        1 |        3000 |    2 |         0 | ms        |      0 | ready      |
 db1                            | 2021-09-10 18:50:15.324 |           0 |           0 |       3 |      1 |     10 | 3650,3650,3650           |          16 |           6 |         100 |        4096 |        1 |        3000 |    2 |         0 | ms        |      0 | ready      |
 db3                            | 2021-09-10 18:52:43.473 |           1 |           1 |       2 |      1 |     10 | 3650,3650,3650           |          16 |           6 |         100 |        4096 |        1 |        3000 |    2 |         0 | ms        |      0 | ready      |
Query OK, 4 row(s) in set (0.000797s)

taos> use db3;
Database changed.

taos> show tables;
           table_name           |      created_time       | columns |          stable_name           |          uid          |     tid     |    vgId     |
==========================================================================================================================================================
 t                              | 2021-09-10 18:52:43.577 |       4 |                                |      1688849877052604 |           1 |           6 |
Query OK, 1 row(s) in set (0.001988s)

taos> select * from t;
           ts            |      a      |          b           |          c           |
======================================================================================
 2019-07-01 00:00:00.000 |           6 |              0.90000 | hello                |
 2019-07-01 00:01:00.000 |           5 |              0.70000 | hello                |
 2019-07-01 00:02:00.000 |           2 |              0.40000 | hello                |
 2019-07-01 00:03:00.000 |           3 |              0.40000 | hello                |
 2019-07-01 00:04:00.000 |          10 |              0.10000 | hello                |
 2019-07-01 00:05:00.000 |           1 |              0.80000 | hello                |
 2019-07-01 00:06:00.000 |           1 |              0.70000 | hello                |
 2019-07-01 00:07:00.000 |           2 |              0.80000 | hello                |
 2019-07-01 00:08:00.000 |           7 |              1.00000 | hello                |
 2019-07-01 00:09:00.000 |           6 |              0.60000 | hello                |

显示DB3数据库副本为3,

这时再启动节点1

taos> show databases;
              name              |      created_time       |   ntables   |   vgroups   | replica | quorum |  days  |   keep0,keep1,keep(D)    |  cache(MB)  |   blocks    |   minrows   |   maxrows   | wallevel |    fsync    | comp | cachelast | precision | update |   status   |
====================================================================================================================================================================================================================================================================================
 log                            | 2021-09-10 16:31:21.010 |           6 |           1 |       1 |      1 |     10 | 30,30,30                 |           1 |           3 |         100 |        4096 |        1 |        3000 |    2 |         0 | us        |      0 | ready      |
 db                             | 2021-09-10 18:47:10.694 |           1 |           1 |       3 |      1 |     10 | 3650,3650,3650           |          16 |           6 |         100 |        4096 |        1 |        3000 |    2 |         0 | ms        |      0 | ready      |
 db1                            | 2021-09-10 18:50:15.324 |           0 |           0 |       3 |      1 |     10 | 3650,3650,3650           |          16 |           6 |         100 |        4096 |        1 |        3000 |    2 |         0 | ms        |      0 | ready      |
 db3                            | 2021-09-10 18:52:43.473 |           1 |           1 |       2 |      1 |     10 | 3650,3650,3650           |          16 |           6 |         100 |        4096 |        1 |        3000 |    2 |         0 | ms        |      0 | ready      |
Query OK, 4 row(s) in set (0.001705s)

taos> use db3;
Database changed.

taos> select * from t;
           ts            |      a      |          b           |          c           |
======================================================================================
 2019-07-01 00:00:00.000 |           6 |              0.90000 | hello                |
 2019-07-01 00:01:00.000 |           5 |              0.70000 | hello                |
 2019-07-01 00:02:00.000 |           2 |              0.40000 | hello                |
 2019-07-01 00:03:00.000 |           3 |              0.40000 | hello                |
 2019-07-01 00:04:00.000 |          10 |              0.10000 | hello                |
 2019-07-01 00:05:00.000 |           1 |              0.80000 | hello                |
 2019-07-01 00:06:00.000 |           1 |              0.70000 | hello                |
 2019-07-01 00:07:00.000 |           2 |              0.80000 | hello                |
 2019-07-01 00:08:00.000 |           7 |              1.00000 | hello                |
 2019-07-01 00:09:00.000 |           6 |              0.60000 | hello                |

显示DB3只有2副本。

发表回复