环境
- windows7,64位(win10也可以),其他未做测试
- mongo4.2
- elasticsearch7.6.1
- ik分词器(7.6.1)
- monstache
mong远程服务器开启replica set及配置
MongoDB 开启复制集
MongoDB复制是将数据同步在多个服务器的过程。
复制提供了数据的冗余备份,并在多个服务器上存储数据副本,提高了数据的可用性, 并可以保证数据的安全性。
复制还允许您从硬件故障和服务中断中恢复数据。
关闭正在运行的MongoDB服务器,开启复制集。
- 通过指定 –replSet 选项来启动mongoDB。–replSet 基本语法格式如下:
./mongod --port "PORT" --dbpath "YOUR_DB_DATA_PATH" --replSet "REPLICA_SET_INSTANCE_NAME"
- 实例
- 在‘data\’下创建rs0和log文件夹(用于存放repilica set的数据)
./mongod --port 27018 --dbpath "/data/rs0" --replSet rs0
# 或者
# mongo.config内容
port=27018
bind_ip=0.0.0.0 # 这个一定要写,不然内网无法访问
dbpath=D:\data\rs0
logpath=D:\data\log\mongodb.log
logappend=true
repliSet=rs0
mongod -f mongo.config
- 注意:这里需要在/目录下创建好/data/rs0这个文件。
- 以上实例会启动一个名为rs0的MongoDB实例,其端口号为27018。
将mongo客户端与副本集成员连接。
-
进入mongodb/bin下
-
启动Mongo客户端并连接上mongoDB服务
./mongo
初始化副本集
- 在Mongo客户端输入rs.initiate(),初始化副本集。
rs.initiate()
{
"info2" : "no configuration specified. Using a default configuration for the set",
"me" : "b48eafd69929:27017",
"ok" : 1
}
- 可以使用rs.conf()来查看副本集的配置。
rs0:SECONDARY> rs.conf()
{
"_id" : "rs0",
"version" : 1,
"protocolVersion" : NumberLong(1),
"members" : [
{
"_id" : 0,
"host" : "test:27017",
"arbiterOnly" : false,
"buildIndexes" : true,
"hidden" : false,
"priority" : 1,
"tags" : {
},
"slaveDelay" : NumberLong(0),
"votes" : 1
}
],
"settings" : {
"chainingAllowed" : true,
"heartbeatIntervalMillis" : 2000,
"heartbeatTimeoutSecs" : 10,
"electionTimeoutMillis" : 10000,
"getLastErrorModes" : {
},
"getLastErrorDefaults" : {
"w" : 1,
"wtimeout" : 0
},
"replicaSetId" : ObjectId("577b74bd0ba41a313110ad62")
}
}
- 看副本集状态使用 rs.status() 命令。
rs0:PRIMARY> rs.status()
{
"set" : "rs0",
"date" : ISODate("2016-07-05T08:50:55.272Z"),
"myState" : 1,
"term" : NumberLong(1),
"heartbeatIntervalMillis" : NumberLong(2000),
"members" : [
{
"_id" : 0,
"name" : "test:27017",
"health" : 1,
"state" : 1,
"stateStr" : "PRIMARY",
"uptime" : 115,
"optime" : {
"ts" : Timestamp(1467708606, 1),
"t" : NumberLong(1)
},
"optimeDate" : ISODate("2016-07-05T08:50:06Z"),
"infoMessage" : "could not find member to sync from",
"electionTime" : Timestamp(1467708605, 2),
"electionDate" : ISODate("2016-07-05T08:50:05Z"),
"configVersion" : 1,
"self" : true
}
],
"ok" : 1
}
- 至此repilica set 开启成功
- 开启mongo repilica set参考文档
https://docs.mongodb.com/manual/tutorial/deploy-replica-set/#procedure
http://www.htsjk.com/Elasticsearch/35093.html
ES远程服务器安装部署及配置
安装
- 下载解压即可
https://www.elastic.co/cn/downloads/elasticsearch - 分词器
https://github.com/medcl/elasticsearch-analysis-ik/tree/v7.6.1
指定elasticsearch7.6.1使用自带的jdk13
- 切到目录C:\elasticsearch-7.6.1-windows-x86_64\elasticsearch-7.6.1\bin
- 修改 elasticsearch-env.bat
- 找到这个地方改成这样即可
if defined JAVA_HOME (
set JAVA="%ES_HOME%\jdk\bin\java.exe"
set JAVA_HOME="%ES_HOME%\jdk"
set JAVA_TYPE=bundled jdk
) else (
set JAVA="%ES_HOME%\jdk\bin\java.exe"
set JAVA_HOME="%ES_HOME%\jdk"
set JAVA_TYPE=bundled jdk
)
- 切到目录C:\elasticsearch-7.6.1-windows-x86_64\elasticsearch-7.6.1\config
- 修改jvm.options像这样
## GC configuration
8-13:-XX:+UseG1GC
8-13:-XX:CMSInitiatingOccupancyFraction=75
8-13:-XX:+UseCMSInitiatingOccupancyOnly
配置elasticsearch.yml
network.host: 192.168.1.171
network.publish_host: 192.168.1.171
http.port: 9201
transport.tcp.port: 9301
-
接着让运维的同学把端口映射到外网访问的地址即可
-好了,es也配置好了 -
指定es7内置jdk设置参考
https://blog.csdn.net/u011783999/article/details/100766759 -
ELK搭建过程中出现的问题与解决方法汇总
monstache远程服务器的安装及配置
安装
- 下载解压即可
https://github.com/rwynn/monstache/releases/tag/v6.5.2
(注意不需要安装go环境即可运行,一定要下载es和mongo都对应的版本)
配置文件的编写
- 按照这样配置即可
mongo-url = "mongodb://127.0.0.1:27018"
elasticsearch-urls = ["http://111.231.92.176:29043"]
elasticsearch-max-conns = 10
namespace-regex = '^app\.app_kankanxinwen*' #aaa表示mongodb的数据库,bbb表示集合,表示要匹配的名字空间
# direct-read-namespaces = [“common.parent_info”,”common.child_info”]
dropped-collections = true
# propogate dropped databases in MongoDB as index deletes in Elasticsearch
dropped-databases = true
resume = true #从上次同步的时间开始同步
# cluster-name = 'tzg' #es集群名
# 更新es而不是覆盖
index-as-update = true
verbose = true
- 启动monstache
monstache -f config.toml
-
注意monstache服务要放在mongo服务器上开启,放其他位置,无法连接远程mongo数据库(应该有这个功能,可能哪里需要配置,我这里没有实现)
-
配置文件参考
https://medium.com/@nehajirafe/mongodb-to-elasticsearch-sync-using-monstache-cfe1177594b6
填坑指南
- 服务器端执行toml文件报错
(last key parsed '')bear keys cannot contain '\ufeff'
这是由于某些系统默认文本编码为BOM,改为utf-8即可
- mongo无法远程连接
检查内网是否调通
一般设置为0.0.0.0即可 - monstache无法连接远程mongo数据库,只能连接本地的
把monstache服务放在mongo本地服务器运行