Possible to use thrift in a mysql plugin? - mysql

I'm using Mysql 5.5 and the plugin need to query a thrift interfaced server for some information. I created the thrift client which basically opens a connection to the server, gets a status, and then closes the connection:
#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;
}
I then changed main() to a function name and added it into the plugin code file and called it from the main function.
The plugin code builds fine but the map now contains a whole lot of thrift references and on trying to load the plugin, I get this error:
ERROR 1126 (HY000): Can't open shared library '/usr/lib/mysql/plugin/libxxxx.so' (errno: 13 undefined symbol: _ZTVN6apache6thrift9transport18TBufferedTransportE)
Is there any way to get these new thrift references resolved on installing the plugin? It installs and runs fine without the above code.

Using the Thrift cpp tutorial code I was able to create a simple hello world MySQL daemon plugin which made a client call to CppServer process:
#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;
}
And this 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
This is just example code and will crash your MySQL server if Thrift CppServer isn't running.
I tested this on Ubuntu 12.04 LTS using gcc 4.6.3, MySQL 5.5.24, Thrift 0.8.0, Boost 1.46

Related

error while connecting mariadb with c : undefined reference to `mysql_init#4'

I am trying to connect to mariadb database using c program. Initially it was showing error for #include <mysql.h> as no such file or directory.
But after including directory name, that problem is solved now, but it is showing another error.
Following is the code I was trying to run:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
// #include "C:/Program Files/MariaDB 10.11/include/mysql/my_global.h"
#include "mysql/mysql.h"
int main (int argc, char* argv[])
{
// Initialize Connection
MYSQL *conn;
if (!(conn = mysql_init(0)))
{
fprintf(stderr, "unable to initialize connection struct\n");
exit(1);
}
// Connect to the database
if (!mysql_real_connect(
conn, // Connection
"mariadb.example.net", // Host
"db_user", // User account
"db_user_password", // User password
"test", // Default database
3306, // Port number
NULL, // Path to socket file
0 // Additional options
));
{
// Report the failed-connection error & close the handle
fprintf(stderr, "Error connecting to Server: %s\n", mysql_error(conn));
mysql_close(conn);
exit(1);
}
// Use the Connection
// ...
// Close the Connection
mysql_close(conn);
return 0;
}
I am getting following error in output:
PS C:\Dev\Win> gcc Db_con.c -o Db_con
C:\Users\hajos\AppData\Local\Temp\ccGZ2Rhz.o:Db_con.c:(.text+0x1e): undefined reference to `mysql_init#4'
C:\Users\hajos\AppData\Local\Temp\ccGZ2Rhz.o:Db_con.c:(.text+0xa1): undefined reference to `mysql_real_connect#32'
C:\Users\hajos\AppData\Local\Temp\ccGZ2Rhz.o:Db_con.c:(.text+0xaf): undefined reference to `mysql_error#4'
C:\Users\hajos\AppData\Local\Temp\ccGZ2Rhz.o:Db_con.c:(.text+0xd9): undefined reference to `mysql_close#4'
collect2.exe: error: ld returned 1 exit status
Can anyone explain what is the problem and how to solve it?
You have to link against the MariaDB Connector/C libraries.
From MariaDB Connector/C documentation:
Linking your application against MariaDB Connector/C
Windows
For static linking the library libmariadb.lib is required, for dynamic linking use libmariadb.dll. Using the MSI installer, these libraries can be found in the lib directory of your MariaDB Connector/C installation.
Unless you use the experimental plugin remote_io (which requires the curl library) there are no dependencies to other libraries than the Windows system libraries.

cannot link to static mysqlclient library, though shared library works

I have a sample program to familiarize myself with mysqlclient APIs. However when I compile and link it to the mysqlclient library statically (.a file), the linker complains it cannot find the file, although it exists in my path. Linking to the shared library (.dylib file on my Mac) works.
Please help me get my head around this behaviour. Much appreciated!
Here's my driver program client.c that calls mysqlclient library.
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <mysql.h>
int main(int argc, char **argv)
{
MYSQL *mysql = NULL;
if (mysql_library_init(argc, argv, NULL)) {
fprintf(stderr, "could not initialize MySQL client library\n");
exit(1);
}
mysql = mysql_init(mysql);
if (!mysql) {
puts("Init faild, out of memory?");
return EXIT_FAILURE;
}
if (!mysql_real_connect(mysql, /* MYSQL structure to use */
NULL, /* server hostname or IP address */
NULL, /* mysql user */
NULL, /* password */
NULL, /* default database to use, NULL for none */
0, /* port number, 0 for default */
NULL, /* socket file or named pipe name */
CLIENT_FOUND_ROWS /* connection flags */ )) {
puts("Connect failed\n");
} else {
const char *query = "SELECT VERSION()";
if (mysql_real_query(mysql, query, strlen(query))) {
printf("Query failed: %s\n", mysql_error(mysql));
} else {
puts("Query OK");
}
}
mysql_close(mysql);
mysql_library_end();
return EXIT_SUCCESS;
}
Here's how I compile it
gcc -I /usr/local/Cellar/mysql/8.0.16/include/mysql client.c -L /usr/local/Cellar/mysql/8.0.16/lib/ -l mysqlclient.a
ld: library not found for -lmysqlclient.a
clang: error: linker command failed with exit code 1 (use -v to see invocation)
Compiling without the .a succeeds, as it links to the shared library, not static one.
Lastly, here's my library files:
ls /usr/local/Cellar/mysql/8.0.16/lib/libmysqlclient*
/usr/local/Cellar/mysql/8.0.16/lib/libmysqlclient.21.dylib /usr/local/Cellar/mysql/8.0.16/lib/libmysqlclient.a /usr/local/Cellar/mysql/8.0.16/lib/libmysqlclient.dylib
This argument:
-l mysqlclient.a
causes the linker to look for a file named libmysqlclient.a.a. Instead, you want something like:
gcc -I /usr/local/Cellar/mysql/8.0.16/include/mysql client.c /usr/local/Cellar/mysql/8.0.16/lib/mysqlclient.a
-lmysqlclient should work. The extension .a does not need.
When you want to use static link, --static should be used.
You may also need to link other libraries
gcc client.c -o client --static -lmysqlclient -lssl -lcrypto -ldl -lpthread

Error when connect my C script to a MySQL online Database

I'm trying to write a simple C script that can connect to an online MySQL Database. I have tried to use Connector/C (libmysqlclient) downloaded by https://dev.mysql.com/downloads/connector/c/ but don't work.
Library is recognized but when I'm compiling from terminal this is the result: Undefined symbols for architecture x86_64: "_mysql_init", referenced from: _main in test.o ld: symbol(s) not found for architecture x86_64
I tried to correct the error described above, but I can not solve the problem with any help.
This is my code test.c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <mysql.h>
#define SIZE 30
int main(int argc, char** argv) {
/* connect to database */
MYSQL mysql;
if(mysql_init(&mysql) == NULL){
printf("Error\n");
return 0;
}
/* other code */
return 0;
}

Qt 5.5 missing MYSQL driver

Today I started working with Qt 5.5 and my first project is to get GPS related data form a MYSQL database and send to another server. The problem is that I have a linking problem with the libqmysql.so driver file. I looked after how to solve the problem and I did the following steps so far:
I copied all the files (libqmysql.so and other drivers) into /usr/lib/i386-linux-gnu/qt5/plugins/sqldrivers and /home/magyarg/Qt5.5.1/5.5/gcc/plugins/sqldrivers
I ran ldd libqmysql.so to check what dependencies are needed; I got the following result:
According this result, I installed libssl and libmysqlclient18.
The problem:
After these steps Qt Creator still throws me the the error:
QSqlDatabase: QMYSQL driver not loaded
QSqlDatabase: available drivers: QSQLITE QMYSQL QMYSQL3 QPSQL QPSQL7
The environment:
Ubuntu Linux 15.04(x86)
Qt5.5
The corresponding code:
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QApplication>
#include <QtSql>
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::on_pushButton_clicked()
{
QSqlDatabase db = QSqlDatabase::addDatabase("QMYSQL");
db.setHostName("127.0.0.1");
db.setDatabaseName("ugyfelhivo");
db.setUserName("root");
db.setPassword("pass");
bool ok = db.open();
if (ok == true) {
QLabel label;
label.setText("Macska");
}
}
Often this solution works to get the QMYSQL loaded:
cd /usr/lib/i386-linux-gnu/
sudo ln -s libmysqlclient_r.so.18 libmysqlclient_r.so.16

Linker error when connecting C to mysql

i want to connect C to mysql using the following code:
#include <C:\Program Files\MySQL\MySQL Connector C 6.0.2\include\my_global.h>
#include <C:\Program Files\MySQL\MySQL Connector C 6.0.2\include\mysql.h>
#include <stdio.h>
MYSQL *conn; /* pointer to connection handler */
int main ( int argc, char *argv[] )
{
conn = mysql_init ( NULL );
mysql_real_connect (
conn,"localhost","root","","operator", 3306, NULL,0 );
mysql_close ( conn );
return 0;
}
When i try to compile ,i get:
Error 3 error LNK2019: unresolved external symbol _mysql_real_connect#32 referenced in function _main C:\Users\kristel\Documents\Visual Studio 2010\Projects\database\database\database.obj database
Error 4 error LNK2019: unresolved external symbol _mysql_init#4 referenced in function _main C:\Users\kristel\Documents\Visual Studio 2010\Projects\database\database\database.obj database
It seems like something is missing but i dont know when.
I have added the mysqllib.lib like in the picture but still the problem is not resolved.
Just another question: Doing my searches,i have found something concerning cmake.Is there any relation between my error and cmake?
Thank you.
While you have included the mySQL header files in your code, you haven't included the mySQL library in your link options.