Maaz Zindani

Loading the index…

↑↓ move↵ openesc close4072 pages
Databases and Redis

elasticsearch-php says it found no alive nodes in the cluster

ErrorNo alive nodes found in your cluster

Short answer

The client tried every host in its list and none answered in a usable way. For a current Elasticsearch the usual reasons are a host or port that does not match the server, HTTPS with a CA the client does not trust, or missing credentials. Call info() with explicit settings to see which one it is.

  • Applies to Elasticsearch 8.x, elasticsearch-php 8.x, PHP 8.3, PHP 8.4
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Fix it fast

  1. 1Find out which cause you have

    Build the client with every setting spelled out and call the Info API, the only connection check the client docs show. The exception then comes from one known configuration.

    $client = Elastic\Elasticsearch\ClientBuilder::create()
        ->setHosts(['https://localhost:9200'])
        ->setBasicAuthentication('elastic', getenv('ES_PASSWORD'))
        ->setCABundle('/path/to/http_ca.crt')
        ->build();
    var_dump($client->info()->asArray());
  2. 2Match what you see, then jump to its fix

    1. 1Every call fails, including the Info APIThe host list does not match where Elasticsearch listensFix
    2. 2The server runs with default security and the code talks plain http or gives no CA fileElasticsearch uses HTTPS and the client has no CA certificateFix
    3. 3The server is reachable in a browser or with the right password, but the client sends noneNo credentials are sentFix

What does the error look like?

  • No alive nodes found in your cluster

Cause 1 of 3

The host list does not match where Elasticsearch listens

1Check

setHosts takes full URLs with scheme, host and port, such as https://localhost:9200. A host that is not reachable from where PHP runs gives no alive node.

grep -rn "setHosts" src/ config/ app/ 2>/dev/null

2Fix

Set the URL that PHP can really reach, with the right scheme and port.

$client = Elastic\Elasticsearch\ClientBuilder::create()
    ->setHosts(['https://es.internal.example:9200'])
    ->build();

3Verify

The Info API returns the cluster information.

var_dump($client->info()->asArray());

Confirmed by elastic.co

Did this fix it?

Cause 2 of 3

Elasticsearch uses HTTPS and the client has no CA certificate

1Check

Elasticsearch secures HTTPS by default. The client docs show the client built with an https host and setCABundle pointing at the http_ca.crt file from the server.

grep -rn "setCABundle\|http://" src/ config/ 2>/dev/null

2Fix

Use an https URL and give the client the CA file from the server's config/certs folder.

->setHosts(['https://localhost:9200'])
->setCABundle('/path/to/http_ca.crt')

3Verify

The Info API call no longer throws.

var_dump($client->info()->asArray());

Confirmed by elastic.co

Did this fix it?

Cause 3 of 3

No credentials are sent

1Check

Requests without credentials are rejected by default, unless anonymous access is enabled. The client sets credentials with setBasicAuthentication, setApiKey, or with the Cloud ID for Elastic Cloud.

grep -rn "setBasicAuthentication\|setApiKey\|setElasticCloudId" src/ config/ 2>/dev/null

2Fix

Add the user and password, or an API key created in the Management page under Security.

->setApiKey(getenv('ES_API_KEY'))
// or
->setBasicAuthentication('elastic', getenv('ES_PASSWORD'))

3Verify

The Info API call returns data.

var_dump($client->info()->asArray());

Confirmed by elastic.co

Did this fix it?

How do I stop it coming back?

  • Keep the host, CA file and credentials in environment variables, one set per environment.
  • Add a health check that calls the Info API and alerts when it fails.
  • Reset the elastic password with elasticsearch-reset-password rather than leaving the first generated one in code.

Sources

How was this page made?

I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.

Which stack is behind this page?

Projects of mine built on the same technologies:

Related fixes

Databases

Laravel bulk insert fails with 'too many placeholders'

SQLSTATE[HY000]: General error: 1390 Prepared statement contains too many placeholders

Laravel sends one placeholder per value, so one insert of many rows times many columns, or a whereIn with a huge list, creates a statement MySQL refuses.

3 causes, each with a fix

Next step

Want this handled for good?

I look after servers, apps and releases end to end. Tell me what keeps breaking.