elasticsearch-php says it found no alive nodes in the cluster
No alive nodes found in your clusterShort 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.
Fix it fast
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());2Match what you see, then jump to its fix
- 1
Every call fails, including the Info APIThe host list does not match where Elasticsearch listensFix - 2
The 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
The server is reachable in a browser or with the right password, but the client sends noneNo credentials are sentFix
- 1
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/null2Fix
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
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/null2Fix
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
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/null2Fix
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
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
A container cannot resolve the name of its database container
php_network_getaddresses: getaddrinfo failed
127.0.0.11 is Docker's own DNS server for custom networks.
3 causes, each with a fixDatabases
PHP cannot connect to SQL Server through FreeTDS
Connection Refused
FreeTDS prints this when it cannot open a session with the server name you gave it.
3 causes, each with a fixDatabases
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 fixDatabases
Laravel query fails: conversion from one collation into another is impossible
SQLSTATE[HY000]: General error: 3988 Conversion from collation utf8_unicode_ci into utf8mb4_0900_ai_ci impossible for parameter
MySQL is comparing or binding a value whose character set cannot be converted into the one on the other side.
3 causes, each with a fixDatabases
PHP cannot connect to MySQL: the connection attempt failed
Error 2003
Error 2003 means the TCP connection to MySQL never opened.
4 causes, each with a fixDatabases
The client cannot reach the MySQL or MariaDB server through its Unix socket file
ERROR 2002 (HY000): Can't connect to local server through socket '/run/mysqld/mysqld.sock' (2) (MariaDB client wording)
Error 2002 means the client tried the Unix socket file named in the message and found nothing it could talk to.
4 causes, each with a fix