1、安裝部署
(1)下載
下載地址https://www.elastic.co/cn/downloads/elasticsearch
可以按照windows版那樣從官網下載TAR格式解壓包,上傳到linux服務器上。
如果你的linux可以訪問外網的話,推薦直接在linux中下載,執行如下命令:
wget https://artifacts.elastic.co/downloads/elasticsearch/elasticsearch-7.3.0-linux-x86_64.tar.gz
(2)解壓
執行解壓命令:
tar -zxvf elasticsearch-7.3.0-linux-x86_64.tar.gz
解壓完成後,會出現elasticsearch-7.3.0目錄:
(3)啓動
執行啓動命令:
./bin/elasticsearch -d -p pid
如果你是root用戶啓動的話,會報"can not run elasticsearch as root"的錯誤。因爲安全問題elasticsearch不讓用root用戶直接運行,所以要創建新用戶,繼續閱讀創建新用戶步驟。
生成pid文件
(4)關閉
pkill -F pid
(5)創建新用戶
第一步:liunx創建新用戶:"adduser es",然後給創建的用戶加密碼:"passwd es",輸入兩次密碼。
第二步:切換剛纔創建的用戶:"su es",然後啓動elasticsearch。如果顯示Permission denied權限不足,則繼續進行第三步。
第三步:給新用戶賦權限,因爲這個用戶本身就沒有權限,肯定自己不能給自己付權限。所以要用root用戶登錄並賦予權限,chown -R es/你的elasticsearch安裝目錄。
chown -R es elasticsearch-7.3.0
或
chown -R es:es elasticsearch-7.3.0
通過上面三步就可以啓動elasticsearch了。
(6)驗證啓動
如果一切正常,Elasticsearch就會在默認的9200端口運行。這時,打開另一個命令行窗口,請求該端口:
curl localhost:9200
如果得到如下的返回,就說明啓動成功了:
默認情況下,Elasticsearch 只允許本機訪問,如果需要遠程訪問,可以修改 Elasticsearch 安裝目錄中的config/elasticsearch.yml文件,去掉network.host的註釋,將它的值改成0.0.0.0,讓任何人都可以訪問,然後重新啓動 Elasticsearch 。
network.host: 0.0.0.0
上面代碼中,"network.host:"和"0.0.0.0"中間有個空格,不能忽略,不然啓動會報錯。線上服務不要這樣設置,要設成具體的 IP。
(7)常見錯誤及處理方式
錯誤一:max virtual memory areas vm.max_map_count [65530] likely too low, increase to at least [262144]
解決:執行下面的命令:
sudo sysctl -w vm.max_map_count=262144
檢查配置是否生效
sysctl -a | grep "vm.max_map_count"
錯誤二:max file descriptors [4096] for elasticsearch process likely too low, increase to at least [65536]
解決:執行下面的命令:
sudo vim /etc/security/limits.conf
然後編輯limits.conf增加如下配置:
# elasticsearch config start
* soft nofile 65536
* hard nofile 131072
* soft nproc 2048
* hard nproc 4096
# elasticsearch config end
保存後,執行:
sudo sysctl -p
錯誤三:the default discovery settings are unsuitable for production use; at least one of [discovery.seed_hosts, discovery.seed_providers, cluster.initial_master_nodes] must be configured
一直都報這個錯誤,如果不配(config/elasticsearch.ym):
#cluster.initial_master_nodes: ["node-1", "node-2"]
cluster.initial_master_nodes: ["node-1"] 這個的話,這裏的node-1是上面一個默認的記得打開就可以了
頁面訪問
http://172.171.16.222:9200/_cat/
2、elasticsearch和head插件安裝
插件地址:
https://www.elastic.co/guide/en/elasticsearch/plugins/7.3/installation.html
CentOS7 Elasticsearch-7.3.0 JDK8
JDK8 下載地址:http://www.oracle.com/technetwork/java/javase/downloads/jdk8-downloads-2133151.html
Elasticsearch-7.3.0下載地址: https://www.elastic.co/downloads/elasticsearch
安裝Head插件
由於head插件本質上還是一個nodejs的工程,因此需要安裝node,使用npm來安裝依賴的包。
(1)安裝Node.js
下載解壓
tar -zxf node-v12.8.0-linux-x64.tar.gz
配置並生效
vim /etc/profile
export NODE_HOME=/home/node-v12.8.0-linux-x64
export PATH=$PATH:$NODE_HOME/bin
source /etc/profile
查看版本驗證
node -v
npm -v
(2)下載head插件
如果未安裝git ,則先安裝git工具
yum install –y git
(3)安裝grunt
cd elasticsearch-head
npm install -g grunt --registry=https://registry.npm.taobao.org
npm install -g grunt-contrib-clean --registry=https://registry.npm.taobao.org --save
npm install -g grunt-contrib-concat --registry=https://registry.npm.taobao.org
npm install -g grunt-contrib-watch --registry=https://registry.npm.taobao.org
npm install -g grunt-contrib-connect --registry=https://registry.npm.taobao.org
npm install -g grunt-contrib-copy --registry=https://registry.npm.taobao.org
npm install -g grunt-contrib-jasmine --ignore-scripts --registry=https://registry.npm.taobao.org
(4)安裝插件
npm install
報錯:
解決方案,進入工作目錄
rm -rf node_modules
rm -rf package-lock.json
npm cache clear --force
npm install
在elasticsearch-head目錄下node_modules/grunt下如果沒有grunt二進制程序,需要執行:
npm install grunt --save --registry=https://registry.npm.taobao.org
(5)修改配置 elasticsearch-head下Gruntfile.js文件
修改connect配置節點
在該配置文件中connect-server-options下添加hostname: '0.0.0.0',
這個配置,這樣就不限制IP地址的訪問了,具體看以下配置文件截圖:
修改 _site/app.js 修改http://localhost:9200字段到本機ES端口與IP
(6)修改 elasticsearch配置文件
修改elasticsearch.yml文件加入以下內容:
# 是否支持跨域
http.cors.enabled: true
# *表示支持所有域名
http.cors.allow-origin: "*"