解决 elasticsearch-head 集群健康值: 未连接问题
# 修改 elasticsearch-7.3.1\config\elasticsearch.yml
# 在后面添加如下内容
http.cors.enabled: true
http.cors.allow-origin: "*"
http.cors.allow-methods: OPTIONS, HEAD, GET, POST, PUT, DELETE
http.cors.allow-headers: "X-Requested-With, Content-Type, Content-Length, X-User"
ElasticSearch 添加用户名密码
有文章说ElasticSearch的添加用户名和密码的插件是要收费的,但是ElasticSearch 7.3版本以上的就是免费的了,我没有去专门查询过,不过我的是7.3.1版本的,确实没有收费
1. 修改elasticsearch.yml
# 修改 elasticsearch-7.3.1\config\elasticsearch.yml
# 在后面添加如下内容
http.cors.enabled: true
http.cors.allow-origin: "*"
http.cors.allow-headers: Authorization
# 因为上面为了解决 elasticsearch-head 集群健康值: 未连接问题配置了这个属性
# 所以直接在原属性后添加本属性值就行了,修改后如下
# http.cors.allow-headers: "X-Requested-With, Content-Type, Content-Length, X-User, Authorization"
xpack.security.enabled: true
xpack.security.transport.ssl.enabled: true
2. 修改password
此处我将所有密码都设置为了123456,大家自行设置即可
# 表示注释
# 先运行 elasticsearch.bat
# 在 elasticsearch-7.3.1\bin 目录下打开 cmd
# 执行指令:
elasticsearch-setup-passwords interactive
# 之后会出现
Initiating the setup of passwords for reserved users elastic,apm_system,kibana,logstash_system,beats_system,remote_monitoring_user.
You will be prompted to enter passwords as the process progresses.
Please confirm that you would like to continue [y/N]
# 按下 y
y
# 表示同意
# 之后依次输入以下的密码即可
Enter password for [elastic]:
Reenter password for [elastic]:
Enter password for [apm_system]:
Reenter password for [apm_system]:
Enter password for [kibana]:
Reenter password for [kibana]:
Enter password for [logstash_system]:
Reenter password for [logstash_system]:
Enter password for [beats_system]:
Reenter password for [beats_system]:
Enter password for [remote_monitoring_user]:
Reenter password for [remote_monitoring_user]:
Changed password for user [apm_system]
Changed password for user [kibana]
Changed password for user [logstash_system]
Changed password for user [beats_system]
Changed password for user [remote_monitoring_user]
Changed password for user [elastic]
3. elasticsearch-head的使用
之后使用 elasticsearch-head 时需要这样使用
http://localhost:9100/?auth_user=elastic&auth_password=123456
后面的 auth_password 跟的是上面设置的 elastic 的密码
4. kibana的使用
# 修改 config\kibana.yml
# 在后面添加如下语句即可
elasticsearch.username: "elastic"
elasticsearch.password: "123456"
5. RestHighLevelClient客户端的使用
final CredentialsProvider credentialsProvider = new BasicCredentialsProvider();
credentialsProvider.setCredentials(AuthScope.ANY,
new UsernamePasswordCredentials("elastic", "123456")); //es账号密码(默认用户名为elastic)
RestHighLevelClient client = new RestHighLevelClient(
RestClient.builder(
new HttpHost("localhost", 9200, "http"))
.setHttpClientConfigCallback(new RestClientBuilder.HttpClientConfigCallback() {
public HttpAsyncClientBuilder customizeHttpClient(HttpAsyncClientBuilder httpClientBuilder) {
httpClientBuilder.disableAuthCaching();
return httpClientBuilder.setDefaultCredentialsProvider(credentialsProvider);
}
}));