Laravel项目使用PHP驱动连接Cassandra数据库失败求助
Let's break down the issues you're facing and fix them step by step — I've dealt with similar Docker + Laravel + Cassandra setups before, so here's what to check:
1. Fix the Port Configuration (Most Likely Culprit)
You're using port 7199 in your database config, but that's Cassandra's JMX monitoring port, not the CQL port that Lacassa uses for connections. The correct port for CQL is 9042.
Update your .env file:
DB_PORT=9042
And double-check your config/database.php entry to make sure the port defaults to 9042 too:
'cassandra' => [ 'driver' => 'Cassandra', 'host' => env('DB_HOST', 'localhost'), 'port' => env('DB_PORT', 9042), // Change this from 7199 'keyspace' => env('DB_DATABASE', 'marketdata'), 'username' => env('DB_USERNAME', ''), 'password' => env('DB_PASSWORD', ''), ],
2. Verify Cassandra Container is Fully Initialized
Cassandra takes a few minutes to start up completely, especially on first run. If your Laravel app tries connecting before Cassandra is ready, you'll get this "no hosts available" error.
- Check if the Cassandra container is healthy: Run
docker logs marketdata_cassandraand look for lines likeStartup completeorReady for CQL clients. - Add a healthcheck to your docker-compose.yml for Cassandra to ensure it's ready before your app starts (if your app is also in Docker):
cassandra: image: cassandra container_name: marketdata_cassandra command: bash -c 'if [ -z "$$(ls -A /var/lib/cassandra/)" ] ; then sleep 0; fi && /docker-entrypoint.sh cassandra -f' environment: - CASSANDRA_CLUSTER_NAME=dev_cluster ulimits: memlock: -1 nproc: 32768 nofile: 100000 volumes: - ./cassandra_data:/var/lib/cassandra ports: - 7000 - 7001 - 7199 - 9042:9042 # Expose 9042 explicitly for testing - 9160 networks: - main_network healthcheck: test: ["CMD", "cqlsh", "-e", "describe keyspaces;"] interval: 30s timeout: 10s retries: 5
3. Check Cassandra Authentication Settings
You've set DB_USERNAME and DB_PASSWORD, but by default, Cassandra doesn't enable password authentication. If you're trying to use credentials without enabling auth, the connection will fail.
Option A: Disable Credentials (For Local Dev)
If you don't need auth locally, update your .env to empty values:
DB_USERNAME= DB_PASSWORD=
Option B: Enable Authentication in Cassandra
If you need auth, you'll need to modify Cassandra's cassandra.yaml config:
- Mount a custom
cassandra.yamlfile to your container:volumes: - ./cassandra_data:/var/lib/cassandra - ./cassandra_config/cassandra.yaml:/etc/cassandra/cassandra.yaml - In
cassandra.yaml, set:authenticator: PasswordAuthenticator authorizer: CassandraAuthorizer - Restart the Cassandra container, then create your user via
cqlsh:docker exec -it marketdata_cassandra cqlsh CREATE ROLE marketdata WITH PASSWORD 'marketdata' AND LOGIN = true AND SUPERUSER = true;
4. Ensure Keyspace Exists
Unlike MongoDB, Cassandra won't automatically create a keyspace for you. You need to create it manually before connecting:
docker exec -it marketdata_cassandra cqlsh CREATE KEYSPACE marketdata WITH replication = {'class': 'SimpleStrategy', 'replication_factor': 1};
5. Verify Docker Network Connectivity
Make sure your Laravel app's container is on the same main_network as Cassandra. If your Laravel app is running outside Docker (on your host), use localhost as DB_HOST instead of marketdata_cassandra, since the host can't resolve Docker container names by default (unless you're using a custom DNS or host entries).
Test connectivity from your Laravel environment:
# If running in Docker docker exec -it your_laravel_container ping marketdata_cassandra # If running on host ping localhost # Test CQL connection cqlsh localhost 9042 # Or cqlsh marketdata_cassandra 9042 if in Docker
6. Check PHP Cassandra Extension Compatibility
You're using older versions of the DataStax C++ driver and PHP extension. For PHP 7.0, try updating to a more recent compatible version (though PHP 7.0 is end-of-life, so consider upgrading PHP if possible):
- Remove the old packages:
dpkg -r cassandra-cpp-driver cassandra-cpp-driver-dev libuv libuv-dev - Install the latest compatible versions for Ubuntu 16.04 (or switch to a newer OS if you can):
apt-get update && apt-get install -y libuv1-dev libgmp-dev pecl install cassandra-1.3.2 # Compatible with PHP 7.0
After making these changes, clear your Laravel config cache:
php artisan config:clear
Let me know if any of these steps resolve your issue — the port mismatch is the most common cause here, so start with that!
内容的提问来源于stack exchange,提问作者user3010617

