Linux服務器是CentOS 7.x
Elasticsearch下載地址:
https://www.elastic.co/downloa
解壓 tar -avxf elasticsearch-7.6.2-linux-x86_64.tar.gz -C /usr/local/
進入解壓後的elasticsearch目錄:
(1)新建data目錄:
mkdir data
1
(2)修改config/elasticsearch.yml:
vim config/elasticsearch.yml
1
取消下列項註釋並修改:
cluster.name: my-application #集羣名稱
node.name: node-1 #節點名稱
#數據和日誌的存儲目錄
path.data: /usr/local/elasticsearch-7.1.1/data
path.logs: /usr/local/elasticsearch-7.1.1/logs
#設置綁定的ip,設置爲0.0.0.0以後就可以讓任何計算機節點訪問到了
network.host: 0.0.0.0
http.port: 9200 #端口
#設置在集羣中的所有節點名稱,這個節點名稱就是之前所修改的,當然你也可以採用默認的也行,目前是單機,放入一個節點即可
cluster.initial_master_nodes: ["node-1"]
修改完畢後,:wq 保存退出vim
準備啓動es
進入/bin目錄執行命令:
./elasticsearch
1
我這裏出現如下錯誤:
Java HotSpot(TM) 64-Bit Server VM warning: INFO: os::commit_memory(0x00000000c5330000, 986513408, 0) failed; error='Cannot allocate memory' (errno=12)
#
# There is insufficient memory for the Java Runtime Environment to continue.
# Native memory allocation (mmap) failed to map 986513408 bytes for committing reserved memory.
# An error report file with more information is saved as:
# logs/hs_err_pid22863.log
[root@VM_0_2_centos bin]#
看來是我這1G的內存太小了啊,elasticsearch使用java的jvm默認是使用1G的內存的,這裏我們修改一下內存,直接把內存改到200m
cd 到es目錄修改 ./config/jvm.options:
vim ./config/jvm.options
修改該內容:
-Xms200m
-Xmx200m
:wq 保存並退出vim,再次啓動es
再次啓動出現如下錯誤:
[root@localhost bin]# ./elasticsearch
future versions of Elasticsearch will require Java 11; your Java version from [/usr/local/java/jdk1.8.0_231/jre] does not meet this requirement
[2020-04-19T20:41:50,510][ERROR][o.e.b.ElasticsearchUncaughtExceptionHandler] [node-1] uncaught exception in thread [main]
org.elasticsearch.bootstrap.StartupException: java.lang.RuntimeException: can not run elasticsearch as root
at org.elasticsearch.bootstrap.Elasticsearch.init(Elasticsearch.java:174) ~[elasticsearch-7.6.2.jar:7.6.2]
at org.elasticsearch.bootstrap.Elasticsearch.execute(Elasticsearch.java:161) ~[elasticsearch-7.6.2.jar:7.6.2]
at org.elasticsearch.cli.EnvironmentAwareCommand.execute(EnvironmentAwareCommand.java:86) ~[elasticsearch-7.6.2.jar:7.6.2]
at org.elasticsearch.cli.Command.mainWithoutErrorHandling(Command.java:125) ~[elasticsearch-cli-7.6.2.jar:7.6.2]
at org.elasticsearch.cli.Command.main(Command.java:90) ~[elasticsearch-cli-7.6.2.jar:7.6.2]
at org.elasticsearch.bootstrap.Elasticsearch.main(Elasticsearch.java:126) ~[elasticsearch-7.6.2.jar:7.6.2]
at org.elasticsearch.bootstrap.Elasticsearch.main(Elasticsearch.java:92) ~[elasticsearch-7.6.2.jar:7.6.2]
Caused by: java.lang.RuntimeException: can not run elasticsearch as root
at org.elasticsearch.bootstrap.Bootstrap.initializeNatives(Bootstrap.java:105) ~[elasticsearch-7.6.2.jar:7.6.2]
at org.elasticsearch.bootstrap.Bootstrap.setup(Bootstrap.java:172) ~[elasticsearch-7.6.2.jar:7.6.2]
at org.elasticsearch.bootstrap.Bootstrap.init(Bootstrap.java:349) ~[elasticsearch-7.6.2.jar:7.6.2]
at org.elasticsearch.bootstrap.Elasticsearch.init(Elasticsearch.java:170) ~[elasticsearch-7.6.2.jar:7.6.2]
... 6 more
uncaught exception in thread [main]
java.lang.RuntimeException: can not run elasticsearch as root
at org.elasticsearch.bootstrap.Bootstrap.initializeNatives(Bootstrap.java:105)
at org.elasticsearch.bootstrap.Bootstrap.setup(Bootstrap.java:172)
at org.elasticsearch.bootstrap.Bootstrap.init(Bootstrap.java:349)
at org.elasticsearch.bootstrap.Elasticsearch.init(Elasticsearch.java:170)
at org.elasticsearch.bootstrap.Elasticsearch.execute(Elasticsearch.java:161)
at org.elasticsearch.cli.EnvironmentAwareCommand.execute(EnvironmentAwareCommand.java:86)
at org.elasticsearch.cli.Command.mainWithoutErrorHandling(Command.java:125)
at org.elasticsearch.cli.Command.main(Command.java:90)
at org.elasticsearch.bootstrap.Elasticsearch.main(Elasticsearch.java:126)
at org.elasticsearch.bootstrap.Elasticsearch.main(Elasticsearch.java:92)
For complete error details, refer to the log at /usr/local/elasticsearch-7.6.2/logs/my-application.log
這是不能使用root用戶操作,添加一個其他的用戶再試試:
創建一個esroot用戶並設置初始密碼
useradd -c 'ES user' -d /home/esroot esroot
passwd esroot
將es安裝目錄屬主權限改爲esroot用戶
chown -R esroot /usr/local/elasticsearch-7.6.2/
切換用戶到esroot
su esroot
改一下es目錄所屬用戶:
chown esroot /usr/local/elasticsearch-7.6.2/ -R
vim 編輯 /etc/security/limits.conf,在末尾加上:
esroot nofile 65536
esroot hard nofile 65536
esroot soft nproc 4096
esroot hard nproc 4096
vim 編輯 vim /etc/security/limits.d/esroot-nproc.conf,將* 改爲用戶名(esroot):
# Default limit for number of user's processes to prevent
# accidental fork bombs.
# See rhbz #432903 for reasoning.
es soft nproc 4096
root soft nproc unlimited
vim 編輯 /etc/sysctl.conf,在末尾加上:
vm.max_map_count = 655360
執行:
[root@VM_0_2_centos ~]# sysctl -p
kernel.printk = 5
vm.max_map_count = 655360
[root@VM_0_2_centos ~]#
登錄剛纔新建的es用戶,並啓動elasticsearch,OK
[root@VM_0_2_centos elasticsearch-7.1.1]# su es
[es@VM_0_2_centos elasticsearch-7.1.1]$ ./bin/elasticsearch
後臺啓動:
[es@VM_0_2_centos elasticsearch-7.1.1]$ ./bin/elasticsearch -d
[es@VM_0_2_centos elasticsearch-7.1.1]$
1
2
查看進程:
[es@VM_0_2_centos elasticsearch-7.1.1]$ ./bin/elasticsearch -d
[es@VM_0_2_centos elasticsearch-7.1.1]$ ps -ef|grep elasticsearch
es 18652 1 19 17:48 pts/2 00:00:00 /usr/local/java/jdk1.8.0_211/bin/java -Xms200m -Xmx200m -XX:+UseConcMarkSweepGC -XX:CMSInitiatingOccupancyFraction=75 -XX:+UseCMSInitiatingOccupancyOnly -Des.networkaddress.cache.ttl=60 -Des.networkaddress.cache.negative.ttl=10 -XX:+AlwaysPreTouch -Xss1m -Djava.awt.headless=true -Dfile.encoding=UTF-8 -Djna.nosys=true -XX:-OmitStackTraceInFastThrow -Dio.netty.noUnsafe=true -Dio.netty.noKeySetOptimization=true -Dio.netty.recycler.maxCapacityPerThread=0 -Dlog4j.shutdownHookEnabled=false -Dlog4j2.disable.jmx=true -Djava.io.tmpdir=/tmp/elasticsearch-182563007296674551 -XX:+HeapDumpOnOutOfMemoryError -XX:HeapDumpPath=data -XX:ErrorFile=logs/hs_err_pid%p.log -XX:+PrintGCDetails -XX:+PrintGCDateStamps -XX:+PrintTenuringDistribution -XX:+PrintGCApplicationStoppedTime -Xloggc:logs/gc.log -XX:+UseGCLogFileRotation -XX:NumberOfGCLogFiles=32 -XX:GCLogFileSize=64m -Dio.netty.allocator.type=unpooled -Des.path.home=/usr/local/elasticsearch-7.1.1 -Des.path.conf=/usr/local/elasticsearch-7.1.1/config -Des.distribution.flavor=default -Des.distribution.type=tar -Des.bundled_jdk=true -cp /usr/local/elasticsearch-7.1.1/lib/* org.elasticsearch.bootstrap.Elasticsearch -d
es 18728 8399 0 17:48 pts/2 00:00:00 grep --color=auto elasticsearch
[es@VM_0_2_centos elasticsearch-7.1.1]$
執行命令ss -taln
[root@localhost ~]# ss -taln
State Recv-Q Send-Q Local Address:Port Peer Address:Port
LISTEN 0 128 127.0.0.1:9000 *:*
LISTEN 0 128 *:80 *:*
LISTEN 0 128 *:22 *:*
LISTEN 0 128 *:88 *:*
LISTEN 0 100 127.0.0.1:25 *:*
LISTEN 0 80 :::3306 :::*
LISTEN 0 128 :::9200 :::*
LISTEN 0 128 :::9300 :::*
LISTEN 0 128 :::22 :::*
LISTEN 0 100 ::1:25
設置防火牆 開啓 9200端口號訪問
firewall-cmd --zone=public --add-port=9200/tcp --permanent
systemctl restart firewalld
firewall-cmd --query-port=9200/tcp
· 可在瀏覽器中輸入如下地址:http://192.168.1.54:9200/
· 如果顯示如上信息,則代表Linux下ES已經搭建完畢(單機)
停止
· 若是es的前臺運行,則用ctrl + c 來停止。
· 若是es的後臺運行,則用kill -9 進程號 來停止。(可通過jps命令,查看es進程號)
linux設置elasticsearch開機啓動
vim /etc/rc.local
添加
#elasticsearch開機自啓動
su esroot
/usr/local/elasticsearch-7.6.2/bin/elasticsearch -d
重啓,查看進程
jps
或者ss -taln