ElasticSearch的安装
Windows下安装elasticSearch
前往https://thans.cn/mirror/elasticsearch.html Elasticsearch 镜像下载站 下载合适的版本.
进入ElasticSearch安装目录下的bin目录 执行命令elasticsearch 即可启动,在浏览器访问IP:9200即可观察结果。
注意:Java开发使用9300端口
linux下利用docker安装elasticsearch
拉取合适的镜像
创建容器
docker run -d -e ES_JAVA_POTS="-Xms256m -Xmx256m" -e "discovery.type=single-node" -p 9200:9200 -p 9300:9300 --name ES3 8f46db60ddd6
elasticsearch的运行需要大内容,测试运行可以调小,linux需要优化,优化步骤如下:
``` 修改/etc/security/limits.conf ,追加内容
- soft nofile 65536
- hard nofile 65536 nofile是单个进程允许打开的最大文件个数 soft nofile 是软限制 hard nofile是硬限制
修改/etc/sysctl.conf,追加内容 vm.max_map_count=655360 限制一个进程可以拥有的VMA(虚拟内存区域)的数量 执行下面命令 修改内核参数马上生效 sysctl ‐p
3.将配置文件挂载出来是为了便于操作,修改配置文件
设置跨域访问允许
http.cors.enabled: true http.cors.allow-origin: “*”
允许远程http访问
http.host: 0.0.0.0
允许远程访问
transport.host: 0.0.0.0
4.重新启动虚拟机,再次启动容器<a name="1885a434"></a>#### 安装head图形化界面来实现Elasticsearch的日常管理1.下载head插件:[https://github.com/mobz/elasticsearch-head](https://github.com/mobz/elasticsearch-head)2.解压到任意目录,但是要和elasticsearch的安装目录区别开3.安装node.js , cpnm
npm install ‐g cnpm ‐‐registry=https://registry.npm.taobao.org
4.将grunt安装为全局命令 。Grunt是基于Node.js的项目构建工具。它可以自动运行你所<br />设定的任务
npm install ‐g grunt‐cli
5.进入head安装目录,安装依赖
cnpm install
6.启动head,在命令提示符下输入命令
grunt server
7.打开浏览器,输入 [http://localhost:9100](http://localhost:9100) 可以看到界面8.此时页面连接elasticsearch的按钮无效,需要修改elasticsearch的配置,让其允许跨域访问 ,修改elasticsearch配置文件:elasticsearch.yml
http.cors.enabled: true http.cors.allow-origin: “*”
<a name="4ec94f5b"></a>#### 为elasticsearch安装中文分词器IK1.前往https://github.com/medcl/elasticsearch-analysis-ik/releases下载与elasticsearch对应版本的分词器2.先将其解压,将解压后的文件夹重命名文件夹为ik3.将ik文件夹拷贝到elasticsearch/plugins 目录下4.重新启动elasticsearch,即可加载IK分词器> K提供了两个分词算法ik_smart 和 ik_max_word其中 ik_smart 为最少切分,ik_max_word为最细粒度划分 ,可以通过在浏览器输入:> http://127.0.0.1:9200/_analyze?analyzer=ik_smart&pretty=true&text=我是程序员> http://127.0.0.1:9200/_analyze?analyzer=ik_max_word&pretty=true&text=我是程序员> 观察区别5.自定义词库1.进入elasticsearch/plugins/ik/config目录2.新建一个my.dic文件,编辑内容:
英雄联盟
3.修改IKAnalyzer.cfg.xml(在ik/config目录下)
<a name="8b356d94"></a>#### elasticsearch与mysql数据库的同步使用Logstash同步数据Logstash是一款轻量级的日志搜集处理框架,可以方便的把分散的、多样化的日志搜集<br />起来,并进行自定义的处理,然后传输到指定的位置,比如某个服务器或者文件 。<a name="33323e7c"></a>##### 安装logstash1.下载地址https://www.elastic.co/cn/downloads/2.解压,进入bin目录创建文件defalutconf.bat
input { stdin {} } output { stdout {} }
<br />测试 logstash的启动
logstash -f defaultconf.bat
3.控制台输入字符,随后就有日志输出 ,说明启动完成<a name="6779f349"></a>##### 同步mysql数据> 数据不删1.在logstash安装目录下创建文件夹mysqletc (名称随意)2.将mysq驱动包mysql-connector-java-5.1.46.jar 放在mysqletc下3.文件夹下创建mysql.conf (名称随意) ,内容如下:```javainput {jdbc {# mysql jdbc connection string to our backup databsejdbc_connection_string => "jdbc:mysql://localhost:3306/tensquare_article?useUnicode=true&characterEncoding=utf-8&useSSL=false"# the user we wish to excute our statement asjdbc_user => "root"jdbc_password => "password"# the path to our downloaded jdbc driverjdbc_driver_library => "D:/tool/elasticsearch/logstash/logstash/mysqletc/mysql-connector-java-5.1.46.jar"# the name of the driver class for mysqljdbc_driver_class => "com.mysql.jdbc.Driver"jdbc_paging_enabled => "true"jdbc_page_size => "50"# 以下对应着要执行的sql的绝对路径。# statement_filepath => ""statement => "select * from tb"# 定时字段 各字段含义(由左至右)分、时、天、月、年,全部为*默认含义为每分钟都更新(测试结果,不同的话请留言指出)schedule => "* * * * *"}}output {elasticsearch {# ESIP地址与端口hosts => "http://127.0.0.1:9200"# ES索引名称(自己定义的)index => "index"# 自增ID编号document_id => "%{id}"document_type => "document"}stdout {# 以JSON格式输出codec => json_lines}}
