Categories
Magento

Magento 2 set custom Elasticsearch port from cli

For sometime now magento has required ElasticSearch. However, occasionally you find a site trapped in a chicken and egg situation where the code will not reindex or is throwing an error eg

Could not ping search engine: No alive nodes found in your cluster

as magento defaults to the default port of 9200. If you are running elasticsearch on any other port you have a problem (eg if you are running ES in docker or a remote location).

This can of course be updated in the admin interface. However, if you are mid-upgrade or having other issues you may be locked out of this (surprising how often this happens)

If you rush to your database thinking you can insert the custom port  you will find that there is no entry for the elasticsearch port.

The good news is you can insert this from the command line using magento’s config:set tool

php bin/magento config:set catalog/search/elasticsearch7_server_port ‘xxxxxx’

If you go back to the database you will now find a new line has been generated.

and now your index will run as expected.

/opt/plesk/php/7.4/bin/php bin/magento index:reindex
Design Config Grid index has been rebuilt successfully in 00:00:00
Customer Grid index has been rebuilt successfully in 00:00:00
Product Flat Data index has been rebuilt successfully in 00:00:31
Category Products index has been rebuilt successfully in 00:00:01
Product Categories index has been rebuilt successfully in 00:00:00
Catalog Rule Product index has been rebuilt successfully in 00:00:00
Product EAV index has been rebuilt successfully in 00:00:02
Stock index has been rebuilt successfully in 00:00:00
Inventory index has been rebuilt successfully in 00:00:00
Catalog Product Rule index has been rebuilt successfully in 00:00:00
Product Price index has been rebuilt successfully in 00:00:02
Catalog Search index has been rebuilt successfully in 00:00:06

Note there are a few of these values that can be set this way

  • php bin/magento config:set catalog/search/elasticsearch7_server_hostname localhost
  • php bin/magento config:set catalog/search/elasticsearch7_enable_auth 1
  • php bin/magento config:set catalog/search/elasticsearch7_username USERNAME
  • php bin/magento config:set catalog/search/elasticsearch7_password PASSWORD
  • php bin/magento config:set catalog/search/elasticsearch7_index_prefix USERNAME.magento2

Leave a Reply

Your email address will not be published. Required fields are marked *