简体   繁体   中英

Unable to connect to Cassandra db using php driver in laravel project

I really need some help with this, spent the last two days trying to figure it out.

I am attempting to migrate a laravel project from Mongo to Cassandra. I use docker containers for my local setup, here is the docker-compose.yml file with the relevant information

version: '2'
services:
mongodb:
    image: mongo
    container_name: marketdata_mongodb
    environment:
    - MONGODB_DATABASE=marketdata
    - MONGODB_USER=marketdata
    - MONGODB_PASS=marketdata
    - MONGO_DATA_DIR=/data/db
    volumes:
    - ./data/db:/data/db
    ports:
        - 27017
    networks:
    - main_network

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
    # Cassandra ulimt recommended settings
    ulimits:
    memlock: -1
    nproc: 32768
    nofile: 100000
    volumes:
    - ./cassandra_data:/var/lib/cassandra
    ports:
    - 7000
    - 7001
    - 7199
    - 9042
    - 9160
    networks:
    - main_network

networks:
main_network:
    driver: bridge

After some searching on google, I found this library https://github.com/cubettech/lacassa and I have been attempting to use it within the project to connect to my Cassandra container but have been unable to do so.

Here are the steps I have taken so far to get this working

I installed the php Cassandra driver dependencies, using these commands

wget http://downloads.datastax.com/cpp-driver/ubuntu/16.04/dependencies/libuv/v1.11.0/libuv_1.11.0-1_amd64.deb
wget http://downloads.datastax.com/cpp-driver/ubuntu/16.04/dependencies/libuv/v1.11.0/libuv-dev_1.11.0-1_amd64.deb
wget http://downloads.datastax.com/cpp-driver/ubuntu/16.04/cassandra/v2.8.0/cassandra-cpp-driver-dev_2.8.0-1_amd64.deb
wget http://downloads.datastax.com/cpp-driver/ubuntu/16.04/cassandra/v2.8.0/cassandra-cpp-driver_2.8.0-1_amd64.deb

dpkg -i libuv_1.11.0-1_amd64.deb
dpkg -i libuv-dev_1.11.0-1_amd64.deb
dpkg -i cassandra-cpp-driver_2.8.0-1_amd64.deb
dpkg -i cassandra-cpp-driver-dev_2.8.0-1_amd64.deb

apt-get install libgmp-dev
pecl install cassandra
echo "extension=cassandra.so" >> /etc/php/7.0/apache2/php.ini
echo "extension=cassandra.so" >> /etc/php/7.0/cli/php.ini

I added the service provider to the config/app file

Cubettech\\Lacassa\\CassandraServiceProvider::class

I also added a new database connection for cassandra to the config/database file

'cassandra' => [
    'driver' => 'Cassandra',
    'host' => env('DB_HOST', 'localhost'),
    'port' => env('DB_PORT', 7199),
    'keyspace' => env('DB_DATABASE', 'cassandra_db'),
    'username' => env('DB_USERNAME', ''),
    'password' => env('DB_PASSWORD', ''),
],

these are my env variables

DB_CONNECTION=cassandra
DB_HOST=marketdata_cassandra
DB_PORT=7199
DB_DATABASE=marketdata
DB_USERNAME=marketdata
DB_PASSWORD=marketdata

Based on all the documentation I have found, this should be enough to get it up and running but I keep running into this error

No hosts available for the control connection

While trying to figure out the issue, I came across this , which said that the issue could potentially be solved by upgrading the PHP driver/extension, so I tried a few different extensions that I found here , but I still have the same issue.

Any help would be greatly appreciated

Port 7199 is the JMX monitoring port; you should change the port from 7199 => 9042.

https://docs.datastax.com/en/cassandra/3.0/cassandra/configuration/secureFireWall.html

If you are still having issues connecting to Apache Cassandra try executing the following to ensure the driver is being properly loaded via CLI:

php -m | grep cassandra

If the PHP driver module shows up then attempt to connect to the server using the driver directly:

<?php

$cluster = \Cassandra::cluster()->build();
$session = $cluster->connect('cassandra_db');

So I got external help figuring out the solution to this.

Host and port are actually not used in the original lacassa library. If you look at the documentation, here it says: “connects to localhost by default” - and that's the same line used in lacassa here

$cluster = Cassandra::cluster()                 // connects to localhost by default
               ->build();

https://docs.datastax.com/en/developer/php-driver/1.3/#quick-start

A fix for this can be found in this library https://github.com/seta0909/lacassa . Alternatively, one could also fork the lacassa library and implement the fix on your own

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM