我使用的是 Mysql 5.5,插件需要查询一个 thrift 接口(interface)服务器以获取一些信息。我创建了 thrift 客户端,它基本上打开到服务器的连接,获取状态,然后关闭连接:
#include "../../xxxx/gen-cpp/Xxxx.h"
#include <transport/TSocket.h>
#include <transport/TBufferTransports.h>
#include <protocol/TBinaryProtocol.h>
using namespace apache::thrift;
using namespace apache::thrift::protocol;
using namespace apache::thrift::transport;
using namespace ::za::co::xxxx;
int main(int argc, char **argv) {
boost::shared_ptr<TSocket> socket(new TSocket("localhost", 9090));
boost::shared_ptr<TTransport> transport(new TBufferedTransport(socket));
boost::shared_ptr<TProtocol> protocol(new TBinaryProtocol(transport));
XxxxServiceClient client(protocol);
transport->open();
client.getStatus();
transport->close();
return 0;
}
然后我将 main() 更改为函数名称并将其添加到插件代码文件中并从主函数调用它。 插件代码构建良好,但 map 现在包含大量 thrift 引用,在尝试加载插件时,我收到此错误:
错误 1126 (HY000):无法打开共享库 '/usr/lib/mysql/plugin/libxxxx.so'(错误号:13 undefined symbol :_ZTVN6apache6thrift9transport18TBufferedTransportE)
有什么方法可以在安装插件时解决这些新的 thrift 引用问题?无需上述代码即可安装并运行良好。
最佳答案
使用 Thrift cpp 教程代码,我能够创建一个简单的 hello world MySQL 守护进程插件,它可以向 CppServer 进程发出客户端调用:
#include <mysql/plugin.h>
#include <mysql_version.h>
#include <protocol/TBinaryProtocol.h>
#include <transport/TSocket.h>
#include <transport/TTransportUtils.h>
#include "gen-cpp/Calculator.h"
using namespace apache::thrift;
using namespace apache::thrift::protocol;
using namespace apache::thrift::transport;
using namespace tutorial;
using namespace shared;
using namespace boost;
static int hello_world_plugin_init(void *p) {
shared_ptr<TTransport> socket(new TSocket("localhost", 9090));
shared_ptr<TTransport> transport(new TBufferedTransport(socket));
shared_ptr<TProtocol> protocol(new TBinaryProtocol(transport));
CalculatorClient client(protocol);
transport->open();
client.ping();
transport->close();
return 0;
}
还有这个 Makefile:
BOOST_DIR = /usr/include/boost
MYSQL_DIR = /usr/include/mysql
THRIFT_DIR = /usr/local/include/thrift
LIB_DIR = /usr/local/lib
GEN_SRC = gen-cpp/SharedService.cpp gen-cpp/shared_types.cpp gen-cpp/tutorial_types.cpp gen-cpp/Calculator.cpp
DEFS = -DMYSQL_DYNAMIC_PLUGIN -DHAVE_NETINET_IN_H
default: hello_thrift.cc
g++ ${DEFS} -fPIC -shared -o libhellothrift.so -I${MYSQL_DIR} -I${THRIFT_DIR} -I${BOOST_DIR} -Igen-cpp -L${LIB_DIR} hello_thrift.cc ${GEN_SRC} -lthrift
这只是示例代码,如果 Thrift CppServer 未运行,将会使您的 MySQL 服务器崩溃。
我在 Ubuntu 12.04 LTS 上使用 gcc 4.6.3、MySQL 5.5.24、Thrift 0.8.0、Boost 1.46 测试了这个
关于mysql - 可以在 mysql 插件中使用 thrift 吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11564105/