Smart Systems for Home

Plain how-to guides for smart home, security and home Wi-Fi

Connect to Apache Cassandra Remotely

· 6 min read · Home Tech

In this article
  1. What You'll Need
  2. Step-by-Step
  3. Common Mistakes
  4. Tips
  5. Troubleshooting
  6. Frequently Asked Questions

This guide walks you through connecting to an Apache Cassandra cluster from a remote machine using the standard cqlsh shell and a Java application. You'll configure Cassandra to listen on a non-loopback interface, set up authentication, and test the connection. Before starting, ensure you have network access to the Cassandra node (usually port 9042 for native protocol) and the necessary credentials.

What You'll Need

  • A running Apache Cassandra node (version 3.x or 4.x) on a server you can reach over the network.
  • The server's IP address or hostname.
  • Credentials for a Cassandra user (default is cassandra/cassandra if authentication is enabled).
  • On your remote machine: cqlsh (comes with Cassandra, or install via pip) and a Java runtime (if using the Java driver).
  • Firewall rules allowing inbound TCP on port 9042 (and 7000/7001 for inter-node, but not needed for a single node).

Step-by-Step

  1. Check the Cassandra configuration on the server. Locate the cassandra.yaml file (usually in /etc/cassandra/ or $CASSANDRA_HOME/conf). Open it and verify the following:
    • rpc_address: must be set to 0.0.0.0 or the server's actual IP address (not localhost).
    • listen_address: should be the server's internal IP (for inter-node communication).
    • seed_provider should list the server's IP or hostname.
    • authenticator: if you want password authentication, set it to PasswordAuthenticator (default in recent versions).
  2. Restart Cassandra after any changes. Use sudo systemctl restart cassandra (systemd) or sudo service cassandra restart (init.d). Wait a minute for it to fully start.
  3. On the remote machine, test connectivity to the server's port 9042. Use telnet server_ip 9042 or nc -zv server_ip 9042. If the connection fails, check firewall rules on the server (e.g., sudo ufw allow 9042/tcp) and any security groups (cloud).
  4. Connect using cqlsh from the remote machine. The syntax is:
    cqlsh server_ip -u cassandra -p cassandra
    Replace server_ip with the actual IP. If you changed the default credentials, use those. If authentication is disabled, omit the -u and -p flags.
  5. If cqlsh fails due to SSL (common in Cassandra 4.x with default settings), you may need to specify the SSL options. Check if client_encryption_options is enabled in cassandra.yaml. If yes, you'll need to provide the appropriate truststore or disable SSL for testing (not recommended in production). For a quick test, you can use cqlsh --ssl if the server uses the default self-signed cert, but you'll likely need to configure a truststore.
  6. For a Java application, use the official DataStax Java driver. Add the dependency to your project (e.g., Maven: com.datastax.oss:java-driver-core:4.17.0). Then create a session:
    CqlSession session = CqlSession.builder()
        .addContactPoint(new InetSocketAddress("server_ip", 9042))
        .withAuthCredentials("cassandra", "cassandra")
        .withLocalDatacenter("datacenter1")
        .build();
        
    Replace datacenter1 with your actual datacenter name (check nodetool status). If SSL is enabled, you'll need to configure the driver's SSL context.
  7. Verify the connection by running a simple query, e.g., SELECT release_version FROM system.local; in cqlsh or via the driver. If you get a result, you're connected.

Common Mistakes

  • Leaving rpc_address as localhost – this binds only to the loopback interface, making remote connections impossible.
  • Forgetting to restart Cassandra after configuration changes – the changes won't take effect.
  • Firewall blocking the port – even if Cassandra listens on all interfaces, the OS firewall or cloud security group can block incoming connections.
  • Using the wrong port – the native protocol uses 9042, not 9160 (Thrift, deprecated) or 7000 (inter-node).
  • Ignoring SSL – if SSL is enabled, connecting without it will fail with a handshake error.
  • Assuming authentication is off – many distributions enable it by default; you must provide credentials.

Tips

  • Use a dedicated user for remote access instead of the default cassandra superuser. Create one with CREATE USER 'remote_user' WITH PASSWORD 'strong_password' NOSUPERUSER; and grant only necessary permissions.
  • Prefer using a hostname in the contact point rather than an IP, so you can change IPs without updating clients.
  • For production, enable SSL and configure a proper certificate authority. For testing, you can disable SSL temporarily, but never in production.
  • Use nodetool status on the server to confirm the node is UN (Up/Normal) and to see the datacenter name.
  • If you're behind NAT or using Docker, ensure port mapping is set correctly (e.g., -p 9042:9042).
  • Test with a simple tool like cqlsh before writing application code – it isolates network issues from driver issues.

Troubleshooting

  • Connection timed out: Check that the server is reachable (ping), the port is open (telnet), and the firewall allows it. Also verify that Cassandra is actually listening: netstat -tlnp | grep 9042 on the server.
  • Authentication failed: Ensure the username/password are correct. If you're using the default cassandra/cassandra, note that some distributions force a password change on first login. If you've forgotten the password, you may need to reset it by temporarily disabling authentication.
  • SSL errors: If you see SSLHandshakeException, the server has SSL enabled. Either configure the client with the correct truststore, or temporarily disable SSL in cassandra.yaml (set enabled: false under client_encryption_options) and restart. For production, properly configure SSL.
  • cqlsh says "Connection refused": Cassandra might not be running. Check with systemctl status cassandra or ps aux | grep cassandra. Also ensure the rpc_address is not 127.0.0.1.
  • Java driver throws NoHostAvailableException: This often means the driver can't reach any contact point. Verify the IP and port, and that the datacenter name matches. Use withLocalDatacenter – if you omit it, the driver may try to connect to a different DC and fail.
  • cqlsh version mismatch: If you're using a very old cqlsh with a new Cassandra, you might get protocol errors. Upgrade cqlsh via pip install --upgrade cqlsh or use the version that ships with Cassandra.

Frequently Asked Questions

Can I connect to Cassandra without changing the default configuration?

No. By default, Cassandra binds to localhost only. You must change rpc_address to 0.0.0.0 or the server's IP and restart. Also, if authentication is enabled, you need valid credentials.

What port does Cassandra use for remote connections?

The native protocol port is 9042. If you're using the older Thrift protocol (deprecated), it's 9160. For inter-node communication, it's 7000/7001, but you don't need those for a client connection.

Is it safe to expose Cassandra directly to the internet?

No. You should restrict access using a firewall or VPN. Cassandra is not designed to be publicly accessible; use a bastion host or SSH tunneling for remote access.

How do I find my datacenter name?

Run nodetool status on the server. The output shows the datacenter name in the first column of the node list. It's often datacenter1 in a default setup.

Can I use cqlsh from a machine that doesn't have Cassandra installed?

Yes. You can install cqlsh via pip install cqlsh or download the standalone version. It doesn't require a full Cassandra installation.

What if I get "Unable to load authentication plugin"?

This usually means the server has authentication enabled but the client isn't sending credentials. Use -u and -p flags in cqlsh, or configure the driver with withAuthCredentials.