第一次接触Elasticsearch,这里记录一下本地Ubuntu Linux环境的安装。过程中遇到不少报错,其中很多都是可以避免的,那就是安装时一定要切换到普通用户(非root账号)。
①检查依赖
Elasticsearch依赖于Java,所以本地一定要有Java环境。
root@ubuntu:~# java -version
openjdk version "10.0.2" 2018-07-17
OpenJDK Runtime Environment (build 10.0.2+13-Ubuntu-1ubuntu0.18.04.4)
OpenJDK 64-Bit Server VM (build 10.0.2+13-Ubuntu-1ubuntu0.18.04.4, mixed mode)
root@ubuntu:~# which java
/usr/bin/java
②下载解压
root@ubuntu:~# wget https://artifacts.elastic.co/downloads/elasticsearch/elasticsearch-6.5.4.tar.gz
root@ubuntu:~# tar -xvf elasticsearch-6.5.4.tar.gz
③root运行
root@ubuntu:~# cd elasticsearch-6.5.4/bin
root@ubuntu:~/elasticsearch-6.5.4/bin# bash elasticsearch
OpenJDK 64-Bit Server VM warning: Option UseConcMarkSweepGC was deprecated in version 9.0 and will likely be removed in a future release.
[2019-01-09T10:43:22,092][WARN ][o.e.b.ElasticsearchUncaughtExceptionHandler] [unknown] 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:140) ~[elasticsearch-6.5.4.jar:6.5.4]
at org.elasticsearch.bootstrap.Elasticsearch.execute(Elasticsearch.java:127) ~[elasticsearch-6.5.4.jar:6.5.4]
at org.elasticsearch.cli.EnvironmentAwareCommand.execute(EnvironmentAwareCommand.java:86) ~[elasticsearch-6.5.4.jar:6.5.4]
at org.elasticsearch.cli.Command.mainWithoutErrorHandling(Command.java:124) ~[elasticsearch-cli-6.5.4.jar:6.5.4]
at org.elasticsearch.cli.Command.main(Command.java:90) ~[elasticsearch-cli-6.5.4.jar:6.5.4]
at org.elasticsearch.bootstrap.Elasticsearch.main(Elasticsearch.java:93) ~[elasticsearch-6.5.4.jar:6.5.4]
at org.elasticsearch.bootstrap.Elasticsearch.main(Elasticsearch.java:86) ~[elasticsearch-6.5.4.jar:6.5.4]
Caused by: java.lang.RuntimeException: can not run elasticsearch as root
at org.elasticsearch.bootstrap.Bootstrap.initializeNatives(Bootstrap.java:103) ~[elasticsearch-6.5.4.jar:6.5.4]
at org.elasticsearch.bootstrap.Bootstrap.setup(Bootstrap.java:170) ~[elasticsearch-6.5.4.jar:6.5.4]
at org.elasticsearch.bootstrap.Bootstrap.init(Bootstrap.java:333) ~[elasticsearch-6.5.4.jar:6.5.4]
at org.elasticsearch.bootstrap.Elasticsearch.init(Elasticsearch.java:136) ~[elasticsearch-6.5.4.jar:6.5.4]
... 6 more
报错很明显“can not run elasticsearch as root”,所以切换到普通用户重新来吧。
④普通账户运行
root@ubuntu:~/elasticsearch-6.5.4/bin# cd
root@ubuntu:~# mv elasticsearch-6.5.4 /home/ubuntu/
root@ubuntu:~# su ubuntu
ubuntu@ubuntu:/root$ cd
ubuntu@ubuntu:~$ cd elasticsearch-6.5.4/bin/
ubuntu@ubuntu:~/elasticsearch-6.5.4/bin$ bash elasticsearch
Exception in thread "main" java.nio.file.AccessDeniedException: /home/ubuntu/elasticsearch-6.5.4/config/jvm.options
at java.base/sun.nio.fs.UnixException.translateToIOException(UnixException.java:90)
at java.base/sun.nio.fs.UnixException.rethrowAsIOException(UnixException.java:111)
at java.base/sun.nio.fs.UnixException.rethrowAsIOException(UnixException.java:116)
at java.base/sun.nio.fs.UnixFileSystemProvider.newByteChannel(UnixFileSystemProvider.java:215)
at java.base/java.nio.file.Files.newByteChannel(Files.java:369)
at java.base/java.nio.file.Files.newByteChannel(Files.java:415)
at java.base/java.nio.file.spi.FileSystemProvider.newInputStream(FileSystemProvider.java:384)
at java.base/java.nio.file.Files.newInputStream(Files.java:154)
at org.elasticsearch.tools.launchers.JvmOptionsParser.main(JvmOptionsParser.java:60)
ubuntu@ubuntu:~/elasticsearch-6.5.4/bin$ sudo chown -R ubuntu ~/elasticsearch-6.5.4/
ubuntu@ubuntu:~/elasticsearch-6.5.4/bin$ sudo chgrp -R ubuntu ~/elasticsearch-6.5.4/
可以看到切换到普通账户运行的时候报了“AccessDeniedException”,这是因为前面是以root账户下载和解压的elasticsearch,其ower和group都属于root,所以后面需要修改一下文件归属。修改完成后就可以启动了。
⑤修改配置
ubuntu@ubuntu:~/elasticsearch-6.5.4/bin$ vim ../config/elasticsearch.yml
刚开始安装试用,所以简单修改以下几项(取消注释然后修改对应值):
cluster.name: Zoo
node.name: Zoo-node-1
network.host: 0.0.0.0
http.port: 9200
“cluster.name”和“node.name”自定义即可,“network.host”的设置是为了开启外网访问。
⑥修改配置后试运行
ubuntu@ubuntu:~/elasticsearch-6.5.4/bin$ bash elasticsearch
…… omitted ……
[2019-01-09T18:44:37,429][INFO ][o.e.b.BootstrapChecks ] [80tiwtn] bound or publishing to a non-loopback address, enforcing bootstrap checks
ERROR: [1] bootstrap checks failed
[1]: max virtual memory areas vm.max_map_count [65530] is too low, increase to at least [262144]
…… omitted ……
因为修改了配置开启了外网访问,又报了系统级别的ERROR和WARNING,切回root修改一下系统配置。
ubuntu@ubuntu:~/elasticsearch-6.5.4/bin$ su root
Password:
root@ubuntu:/home/ubuntu/elasticsearch-6.5.4/bin# vim /etc/security/limits.conf
添加以下内容:
# Elasticsearch Relevant
elasticsearch soft nofile 65536
elasticsearch hard nofile 65536
elasticsearch soft nproc 4096
elasticsearch hard nproc 4096
root@ubuntu:/home/ubuntu/elasticsearch-6.5.4/bin# vim /etc/sysctl.conf
添加以下内容:
vm.max_map_count=262144
load一下sysctl修改。
root@ubuntu:/home/ubuntu/elasticsearch-6.5.4/bin# sysctl -p
vm.max_map_count = 262144
⑦静默模式执行
命令行添加-d参数启动daemon运行模式,-p参数将进程ID写入指定文件。
ubuntu@ubuntu:~/elasticsearch-6.5.4/bin$ bash elasticsearch -d -p Zoo.pid
OpenJDK 64-Bit Server VM warning: Option UseConcMarkSweepGC was deprecated in version 9.0 and will likely be removed in a future release.
这个时候elasticsearch已经在后台运行了,尝试再启动的话就会报错了。
ubuntu@ubuntu:~/elasticsearch-6.5.4/bin$ bash elasticsearch
OpenJDK 64-Bit Server VM warning: Option UseConcMarkSweepGC was deprecated in version 9.0 and will likely be removed in a future release.
[2019-01-09T19:04:59,829][WARN ][o.e.b.ElasticsearchUncaughtExceptionHandler] [unknown] uncaught exception in thread [main]
org.elasticsearch.bootstrap.StartupException: java.lang.IllegalStateException: failed to obtain node locks, tried [[/home/ubuntu/elasticsearch-6.5.4/data/Zoo]] with lock id [0]; maybe these locations are not writable or multiple nodes were started without increasing [node.max_local_storage_nodes] (was [1])? at org.elasticsearch.bootstrap.Elasticsearch.init(Elasticsearch.java:140) ~[elasticsearch-6.5.4.jar:6.5.4]
at org.elasticsearch.bootstrap.Elasticsearch.execute(Elasticsearch.java:127) ~[elasticsearch-6.5.4.jar:6.5.4]
at org.elasticsearch.cli.EnvironmentAwareCommand.execute(EnvironmentAwareCommand.java:86) ~[elasticsearch-6.5.4.jar:6.5.4]
at org.elasticsearch.cli.Command.mainWithoutErrorHandling(Command.java:124) ~[elasticsearch-cli-6.5.4.jar:6.5.4]
at org.elasticsearch.cli.Command.main(Command.java:90) ~[elasticsearch-cli-6.5.4.jar:6.5.4]
at org.elasticsearch.bootstrap.Elasticsearch.main(Elasticsearch.java:93) ~[elasticsearch-6.5.4.jar:6.5.4]
at org.elasticsearch.bootstrap.Elasticsearch.main(Elasticsearch.java:86) ~[elasticsearch-6.5.4.jar:6.5.4]
Caused by: java.lang.IllegalStateException: failed to obtain node locks, tried [[/home/ubuntu/elasticsearch-6.5.4/data/Zoo]] with lock id [0]; maybe these locations are not writable or multiple nodes were started without increasing [node.max_local_storage_nodes] (was [1])? at org.elasticsearch.env.NodeEnvironment.(NodeEnvironment.java:297) ~[elasticsearch-6.5.4.jar:6.5.4]
at org.elasticsearch.node.Node.(Node.java:296) ~[elasticsearch-6.5.4.jar:6.5.4]
at org.elasticsearch.node.Node.(Node.java:265) ~[elasticsearch-6.5.4.jar:6.5.4]
at org.elasticsearch.bootstrap.Bootstrap$5.(Bootstrap.java:212) ~[elasticsearch-6.5.4.jar:6.5.4]
at org.elasticsearch.bootstrap.Bootstrap.setup(Bootstrap.java:212) ~[elasticsearch-6.5.4.jar:6.5.4]
at org.elasticsearch.bootstrap.Bootstrap.init(Bootstrap.java:333) ~[elasticsearch-6.5.4.jar:6.5.4]
at org.elasticsearch.bootstrap.Elasticsearch.init(Elasticsearch.java:136) ~[elasticsearch-6.5.4.jar:6.5.4]
... 6 more
可以查看一下进程列表。
ubuntu@ubuntu:~/elasticsearch-6.5.4/bin$ ps aux | grep elasticsearch
ubuntu 2933 10.6 15.7 3736144 1282148 pts/0 Sl 19:04 0:19 /usr/bin/java -Xms1g -Xmx1g -XX:+UseConcMarkSweepGC -XX:CMSInitiatingOccupancyFraction=75 -XX:+UseCMSInitiatingOccupancyOnly -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.khIDF3OL -XX:+HeapDumpOnOutOfMemoryError -XX:HeapDumpPath=data -XX:ErrorFile=logs/hs_err_pid%p.log -Xlog:gc*,gc+age=trace,safepoint:file=logs/gc.log:utctime,pid,tags:filecount=32,filesize=64m -Djava.locale.providers=COMPAT -XX:UseAVX=2 -Des.path.home=/home/ubuntu/elasticsearch-6.5.4 -Des.path.conf=/home/ubuntu/elasticsearch-6.5.4/config -Des.distribution.flavor=default -Des.distribution.type=tar -cp /home/ubuntu/elasticsearch-6.5.4/lib/* org.elasticsearch.bootstrap.Elasticsearch -d -p Zoo.pid
ubuntu 2949 0.0 0.0 136508 7252 pts/0 Sl 19:04 0:00 /home/ubuntu/elasticsearch-6.5.4/modules/x-pack-ml/platform/linux-x86_64/bin/controller
ubuntu 3070 0.0 0.0 21536 1000 pts/0 S+ 19:07 0:00 grep --color=auto elasticsearch
pid文件默认会写在elasticsearch的主目录。
ubuntu@ubuntu:~/elasticsearch-6.5.4/bin$ ls ..
bin config data lib LICENSE.txt logs modules NOTICE.txt plugins README.textile Zoo.pid
打开浏览器输入“http://localhost:9200/?pretty”可以测试elasticsearch是否成功运行。
⑧关闭程序
以守护进程模式运行,因为将进程ID写入了pid文件,所以要结束运行的话,kill掉进程即可。
ubuntu@ubuntu:~/elasticsearch-6.5.4/bin$ kill -9 $(cat ../Zoo.pid)
运行日子记录在elasticsearch主目录的logs子目录下。
ubuntu@ubuntu:~/elasticsearch-6.5.4/bin$ ls ../logs/
elasticsearch_access.log elasticsearch_index_indexing_slowlog.log gc.log Zoo_deprecation.log Zoo.log
elasticsearch_audit.log elasticsearch_index_search_slowlog.log Zoo_access.log Zoo_index_indexing_slowlog.log
elasticsearch_deprecation.log elasticsearch.log
参考:
https://www.elastic.co/guide/en/elasticsearch/reference/current/getting-started-install.html
https://www.elastic.co/guide/en/elasticsearch/reference/6.5/zip-targz.html#setup-installation-daemon