访问在 Java 中使用 'PasswordAuthenticator' 的 Cassandra 数据库
Accesing Cassandra Database which uses 'PasswordAuthenticator' in Java
我在 cassandra.yaml 文件中将 Cassandra 数据库的身份验证器值更改为 'PasswordAuthenticator'。
以前我使用以下代码使用 java.
连接到数据库
public void connect(String node) {
cluster = Cluster.builder()
.addContactPoint(node).build();
Metadata metadata = cluster.getMetadata();
System.out.printf("Connected to cluster: %s\n",
metadata.getClusterName());
for ( Host host : metadata.getAllHosts() ) {
System.out.printf("Datatacenter: %s; Host: %s; Rack: %s\n",
host.getDatacenter(), host.getAddress(), host.getRack());
}
session = cluster.connect();
}
现在这段代码给我错误提示
Exception in thread "main" com.datastax.driver.core.exceptions.AuthenticationException: Authentication error on host /127.0.0.1: Host /127.0.0.1 requires authentication, but no authenticator found in Cluster configuration
我知道我需要使用我的超级用户用户名和密码连接到数据库。使用 java 连接到数据库时如何提供这些详细信息?
您可以通过将 .withCredentials
方法添加到您的集群构建器来实现,如下所示:
cluster = Cluster.builder()
.addContactPoint(node)
.withCredentials("yourusername", "yourpassword")
.build();
我在 cassandra.yaml 文件中将 Cassandra 数据库的身份验证器值更改为 'PasswordAuthenticator'。 以前我使用以下代码使用 java.
连接到数据库public void connect(String node) {
cluster = Cluster.builder()
.addContactPoint(node).build();
Metadata metadata = cluster.getMetadata();
System.out.printf("Connected to cluster: %s\n",
metadata.getClusterName());
for ( Host host : metadata.getAllHosts() ) {
System.out.printf("Datatacenter: %s; Host: %s; Rack: %s\n",
host.getDatacenter(), host.getAddress(), host.getRack());
}
session = cluster.connect();
}
现在这段代码给我错误提示
Exception in thread "main" com.datastax.driver.core.exceptions.AuthenticationException: Authentication error on host /127.0.0.1: Host /127.0.0.1 requires authentication, but no authenticator found in Cluster configuration
我知道我需要使用我的超级用户用户名和密码连接到数据库。使用 java 连接到数据库时如何提供这些详细信息?
您可以通过将 .withCredentials
方法添加到您的集群构建器来实现,如下所示:
cluster = Cluster.builder()
.addContactPoint(node)
.withCredentials("yourusername", "yourpassword")
.build();