Couldn't connect mongodb with GoLang - mongodb

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

Related

Error connecting to mongodb container version 6.0.2 | amd64/mongo

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/

How to connect Google App Engine (Flex) Go app to Google Cloud Postgres instance

I'm building an app in Go and am using Google App Engine for deployment.
I have set up a PostgreSQL instance on Google Cloud, have enabled the API and have successfully connected to it using the SQL proxy from my local machine, both from a local PSequel client and from my app.
When I do gcloud app deploy, however, I get this error:
ERROR: (gcloud.app.deploy) Error Response: [9]
Application startup error:
panic: dial unix /cloudsql/sapling:europe-west1:sapling/.s.PGSQL.5432: connect: no such file or directory
I have tried downgrading the Postgres instance from version 11 Beta to 9.6. Have also tried using this driver https://github.com/broady/gae-postgres, which seemed promising but I couldn't get working for the flex environment.
app.yaml
runtime: go
#Here we select the AppEngine Flex environment which performs a lot of the backend preparation for us including the ability to scale give the demand
env: flex
env_variables:
POSTGRES_CONNECTION: "user=postgres password=thisisntmypwd dbname=postgres host=/cloudsql/sapling:europe-west1:sapling"
manual_scaling:
instances: 1
#Select resource size
resources:
cpu: 1
memory_gb: 0.5
disk_size_gb: 10
beta_settings:
cloud_sql_instances: sapling:europe-west1:sapling=tcp:5432
config/db.go
package config
import (
"database/sql"
"fmt"
_ "github.com/lib/pq"
"os"
)
var Db *sql.DB
//Set up connection to the database
func init() {
var err error
datastoreName := os.Getenv("POSTGRES_CONNECTION")
Db, err = sql.Open(
"postgres",
datastoreName,
)
if err != nil {
panic(err)
}
err = Db.Ping()
if err != nil {
panic(err)
}
fmt.Println("Connected to database")
}
I replicated this issue in my testing environment and found that the error you are getting is due to the beta_settings having the port specified. I tried it with the =tcp:5432 at the end, and it gave me the same error, then I tried it without it and it got deployed without any issues.

How do you connect to MongoDB Atlas with the official mongo-go-driver [duplicate]

This question already has answers here:
How to use new URL from mongodb 3.6 to connect from golang
(2 answers)
Closed 3 years ago.
I'm looking at the tutorial offered in conjunction with the release of the official mongo-go-driver and the connection example uses a MongoDB server on localhost
// Set client options
clientOptions := options.Client().ApplyURI("mongodb://localhost:27017")
However, the new hosted MongoDB service Atlas requires username and password to login. The connection string takes the format
mongodb://[username:password#]host1[/[database][?options]]
but there is no Golang example in the driver examples for Atlas.
So I'm wondering, what is the best way to log into Atlas without hard coding a password into a source file that will be posted on Github?
I am hosting my test Atlas cluster on AWS so I wanted to have similar credential management to the AWS process. From the AWS credentials page:
The default provider chain looks for credentials in the following order:
Environment variables.
Shared credentials file.
If your application is running on an Amazon EC2 instance, IAM role for Amazon EC2.
Therefore, I wanted to implement the environment veriable for my simple login to Atlas example. Code below assumes that the following line has been issued at the command line
export MONGO_PW='<your Atlas admin user password>'
Then the following program will verify your connection
package main
import (
"context"
"fmt"
"os"
"go.mongodb.org/mongo-driver/mongo"
"go.mongodb.org/mongo-driver/mongo/options"
)
var username = "<username>"
var host1 = "<atlas host>" // of the form foo.mongodb.net
func main() {
ctx := context.TODO()
pw, ok := os.LookupEnv("MONGO_PW")
if !ok {
fmt.Println("error: unable to find MONGO_PW in the environment")
os.Exit(1)
}
mongoURI := fmt.Sprintf("mongodb+srv://%s:%s#%s", username, pw, host1)
fmt.Println("connection string is:", mongoURI)
// Set client options and connect
clientOptions := options.Client().ApplyURI(mongoURI)
client, err := mongo.Connect(ctx, clientOptions)
if err != nil {
fmt.Println(err)
os.Exit(1)
}
err = client.Ping(ctx, nil)
if err != nil {
fmt.Println(err)
os.Exit(1)
}
fmt.Println("Connected to MongoDB!")
}
From here the rest of the tutorial linked in my original question goes smoothly.

How to connect to Google CloudSQL PostgresSQL

