mongo4.0.7源代码下载编译启动运行

 1、环境安装

mongodb4.0.7源代码

win10环境

Python 3.7.9

Visual Studio 2017(开始使用的是Visual Studio 2022,编译的时候版本对不上,需要修源代码地方比较多)

Visual Studio 2017安装插件【使用C++的桌面开发】,对应CV++ 是14.1版本

2、源码编译

Visual Studio 2017引入mongo-r4.0.7源码文件夹

引入mongo-r4.0.7源码成功之后,右击项目src文件,选择打开开发人员命令提示

进入mongo-r4.0.7源码根目录

安装编译环境需要的插件

pip install -r buildscripts/requirements.txt

SCons 是一个软件构建工具,它使用 Python 语言来编写构建脚本,为软件开发提供了一种灵活、强大且易于维护的构建方式。SConstruct 文件是 SCons 的构建脚本。SConstruct 文件在mongo-r4.0.7源码根目录。Scons 默认可能只使用一个核心进行编译,你可以通过-j参数指定使用的核心数量,这样能并行编译多个源文件,加快编译速度。

mongo-r4.0.7源码scons命令是:scons -j 8 --disable-warnings-as-errors MONGO_VERSION=4.0.7

scons第一次运行,速度比较慢,大概2个小时,后面有缓存了,速度就快了。

构建结果mongod.exe输出到文件夹\mongo-r4.0.7\build\opt\mongo,如下图:

思路扩展:如果想debug模式输出各种详细日志信息,执行命令是scons --dbg=on -j 8 --disable-warnings-as-errors MONGO_VERSION=4.0.7。输出结果文件夹是\mongo-r4.0.7\build\debug\mongo。

3、修改源码,打印日志

研究mongo-r4.0.7源代码最需要做的事情是学会打印日志,这样有助于理解代码调用流程。找到db.cpp入口vmain方法,增加一个日志std::cout << "conca " << " this is win32..." ;

#if defined(_WIN32)
// In Windows, wmain() is an alternate entry point for main(), and receives the same parameters
// as main() but encoded in Windows Unicode (UTF-16); "wide" 16-bit wchar_t characters.  The
// WindowsCommandLine object converts these wide character strings to a UTF-8 coded equivalent
// and makes them available through the argv() and envp() members.  This enables mongoDbMain()
// to process UTF-8 encoded arguments and environment variables without regard to platform.
int wmain(int argc, wchar_t* argvW[], wchar_t* envpW[]) {
    mongo::WindowsCommandLine wcl(argc, argvW, envpW);
	std::cout << "conca " << " this is win32..." ;
    int exitCode = mongo::mongoDbMain(argc, wcl.argv(), wcl.envp());
    mongo::quickExit(exitCode);
}
#else
int main(int argc, char* argv[], char** envp) {
    int exitCode = mongo::mongoDbMain(argc, argv, envp);
    mongo::quickExit(exitCode);
}
#endif

再次执行编译构建命令scons -j 8 --disable-warnings-as-errors MONGO_VERSION=4.0.7

