我在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();