node-gyp构建时mysql_real_connect触发Segmentation fault问题求助
Node-gyp构建后mysql_real_connect触发段错误问题
直接使用g++编译时,dosomething()函数可正常运行,但通过node-gyp构建后程序无法执行,已定位到问题出在mysql_real_connect调用时触发Segmentation fault (core dumped)。
现象
- 当host设为
localhost时,node-gyp构建的程序可执行成功 - 当host设为
localhost或外部IP时,使用命令g++ -o conn $(mysql_config --cflags) test_db_conn.cpp -std=c++17 $(mysql_config --libs)编译的程序可执行成功 - 无错误码,仅出现
Segmentation fault
已尝试操作
- 在MySQL配置中设置
bind-address=0.0.0.0 - 将目标主机添加到
/etc/hosts并命名为"jonah",可ping通该主机,且g++编译的程序使用该名称作为host可正常连接
确认信息
- 数据库凭据中所有字符无需转义
环境信息
- 操作系统:Ubuntu 22.04.02 LTS (server)
- Node.js版本:v16.20.0
- node-gyp版本:^9.4.0
- MySQL版本:Ver 8.0.35-0ubuntu0.22.04.1
- MySQL C API版本:Ver 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++"] } ] } ] }
内容的提问来源于stack exchange,提问作者Jonah
相关产品推荐
相关产品推荐

