Hi,
(I could've sworn I asked this question but my post seems to have disappeared.) Please note I am using Delphi 10.3 (still on a Win8.1 system).
On the MongoDB system, using mongoshell:
use testdb
db.createUser( { user: "testx", pwd: passwordPrompt(), roles: [ { role: "readWrite", db: "testdb" }] })
I then created a console application using a set of test credentials for a test db on a MongoDB 7.x system.
Code:
program mongotestproj;
{$APPTYPE CONSOLE}
{$R *.res}
uses
System.SysUtils, Data.DB, DBAccess, Uni, UniProvider, MongoDBUniProvider;
var
uq : TUniQuery;
uc : TUniConnection;
mp : TMongoDBUniProvider;
td : TDataSource;
ver : string;
begin
uc := TUniConnection.Create(nil);
uq := TUniQuery.Create(nil);
td := TDatasource.Create(nil);
td.DataSet := uq;
ver := '0.1.0';
try
{ TODO -oUser -cConsole Main : Insert code here }
writeln(ver);
uc.ProviderName := 'MongoDB';
uc.Port := 27018;
uc.Database := 'testdb';
uc.SpecificOptions.Add('SQLEngine=true');
uc.SpecificOptions.Add('MongoDB.BSONLibrary=f:\delphi\thirdparty\unidac\' +
'bin\win32\libbson-1.0.dll');
uc.SpecificOptions.Add('MongoDB.ClientLibrary=f:\delphi\thirdparty\' +
'unidac\bin\win32\libmongoc-1.0.dll');
uc.username := 'testx';
uc.Server := '192.168.27.123';
uc.Password := 'testthis';
uc.connected := true;
except
on E: Exception do
Writeln(E.ClassName, ': ', E.Message);
end;
end.
When I compile this, I get the following:
C:\Users\cc\Documents\Embarcadero\Studio\Projects\tmongoconsole\Win32\Debug>mongotestproj.exe
0.1.1
2024/06/26 16:30:06.0608: [ 1836]: DEBUG: cluster: Authentication failed: Authentication failed.
EUniError: Authentication failed.
On the Mongoserver log, I have the following:
{"t":{"$date":"2024-06-26T08:34:09.822+08:00"},"s":"I", "c":"NETWORK", "id":22943, "ctx":"listener","msg":"Connection accepted","attr":{"remote":"192.168.11.7:53390","uuid":{"uuid":{"$uuid":"<uuidstr>"}},"connectionId":43779,"connectionCount":10}}
{"t":{"$date":"2024-06-26T08:34:09.826+08:00"},"s":"I", "c":"NETWORK", "id":51800, "ctx":"conn43779","msg":"client metadata","attr":{"remote":"192.168.11.7:53390","client":"conn43779","negotiatedCompressors":[],"doc":{"driver":{"name":"mongoc","version":"1.23.1"},"os":{"type":"Windows","name":"Windows","version":"6.2 (9200)","architecture":"x86"},"platform":"cfg=0x0204172063 CC=MSVC 1916 CFLAGS=\"/DWIN32 /D_WINDOWS /W3\" LDFLAGS=\"/machine:X86\""}}}
{"t":{"$date":"2024-06-26T08:34:09.834+08:00"},"s":"I", "c":"ACCESS", "id":20251, "ctx":"conn43779","msg":"Supported SASL mechanisms requested for unknown user","attr":{"user":{"user":"testx","db":"admin"}}}
{"t":{"$date":"2024-06-26T08:34:09.836+08:00"},"s":"I", "c":"ACCESS", "id":6788604, "ctx":"conn43779","msg":"Auth metrics report","attr":{"metric":"acquireUser","micros":0}}
{"t":{"$date":"2024-06-26T08:34:09.844+08:00"},"s":"I", "c":"ACCESS", "id":5286307, "ctx":"conn43779","msg":"Failed to authenticate","attr":{"client":"192.168.11.7:53390","isSpeculative":true,"isClusterMember":false,"mechanism":"SCRAM-SHA-256","user":"testx","db":"admin","error":"UserNotFound: Could not find user \"testx\" for db \"admin\"","result":11,"metrics":{"conversation_duration":{"micros":8324,"summary":{"0":{"step":1,"step_total":2,"duration_micros":7966}}}},"extraInfo":{}}}
{"t":{"$date":"2024-06-26T08:34:09.850+08:00"},"s":"I", "c":"ACCESS", "id":6788604, "ctx":"conn43779","msg":"Auth metrics report","attr":{"metric":"acquireUser","micros":0}}
{"t":{"$date":"2024-06-26T08:34:09.858+08:00"},"s":"I", "c":"ACCESS", "id":5286307, "ctx":"conn43779","msg":"Failed to authenticate","attr":{"client":"192.168.27.7:53390","isSpeculative":false,"isClusterMember":false,"mechanism":"SCRAM-SHA-1","user":"testx","db":"admin","error":"UserNotFound: Could not find user \"testx\" for db \"admin\"","result":11,"metrics":{"conversation_duration":{"micros":8308,"summary":{"0":{"step":1,"step_total":2,"duration_micros":7900}}}},"extraInfo":{}}}
{"t":{"$date":"2024-06-26T08:34:09.864+08:00"},"s":"I", "c":"NETWORK", "id":22944, "ctx":"conn43779","msg":"Connection ended","attr":{"remote":"192.168.11.7:53390","uuid":{"uuid":{"$uuid":"<uuidstr>"}},"connectionId":43779,"connectionCount":9}}
I don't quite understand why the testx user needs to be included in the admin database. When using pymongo, I could use the "testx"/"testthis" credential to access the testdb database. Is there something about Uniconnection that I'm missing?
(I know for a fact I had posted a similar question; but iirc, it was in the main "Delphi Data Access Components" section. I'm guessing it was deleted.)
Any help appreciated.
Ed