C:\me\studyhome\mongodb\mongo-r4.0.7>scons -j 8 --disable-warnings-as-errors MONGO_VERSION=4.0.7
scons: Reading SConscript files ...
scons: running with args c:\users\conca\appdata\local\programs\python\python37\python.exe C:\Users\conca\AppData\Local\Programs\Python\Python37\Scripts\scons -j 8 --disable-warnings-as-errors MONGO_VERSION=4.0.7
scons version: 4.9.1
python version: 3 7 9 'final' 0
CC is cl
cl found in $PATH at C:\Program Files (x86)\Microsoft Visual Studio\2017\Professional\VC\Tools\MSVC\14.16.27023\bin\HostX86\x86\cl.EXE
CXX is $CC
$CC was not found in $PATH
$CC resolves to C:\me\studyhome\mongodb\mongo-r4.0.7\$CC
Checking whether the C compiler works... (cached) yes
Checking whether the C++ compiler works... (cached) yes
Checking that the C++ compiler can link a C++ program... (cached) yes
Checking if C++ compiler "$CC" is MSVC... (cached) yes
Checking if C compiler "cl" is MSVC... (cached) yes
Detected a x86_64 processor
Checking if target OS windows is supported by the toolchain... (cached) yes
Checking if C compiler is Microsoft Visual Studio 2017 15.9 or newer...(cached) yes
Checking if C++ compiler is Microsoft Visual Studio 2017 15.9 or newer...(cached) yes
Checking if we are using libstdc++... (cached) no
Checking for C++17... (cached) yes
Checking for memset_s... (cached) no
Checking for C function strnlen()... (cached) yes
Checking Windows SDK is 8.1 or newer... (cached) yes
Checking if we are on a POSIX system... (cached) no
Checking for storage class thread_local (cached) yes
Checking for C++14 std::enable_if_t support...(cached) yes
Using SSL Provider: windows
Checking for C++ header file execinfo.h... (cached) no
Checking for C library pcap... (cached) no
Checking for C library wpcap... (cached) no
Checking if std::atomic<int64_t> works... (cached) yes
Checking if std::atomic<uint64_t> works... (cached) yes
Checking if std::atomic<int32_t> works... (cached) yes
Checking if std::atomic<uint32_t> works... (cached) yes
Checking for extended alignment 64 for concurrency types... (cached) yes
Checking for C library mongoc-1.0... (cached) no
Checking for C function fallocate()... (cached) no
Checking for C function sync_file_range()... (cached) no
Checking for C header file x86intrin.h... (cached) no
Checking for C header file arm_neon.h... (cached) no
scons: done reading SConscript files.
scons: Building targets ...
Compiling build\opt\mongo\db\db.obj
db.cpp
Linking build\opt\mongo\mongod.exe
  正在创建库 build\opt\mongo\mongod.lib 和对象 build\opt\mongo\mongod.exp
tcmalloc_set_parameter.lib(tcmalloc_set_parameter.obj) : warning LNK4217: 本地定义的符号 ?instance@MallocExtension@@SAPEAV1@XZ (public: static class MallocExtension * __cdecl MallocExtension::instance(void)) 在函数 "class mongo::StatusWith<unsigned __int64> __cdecl mongo::`anonymous namespace'::getProperty(class mongo::StringData)" (?getProperty@?A0x1e33d25e@mongo@@YA?AV?$StatusWith@_K@2@VStringData@2@@Z) 中导入
tcmalloc_set_parameter.lib(tcmalloc_server_status_section.obj) : warning LNK4217: 本地定义的符号 ?instance@MallocExtension@@SAPEAV1@XZ (public: static class MallocExtension * __cdecl MallocExtension::instance(void)) 在函数 "public: virtual class mongo::BSONObj __cdecl mongo::`anonymous namespace'::TCMallocServerStatusSection::generateSection(class mongo::OperationContext *,class mongo::BSONElement const &)const " (?generateSection@TCMallocServerStatusSection@?A0x14c3ba4c@mongo@@UEBA?AVBSONObj@3@PEAVOperationContext@3@AEBVBSONElement@3@@Z) 中导入
wiredtiger.lib(os_alloc.obj) : warning LNK4217: 本地定义的符号 tc_malloc 在函数 __wt_malloc 中导入
wiredtiger.lib(os_alloc.obj) : warning LNK4217: 本地定义的符号 tc_free 在函数 __wt_free_int 中导入
wiredtiger.lib(os_alloc.obj) : warning LNK4217: 本地定义的符号 tc_realloc 在函数 __realloc_func 中导入
wiredtiger.lib(os_alloc.obj) : warning LNK4217: 本地定义的符号 tc_calloc 在函数 __wt_calloc 中导入
Install file: "build\opt\mongo\mongod.exe" as "mongod.exe"
Install file: "build\opt\mongo\mongod.pdb" as "mongod.pdb"
scons: done building targets.

启动mongod实例mongod --dbpath mongod数据文件夹 -v日志级别

mongod启动输出日志会看到刚才打印conca  this is win32...。到此说明mongodb源代码编译启动成功了,后面可以更方便研究mongodb源代码了。

