How to connect Mongolab with MongoDb.Driver - mongodb

i'm testing mongolab with .netCore using MongoDb.Driver.
I have this connection string
mongodb://<dbuser>:<dbpassword>#mymongolaburl:46367/somedatabase
And i'm connecting this way
var connectionString = #"mongodb://<dbuser>:<dbpassword>#mymongolaburl:46367/somedatabase";
var databaseName = "somedatabase";
var client = new MongoClient(connectionString);
if (client != null)
{
_database = client.GetDatabase(databaseName);
_database.GetCollection<User>("User").InsertOne(new User {Name="Luke Skywalker" });
}
It is not working 'cause it says the database name is invalid, if i use the connection string without the database name
mongodb://<dbuser>:<dbpassword>#mymongolaburl:46367
I get a timeout execption.
I Already connected to database using Robo 3T.
Thanks in advance.

After one day i found how to do that in this link
The The solution is to specify which is database in the connection string
mongodb://<dbuser>:<dbpassword>#mymongolaburl:46367/?authSource=somedatabase
Thank you!

Related

Pulumi: How can a Function App reference a connection string from a SQL Server Database declaration?

Using Pulumi, how can a Function App reference a connection string from a SQL Server Database declaration?
I've tried building the following SQL connection string:
var sqlServer = server as Pulumi.Azure.Sql.SqlServer;
var connectionString = $"Server=tcp:{sqlServer.FullyQualifiedDomainName},1433;Initial Catalog={sqlDatabase.Name};Persist Security Info=False;User ID={sqlServer.AdministratorLogin};Password={sqlServer.AdministratorLoginPassword};MultipleActiveResultSets=False;Encrypt=True;TrustServerCertificate=False;Connection Timeout=30;";
Unfortunately, the Output<string> property values that are embedded within the connection string, never get resolved after observing the App Settings of an Azure function app.
In Pulumi, does a connection string property exist for an Azure SQL Server database?
The following code resolves a SQL connection string:
var connectionStringArgs = new ConnStringInfoArgs(){
Name = "MySqlDbConnection-dev",
Type = ConnectionStringType.SQLAzure,
ConnectionString = Output.Tuple(sqlServer.Name, sqlDatabase.Name, sqlServer.AdministratorLogin, sqlServer.AdministratorLoginPassword).Apply(t => {
(string server, string database, string username, string pwd) = t;
return
$"Server= tcp:{server}.database.windows.net;initial catalog={database};userID={username};password={pwd};Min Pool Size=0;Max Pool Size=30;Persist Security Info=true;";
}),
};
var connectionString = connectionStringArgs.ConnectionString;

Heroku Express + Postgres addon requires elevated privileges

[2018-05-16 08:27:12.979] [ERROR] server - Pipe
postgres://xnmcmqfirrdzkg:638d94fcd7aa5178e1054f0ff604613826033d5e8845fb42614c17a3d386823a#ec2-107-21-126-193.compute-1.amazonaws.com:5432/d5l5g60nk6l27a
requires elevated privileges
I'm trying to deploy server Express + Sequelize + Postgres on Heroku.
if (config.use_env_variable) {
var sequelize = new Sequelize(process.env[config.use_env_variable]);
}
else {
config.logging = function (str) {
log4js.getLogger("sequelize").info(str);
}
var sequelize = new Sequelize(config.database, config.username, config.password, config);
}
The data's credential is set at default. I tried searching on the internet but there is no one having the same problem with me.
I am. But I have not been able to figure it out yet. And the docs I have been provided are more than two years old.

"Not authorized on ___ to execute command" with mLab + MongoDB ^3.0

Connects without a hitch, but on insert() throws me this error.
var MongoClient = require('mongodb').MongoClient;
const assert = require('assert');
var url = 'mongodb://____:____#ds125565.mlab.com:25565/heroku_w268n9pj';
MongoClient.connect(url, function(err, client) {
assert.equal(null, err);
db = client.db('temp');
console.log("connected!");
const collection = db.collection('temp');
collection.insert([{
something: please
}
});
I saw some other answers regarding mLab accounts and credentials, but I just created a new admin account for this. Frustrating because it was working previously with v2.3.
When attempting to connect to an mlab database, you have to correctly specify the client. It's located at the end of your connection string, just after the final forward slash.
mlab_url = "mongodb://db_user_name:db_password#ds139725.mlab.com:39725/heroku_sd1fp182?retryWrites=false"
client = MongoClient(url)
db = client["heroku_sd1fp182"]
collection = db["coinHack"]
You may also get the error:
This MongoDB deployment does not support retryable writes. Please add retryWrites=false to your connection string.
Just add "?retryWrites=false" to your connection string, as shown above.

How can I authenticate any database with given username and password in Mongo Java Driver 2.13.0?

Previously I could use db.authenticate(String username, char[] password) method. With 2.13.0, how can I achieve this?
There is no replacement for db.authenticate(). The driver will use the credentials provided and make sure the connections are authenticated as they are created.
Based on this mongodb-user discussion the Java Driver team is open to discussions on what the real need for the db.authenticate(...) method.
Use
import com.mongodb.MongoCredential;
MongoCredential mongoCred =
MongoCredential.createMongoCRCredential(String username, String
dbName, char[] password);
and create mongoclient using mongocredentials
com.mongodb.MongoClient.MongoClient(List seeds, List
credentialsList, MongoClientOptions options)
We can have user-password based authentication for databases, in that case we need to provide authorization credentials like below for new version.
MongoCredential journaldevAuth = MongoCredential.createPlainCredential("pankaj", "journaldev", "pankaj123".toCharArray());
MongoCredential testAuth = MongoCredential.createPlainCredential("pankaj", "test", "pankaj123".toCharArray());
List<MongoCredential> auths = new ArrayList<MongoCredential>();
auths.add(journaldevAuth);
auths.add(testAuth);
ServerAddress serverAddress = new ServerAddress("localhost", 27017);
MongoClient mongo = new MongoClient(serverAddress, auths);
If you are using older versions, you need to provide authentication details after getting the DB object like below
MongoClient mongo = new MongoClient("localhost", 27017);
DB db = mongo.getDB("journaldev");
boolean auth = db.authenticate("pankaj", "pankaj123".toCharArray());

Unable to connecto to mongoDB

Trying to connect to mongoDB using MongoLab AddOn, I get the following error:
{ "$err" : "not authorized for query on myAppDB.Users", "code" : 13 }
when I run the following code:
private string connectionString = "mongodb://appharbor_93126661-e8b7-4986-958c-74e4ed8401e6:qj0boq192osh10rXXXXXXXX#ds027521.mongolab.com:27521/appharbor_93126661-e8b7-4986-958c-74e4ed8401e6";
private string dbName = "myAppDB";
private string userCollectionName = "Users";
private MongoCollection<User> GetUsersCollection()
{
MongoUrl url = new MongoUrl(connectionString);
MongoClient client = new MongoClient(url);
mongoServer = client.GetServer();
MongoDatabase database = mongoServer.GetDatabase(dbName);
MongoCollection<User> userCollection = database.GetCollection<User>(userCollectionName);
return userCollection;
}
I tried to connect from shell running the command:
mongo ds027521.mongolab.com:27521/appharbor_93126661-e8b7-4986-958c-74e4ed8401e6 -u appharbor_93126661-e8b7-4986-958c-74e4ed8401e6 -p qj0boq192osh10rXXXXXXXX
but also haven't managed to connect.
If anyone encountered similar issue or knows what is causing the problem I would be very thankful for any suggestion in prder to solve the problem.