How to connect Cassandra to localhost using cqlsh?

2020-05-30 04:02发布

问题:

I set rpc_port to the public IP address, and now I can connect to Cassandra just fine from an outside server.

However, I cannot connect from the Cassandra server itself, using cqlsh

I am getting an error.Thar are:

 Connection error: Could not connect to localhost:9160

Is there a configuration, I can change to be able to connect from the server itself ?

回答1:

  1. Check the cassandra.yaml file on the line with rpc_port : normally by default = 9160
  2. Login to the machine hosting cassandra
  3. Try cqlsh 127.0.0.1 9160 : should be ok in any case
  4. try cqlsh [IP of host] 9160 : if ok then cassandra reachable from lan, if ok then cassandra is not reachable from lan but only localhost 127.0.0.1


回答2:

Consider changing /etc/cassandra.yaml:

# Whether to start the thrift rpc server.
start_rpc: false

to

start_rpc: true


回答3:

You need to connect to cassandra through the rpc_address defined in cassandra.yaml. For example, I use cqlsh 10.0.80.49 9160.



回答4:

if you see this in cassandra logs:

INFO  [main] 2015-07-21 12:06:27,426 CassandraDaemon.java:406 - Not starting RPC server as requested. Use JMX (StorageService->startRPCServer()) or nodetool (enablethrift) to start it

then just open a terminal and

$ nodetool enablethrift

as written in the INFO message. Should now work. Got this when my system upgraded to cassandra 2.2.0



回答5:

If you are on OSX

brew install cassandra

First start the Cassandra

cassandra

Connect via CQL shell

cqlsh 127.0.0.1



回答6:

Maybe start cassandra on your local machine by bin/cassandra -f first?



回答7:

It's not listening on 127.0.0.1 since you told it to only listen on <public IP>. Make the listen address 0.0.0.0 to listen on all addresses (or just omit it if possible as this is usually the default). See Listening Sockets .NET tutorial or any other socket tutorial to get a basic understanding of socket binding.

Update (@c45556037):

Note that listen_address is the one for other nodes to use to connect to this one (a misleading name). rpc_address is the actual address to locally bind to. It's unclear from the 2.0 docs and is explained better in the 1.0 docs.



回答8:

I faced same situation while starting cqlsh . I got following error while starting cassandra

Enter only ----cassandra----- in terminal.

it will show all jars and log files . if terminal hang , just escape from it and then enter cqlsh. then it will enter to cassandra cli.

This worked for me



回答9:

for 2.0.5 the following works for me ..

   $CASSANDRA_HOME/bin/cqlsh xx.xx.xxx.xxx 9160


回答10:

Exception connecting to localhost/9160. reason: connection refused

Connection refused to cassandra cli mode .

goto the root directory of cassandra :

bin/cassandra -- host {host-ip} --port {9160}

if you are having trouble with this , check your {cassandra-root-directory}/conf/cassandra.yaml

the thrift ip or rpc_address is the address used as the host-ip for connecting to cli . make it your local IP and if you are having trouble connecting using the port 9160 , try changing the rpc_port to 8070 and now try connecting to cassandra-cli mode using the command

bin/cassandra --host {local-IP} --port 8070

This worked for me, hope it works for you too .



回答11:

Please execute the below query to resolve the issue

#!/bin/bash
export CASSANDRA_HOME=/opt/apache-cassandra-2.1.8
export CQLSH_HOST=192.168.1.200
export CQLSH_PORT=9042
echo $@
$CASSANDRA_HOME/bin/cqlsh $@'    

Make sure to change the IP and location of cassandra Home Directory



回答12:

Use this command

sudo service cassandra start

to start cassandra and then use

cqlsh command


回答13:

Consider changing /etc/cassandra-env.sh:

JVM_OPTS="$JVM_OPTS -Dcom.sun.management.jmxremote.ssl=true"
not "true" but "false"
JVM_OPTS="$JVM_OPTS -Dcom.sun.management.jmxremote.ssl=false"

sudo service cassandra restart



回答14:

How to change default port 9042 in Cassandra ?

I resolved issue using below steps :

1) Stop cassandara services

sudo su -
systemctl stop datastax-agent
systemctl stop opscenterd
systemctl stop app-dse

2) Take backup and Change port from 9042 to 9035

cp /opt/dse/resources/cassandra/conf/cassandra.yaml /opt/dse/resources/cassandra/conf/bkp_cassandra.yaml
vi /opt/dse/resources/cassandra/conf/cassandra.yaml

native_transport_port: 9035

3) Start Cassandra services

systemctl start datastax-agent
systemctl start opscenterd
systemctl start app-dse

4) create cqlshrc file.

vi  /root/.cassandra/cqlshrc

[connection]
hostname = 198.168.1.100
port = 9035