Connection Problems using Matlab mongoc

Hi everyone,

I am facing problems to connect to a mongoDB container from Matlab using mongoc. I am able to connect using MongoDB Compass and pymongo with two different username/password combinations. The Python code looks as follows:

# Define MongoDB connection parameters
host = 'localhost'       # Hostname where MongoDB is running
port = 27017             # Port MongoDB is listening on
database_name = 'test'   # Database you want to connect to

# Define credentials
username = 'testUser'
password = 'testPassword'

# Construct the connection URI
uri = f'mongodb://{username}:{password}@{host}:{port}'

try:
    # Create a MongoClient object
    client = MongoClient(uri)
    
    # Access the database
    db = client[database_name]
    
    # Print a success message
    print('Successfully connected to MongoDB with authentication')
    
    # Optionally, check if you can list collections (as a simple test)
    collections = db.list_collection_names()
    print('Collections in the database:', collections)
    
except ServerSelectionTimeoutError as e:
    # Handle server selection timeout errors
    print('Failed to connect to MongoDB: Server selection timeout', e)
except ConnectionFailure as e:
    # Handle general connection failures
    print('Failed to connect to MongoDB: Connection failure', e)
except Exception as e:
    # Handle any other exceptions
    print('An error occurred:', e)

If I try to connect with Matlab I use

 conn = mongoc("localhost",27017,"test", "UserName", "testUser", "Password", "testPassword");

Unfortunately the obtained error Message just indicates that authentication fails:

Error using database.mongo.connection
[Mongo Driver Error]: Authentication failed..

Error in mongoc (line 52)
    conn = database.mongo.connection(varargin{:});

If I the other username/password I obtain the same error. I would highly appreciate any suggestions what to do.

All the best
Moritz

Hi @Moritz_H - welcome to the MongoDB community forums!

I am not a user of mongoc, but having a look at the docs, I think your syntax is slightly off. You wrote:

conn = mongoc("localhost",27017,"test", "UserName", "testUser", "Password", "testPassword");

… but I think it should be:

conn = mongoc("localhost",27017,"test", UserName="testUser", Password="testPassword");

That’s from the MathWorks docs

I am a Python developer, and I wanted to point something out, in case it’s confusing later!

PyMongo is super-lazy, so it doesn’t connect to the server when you think it does.

# Create a MongoClient object - DOES NOT CONNECT TO SERVER
client = MongoClient(uri)
    
# Get a database object - DOES NOT CONNECT TO SERVER
db = client[database_name]

# DOES CONNECT TO THE SERVER (because it actually needs to get the list of collection names in the database)
collections = db.list_collection_names()

In this case, if you left out the call to list_collection_names then your uri could be wrong, and you’d never know because PyMongo isn’t even going to attempt to connect to your server. Hope this helps!

Mark

Thanks for the helpful advice :slight_smile:

Thanks Mark. I found both ways to implement online and they throw a similar error.