有个数据库表有点大,而且有多条件查询,于是决定使用es来缓解查询等需求,正好重新整理下安装过程.
下载与安装
需要先安装好jdk
官网直接下载 https://www.elastic.co/guide/en/elasticsearch/reference/5.6/zip-targz.html
版本使用是5.6,现在最新版本是6
更改配置
如果你不是本机装,就要改下ip 的配置,否则其它机器访问不了:
vi config/elasticsearch.yml
以下配置改成 0.0.0.0
network.host: 0.0.0.0
启动与问题处理
es是不能用root启动的,需要另外用户来启动
bin/elasticsearch -d
加-d 是为了后台启动,配置文件会自己找到config/elasticsearch.yml,默认端口为9200
如果出现下面的错误
ERROR: [1] bootstrap checks failed
[1]: max file descriptors [4096] for elasticsearch process is too low, increase to at least [65536]
[2018-01-14T19:37:43,195][INFO ][o.e.n.Node ] [LONqFhr] stopping ...
[2018-01-14T19:37:44,065][INFO ][o.e.n.Node ] [LONqFhr] stopped
[2018-01-14T19:37:44,065][INFO ][o.e.n.Node ] [LONqFhr] closing ...
[2018-01-14T19:37:44,125][INFO ][o.e.n.Node ] [LONqFhr] closed
这是文件打开数的配置太低了,修改下ulimit, 请查看文章末尾的 <怎么修改ulimit > 章节
如果出现
max virtual memory areas vm.max_map_count [65530] is too low, increase to at least [262144]
则如下操作:
切换root账户 vim /etc/sysctl.conf
增加一行 vm.max_map_count=655360
接着执行 sysctl -p
访问es
访问查看:
http://xxxx:9200/
如 curl -XGet http://127.0.0.1:9200
返回像这样:
{
"name" : "LONqFhr",
"cluster_name" : "elasticsearch",
"cluster_uuid" : "nMxOErJrSgmZLVBxDcSBQg",
"version" : {
"number" : "5.6.5",
"build_hash" : "6a37571",
"build_date" : "2017-12-04T07:50:10.466Z",
"build_snapshot" : false,
"lucene_version" : "6.6.1"
},
"tagline" : "You Know, for Search"
}
安装 kibana
kibana类似一个查看es的界面系统.安装 和es一样版本的kibana
下载
https://www.elastic.co/downloads/past-releases
解析后修改
vi config/kibana.yml
修改以下内容
server.host: "0.0.0.0"
elasticsearch.url: "http://localhost:9200"
直接访问
http://10.202.17.163:5601
开始的时候,会经常使用
dev tools
安装elasticsearch-head
es-head 是es的辅助工具,用来查看es集群状态,索引,数据等的web工具,elasticsearch-head 是和es 不同的进程,启动后,在界面上把es的访问地址输入即可查看es的内容。
https://github.com/mobz/elasticsearch-head
直接下载后 (npm 是node 的一个工具,要安装node,版本在6以上)
npm install
然后npm run start
访问: http://1xx.1xx.51.98:9100/
不过es-head要访问es 需要es做个简单的配置,config/elasticsearch.yml 最后增加,这是让es服务可供跨域的一个配置
增加后重启es:
http.cors.enabled : true
http.cors.allow-origin : "*"
搞es 集群
es集群只需要改配置文件即可,设定一个master,其它机器作为slave 指向这个master即可.
- 设定master:
配置追加:
cluster.name: a_cluster
node.name: master
node.master: true
- 新建slave:
重新解压内容后配置追加:
cluster.name: a_cluster
node.name: slave1
network.host: 0.0.0.0
http.port: 9201
discovery.zen.ping.unicast.hosts: ["127.0.0.1"]
cluster.name 是要和master一样的
http.port 是为了 和master的端口不一样
discovery.zen.ping.unicast.hosts 则是找寻 master
如果要再加一个slave,则可如下配置:
cluster.name: a_cluster
node.name: slave2
network.host: 0.0.0.0
http.port: 9202
discovery.zen.ping.unicast.hosts: ["127.0.0.1"]
上面是三个es文件夹,启动三个ES服务
es-head 可以看到集群情况:

es的简单使用:
创建索引:
http://1xx.1xx.51.98:9200/people 用put方法
{
"settings": {
"number_of_shards": 3,
"number_of_replicas": 1
},
"mappings":{
"man": {
"properties": {
"name": {
"type":"text"
}
"age":{
"type":"integer"
},
"bithdate": {
"type": "bithdate",
"format":"yyyy-MM-dd HH:mm:ss||yyyy-MM-dd||epoch_millis"
}
}
}
}
}
插入数据:
http://1xx.1xx.51.98:9200/people/man 用post方法
{
"name":"abc3",
"age":31,
"bithdate":"1928-03-04"
}
查询数据
http://1xx.1xx.51.98:9200/people/man/_search
{
“query”:{
“match_phrase”:{
“name”:“abc2”
}
}
}
其它资料:
官方文档:
https://www.elastic.co/guide/en/elasticsearch/reference/5.6/index.html
https://coding.imooc.com/learn/list/167.html
错误
重启时出现:
Caused by: java.lang.IllegalStateException: failed to obtain node locks, tried [[/home/comp/es/elasticsearch5/data/a_cluster]] with lock id [0]; maybe these locations are not writable or multiple nodes were started without increasing [node.max_local_storage_nodes] (was [1])?
解决 : ps aux | grep elastic
是进程没有关
ERROR: [1] bootstrap checks failed
[1]: initial heap size [524288000] not equal to maximum heap size [1073741824]; this can cause resize pauses and prevents mlockall from locking the entire heap
调整一下config/jvm… 把xms和xmx调一下,如1600m
怎么修改ulimit
1,查看ulimit (这个和用户有关)
ulimit -n
2,修改ulimit
ulimit -n 65536 ( 这个要有root权限,且是只当前会话生效, 如果没有sudo权限,则要看下一步)
3, 修改全局的 ulimit
- 修改配置
/etc/security/limits.conf
如修改成以下,soft是指不硬性即会warn,hard则是硬性规定
* soft nofile 66536
* hard nofile 66537
重新登陆下用户,ulimit -n 看看是否生效,
这里可以通过 su 命令的方式回到当前用户去ulimit -n ,看看是否生效
如果不生效,则以下:
-
找到 pam_limits.so
find /lib -name pam_limits.so -
修改 vim /etc/pam.d/sshd
[Add the line]
session required /lib/security/pam_limits.so
-
重启下ssh
service ssh restart -
查看是否生效,
ulimit -n
看值 是否是 66536
由于网上的内容很多,我在实验过程不清楚是自己客户端的问题,还是什么,再登陆的时候,总是不生效,
第一次,修改完后再service ssh restart ,生效了,但后来再修改/etc/security/limits.conf 的值,和重启,后一直不生效
不过我发现如果通过 su的方式重新进入当前用户,都会直接生效,且这种方法并不需要service ssh restart .
本文详细介绍Elasticsearch的安装步骤、配置方法及常见问题解决,包括如何搭建ES集群、安装Kibana与elasticsearch-head等辅助工具。
1183

被折叠的 条评论
为什么被折叠?