I tried to connect to Google CloudSQL PostgresSQL using Gorm golang and seems like it is not working.
Here's the code
func InitDB() *gorm.DB {
psqlInfo := fmt.Sprintf("host=%s port=%s user=%s password=%s dbname=%s sslmode=disable", os.Getenv("DB_HOST"), os.Getenv("DB_PORT"), os.Getenv("DB_USER"), os.Getenv("DB_PASSWORD"), os.Getenv("DB_NAME"))
db, err := gorm.Open("postgres", psqlInfo)
if err != nil {
fmt.Println("Failed to connect to the Database")
}
fmt.Println("Connected to the Database")
DB = db
return DB
}
If im using the localhost config everything works fine. See my .env file for cloudSQL config
DB_HOST=trader-234509:us-central1:testv1
DB_PORT=5432
DB_USER=username
DB_NAME=testv1
DB_PASSWORD=
Error is saying
dial tcp: lookup trader-234509:us-central1:testv1: no such host
panic: runtime error: invalid memory address or nil pointer dereference
[signal SIGSEGV: segmentation violation code=0x1 addr=0x40 pc=0x164681d]
My local config (This one works fine)
DB_HOST=localhost
DB_PORT=5432
DB_USER=username
DB_NAME=test
DB_PASSWORD=
Did i do anything wrong?
Here's roughly how I connect from AppEngine:
import (
_ "github.com/GoogleCloudPlatform/cloudsql-proxy/proxy/dialers/postgres"
"fmt"
"gorm.io/driver/postgres"
"gorm.io/gorm"
)
func New() (*gorm.DB, error) {
user := "theuser"
password := "apassword"
dbHost := "my-project:the-region:my-db-instance-name"
databaseName := "mydbname"
connString := fmt.Sprintf("host=%s user=%s dbname=%s sslmode=disable password=%s", dbHost, user, databaseName, password)
return gorm.Open(postgres.New(postgres.Config{
DriverName: "cloudsqlpostgres",
DSN: connString,
}))
}
Cloud SQL doesn't support direct connection to instance name for 3rd party application yet, for details: https://cloud.google.com/sql/docs/mysql/connect-external-app
Based on my experience, there are 2 solutions:
As per above instruction, you can setup a Cloud Proxy following the steps and the connections flow would be: Golang app -> Cloud Proxy -> Cloud SQL
This approach is flexible and you're able to control the connection using firewall.
But you have to spend extra $$ to maintain the server and this cloud proxy instance, I've also heard that the cloud proxy may have some performance issues but I don't have exact evidence so far
Assign a Private IP to the Cloud SQL instance, refer to https://cloud.google.com/sql/docs/mysql/private-ip, so that you application can access the DB directly using this IP.
The trade-off is obvious that the app must be in the same network of the project, but this is a much more convenient approach, esp. all your applications are hosted in the same network
I didn't try out the public IP access approach as Cloud Proxy is just what I need if remote network connection is needed
In short, you need to deploy your code in a VM or other Google Managed service using option 2, or setup the cloud proxy to support your local debugging for option 1
Hope it helps

Delphi mORMot No connection could be made because the target machine actively refused it

I test mORMot component.
I compiled standart demo "28 - Simple RESTful ORM Server"
Run it and get an eror.
Code:
var
aModel: TSQLModel;
aProps: TSQLDBConnectionProperties;
aRestServer: TSQLRestServerDB;
aHttpServer: TSQLHttpServer;
begin
// set logging abilities
SQLite3Log.Family.Level := LOG_VERBOSE;
//SQLite3Log.Family.EchoToConsole := LOG_VERBOSE;
SQLite3Log.Family.PerThreadLog := ptIdentifiedInOnFile;
// ODBC driver e.g. from http://ftp.postgresql.org/pub/odbc/versions/msi
aProps := TODBCConnectionProperties.Create('','Driver=PostgreSQL Unicode'+
{$ifdef CPU64}'(x64)'+{$endif}';Database=postgres;'+
'Server=127.0.0.1;Port=5433;UID=postgres;Pwd=postgres','','');
//readln;
try
// get the shared data model
aModel := DataModel;
// use PostgreSQL database for all tables
VirtualTableExternalRegisterAll(aModel,aProps);
try
// create the main mORMot server
aRestServer := TSQLRestServerDB.Create(aModel,':memory:',false); // authentication=false
try
// optionally execute all PostgreSQL requests in a single thread
aRestServer.AcquireExecutionMode[execORMGet] := amBackgroundORMSharedThread;
aRestServer.AcquireExecutionMode[execORMWrite] := amBackgroundORMSharedThread;
// create tables or fields if missing
aRestServer.CreateMissingTables;
// serve aRestServer data over HTTP
aHttpServer := TSQLHttpServer.Create(SERVER_PORT,[aRestServer],'+',useHttpApiRegisteringURI);
try
aHttpServer.AccessControlAllowOrigin := '*'; // allow cross-site AJAX queries
writeln('Background server is running.'#10);
write('Press [Enter] to close the server.');
readln;
finally
aHttpServer.Free;
end;
finally
aRestServer.Free;
end;
finally
aModel.Free;
end;
finally
aProps.Free;
end;
end.
error
{"Message":"TODBCLib error: [08001] Could not connect to the server;\nNo connection could be made because the target machine actively refused it.\r\n [127.0.0.1:5433]
How to clear it.
I just tested it with Delphi Seattle 10 and the ODBC driver from http://ftp.postgresql.org/pub/odbc/versions/msi/psqlodbc_09_03_0400.zip - with no problem.
Ensure your PostgreSQL server has been defined to run on the same port as expected by the source. Edit the 5433 value into 5432 if you used the default port number.
Being paranoid, I try to always change the default port, which reduces network scan attacks (at least from fast scan). I never use port 22 for ssh, nor 5432 for PostgreSQL. Sorry for the inconvenience.