单节点部署官方文档:https://docs.timescale.com/install/latest/self-hosted/installation-redhat/
一、部署单节点的Timescaledb
1、以 root 身份添加 PostgreSQL 仓库以获取最新的 PostgreSQL 包:
1 | yum install https: //download .postgresql.org /pub/repos/yum/reporpms/EL- $(rpm -E %{rhel})-x86_64 /pgdg-redhat-repo-latest .noarch.rpm |
2、创建Timescaledb的仓库:
1 2 3 4 5 6 7 8 9 10 11 12 | tee /etc/yum .repos.d /timescale_timescaledb .repo <<EOL [timescale_timescaledb] name=timescale_timescaledb baseurl=https: //packagecloud .io /timescale/timescaledb/el/ $(rpm -E %{rhel})/\$basearch repo_gpgcheck=1 gpgcheck=0 enabled=1 gpgkey=https: //packagecloud .io /timescale/timescaledb/gpgkey sslverify=1 sslcacert= /etc/pki/tls/certs/ca-bundle .crt metadata_expire=300 EOL |
3、更新yum本地仓库列表:
1 | yum update -y |
4、安装Timescaledb:
1 | yum install timescaledb-2-postgresql-14 -y |
5、初始化数据库:
1 | /usr/pgsql-14/bin/postgresql-14-setup initdb |
完成安装后,您需要配置数据库以便可以使用它。最简单的方法是运行timescaledb-tune 脚本,它包含在timescaledb-tools软件包中。
6、使用timescaledb-tune脚本配置数据库
1 | timescaledb-tune --pg-config= /usr/pgsql-14/bin/pg_config |
7、启动pgsql服务,并设置开机自启:
1 2 | systemctl enable postgresql-14 systemctl start postgresql-14 |
8、以postgres用户身份连接pgsql:
1 2 | su postgres psql |
9、设置postgres数据库的密码:
1 | \password postgres |
10、重新连接pgsql数据库并创建数据库:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 | psql -U postgres -h localhost # 创建tsdb数据库 CREATE database tsdb; # 连接到tsdb数据库 \c tsdb # 添加timescaledb的扩展 CREATE EXTENSION IF NOT EXISTS timescaledb; # 检查是否安装了 TimescaleDB 扩展psql,输出如下 tsdb= # \dx List of installed extensions Name | Version | Schema | Description -------------+---------+------------+------------------------------------------------------------------- plpgsql | 1.0 | pg_catalog | PL /pgSQL procedural language timescaledb | 2.9.1 | public | Enables scalable inserts and complex queries for time -series data (2 rows) |
11、此时我们可以通过以下命令直接连接到tsdb数据库:
1 | psql -U postgres -h localhost -d tsdb |
二、让pgsql支持其他节点连接
1、通过以下命令找到需要配置的核心文件:
1 2 | find / -name postgresql.conf find / -name pg_hba.conf |
2、修改 postgresql.conf 配置文件:
1 2 3 4 | [root@node2 jiguiquan] # vim /var/lib/pgsql/14/data/postgresql.conf #添加如下内容: listen_addresses = '*' |
3、修改 pg_hba.conf 配置文件:
1 2 3 4 5 6 | [root@node2 jiguiquan] # vim /var/lib/pgsql/14/data/pg_hba.conf # 修改以下内容: # IPv4 local connections: host all all 127.0.0.1 /32 trust host all all 0.0.0.0 /0 password |
4、重启 pgsql 服务:
1 | [root@node2 jiguiquan] # systemctl restart postgresql-14 |
此时,外部程序即可正常连接tsdb数据库了!
三、搭建Timescaledb集群:
1、修改数据库配置:
1 2 3 4 5 6 7 8 9 | vim /var/lib/pgsql/14/data/postgresql .conf # 访问节点 max_prepared_transactions = 500 enable_partitionwise_aggregate = on jit = off # 数据节点 max_prepared_transactions = 500 wal_level = logical |
2、现在我们在1#节点上连接到tsdb数据库:
1 | psql -U postgres -h localhost -d tsdb |
3、执行以下命令,将另外2个数据节点添加到集群:
1 2 | SELECT add_data_node( 'dn1' , '10.206.114.7' , 'tsdb' ,5432, false , true , 'postgres' ); SELECT add_data_node( 'dn2' , '10.206.114.8' , 'tsdb' ,5432, false , true , 'postgres' ); |
如果报错如下:
说明,并需要在dn1和dn2节点上手动创建tsdb数据库,我们删除掉试试!
1 2 3 4 | psql -U postgres -h localhost postgres= # drop database tsdb; DROP DATABASE |
4、重新执行上面的添加数据节点的方法:
1 2 3 4 5 6 7 8 9 10 11 | tsdb= # SELECT add_data_node('dn1','10.206.114.7','tsdb',5432,false,true,'postgres'); add_data_node ------------------------------------ (dn1,10.206.114.7,5432,tsdb,t,t,t) (1 row) tsdb= # SELECT add_data_node('dn2','10.206.114.8','tsdb',5432,false,true,'postgres'); add_data_node ------------------------------------ (dn2,10.206.114.8,5432,tsdb,t,t,t) (1 row) |
执行成功,此时Timescaledb集群就搭建成功了;
1 2 | 访问节点:10.206.114.6 数据节点:10.206.114.7、10.206.114.8 |