Error connecting to mongodb container version 6.0.2 | amd64/mongo - mongodb

Iam able to connect mongo container versions 4.X and 5.X with the below code using amd64/mongo
auth := options.Credential{
AuthSource: admin,
Username: userName,
Password: pass,
}
opts := options.Client().ApplyURI(URI).SetAuth(auth).SetTLSConfig(&config)
client, err := mongo.Connect(ctx, opts)
But when i try to upgrade the container to version 6.0.2 from amd64/mongo
It fails with the below error
:Unable to connect to thedatabase :connection() error occurred during connection handshake: auth error: sasl conversation error: unable to authenticate using mechanism "SCRAM-SHA-1": (AuthenticationFailed) Authentication failed.
I believe by default it tries to pick SCRAM-SHA-1
do I need to set a mongoDB server param in my mongo run script file like below ?
--authenticationMechanisms=SCRAM-SHA-1
All i'm trying to do is connect to db and change the admin and db password using below code , not sure even if this is depreciated now in the mongo version 6.0.2
res := struct{ Ok int }{}
opts := options.RunCmd().SetReadPreference(readpref.Primary())
command := bson.D{{"updateUser", usrName}, {"pwd", pass}}
err = client.Database(db).RunCommand(context.TODO(), command, opts).Decode(&res)
not sure where am i making mistake , the error message is not straight forward . Can anyone help me here ?

So was able to resolve this , The problem was my scripts were using mongo but that is removed in the latest mongo version 6.0 , So i used mongosh, when I try to init the mongo container, that worked.
https://www.mongodb.com/docs/mongodb-shell/

Related

Can't connect to PostgreSQL database using pgx driver but can using terminal

From code
The code bellow outputs the following:
2022/06/21 16:01:07 Failed to connect to db: failed to connect to 'host=localhost user=postgres database=local': server error (FATAL: Ident authentication failed for user "postgres" (SQLSTATE 28000)) exit status 1
import (
"context"
"log"
"github.com/jackc/pgx/v4"
)
func main() {
dbCtx := context.Background()
db, err := pgx.Connect(
dbCtx,
"postgres://postgres:smashthestate#localhost:5432/local",
)
if err != nil {
log.Fatalf("Failed to connect to db: %v\n", err)
}
defer db.Close(dbCtx)
// do stuff with db...
}
From terminal
However, connection to db is possible from terminal. For example, this command if run with the same parameters (db name, user name, password) will give correct output:
psql -d local -U postgres -W -c 'select * from interest;'
Notes
Command works correctly even if sent by user that is neither root nor postgres.
Authentication method for local connection is set to trust inside pg_hba.conf:
local all all trust
So, what am I missing here? Why everything works fine from the command line but doesn't work from code?
Go's defaults are different from psql's. If no hostname is given, Go defaults to using localhost, while psql defaults to using the Unix domain sockets.
To specify a Unix domain socket to Go, you need to do it oddly to get it to survive URI validation:
postgres://postgres:smashthestate#:5432/local?host=%2Ftmp
Though the end might need to be more like ?host=%2Fvar%2Frun%2Fpostgresql, depending on how the server was configured.
But why are you specifying a password which is not needed? That is going to cause pointless confusion.

Couldn't connect mongodb with GoLang

I couln't connect mongodb with golang.
telnet mongoHost 27017 it is okey
ping mongoHost it is okey
MongoHost is my private host not docker
Mongodb version : 2.4.3
MongoDriver: 1.3.2
Go version: go version go1.14.1 darwin/amd64
Operation System: Mac
Here is my connection code
clientOptions := &options.ClientOptions{}
clientOptions.SetServerSelectionTimeout(4 * time.Second)
clientOptions.SetDirect(true)
clientOptions.SetAppName("tests")
clientOptions.SetHosts([]string{"mongoHost:27017"})
clientOptions.SetReadPreference(readpref.Secondary())
client, err := mongo.NewClient(clientOptions)
if err != nil {
log.Fatal(err)
}
ctx, _ := context.WithTimeout(context.Background(), 5*time.Second)
err = client.Connect(ctx)
if err != nil {
log.Fatal(err)
}
I changed timeout but It doesn't changed connection every time I got this error:
2020/04/12 14:06:19 server selection error: server selection timeout, current topology: { Type: Unknown, Servers: [{ Addr: mongoHost:27017, Type: Standalone, State: Connected, Average RTT: 13849652 }, ] }
The client settings that you've set are contradictory. Either you're establishing a direct connection to a single replica set member, or establishing a connection to a replica set with read preference secondary. I would suggest to change it to something simple:
client, err := mongo.NewClient(options.Client().ApplyURI("mongodb://mongoHost:27017/?replicaSet=FOOBAR"))
See also mongo-driver: Connect to see various code examples on how to connect.
Mongodb version : 2.4.3
Please upgrade your MongoDB server version. The least compatible version for MongoDB Go driver version is MongoDB server version 2.6. See MongoDB Compatibility chart for more information.
MongoDB v2.4 was released in 2013 (7 years ago), currently version 4.2. I would strongly recommend to upgrade, please see Upgrade MongoDB to v2.6 then follow through the upgrades until the most recent versions that satisfy your requirements.
Go mongo driver doesn't support Mongo 2.4
https://jira.mongodb.org/browse/GODRIVER-1571

