Debian/Ubuntu操作如下:

1
2
apt install libsnappy-dev zlib1g-dev libbz2-dev liblz4-dev libzstd-dev libgflags-dev
1

下载,编译,安装:

1
2
3
4
5
6
wget https://github.com/facebook/rocksdb/archive/v6.6.4.zip #6.6.4 (2020-01-31)
unzip v6.6.4.zip
cd rocksdb-6.6.4/
make static_lib && sudo make install-static
make shared_lib && sudo make install-shared
12345

CentOS操作如下:

1
2
yum install snappy snappy-devel zlib zlib-devel bzip2 bzip2-devel lz4-devel libasan
1

安装gflags:

1
2
3
4
5
git clone https://github.com/gflags/gflags.git
cd gflags
git checkout v2.0
./configure && make && sudo make install
1234

安装zstandard:

1
2
3
4
5
6
wget https://github.com/facebook/zstd/archive/v1.1.3.tar.gz
mv v1.1.3.tar.gz zstd-1.1.3.tar.gz
tar zxvf zstd-1.1.3.tar.gz
cd zstd-1.1.3
make && sudo make install
12345

下载,编译,安装:

1
2
3
4
5
6
wget https://github.com/facebook/rocksdb/archive/v6.1.2.zip #6.1.2 (6/4/2019)
unzip v6.1.2.zip
cd rocksdb-6.1.2/
make static_lib && sudo make install-static
make shared_lib && sudo make install-shared
12345

测试程序:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
#include <cstdio>
#include <string>

#include "rocksdb/db.h"
#include "rocksdb/slice.h"
#include "rocksdb/options.h"

using namespace std;
using namespace rocksdb;

const std::string PATH = "/tmp/rocksdb_tmp";

int main(){
DB* db;
Options options;
options.create_if_missing = true;
Status status = DB::Open(options, PATH, &db);
assert(status.ok());
Slice key("foo");
Slice value("bar");

std::string get_value;
status = db->Put(WriteOptions(), key, value);
if(status.ok()){
status = db->Get(ReadOptions(), key, &get_value);
if(status.ok()){
printf("get %s success!!\n", get_value.c_str());
}else{
printf("get failed\n");
}
}else{
printf("put failed\n");
}

delete db;
}
123456789101112131415161718192021222324252627282930313233343536

编译,运行

动态编译:

1
2
3
g++ -std=c++11 -o rocksdbtest test.cpp -lrocksdb  -lpthread
./rocksdbtest
12

静态编译:

1
2
g++ -std=c++11 -o rocksdbtest test.cpp ./librocksdb.a -lpthread -lsnappy  -lz -lbz2 -lzstd /usr/lib/x86_64-linux-gnu/liblz4.a
1

参考:Getting Started with RocksDB in CentOS 7 - Jeff Li
如果报错:

1
2
./rocksdbtest: error while loading shared libraries: librocksdb.so.6.1: cannot open shared object file: No such file or directory
1

操作如下:

1
2
3
#echo "/usr/local/lib" |sudo tee /etc/ld.so.conf.d/rocksdb-x86_64.conf
sudo ldconfig -v #refresh the ldconfig cache
12

或者:

1
2
3
INSTALL_PATH=/usr 
make shared_lib && sudo make install-shared
sudo ldconfig -v #refresh the ldconfig cache

https://blog.csdn.net/zhangpeterx/article/details/96869454

https://github.com/facebook/rocksdb/blob/master/INSTALL.md