MongoDB执行collection find时抛出TimeoutException求助
Hey there! Let's work through this issue together—this is a super common mix-up for folks new to MongoDB, so you're not alone here.
First, let's zero in on the most obvious red flag in your error message:
EndPoint : "127.0.0.1:3306"
The Core Problem: Wrong Port Number
Port 3306 is the default port for MySQL, not MongoDB! MongoDB uses 27017 as its default port. Your code is trying to connect to a MySQL port where MongoDB isn't running, hence the timeout and disconnected state.
Step-by-Step Fixes
Update your connection string
Make sure yourMongoClientis pointing to the correct MongoDB port. A basic local connection string should look like this:var client = new MongoClient("mongodb://localhost:27017"); var database = client.GetDatabase("your_database_name");If you've changed MongoDB's default port during setup, replace
27017with your custom port number.Confirm MongoDB is running locally
Before testing your code, verify MongoDB is active:- On Windows: Check if the "MongoDB Server" service is running in Services.msc
- On macOS/Linux: Run
mongodin your terminal to start the server (or check if it's running as a background service)
Test the connection explicitly
To rule out further issues, add a quick connection test before querying data. This helps catch connection problems earlier:var client = new MongoClient("mongodb://localhost:27017"); // Test connection by listing databases await client.ListDatabasesAsync(); var database = client.GetDatabase("your_database_name");
Why Your Code Threw the Error
The line var playerData = collection.Find(...).First(); failed because your MongoDatabase instance was never able to establish a valid connection to MongoDB. Fixing the port and confirming the server is running will resolve the timeout exception.
Once you fix the connection, your existing query code should work as expected—just make sure your User class maps correctly to the habbo_users collection documents.
内容的提问来源于stack exchange,提问作者user5405648

