检查
需要确保是否x86_64处理器构架、Linux并且支持SSE 4.2指令集
grep -q sse4_2 /proc/cpuinfo && echo "SSE 4.2 supported" || echo "SSE 4.2 not supported"
官网给的是Ubuntu/Debian的安装方式:如下
sudo apt-get install dirmngr sudo apt-key adv --keyserver hkp://keyserver.ubuntu.com:80 --recv E0C56BD4 echo "deb http://repo.yandex.ru/clickhouse/deb/stable/ main/" | sudo tee /etc/apt/sources.list.d/clickhouse.list sudo apt-get update sudo apt-get install -y clickhouse-server clickhouse-client sudo service clickhouse-server start clickhouse-client
centos的安装
参考:https://github.com/Altinity/clickhouse-rpm-install
#step 1
#先下载安装脚本
curl -s https://packagecloud.io/install/repositories/altinity/clickhouse/script.rpm.sh | sudo bash
#可以指定系统版本
curl -s https://packagecloud.io/install/repositories/altinity/clickhouse/script.rpm.sh | sudo os=centos dist=7 bash
#这里注意,我当时指定错了系统版本,导致找不到libstdc++的依赖包,汗颜!!
#step 2
#查看库里clickhouse相关的包
sudo yum list 'clickhouse*'
#step 3
#安装核心的服务和客户端
sudo yum install -y clickhouse-server clickhouse-client
#step 4
#启动服务
service clickhouse-server start
#step 5
#启动客户端
clickhouse-client
离线安装
离线安装包下载地址http://repo.yandex.ru/clickhouse/
另一个下载地址https://packagecloud.io/altinity/clickhouse
所以安装顺序如下: clickhouse-common-static clickhouse-server clickhouse-client 安装完后启动 #启动服务 service clickhouse-server start #启动客户端 clickhouse-client 或者clickhouse-client -h127.0.0.1 进入命令行后show databases;看到如下,说明安装成功 SHOW DATABASES ┌─ name ─┐ │ default │ │ system │ └── —— ──┘
通过java JDBC链接Clickhouse
maven 依赖
<dependency> <groupId>ru.yandex.clickhouse</groupId> <artifactId>clickhouse-jdbc</artifactId> <version>0.1.54</version> </dependency>
java代码
JDBC Driver Class: ru.yandex.clickhouse.ClickHouseDriver
应为我没有配置账号密码,所以直接给个url就可以创建链接,代码如下:
public static Connection getClickHouseConnection() throws SQLException { String url="jdbc:clickhouse://ip:8123/dataBaseName"; ClickHouseDataSource dataSou=new ClickHouseDataSource(url);//这是官方给的创建方式 return dataSou.getConnection();
//return dataSou.getConnection(username, password);
}
PS:这里可以不用Class.forName()去DriverManager注册初始化drive,感兴趣的同学可以研究一下java类加载机制和jdk1.6以后引入的service provider概念。
远程连接配置项
注意:vi /etc/clickhouse-server/config.xml中的如下配置项
类似mysql中的远程连接权限,放开ipv4连接打开注释
<listen_host>0.0.0.0</listen_host>
<!-- Listen specified host. use :: (wildcard IPv6 address), if you want to accept connections both with IPv4 and IPv6 from everywhere. --> <!-- <listen_host>::</listen_host> --> <!-- Same for hosts with disabled ipv6: --> <listen_host>0.0.0.0</listen_host> <!-- Default values - try listen localhost on ipv4 and ipv6: --> <!-- <listen_host>::1</listen_host> <listen_host>127.0.0.1</listen_host> -->
ps:我还发现个神奇的事情
配置上面远程连接后,本机用 clickhouse-client 或者 clickhouse-client -h localhost 连接本机服务,他是拒绝的
ClickHouse client version 19.9.2.4 (official build). Connecting to localhost:9000 as user default. Code: 210. DB::NetException: Connection refused (localhost:9000, ::1)
但是用 clickhouse-client -h 本机ip 或者 clickhouse-client -h 127.0.0.1 是可以连接的。(clickhouse版本如上 version 19.9.2.4)
备注:系统 /etc/hosts 内是有 127.0.0.1 到 localhost的映射的。
附:Clickhouse官方文档 https://clickhouse.yandex/docs/zh/