I'm trying to use the (slightly older version of) MySQL C++ connector (see here) to open up a connection to a database and write stuff to it. My code works on my local Ubuntu 18.04 laptop. However, it doesn't work on my remote server (same OS).
I have tried connecting to tcp://localhost:3306
, tcp://127.0.0.1:3306
, as well as several other ports. The error I receive is
terminate called after throwing an instance of 'sql::SQLException'
what(): Unknown MySQL server host 'tcp' (2)
The server and laptop share the same OS and the same g++
compiler, almost all of the same files (except for one or two things I changed to reflect different paths), and have databases set up in the same way. I was thinking that it could've been some sort of config file issue.
Is it possible I could've botched the mysqlcppconn installation? I installed it in a pretty sketchy way--I manually moved headers into /usr/include/
and manually moved shared libraries into /usr/lib/x86_64-linux-gnu/
. When you see the files in the lib/
folder
libcrypto.so libmysqlcppconn-static.a libmysqlcppconn.so.7 libssl.so
libcrypto.so.1.0.0 libmysqlcppconn.so libmysqlcppconn.so.7.1.1.12 libssl.so.1.0.0
you'll notice that there's some libcrypto
and libssl
stuff in there--I didn't move those in.
Also, when I tried to change ip address strings to hardcoded string literals, I remember seeing a std::bad_alloc
error, and google showed me some threads that were suggesting it was something to do with varying compiler versions...
Anybody have an idea what's going on here? Here's the relevant piece of c++ code, but like I said, it works on my laptop so I'm pretty sure this isn't the problem:
MarketHistoryWriter::MarketHistoryWriter(
const MySqlConfig& msql_config,
unsigned num_symbols,
const std::string& table_name,
bool printing)
: m_msql_config(msql_config), m_table_name(table_name), m_num_sym(num_symbols), m_printing(printing)
{
// configure driver and connection
m_driver = get_driver_instance();
std::string conn_str = "tcp://"
+ m_msql_config.host + ":"
+ std::to_string(m_msql_config.port);
m_conn = m_driver->connect(conn_str,
m_msql_config.credentials.username,
m_msql_config.credentials.password);
m_conn->setSchema(m_msql_config.schema);
}
Also, if it helps, here's the traceback produced by gdb
:
(gdb) bt
#0 __GI_raise (sig=sig@entry=6) at ../sysdeps/unix/sysv/linux/raise.c:51
#1 0x00007ffff6834801 in __GI_abort () at abort.c:79
#2 0x00007ffff70a8957 in ?? () from /usr/lib/x86_64-linux-gnu/libstdc++.so.6
#3 0x00007ffff70aeab6 in ?? () from /usr/lib/x86_64-linux-gnu/libstdc++.so.6
#4 0x00007ffff70aeaf1 in std::terminate() () from /usr/lib/x86_64-linux-gnu/libstdc++.so.6
#5 0x00007ffff70aed24 in __cxa_throw () from /usr/lib/x86_64-linux-gnu/libstdc++.so.6
#6 0x00007ffff7638a4a in sql::mysql::MySQL_Connection::init (this=this@entry=0x555555818a00,
properties=std::map with 3 elements = {...})
at /export/home/pb2/build/sb_0-32258110-1547655664.03/mysql-connector-c++-1.1.12/driver/mysql_connection.cpp:900
#7 0x00007ffff763b5ea in sql::mysql::MySQL_Connection::MySQL_Connection (this=0x555555818a00,
_driver=<optimized out>, _proxy=..., hostName=..., userName=..., password=...)
at /export/home/pb2/build/sb_0-32258110-1547655664.03/mysql-connector-c++-1.1.12/driver/mysql_connection.cpp:146
#8 0x00007ffff763fc4f in sql::mysql::MySQL_Driver::connect (this=0x5555557fa5c0, hostName=...,
userName=..., password=...)
at /export/home/pb2/build/sb_0-32258110-1547655664.03/mysql-connector-c++-1.1.12/driver/mysql_driver.cpp:132
#9 0x00005555555b8c6e in MarketHistoryWriter::MarketHistoryWriter(MySqlConfig const&, unsigned int, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, bool) ()
#10 0x000055555559fccd in TestCppClient::TestCppClient() ()
#11 0x000055555559c451 in main ()
I run this short program below and I get this error
root@ubuntu-s-1vcpu-1gb-nyc1-01:~/test_mysql_conn# ./main
terminate called after throwing an instance of 'std::bad_alloc'
what(): std::bad_alloc
Aborted (core dumped)
Here's the program
#include <stdlib.h>
#include <iostream>
#include "mysql_connection.h"
#include <cppconn/driver.h>
#include <cppconn/exception.h>
#include <cppconn/resultset.h>
#include <cppconn/statement.h>
using namespace std;
int main(void)
{
try {
sql::Driver *driver;
sql::Connection *con;
sql::Statement *stmt;
driver = get_driver_instance();
con = driver->connect("tcp://127.0.0.1:3306", "root", "secretpassword");
//con->setSchema("ib");
delete con;
} catch (sql::SQLException &e) {
cout << "# ERR: SQLException in " << __FILE__;
cout << "(" << __FUNCTION__ << ") on line " << __LINE__ << endl;
cout << "# ERR: " << e.what();
cout << " (MySQL error code: " << e.getErrorCode();
cout << ", SQLState: " << e.getSQLState() << " )" << endl;
}
cout << endl;
return EXIT_SUCCESS;
}
Here's the makefile:
CXXFLAGS=-pthread -Wall -Wno-switch -std=c++11
LDFLAGS=-lmysqlcppconn
INCLUDES=-I/usr/include/cppconn
TARGET=main
$(TARGET):
$(CXX) $(CXXFLAGS) $(INCLUDES) ./*.cpp -o$(TARGET) $(LDFLAGS)
clean:
rm -f $(TARGET) *.o
I was correct that the code was "fine." I fixed this by reinstalling mysqlcppconnector. Instead of using 1.1.12, I reinstalled with the most recent edition. Also, I didn't install by manually copying files around, I installed with
sudo apt-get install libmysqlcppconn-dev
I don't know if this is a coincidence, but I also notice that clicking on "Looking for previous GA versions?" at the download page doesn't redirect you to version 1.1.12 anymore--it directs you to 1.1.13.
After this the program ran. This issue is unrelated, but it gave me the
Access Denied for User 'root'@'localhost'
error. I checked the permissions by typing SELECT user,authentication_string,plugin,host FROM mysql.user;
, saw that localhost didn't have mysql_native_password permissions (only auth_socket), so I changed that by doing
ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY 'secret_password_here';
Hope this helps someone else--this was a strange one.