python :3.6.0
ELasticsearch:5.5.0
MongoDB:3.0.6
mongo连接工具:NOSQLBooster
使用自带的pip 安装mongo-connector
注意:mongo-connector原理是使用Mongo的操作日志oplog,所以必须配置Mongodb为副本模式
mongo-connector replicates operations from the MongoDB oplog, so areplica set must be running before startup. For development purposes, you may find it convenient to run a one-node replica set
1、安装mongodb---忽略
2、关闭运行的mongodb服务,以下多种方式
- 使用net stop MongoDB
- 登陆mongo -->use admin --> db.shutdownServer()
- mongod --shutdown -dbpath /mongodb/data
- 使用kill命令
①查看mongo相关进程
ps -ef |grep mongo
②kill mongo进程
备注:可以使用看kill命令,给mongod进程发送SIGINT或者SIGTERM信号,
即使用“kill -2 PID”或者“kill -15 PID”
建议不要使用“kill -9 pid”,因为如果MongoDB运行在没开启日志(--journal)的情况下,可能会造成数据损失
3、创建mongodb(如下创建一个名为rs0,端口为27017的实例)
mongod --port 27017 --dbpath "D:\mongo\data" --replSet rs0
4、连接mongo,启动一个副本集
$mongo
>rs.initiate() ---必须
>rs.status() -----可查看状态(不必须)
>rs.conf() -----可查看配置(不必须)
>rs.add(HOST_NAME:PORT) --可添加副本集成员(不必须)
5、mongo-connector安装
python -m pip install elastic2_doc_manager(for ES2.X)
python -m pip install mongo-connector
6、开始同步
由于采用的单机使用127.0.0.1,执行下面命令:
mongo-connector --auto-commit-interval=0 -m 127.0.0.1:27017 -t 127.0.0.1:9200 -d elastic2_doc_manager
命令解释如下:
-m Mongod实例的访问路径
-t Solr的BASE_URL
-o 记录oplog处理时间戳的文件
-n mongo命名空间,是监听哪个database哪个collection的设置,以逗号分隔多个命名空间,这里是监听test库中的test集合
-d 就是处理doc的py文件
7、新开一个窗口,连接mongo,插入数据做测试
>use runoob
>db.col_02.insert({name:"renzhengfei", birth:"1954-03-21", sex:"man", company:"huawei"});
> db.col_03.insert({name:"renzhengfei", birth:"1954-03-21", sex:"man", company:"huawei"});
得到如下结果:
报错:
urllib3.exceptions.ReadTimeoutError: HTTPConnectionPool(host='127.0.0.1', port=9200): Read timed out. (read timeout
解决办法:找到文件:elastic2_doc_manager.py
$vim /usr/python36/lib/python3.6/site-packages/mongo_connector/doc_managers/elastic2_doc_manager.py
将文件中的self.elastic = Elasticsearch(hosts=url, **client_options)
修改为self.elastic = Elasticsearch(hosts=url,timeout=200, **client_options)
重新执行mongo-connector同步代码,测试成功:
以上测试中,在单机mongo添加数据后,需要约2分钟作用才能在elasticsearch中刷新看到同步的数据,具体问题还待研究
参考链接:
https://blog.csdn.net/shiyaru1314/article/details/52468436
https://blog.csdn.net/laoyang360/article/details/51842822
http://www.bankaiyuan.com/t/209
https://blog.csdn.net/zhujq_icode/article/details/81318687
https://es.xiaoleilu.com/010_Intro/25_Tutorial_Indexing.html(Elasticsearch权威指南(中文版))