After much tweaking, I found a workaround but it may have security risk. I would appreciate if anyone could recognize the risk and recommend a better solution.
Step 1: (in WSL2) Start mongod but bind to all IP, not just localhost
$ mongod --bind-ip-all
Step 2: (in WSL2) Find the IP address of WSL2
$ ip addr | grep eth0
Copy the IP address right after “inet”. For example, if the result returns “inet 162.38.25.44/20 brd 162.38.30.255 scope global eth0”, then the IP address is 162.38.25.44.
Step 3: (in Windows) Suppose my WSL2 IP address is 162.38.25.44, then run MongoDB compass and connect to the address “mongodb://162.38.25.44:27017”
What I don’t understand is why the first step requires binding to all IP, while binding to just localhost or 127.0.0.1 will not work. Any suggestions?