我正在尝试使用(稍旧版本的)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++
编译器,几乎所有相同的文件(除了我更改的一两个内容以反射(reflect)不同的路径),并且设置了数据库以同样的方式。 I was thinking that it could've been some sort of config file issue .
我是否可能搞砸了 mysqlcppconn 安装?我以一种非常粗略的方式安装了它 - 我手动将 header 移动到 /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;
}
这是生成文件:
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
最佳答案
我是正确的,代码“很好”。我通过重新安装 mysqlcppconnector 解决了这个问题。我没有使用 1.1.12,而是使用最新版本重新安装。另外,我没有通过手动复制文件来安装,而是使用
安装sudo apt-get install libmysqlcppconn-dev
我不知道这是否是巧合,但我还注意到点击“寻找以前的 GA 版本?”在the download page不再将您重定向到版本 1.1.12,而是将您重定向到 1.1.13。
此后程序运行。这个问题无关,但它给了我
Access Denied for User 'root'@'localhost'
错误。我通过输入 SELECT user,authentication_string,plugin,host FROM mysql.user; 来检查权限,发现 localhost 没有 mysql_native_password 权限(只有 auth_socket),所以我通过执行
ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY 'secret_password_here';
希望这对其他人有帮助——这是一个奇怪的事情。
关于c++ - 为什么mysql拒绝本地连接,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58550337/