C:\me\studyhome\mongodb\mongo-r4.0.7>mongod --dbpath C:\me\studyhome\mongodb\mongo-r4.0.7\data -v
conca  this is win32...2025-04-13T20:59:08.152+0800 D1 CONTROL  [main] Loading library: Schannel.dll
2025-04-13T20:59:08.155+0800 I  CONTROL  [main] Automatically disabling TLS 1.0, to force-enable TLS 1.0 specify --sslDisabledProtocols 'none'
2025-04-13T20:59:08.161+0800 I  CONTROL  [initandlisten] MongoDB starting : pid=18736 port=27017 dbpath=C:\me\studyhome\mongodb\mongo-r4.0.7\data 64-bit host=LAPTOP-78C0012V
2025-04-13T20:59:08.162+0800 I  CONTROL  [initandlisten] targetMinOS: Windows 7/Windows Server 2008 R2
2025-04-13T20:59:08.164+0800 I  CONTROL  [initandlisten] db version v4.0.7
2025-04-13T20:59:08.164+0800 I  CONTROL  [initandlisten] git version: nogitversion
2025-04-13T20:59:08.165+0800 I  CONTROL  [initandlisten] allocator: tcmalloc
2025-04-13T20:59:08.166+0800 I  CONTROL  [initandlisten] modules: none
2025-04-13T20:59:08.166+0800 I  CONTROL  [initandlisten] build environment:
2025-04-13T20:59:08.167+0800 I  CONTROL  [initandlisten]     distarch: x86_64
2025-04-13T20:59:08.168+0800 I  CONTROL  [initandlisten]     target_arch: x86_64
2025-04-13T20:59:08.168+0800 I  CONTROL  [initandlisten] options: { storage: { dbPath: "C:\me\studyhome\mongodb\mongo-r4.0.7\data" }, systemLog: { verbosity: 1 } }
2025-04-13T20:59:08.172+0800 I  STORAGE  [initandlisten] Detected data files in C:\me\studyhome\mongodb\mongo-r4.0.7\data created by the 'wiredTiger' storage engine, so setting the active storage engine to 'wiredTiger'.
2025-04-13T20:59:08.172+0800 I  STORAGE  [initandlisten] conca system Mem is 16238
2025-04-13T20:59:08.172+0800 I  STORAGE  [initandlisten] conca cacheMB is 7607
2025-04-13T20:59:08.173+0800 I  STORAGE  [initandlisten] wiredtiger_open config: create,cache_size=7607M,cache_overflow=(file_max=0M),session_max=33000,eviction=(threads_min=4,threads_max=4),config_base=false,statistics=(fast),log=(enabled=true,archive=true,path=journal,compressor=snappy),file_manager=(close_idle_time=100000,close_scan_interval=10,close_handle_minimum=250),statistics_log=(wait=0),verbose=[recovery_progress,checkpoint_progress,compact_progress],
2025-04-13T20:59:08.197+0800 I  STORAGE  [initandlisten] WiredTiger message [1744549148:197017][18736:140714669791376], txn-recover: Recovering log 28 through 29
2025-04-13T20:59:08.251+0800 I  STORAGE  [initandlisten] WiredTiger message [1744549148:250881][18736:140714669791376], txn-recover: Recovering log 29 through 29
2025-04-13T20:59:08.309+0800 I  STORAGE  [initandlisten] WiredTiger message [1744549148:308680][18736:140714669791376], txn-recover: Main recovery loop: starting at 28/4608 to 29/256
2025-04-13T20:59:08.413+0800 I  STORAGE  [initandlisten] WiredTiger message [1744549148:413401][18736:140714669791376], txn-recover: Recovering log 28 through 29
2025-04-13T20:59:08.478+0800 I  STORAGE  [initandlisten] WiredTiger message [1744549148:478230][18736:140714669791376], txn-recover: Recovering log 29 through 29
2025-04-13T20:59:08.529+0800 I  STORAGE  [initandlisten] WiredTiger message [1744549148:528682][18736:140714669791376], txn-recover: Set global recovery timestamp: (0,0)
2025-04-13T20:59:08.541+0800 I  RECOVERY [initandlisten] WiredTiger recoveryTimestamp. Ts: Timestamp(0, 0)
2025-04-13T20:59:08.544+0800 D1 COMMAND  [WTIdleSessionSweeper] BackgroundJob starting: WTIdleSessionSweeper
2025-04-13T20:59:08.544+0800 D1 STORAGE  [WTIdleSessionSweeper] starting WTIdleSessionSweeper thread
2025-04-13T20:59:08.548+0800 D1 STORAGE  [initandlisten] Registering collection admin.system.version with UUID a76860e7-84c4-4a43-8deb-42358f491495
2025-04-13T20:59:08.548+0800 D1 STORAGE  [initandlisten] Registering collection local.startup_log with UUID 1e5195da-81b3-4f93-ba61-da40cbdccf67
2025-04-13T20:59:08.549+0800 D1 STORAGE  [initandlisten] Registering collection config.system.sessions with UUID 0a2b2726-f110-4c84-a01a-3c5ccae550a7
2025-04-13T20:59:08.550+0800 D1 STORAGE  [initandlisten] Registering collection db.user with UUID 3085070b-fc6c-4a15-bd83-57d650b71ff8
2025-04-13T20:59:08.552+0800 D1 COMMAND  [WTJournalFlusher] BackgroundJob starting: WTJournalFlusher
2025-04-13T20:59:08.552+0800 D1 STORAGE  [WTJournalFlusher] starting WTJournalFlusher thread
2025-04-13T20:59:08.553+0800 D1 COMMAND  [WTCheckpointThread] BackgroundJob starting: WTCheckpointThread
2025-04-13T20:59:08.553+0800 D1 STORAGE  [WTCheckpointThread] starting WTCheckpointThread thread
2025-04-13T20:59:08.553+0800 I  STORAGE  [initandlisten] Timestamp monitor starting
2025-04-13T20:59:08.557+0800 I  CONTROL  [initandlisten]
2025-04-13T20:59:08.560+0800 I  CONTROL  [initandlisten] ** WARNING: Access control is not enabled for the database.
2025-04-13T20:59:08.563+0800 I  CONTROL  [initandlisten] **          Read and write access to data and configuration is unrestricted.
2025-04-13T20:59:08.563+0800 I  CONTROL  [initandlisten]
2025-04-13T20:59:08.564+0800 I  CONTROL  [initandlisten] ** WARNING: This server is bound to localhost.
2025-04-13T20:59:08.565+0800 I  CONTROL  [initandlisten] **          Remote systems will be unable to connect to this server.
2025-04-13T20:59:08.565+0800 I  CONTROL  [initandlisten] **          Start the server with --bind_ip <address> to specify which IP
2025-04-13T20:59:08.566+0800 I  CONTROL  [initandlisten] **          addresses it should serve responses from, or with --bind_ip_all to
2025-04-13T20:59:08.567+0800 I  CONTROL  [initandlisten] **          bind to all interfaces. If this behavior is desired, start the
2025-04-13T20:59:08.568+0800 I  CONTROL  [initandlisten] **          server with --bind_ip 127.0.0.1 to disable this warning.
2025-04-13T20:59:08.568+0800 I  CONTROL  [initandlisten]
2025-04-13T20:59:08.571+0800 D1 STORAGE  [initandlisten] admin.system.version: clearing plan cache - collection info cache reset
2025-04-13T20:59:08.571+0800 D1 -        [initandlisten] reloading view catalog for database admin
2025-04-13T20:59:08.573+0800 D1 STORAGE  [initandlisten] config.system.sessions: clearing plan cache - collection info cache reset
2025-04-13T20:59:08.577+0800 D1 -        [initandlisten] reloading view catalog for database config
2025-04-13T20:59:08.581+0800 D1 STORAGE  [initandlisten] db.user: clearing plan cache - collection info cache reset
2025-04-13T20:59:08.581+0800 D1 -        [initandlisten] reloading view catalog for database db
2025-04-13T20:59:08.582+0800 D1 STORAGE  [initandlisten] local.startup_log: clearing plan cache - collection info cache reset
2025-04-13T20:59:08.582+0800 D1 -        [initandlisten] reloading view catalog for database local
2025-04-13T20:59:08.584+0800 I  SHARDING [initandlisten] Marking collection local.system.replset as collection version: <unsharded>
2025-04-13T20:59:08.585+0800 D1 STORAGE  [initandlisten]     Recovering database: admin
2025-04-13T20:59:08.585+0800 D1 QUERY    [initandlisten] conca IDHACK _key={ _id: "featureCompatibilityVersion" }
2025-04-13T20:59:08.587+0800 D1 STORAGE  [initandlisten] conca keyStringValue:3C66656174757265436F6D7061746962696C69747956657273696F6E0004
2025-04-13T20:59:08.587+0800 D1 QUERY    [initandlisten] conca IDHACK recordId=RecordId(1)
2025-04-13T20:59:08.590+0800 D1 STORAGE  [initandlisten] seekExact id :RecordId(1)
2025-04-13T20:59:08.595+0800 D1 QUERY    [initandlisten] cursor :struct mongo::unowned_ptr<class mongo::SeekableRecordCursor>
2025-04-13T20:59:08.595+0800 D1 QUERY    [initandlisten] record :class boost::optional<struct mongo::Record>
2025-04-13T20:59:08.596+0800 D1 QUERY    [initandlisten] record->data.releaseToBson() :{ _id: "featureCompatibilityVersion", version: "4.4" }
2025-04-13T20:59:08.597+0800 D1 QUERY    [initandlisten] currentSnapshotId:7
2025-04-13T20:59:08.598+0800 D1 STORAGE  [initandlisten]     Recovering database: config
2025-04-13T20:59:08.599+0800 D1 STORAGE  [initandlisten]     Recovering database: db
2025-04-13T20:59:08.600+0800 D1 STORAGE  [initandlisten]     Recovering database: local
2025-04-13T20:59:08.601+0800 D1 STORAGE  [initandlisten] done repairDatabases
2025-04-13T20:59:08.602+0800 I  STORAGE  [initandlisten] Flow Control is enabled on this deployment.
2025-04-13T20:59:08.603+0800 I  SHARDING [initandlisten] Marking collection admin.system.roles as collection version: <unsharded>
2025-04-13T20:59:08.603+0800 I  COMMAND  [initandlisten] command admin.system.roles command: find { find: "system.roles", $db: "admin" } planSummary: EOF keysExamined:0 docsExamined:0 cursorExhausted:1 numYields:0 nreturned:0 reslen:107 locks:{ ReplicationStateTransition: { acquireCount: { w: 1 } }, Global: { acquireCount: { r: 1 } }, Database: { acquireCount: { r: 1 } }, Collection: { acquireCount: { r: 2 } }, Mutex: { acquireCount: { r: 1 } } } storage:{ data: { bytesRead: 278 } } protocol:op_msg 0ms
2025-04-13T20:59:08.605+0800 D1 ACCESS   [initandlisten] There were no users to pin, not starting tracker thread
2025-04-13T20:59:08.611+0800 I  SHARDING [initandlisten] Marking collection admin.system.version as collection version: <unsharded>
2025-04-13T20:59:08.612+0800 D1 QUERY    [initandlisten] conca IDHACK _key={ _id: "authSchema" }
2025-04-13T20:59:08.612+0800 D1 STORAGE  [initandlisten] conca keyStringValue:3C61757468536368656D610004
2025-04-13T20:59:08.613+0800 D1 QUERY    [initandlisten] conca IDHACK recordId=RecordId(0)
2025-04-13T20:59:08.615+0800 D1 QUERY    [initandlisten] conca IDHACK _key={ _id: "shardIdentity" }
2025-04-13T20:59:08.615+0800 D1 STORAGE  [initandlisten] conca keyStringValue:3C73686172644964656E746974790004
2025-04-13T20:59:08.616+0800 D1 QUERY    [initandlisten] conca IDHACK recordId=RecordId(0)
2025-04-13T20:59:08.619+0800 I  SHARDING [initandlisten] Marking collection local.startup_log as collection version: <unsharded>
2025-04-13T20:59:09.475+0800 I  FTDC     [initandlisten] Initializing full-time diagnostic data capture with directory 'C:/me/studyhome/mongodb/mongo-r4.0.7/data/diagnostic.data'
2025-04-13T20:59:09.477+0800 D1 EXECUTOR [FreeMonHTTP-0] starting thread in pool FreeMonHTTP
2025-04-13T20:59:09.478+0800 D1 QUERY    [FreeMonProcessor] conca _idRetrying :18446744073709551615
2025-04-13T20:59:09.480+0800 D1 QUERY    [FreeMonProcessor] conca  child()->work(&id) :class std::unique_ptr<class mongo::PlanStage,struct std::default_delete<class mongo::PlanStage> >
2025-04-13T20:59:09.480+0800 D1 COMMAND  [TTLMonitor] BackgroundJob starting: TTLMonitor
2025-04-13T20:59:09.481+0800 D1 QUERY    [FreeMonProcessor] conca tatus : >>status end
2025-04-13T20:59:09.481+0800 D1 COMMAND  [ClientCursorMonitor] BackgroundJob starting: ClientCursorMonitor
2025-04-13T20:59:09.482+0800 D1 COMMAND  [PeriodicTaskRunner] BackgroundJob starting: PeriodicTaskRunner
2025-04-13T20:59:09.483+0800 D1 QUERY    [FreeMonProcessor] conca _idRetrying :18446744073709551615
2025-04-13T20:59:09.485+0800 D1 QUERY    [FreeMonProcessor] conca  child()->work(&id) :class std::unique_ptr<class mongo::PlanStage,struct std::default_delete<class mongo::PlanStage> >
2025-04-13T20:59:09.486+0800 I  SHARDING [LogicalSessionCacheRefresh] Marking collection config.system.sessions as collection version: <unsharded>
2025-04-13T20:59:09.486+0800 D1 QUERY    [FreeMonProcessor] conca tatus : >>status end
2025-04-13T20:59:09.487+0800 I  NETWORK  [initandlisten] Listening on 127.0.0.1
2025-04-13T20:59:09.488+0800 I  COMMAND  [LogicalSessionCacheReap] command config.system.sessions command: listIndexes { listIndexes: "system.sessions", cursor: {}, $db: "config" } numYields:0 reslen:245 locks:{ ReplicationStateTransition: { acquireCount: { w: 1 } }, Global: { acquireCount: { r: 1 } }, Database: { acquireCount: { r: 1 } }, Collection: { acquireCount: { r: 1 } }, Mutex: { acquireCount: { r: 1 } } } storage:{} protocol:op_msg 1ms
2025-04-13T20:59:09.488+0800 I  COMMAND  [LogicalSessionCacheRefresh] command config.system.sessions command: listIndexes { listIndexes: "system.sessions", cursor: {}, $db: "config" } numYields:0 reslen:245 locks:{ ReplicationStateTransition: { acquireCount: { w: 1 } }, Global: { acquireCount: { r: 1 } }, Database: { acquireCount: { r: 1 } }, Collection: { acquireCount: { r: 1 } }, Mutex: { acquireCount: { r: 1 } } } storage:{} protocol:op_msg 2ms
2025-04-13T20:59:09.495+0800 I  SHARDING [LogicalSessionCacheReap] Marking collection config.transactions as collection version: <unsharded>
2025-04-13T20:59:09.495+0800 I  NETWORK  [initandlisten] waiting for connections on port 27017
2025-04-13T20:59:09.497+0800 I  COMMAND  [LogicalSessionCacheReap] command config.transactions command: find { find: "transactions", filter: { lastWriteDate: { $lt: new Date(1744547349491) } }, projection: { _id: 1 }, sort: { _id: 1 }, $db: "config" } planSummary: EOF keysExamined:0 docsExamined:0 cursorExhausted:1 numYields:0 nreturned:0 reslen:108 locks:{ ReplicationStateTransition: { acquireCount: { w: 1 } }, Global: { acquireCount: { r: 1 } }, Database: { acquireCount: { r: 1 } }, Collection: { acquireCount: { r: 2 } }, Mutex: { acquireCount: { r: 1 } } } storage:{} protocol:op_msg 1ms
2025-04-13T20:59:10.006+0800 I  SHARDING [ftdc] Marking collection local.oplog.rs as collection version: <unsharded>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值