[英]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. 我正在尝试将laravel项目从Mongo迁移到Cassandra。 I use docker containers for my local setup, here is the docker-compose.yml file with the relevant information
我使用docker容器进行本地设置,这是带有相关信息的docker-compose.yml文件
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. 在Google上进行一些搜索后,我发现了这个库https://github.com/cubettech/lacassa,并且我一直试图在项目中使用它来连接到我的Cassandra容器,但一直无法这样做。
Here are the steps I have taken so far to get this working 这是我到目前为止为使此工作已采取的步骤
I installed the php Cassandra driver dependencies, using these commands 我使用以下命令安装了PHP Cassandra驱动程序依赖项
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 我已将服务提供商添加到config / app文件中
Cubettech\\Lacassa\\CassandraServiceProvider::class
I also added a new database connection for cassandra to the config/database file 我还将cassandra的新数据库连接添加到config / database文件中
'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. 在尝试找出问题时,我遇到了这个问题 ,它说可以通过升级PHP驱动程序/扩展来解决此问题,因此我尝试了一些在此处找到的不同扩展,但仍然遇到相同的问题。
Any help would be greatly appreciated 任何帮助将不胜感激
Port 7199 is the JMX monitoring port; 端口7199是JMX监视端口; you should change the port from 7199 => 9042.
您应该从7199 => 9042更改端口。
https://docs.datastax.com/en/cassandra/3.0/cassandra/configuration/secureFireWall.html 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: 如果仍然无法连接到Apache Cassandra,请尝试执行以下操作以确保已通过CLI正确加载了驱动程序:
php -m | grep cassandra
If the PHP driver module shows up then attempt to connect to the server using the driver directly: 如果显示PHP驱动程序模块,则尝试直接使用驱动程序连接到服务器:
<?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. 实际上,原始lacassa库中未使用主机和端口。 If you look at the documentation, here it says: “connects to localhost by default” - and that's the same line used in lacassa here
如果你看一下文档,在这里它说:“连接默认为localhost” -这是在lacassa使用同一条线路在这里
$cluster = Cassandra::cluster() // connects to localhost by default
->build();
https://docs.datastax.com/en/developer/php-driver/1.3/#quick-start 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 . 可以在此库https://github.com/seta0909/lacassa中找到对此的修复程序。 Alternatively, one could also fork the lacassa library and implement the fix on your own
或者,也可以派生lacassa库并自行实现此修复程序
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.