clickhouse版本:22.1.2.21。Mysql引擎(不推荐)CREATEDATABASE[IFNOTEXISTS]db_name[ONCLUSTERcluster]ENGINE=MySQL('host:port',['database'|database],'user','password')engineparametershost:port--MySQL服务地址数据库--MySQL数据库名用户--MySQL用户名密码--MySQL用户密码表映射到ClickHouse,MySQL数据库引擎会将其查询转换成MySQL语法发送给MySQL服务器(吐槽:什么是和直接用mysql的区别,无语)2.MaterializedMySQL(不推荐)这个引擎创建的ClickHouse数据库包含了MySQL中的所有表以及这些表中的所有数据。ClickHouse服务器充当MySQL副本。它读取binlog并执行DDL和DML查询。CREATEDATABASE[IFNOTEXISTS]db_name[ONCLUSTERcluster]ENGINE=MaterializeMySQL('host:port',['database'|database],'user','password')[SETTINGS...]引擎参数host:port--MySQL服务地址数据库--MySQL数据库名用户--MySQL用户名密码--MySQL用户密码引擎配置max_rows_in_buffer--内存中允许缓存数据的最大行数(针对单表和缓存数据不可查询)。当超过行数时,数据将被物化。默认值:65505。max_bytes_in_buffer—允许在内存中缓存数据的最大字节数(针对单表和无法查询的缓存数据)。当超过行数时,数据将被物化。默认值:1048576.max_rows_in_buffers—允许数据缓存在内存中的最大行数(对于无法查询的数据库和缓存数据)。当超过行数时,数据将被物化。默认值:65505.max_bytes_in_buffers—允许在内存中缓存数据的最大字节数(对于数据库和未查询的缓存数据)。当超过行数时,数据将被物化。默认值:1048576.max_flush_data_time—允许数据缓存在内存中的最大毫秒数(对于无法查询的数据库和缓存数据)。当超过这个时间时,数据将被物化。默认值:1000.max_wait_time_when_mysql_unavailable—MySQL不可用时的重试间隔(以毫秒为单位)。负值禁用重试。默认值:1000.allows_query_when_mysql_lost—允许在mysql丢失时查询物化表。默认值:0(假)。例如:CREATEDATABASEmysqlENGINE=MaterializeMySQL('localhost:3306','db','user','***')SETTINGSallows_query_when_mysql_lost=true,max_wait_time_when_mysql_unavailable=10000;Usesteps:1.修改mysql的配置,重启gtid_mode=ONenforce_gtid_consistency=1binlog_format=ROWlog_bin=mysql-binexpire_logs_days=7max_binlog_size=256mbinlog_cache_size=4mmax_binlog_cache_size=512mserver-id=12.使用以下命令即可setallow_experimental_database_materialized_mysql=1;CREATEDATABASEtestENGINE=MaterializeMySQL('ip:port','mysql库','mysql账号','mysql密码');问题:1.这个引擎大大方便了mysql导入数据到clickhouse,但是官方提示还在实验中,不要在生产环境中使用2.查询的时候带上虚拟列_version,否则会使用finalby默认,效率很低。3、使用集群会有很多限制。3、mysql表函数clickhouse允许对存储在远程MySQL服务器上的数据进行SELECT和INSERT查询mysql('host:port','database','table','user','password'[,replace_query,'on_duplicate_clause']);参数host:port—MySQL服务器地址.database—远程数据库名称.table—远程表名称.user—MySQLuser.password—用户密码.replace_query—将INSERTINTO查询转换为REPLACEINTO的标志。如果replace_query=1`,查询被替换。on_duplicate_clause—添加ONDUPLICATEKEYon_duplicate_clause表达式到INSERT查询。明确规定只能使用replace_query=0。如果同时设置replace_query=1和on_duplicate_clause`,ClickHouse会产生异常。将mysql的所有数据导入到clickhouse1中。创建clickhouse的表2.执行命令INSERTINTOtableSELECT*FROMmysql('ip:port','database','table','user','password')[where]注:1.如果*是这里直接使用,clickhouse字段的顺序必须和mysql保持一致,否则会出问题,如果只能手动指定部分Field2.这里可以自带查询条件导入3.Clickhouse会有数据重复,即使是ReplacingMergeTree也可能有重复数据4.渠道同步1.准备jar包(切忌踩坑):https://mvnrepository.com/clickhouse-jdbc-0.2.6.jarhttpclient-4.5.13。jarhttpcore-4.4.13.jarlz4-1.5.0.jarlz4-java-1.8.0.jardruid-1.2.8.jar(这个版本不要太低,否则druid连接池不支持clickhouse)2.docker安装Canal-Serverdockerrun--namecanal-server-p11111:11111-dcanal/canal-server:latest3.进入要复制的容器/home/admin/canal-server/conf/example是新建的名字你要创建的数据库实例,这里使用example24。修改example2下的instance.properties配置#MySQL地址canal.instance.master.address=127.0.0.1:3306#mysql账号canal.instance.dbUsername=rootcanal.instance.dbPassword=root#过滤规则canal.instance.filter.regex=示例2\\*5。修改/小时ome/admin/canal-server/conf/canal.properties配置canal.serverMode=tcpcanal.destinations=example26.安装canal-adapter(也可以使用kafka)dockerrun--namecanal-adapter-p8081:8081-dslpcat/canal-adapter:v1.1.57。修改canal-adapter的/opt/canal-adapter/conf/application.yml配置文件(容器内)canal.conf:#注意这里的模式:tcpflatMessage:truezookeeperHosts:syncBatchSize:1000retries:0timeout:accessKey:secretKey:consumerProperties:#注意canal服务的地址canal.tcp.server.host:127.0.0.1:11111canal.tcp.zookeeper.hosts:canal.tcp.batch.size:500canal.tcp.username:canal.tcp.password:srcDataSources:#注意mysql连接defaultDShere:url:jdbc:mysql://127.0.0.1:3306/example2?useUnicode=trueusername:rootpassword:rootcanalAdapters:-instance:example2groups:-groupId:g1outerAdapters:-name:logger-name:rdbkey:mysql1#clickhouse配置属性:jdbc.driverClassName:ru.yandex.clickhouse.ClickHouseDriverjdbc.url:jdbc:clickhouse://127.0.0.1:8123jdbc.username:defaultjdbc.password:default8.修改/opt/canal-adapter/conf/rdb下的mytest_user.ymldataSourceKey:defaultDSdestination:example2groupId:g1outerAdapterKey:mysqlconcurrent:truedbMapping:mirrorDb:truedatabase:example29.重启服务器和适配器,插入数据到mysql到检查是否有数据同步。如果不是,请检查日志以解决问题。Note1.mysql需要开启配置gtid_mode=ONenforce_gtid_consistency=1binlog_format=ROWlog_bin=mysql-binexpire_logs_days=7max_binlog_size=256mbinlog_cache_size=4mmax_binlog_cache_size=512mserver-id=12。mysql和clickhouse的库和表要提前创建好3.注意docker配置Host模式要使用127.0,否则容器之间无法通信。4.如果mysql或clickhouse无法连接成功,检查驱动版本和jdbc连接是否写对了。5.其他解决方案:RedHat的debezium