Go postgres connection SSL not enabled

I'm trying to connect to my localhost postgresql server without SSL and I'm getting this error:
pq: SSL is not enabled on the server
That's fine, I know how to fix it:
type App struct {
Router *mux.Router
DB *sql.DB
}
func (a *App) Initialize(dbname string) {
connectionString := fmt.Sprintf("dbname=%s sslmode=disable", dbname)
var err error
a.DB, err = sql.Open("postgres", connectionString)
if err != nil {
log.Fatal(err)
}
defer a.DB.Close()
}
However I'm still getting the error!
I was able to recreate your error with a fresh install of postgres. While the error output was
pq: SSL is not enabled on the server
the real error was not having any databases created. To create a testdb let's run
createdb testdb
in your terminal with postgres already running in the background.
I think the answer comes more just to assign a parameter flag to your Postgres connection string url.
I was connecting to an AWS RDS by URL and I've just change to localhost on 5432 default port.
http://127.0.0.1:5432?sslmode=disable
or
http://localhost:5432?sslmode=disable
I stumbled upon this trying to connect to Postgresql server from my Go program with wrong password.

Play-ReactiveMongo Authentication Failed with MongoDB 3

I have problem using play-reactivemongo to connect to MongoDB 3 and encountered the "'not authorized for query on XXX' (code = 13)" problem. The program ran perfectly ok when MongoDB was started without --auth.
The database version should be 3.0.4, as shown in the log.
2015-10-24T15:58:09.868+0800 I CONTROL [initandlisten] db version v3.0.4
I have used version 0.11.7 play2-reactivemongo plugin.
libraryDependencies ++= Seq(
"org.reactivemongo" %% "play2-reactivemongo" % "0.11.7.play24",
I have specified the authMode in the mongodb.uri like this.
mongodb.uri = "mongodb://postsUser:12345678#localhost:27017/posts?authMode=?authMode=scram-sha1&authSource=posts&rm.tcpNoDelay=true"
I found the following error in mongodb.log.
2015-10-24T16:41:24.977+0800 I ACCESS [conn105] Failed to authenticate postsUser#posts with mechanism MONGODB-CR: AuthenticationFailed MONGODB-CR credentials missing in the user document
Why MONGODB-CR is used instead of SCRAM-SHA-1? Suppose SCRAM-SHA-1 should be default, right?
Appending following to the connection string works for me
authMode=scram-sha1
so the final connection string is
mongodb://user:pass#ipaddress:27017/litmus?3treadPreference=primary&authMode=scram-sha1
I also have nodejs script connecting to same db, interestingly it connects perfectly without appending authMode=scram-sha1 flag.
Seems to be unhandled case in reactive mongo
Sorry, I made a typo in the mongodb.uri
authMode=?authMode=scram-sha1
and causing the DB to authenticate with mongodb-cr. However, I still got the "not authorized to query" error on first access to DB and subsequent db access were fine. In mongodb.log, I could see the following.
2015-10-25T01:28:45.242+0800 I QUERY [conn33] assertion 13 not authorized for query on posts.posts ns:posts.posts query:{}
2015-10-25T01:28:45.247+0800 I ACCESS [conn33] Successfully authenticated as principal postsUser on posts
I better start another question tomorrow.

Error in connecting to mongodb (mongodb-linux-x86_64-2.8.0-rc5) : auth failed, when starting nodejs appserver

I am new to mongodb, I am able to get my nodejs appserver started with mongodb 2.4 and 2.6. I have a requirement to test the latest mongodb, which is 2.8 with our app. I am able to start the mongodb 2.8 but when I try to start my nodejs appserver, I get following error.
2015-01-16T18:43:51.115Z <warn> globals.js:45 () MONGO URI = mongodb://myuser:password#localhost:27017/test
2015-01-16T18:43:51.178Z <error> globals.js:45 () #### Error in connecting to mongodb
2015-01-16T18:43:51.180Z <error> globals.js:45 () { [MongoError: auth failed] name: 'MongoError', ok: 0, errmsg: 'auth failed', code: 18 }
2015-01-16T18:43:51.182Z <error> globals.js:45 () MongoError: auth failed
.........
Also, I have created myuser in the test db and authenticated it successfully. Please help, I have tried all possible ways and with no success. Thank you very much in advance. (my environment is Ubantu Linux 14.04, virtualbox) One more thing that I forgot to mention is that I am using wiredTiger storageEngine. I tried with MMAPv1 as well and same result.
I had the same issue. It worked for me on Mac, but on Linux I hit this failure until I upgraded module mongoose from 3.6.x to 3.8.x (3.8.25). That solved the problem for me. You might consider doing the same, at least to rule out this problem.