MySQL NDB Cluster API 开发人员指南 / 第 2 章 NDB API / 2.5 NDB API 示例 / 2.5.1 基本 NDB API 示例 /
2.5.1.1 NDB API 基本连接示例
此示例(也可以在 中找到
storage/ndb/ndbapi-examples/ndbapi_basic/ndbapi_basic_connect.cpp
)演示了使用
Ndb_cluster_connection
给定连接字符串连接到 NDB 管理服务器的用法。成功后,它会获取并打印出有关集群的一些信息。如果它无法连接到管理节点或数据节点,它会打印相应的错误并退出。
#include <iostream>
#include <cstdlib>
#include <NdbApi.hpp>
namespace
{
inline void test_connection(const Ndb_cluster_connection &connection)
{
std::cout << "Connected to: " << connection.get_system_name()
<< ",\n\ton port: " << connection.get_connected_port()
<< ",\n\tactive NDBDs: " << connection.get_active_ndb_objects()
<< std::endl;
}
}
int main(int argc, char **argv)
{
if (argc != 2)
{
std::cout << "Usage: ndb_ndbapi_basic_connect <connectstring>"
<< std::endl;
return EXIT_FAILURE;
}
const char *connectstring = argv[1];
ndb_init();
{
Ndb_cluster_connection connection(connectstring);
if (connection.connect() != 0)
{
std::cout << "Cannot connect to cluster management server" << std::endl;
return EXIT_FAILURE;
}
if (connection.wait_until_ready(30, 0) != 0)
{
std::cout << "Cluster was not ready within 30 secs" << std::endl;
return EXIT_FAILURE;
}
// Let's verify connection
test_connection(connection);
}
ndb_end(0);
return EXIT_SUCCESS;
}