提问人:Jonah 提问时间:11/1/2023 最后编辑:Jonah 更新时间:11/1/2023 访问量:73
C API mysql_real_connect node-gyp 构建时抛出分段错误(核心转储)
C API mysql_real_connect throw Segmentation fault (core dumped) when it is build by node-gyp
问:
问题
当它直接使用 g++ 编译时,我可以运行该函数。但是当它使用node-gyp构建时,我无法运行它。我能够将我的问题缩小到几个文件,我希望你们中的一些人能让我离开这里。提前致谢:-)dosomething()
我观察到:
- 当 host= 时,使用
localhost
node-gyp
- 当 host= 或 时,使用
localhost
external IP
g++ -o conn $(mysql_config --cflags) test_db_conn.cpp -std=c++17 $(mysql_config --libs)
- 没有错误代码,只有
Segmentation fault
我做了:
- 设置
bind-address=0.0.0.0
- 将目标主机添加到 /etc/hosts(我能够从终端连接到主机数据库)并将其称为“jonah”,我能够 ping 并使用“jonah”执行程序
host
我敢肯定:
- 凭据中使用的任何字符都不需要转义
环境:
- 操作系统: Ubuntu 22.04.02 LTS (server)
- 节点:v16.20.0
- 节点-gyp:^9.4.0
- MySQL:版本 8.0.35-0ubuntu0.22.04.1
- MySQL C API:版本 8
// test_db_conn.cpp
/**
*
* g++ -o conn $(mysql_config --cflags) test_db_conn.cpp -std=c++17
* $(mysql_config --libs)
*
*/
#include <mysql.h>
#include <cstring>
#include <iostream>
void dosomething();
int main() {
dosomething();
return 0;
}
void dosomething() {
MYSQL *conn;
MYSQL_RES *res;
MYSQL_ROW row;
constexpr int QUERY_STATEMENT_LEN = 512;
char query[QUERY_STATEMENT_LEN] = {0}, *end_pos = nullptr;
long lTime = 0;
std::string stmt = "SELECT MIN(time) FROM Log";
std::cout << stmt << '\n';
try {
end_pos = stpcpy(query, stmt.c_str());
conn = mysql_init(NULL);
const char *host = "IP"; // 172.x.x.x
const char *user = "username";
const char *password = "password";
const char *database = "dbname";
const int port = 3306;
if (!mysql_real_connect(conn, host, user, password, database, port, NULL, 0)) {
throw mysql_error(conn);
}
if (mysql_real_query(conn, query, (unsigned int)(end_pos - query))) {
throw mysql_error(conn);
}
res = mysql_use_result(conn);
while ((row = mysql_fetch_row(res)) != NULL) {
if (row[0] == NULL) break;
lTime = std::stol(row[0]);
break;
}
// release resources
mysql_free_result(res);
mysql_close(conn);
std::cout << "ltime " << lTime << '\n';
} catch (const char *e) {
if (res) mysql_free_result(res);
if (conn) mysql_close(conn);
std::cerr << "e " << e << '\n';
throw e;
}
}
// CMySQL.h
#ifndef CMYSQL_H
#define CMYSQL_H
#include <mysql.h> // find /usr/ -type f -name mysql.h
#include <node.h> // find /usr/ -type f -name node.h
#include <node_object_wrap.h>
#include <cstring>
#include <exception>
#include <iostream>
#include <memory>
#include <string>
#include <unordered_map>
#include <vector>
#include "DBCredential.h"
class CMySQL : public node::ObjectWrap {
private:
DBCredential m_db;
void dosomething();
public:
explicit CMySQL(const DBCredential db) : m_db(db) {}
~CMySQL() {}
static void Init(v8::Local<v8::Object> exports);
static void New(const v8::FunctionCallbackInfo<v8::Value> &args);
static void pull(const v8::FunctionCallbackInfo<v8::Value> &args);
};
#endif
// binding.gyp
{
"targets":[
{
"target_name": "CMySQL_Addon",
"sources": [
"./DBCredential.cc",
"./CMySQL.cc",
"./MySQL_Addon.cc"
],
"cflags_cc":[
"-fexceptions",
"-std=c++17"
],
"cflags": [
"-I/usr/include/mysql"
],
"libraries": [
"-L/usr/lib/x86_64-linux-gnu",
"-lmysqlclient",
"-lzstd",
"-lssl",
"-lcrypto",
"-lresolv",
"-lm",
"-lz"
],
"make_global_settings": [
{
"CC": ["/usr/bin/g++"]
}
]
}
]
}
答: 暂无答案
评论
gcc
clang
-g -fsanitize=address,undefined
undefined reference to symbol '_ZTIPKc@@CXXABI_1.3'
typeinfo for char const*
<typeinfo>
typeid
g++ -g -fsanitize=address,undefined ...