有個數據庫表有點大,而且有多條件查詢,於是決定使用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 .