gpt4 book ai didi

c# - 如何连接到 Cassandra 虚拟机

转载 作者:行者123 更新时间:2023-12-03 19:12:14 29 4
gpt4 key购买 nike

如果 Cassandra 和代码在同一台机器上,则以下代码有效:

using System;
using Cassandra;

namespace CassandraInsertTest
{
class Program
{
static void Main(string[] args)
{
var cluster = Cluster.Builder()
.AddContactPoint("127.0.0.1")
.Build();

var session = cluster.Connect("test_keyspace");

session.Execute("INSERT INTO test_table (id, col1, col2) VALUES (1, 'data1', 'data2')");

Console.WriteLine($"Finished");
Console.ReadKey();
}
}
}

如果代码在一台机器上而 cassandra 在另一台机器上(不同的 IP 地址),假设需要用户名和密码?所以我试过:
        var cluster = Cluster.Builder()
.AddContactPoint("192.168.0.18") <- the ip address for the cassandra node
.WithPort(9042)
.WithCredentials("username to log into the cassandra node","password to log into the cassandra node")
.Build();

我收到以下错误消息:
userone@desktop:~/Desktop/vsc$ dotnet run
Unhandled exception. Cassandra.NoHostAvailableException: All hosts tried for query failed (tried 192.168.0.18:9042: SocketException 'Connection refused')
at Cassandra.Connections.ControlConnection.Connect(Boolean isInitializing)
at Cassandra.Connections.ControlConnection.InitAsync()
at Cassandra.Tasks.TaskHelper.WaitToCompleteAsync(Task task, Int32 timeout)
at Cassandra.Cluster.Cassandra.SessionManagement.IInternalCluster.OnInitializeAsync()
at Cassandra.ClusterLifecycleManager.InitializeAsync()
at Cassandra.Cluster.Cassandra.SessionManagement.IInternalCluster.ConnectAsync[TSession](ISessionFactory`1 sessionFactory, String keyspace)
at Cassandra.Cluster.ConnectAsync(String keyspace)
at Cassandra.Tasks.TaskHelper.WaitToComplete(Task task, Int32 timeout)
at Cassandra.Tasks.TaskHelper.WaitToComplete[T](Task`1 task, Int32 timeout)
at Cassandra.Cluster.Connect(String keyspace)
at HelloWorld.Program.Main(String[] args) in /home/userone/Desktop/vsc/Program.cs:line 17
userone@desktop:~/Desktop/vsc$

节点(cassandra 服务器)上的 iptables 目前设置如下:
node1@node1:~$ sudo iptables -S
-P INPUT ACCEPT
-P FORWARD ACCEPT
-P OUTPUT ACCEPT
-A INPUT -i lo -j ACCEPT
-A IMPUT -m conntrack --ctstate RELATED,ESTABLISHED -j ACCEPT
-A INPUT -p tcp -m tcp --dport 22 -j ACCEPT
-A INPUT -p tcp -m tcp --dport 80 -j ACCEPT
-A INPUT -s 192.168.0.73/32 -p tcp -m multiport --dports 7000,7001,7199,9042,9160,9142 -m state --state NEW,ESTABLISHED -j ACCEPT
node1@node1:~$

注意1:安装了应用程序的机器和安装了cassandra的机器都可以双向ping和traceroute。

注2:我已经测试了用户名和密码,直接在服务器上尝试可以登录cassandra服务器,没有任何问题。

注 3:Cassandra 在我今天刚刚创建的 VM 中运行。 VM 是运行代码的主机上的 guest 机器。

注 4:Host OS 和 Guest OS 都是 Linux。

最佳答案

.AddContactPoint("127.0.0.1")

如果这在同一台机器上有效,那么您可能已将 Cassandra 绑定(bind)到该 IP。如果您需要远程连接到您的节点,则需要将可路由 IP 绑定(bind)到该节点。

运行 nodetool status .如果您看到集群状态显示您的节点的 IP 为 127.0.0.1,那么从本地计算机连接到本地计算机是唯一可行的方案。

尝试在您的节点上运行以下命令:
grep _address cassandra.yaml

输出中返回的 IP 地址是唯一允许应用程序连接的 IP 地址。如果您希望能够连接到 192.168.0.18,那么 listenrpc地址应如下所示:
listen_address: 192.168.0.18
rpc_address: 192.168.0.18

请注意,您需要更改您的 seeds也列出来。

此外,如果您使用的虚拟机/提供程序同时具有内部和外部 IP 地址,那么您还需要设置 broadcast_外部 IP 地址:
broadcast_address: 10.6.5.5
broadcast_rpc_address: 10.6.5.5
listen_address: 192.168.0.18
rpc_address: 192.168.0.18

但尝试设置 listenrpc先到 192.168.0.18。

编辑 20191022

Just wanted to double check, do I add 192.168.0.18 as the listen_address and rpc_address to the cassandra node where the cassandra node has the ip address 192.168.0.18?



是的。还要确保您的节点的种子列表设置如下:
- seeds: "192.168.0.18"

Before I did that, the value of the listen_address and rpc_address were set to localhost



我是这么想的。

However, after making the changes you suggested, nodetool status now gives me


Failed to connect to 127.0.0.1:7199 - connection refused

具有讽刺意味的是,这与当 Cassandra 未运行时 nodetool 返回的消息相同。此时我会检查系统日志,看看它是否返回可能阻止它启动的错误。我怀疑种子列表仍然显示“127.0.0.1”。

tl;博士;

如果您打算远程连接到集群/节点,则不能使用将 Cassandra 绑定(bind)到主 IP (127.0.0.1/localhost) 的默认配置。这包括所有 _address设置,以及您的 seeds列表。

关于c# - 如何连接到 Cassandra 虚拟机,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58466825/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com