我正在尝试使用MySQL C ++连接器(see here)(稍旧的版本)来建立与数据库的连接并向其中写入内容。我的代码可在本地Ubuntu 18.04笔记本电脑上使用。但是,它在我的远程服务器(相同的操作系统)上不起作用。
我已经尝试连接到tcp://localhost:3306
,tcp://127.0.0.1:3306
以及其他几个端口。我收到的错误是
terminate called after throwing an instance of 'sql::SQLException'
what(): Unknown MySQL server host 'tcp' (2)
服务器和笔记本电脑共享相同的操作系统和相同的g++
编译器,几乎所有相同的文件(除了我更改以反映不同路径的一两个东西之外),并以相同的方式设置了数据库。 I was thinking that it could've been some sort of config file issue。
是否可以破坏mysqlcppconn安装?我以一种非常粗略的方式安装了它-我将标头手动移至/usr/include/
,并将共享库手动移至/usr/lib/x86_64-linux-gnu/
。当您在lib/
文件夹中看到文件时>
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
[您会注意到里面有
libcrypto
和libssl
的东西-我没有将它们移入。
而且,当我尝试将ip地址字符串更改为硬编码的字符串文字时,我还记得看到一个std::bad_alloc
错误,并且google向我展示了一些暗示与不同编译器版本有关的线程...
有人知道这里发生了什么吗?这是相关的C ++代码,但是就像我说的那样,它可以在我的笔记本电脑上工作,所以我很确定这不是问题:
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); }
同样,如果有帮助,这是
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 ()
编辑:一个较小的,可重复的示例。
我在下面运行了这个简短的程序,但出现此错误
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)
这里是程序
#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; }
这是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
我正在尝试使用MySQL C ++连接器(稍旧的版本)(请参见此处)来建立与数据库的连接并向其中写入内容。我的代码可在本地Ubuntu 18.04笔记本电脑上使用。但是,它是...
我是对的,代码是“正确的”。我通过重新安装mysqlcppconnector来解决此问题。我没有使用1.1.12,而是重新安装了最新版本。另外,我不是通过手动复制文件来安装的,而是通过