# RocksDB安装部署 RocksDB是使用C++开发的开源嵌入式数据库,长安链采用golang开发,为了集成RocksDB,目前长安链使用了gorocksdb第三方开源的golang库,gorocksdb需要依赖RocksDB的库文件,因此如果选用RocksDB作为存储数据库,需要在本地安装RocksDB的环境,并使用条件编译来集成RocksDB。过程如下: - 1.RocksDB安装:https://github.com/facebook/rocksdb/blob/master/INSTALL.md - 2.gorocksdb安装:https://github.com/yiyanwannian/gorocksdb - 3.使用rocksdb需要通过-tag方式启动,build方式: ```sh go build -tags=rocksdb ``` Linux下Rocksdb环境安装 1 安装依赖 安装gcc、zlib、snappy、lz4等依赖工具 ```sh yum -y install lrzsz git gcc gcc-c++ lz4-devel yum -y install snappy snappy-devel zlib zlib-devel bzip2 bzip2-devel lz4 lz4-devel zstd ``` 2 安装cmake gflags-2.2.2对cmake版本有要求,所以需要指定版本的cmake ```sh curl -O https://cmake.org/files/v3.6/cmake-3.6.0-Linux-x86_64.tar.gz mv cmake-3.6.0-Linux-x86_64.tar.gz /opt/ cd /opt/ tar -xvzf cmake-3.6.0-Linux-x86_64.tar.gz yum remove cmake cat >>/etc/profile <>/etc/profile <>/etc/profile < when necessary The following optional parameters control if keys/values are input/output as hex or as plain strings: --key_hex : Keys are input/output as hex --value_hex : Values are input/output as hex --hex : Both keys and values are input/output as hex The following optional parameters control the database internals: --column_family= : name of the column family to operate on. default: default column family --ttl with 'put','get','scan','dump','query','batchput' : DB supports ttl and value is internally timestamp-suffixed --try_load_options : Try to load option file from DB. --ignore_unknown_options : Ignore unknown options when loading option file. --bloom_bits= --fix_prefix_len= --compression_type= --compression_max_dict_bytes= --block_size= --auto_compaction= --db_write_buffer_size= --write_buffer_size= --file_size= Data Access Commands: put [--ttl] get [--ttl] batchput [ ] [..] [--ttl] scan [--from] [--to] [--ttl] [--timestamp] [--max_keys=q] [--start_time=:- is inclusive] [--end_time=:- is exclusive] [--no_value] delete deleterange query [--ttl] Starts a REPL shell. Type help for list of available commands. approxsize [--from] [--to] checkconsistency Admin Commands: dump_wal --walfile= [--header] [--print_value] [--write_committed=true|false] compact [--from] [--to] reduce_levels --new_levels= [--print_old_levels] change_compaction_style --old_compaction_style= --new_compaction_style= dump [--from] [--to] [--ttl] [--max_keys=] [--timestamp] [--count_only] [--count_delim=] [--stats] [--bucket=] [--start_time=:- is inclusive] [--end_time=:- is exclusive] [--path=] load [--create_if_missing] [--disable_wal] [--bulk_load] [--compact] manifest_dump [--verbose] [--json] [--path=] list_column_families full_path_to_db_directory dump_live_files idump [--from] [--to] [--input_key_hex] [--max_keys=] [--count_only] [--count_delim=] [--stats] repair backup [--backup_env_uri] [--backup_dir] [--num_threads] [--stderr_log_level=] restore [--backup_env_uri] [--backup_dir] [--num_threads] [--stderr_log_level=] checkpoint [--checkpoint_dir] write_extern_sst ingest_extern_sst [--move_files] [--snapshot_consistency] [--allow_global_seqno] [--allow_blocking_flush] [--ingest_behind] [--write_global_seqno] [root@VM-219-157-centos /opt/rocksdb-5.18.3/build/tools]# [root@VM-219-157-centos /opt/rocksdb-5.18.3/build/tools]# pwd /opt/rocksdb-5.18.3/build/tools [root@VM-219-157-centos /opt/rocksdb-5.18.3/build/tools]# ll /tmp/ total 32 srwxrwxrwx 1 root root 0 Dec 18 23:04 agent_cmd.sock drwxr-xr-x 2 root root 4096 Dec 25 10:14 commandnotfound -rw-r--r-- 1 root root 53 Dec 17 10:44 cpuidle_support.log -rw-r--r-- 1 root root 2513 Dec 17 10:43 cvm_init.log -rw-r--r-- 1 root root 297 Dec 17 10:44 net_affinity.log -rw-r--r-- 1 root root 26 Dec 17 10:44 nv_gpu_conf.log -rw-r--r-- 1 root root 155 Dec 17 10:44 setRps.log drwx------ 3 root root 4096 Dec 17 10:44 systemd-private-e9868203df724169bf07a791d55819cd-ntpd.service-mA2nuw -rw-r--r-- 1 root root 2017 Dec 17 10:44 virtio_blk_affinity.log [root@VM-219-157-centos /opt/rocksdb-5.18.3/build/tools]# ./ldb --db=/tmp/test_db --create_if_missing put a1 b1 OK [root@VM-219-157-centos /opt/rocksdb-5.18.3/build/tools]# ./ldb --db=/tmp/test_db scan a1 : b1 [root@VM-219-157-centos /opt/rocksdb-5.18.3/build/tools]# ./ldb --db=/tmp/test_db get a1 b1 [root@VM-219-157-centos /opt/rocksdb-5.18.3/build/tools]# ./ldb --db=/tmp/test_db get a2 Failed: NotFound: [root@VM-219-157-centos /opt/rocksdb-5.18.3/build/tools]# cd /tmp/test_db/ [root@VM-219-157-centos /tmp/test_db]# ll total 920 -rw-r--r-- 1 root root 26 Dec 25 11:13 000003.log -rw-r--r-- 1 root root 16 Dec 25 11:13 CURRENT -rw-r--r-- 1 root root 37 Dec 25 11:13 IDENTITY -rw-r--r-- 1 root root 0 Dec 25 11:13 LOCK -rw-r--r-- 1 root root 16246 Dec 25 11:14 LOG -rw-r--r-- 1 root root 19272 Dec 25 11:13 LOG.old.1608866049557780 -rw-r--r-- 1 root root 15784 Dec 25 11:14 LOG.old.1608866049562808 -rw-r--r-- 1 root root 16246 Dec 25 11:14 LOG.old.1608866058035881 -rw-r--r-- 1 root root 15788 Dec 25 11:14 LOG.old.1608866058041253 -rw-r--r-- 1 root root 16250 Dec 25 11:14 LOG.old.1608866062086538 -rw-r--r-- 1 root root 15784 Dec 25 11:14 LOG.old.1608866062091559 -rw-r--r-- 1 root root 13 Dec 25 11:13 MANIFEST-000001 -rw-r--r-- 1 root root 4744 Dec 25 11:13 OPTIONS-000005 ``` 6 安装zstd zstd是facebook为适配rocksdb开发的zstandard数据压缩工具,如果不安装该软件,会导致gorocksdb安装失败。 安装步骤如下: ```sh cd /usr/local git clone https://github.com/facebook/zstd.git cd zstd make make install ``` 7 安装gorocksdb 通过1-6安装步骤后,rocksdb会被安装在 usr/local/rocksdb 这个目录下,我们使用go版本的rocksdb需要依赖于该路径。 目前使用的gorocksdb为:github.com/yiyanwannian/gorocksdb 安装上述的安装路径,使用下面的命令即可: ```sh CGO_CFLAGS="-I/usr/local/rocksdb/include" \ CGO_LDFLAGS="-L/usr/local/rocksdb -lrocksdb -lstdc++ -lm -lz -lbz2 -lsnappy -llz4 -lzstd" \ go get github.com/yiyanwannian/gorocksdb ``` 如果安装目录有变化,则修改对应的/usr/local/rocksdb对应的路径