Can't connect Go api to MongoDB database | ReplicaSetNoPrimary error - mongodb

Here is the error showing after "go run main.go"
❯ go run main.go
2022/08/10 20:07:51 server selection error: context deadline exceeded, current topology: { Type: ReplicaSetNoPrimary, Servers: [{ Addr: ac-nldr28t-shard-00-00.pfel30m.mongodb.net:27017, Type: Unknown, Last error: connection() error occurred during connection handshake: connection(ac-nldr28t-shard-00-00.pfel30m.mongodb.net:27017[-28]) socket was unexpectedly closed: EOF }, { Addr: ac-nldr28t-shard-00-01.pfel30m.mongodb.net:27017, Type: Unknown, Last error: connection() error occurred during connection handshake: connection(ac-nldr28t-shard-00-01.pfel30m.mongodb.net:27017[-27]) socket was unexpectedly closed: EOF }, { Addr: ac-nldr28t-shard-00-02.pfel30m.mongodb.net:27017, Type: Unknown, Last error: connection() error occurred during connection handshake: connection(ac-nldr28t-shard-00-02.pfel30m.mongodb.net:27017[-26]) socket was unexpectedly closed: EOF }, ] }
exit status 1

Related

Error: Introspection error Error querying the database: Server selection timeout: No available servers. Topology

Error: Introspection error
Error querying the database: Server selection timeout: No available servers. Topology: { Type: Unknown, Servers: [ { Address: <host>:27017, Type: Unknown, Error: failed to lookup address information: nodename nor servname provided, or not known }, ] }
// This is your Prisma schema file,
// learn more about it in the docs: https://pris.ly/d/prisma-schema
generator client {
provider = "prisma-client-js"
}
datasource db {
provider = "mongodb"
url = env("DATABASE_URL")
}
DATABASE_URL="mongodb://<username>:<password>#<host>:27017/<database_name>"
npx prisma db pull

Cannot connect to mongodb Error error occurred during connection

im trying to connect to my mongodb from my api written in golang
but every time i run my app it gives me this error:
2022/12/06 19:09:25 server selection error: server selection timeout, current topology: { Type: ReplicaSetNoPrimary, Servers: [{ Addr: ac-tqorttq-shard-00-02.atpezds.mongodb.net:27017, Type: Unknown, Last error: connection() error occurred during connection handshake: dial tcp 52.28.222.2:27017: i/o timeout }, { Addr: ac-tqorttq-shard-00-00.atpezds.mongodb.net:27017, Type: Unknown, Last error: connection() error occurred during connection handshake: dial tcp 18.197.37.129:27017: i/o timeout }, { Addr: ac-tqorttq-shard-00-01.atpezds.mongodb.net:27017, Type: Unknown, Last error: connection() error occurred during connection handshake: dial tcp 18.195.134.220:27017: i/o timeout }, ] }
my code:
func init() {
clientOptions := options.Client().ApplyURI(configs.GetEnv("MONGOURI"))
client, err := mongo.Connect(context.TODO(), clientOptions)
checkErr(err)
err = client.Ping(context.TODO(), nil)
checkErr(err)
fmt.Println("connected to database successfully")
notesCollection = openCollection("notes_collection")
userCollection = openCollection("user_collection")
}
i tried to remove the "+srv" from the connection string then
it gives me this error:
2022/12/06 19:16:46 server selection error: server selection timeout, current topology: { Type: Unknown, Servers: [{ Addr: cluster0.atpezds.mongodb.net:27017, Type: Unknown, Last error: connection() error occurred during connection handshake: dial tcp: lookup cluster0.atpezds.mongodb.net: No address associated with hostname }, ] }
hope you guys help me
and thanks
func main() {
client, err := mongo.NewClient(options.Client().ApplyURI("<ATLAS_URI_HERE>"))
if err != nil {
log.Fatal(err)
}
ctx, _ := context.WithTimeout(context.Background(), 10*time.Second)
err = client.Connect(ctx)
if err != nil {
log.Fatal(err)
}
defer client.Disconnect(ctx)
err = client.Ping(ctx, readpref.Primary())
if err != nil {
log.Fatal(err)
}
databases, err := client.ListDatabaseNames(ctx, bson.M{})
if err != nil {
log.Fatal(err)
}
fmt.Println(databases)
}
Check that you are using the correct username and password for your database user, and that you are connecting to the correct database deployment. Check that you are specifying the correct authSource database in your connection string.
Hope this helps!!

Goland mongodb "connection refused" error handling

Good day!
I have a service which is running in loop like this:
for {
select {
case <-ctx.Done():
return fmt.Errorf("context error: %w", ctx.Err())
default:
}
task, err := provider.Next(ctx)
if !errors.Is(err, ErrNotFound) {
return fmt.Errorf("next: %w", err)
}
// some work todo
}
Sometimes something happens with my mongodb and I get an error like
connection() : connection(mongorc:27017[-48]) incomplete read of message header: context deadline exceeded"
So I have to check
errors.Is(err, context.DeadlineExceeded).
as well as ErrNotFound.
But If I dont put timeout to mongo requests I get error like
server selection timeout, current topology: { Type: Single, Servers: [{ Addr: localhost:27017, Type: Unknown, State: Connected, Average RTT: 0, Last error: connection() : dial tcp 127.0.0.1:27017: connect: connection refused }, ] }
And I have no idea how to check it - I didnt find any "mongo.Err..." that is up to this error. Is there a way to check it in the code?

Why can't mongomirror initiate connection to the destination source? [server selection timeout]

I want to migrate a database from one cluster to another. The source and destination are withing the same VPC, but I keep getting Unknown, Last error: connection() error occured during connection handshake error.
mongomirror --host "Replicaset-Name/host-0.domain.com:27017,host-1.domain.com:27017,host-2.domain.com:27017" \
--username "<username>" \
--password "<password>" \
--authenticationDatabase "admin" \
--destination "Replicaset-Name/mongodb-0.mongodb-svc.mongodb.svc.cluster.local:27017,mongodb-1.mongodb-svc.mongodb.svc.cluster.local:27017,mongodb-2.mongodb-svc.svc.cluster.local:27017" \
--destinationUsername "<username>" \
--destinationPassword "<password>"
Output:
mongomirror version: 0.12.5
git version: 6e5a5489944845758420e8762dd5e5a89d2e8654
Go version: go1.16.9
os: linux
arch: amd64
compiler: gc
2022-08-16T16:53:29.353+0000 Source isMaster output: {IsMaster:true MinWireVersion:0 MaxWireVersion:7 Hosts:[host-0.domain.com:27017 host-1.domain.com:27017 host-2.domain.com:27017}
2022-08-16T16:53:29.353+0000 Source buildInfo output: {Version:4.0.12 VersionArray:[4 0 12 0] GitVersion:5776e3cbf9e7afe86e6b29e22520ffb6766e95d4 OpenSSLVersion: SysInfo: Bits:64 Debug:false MaxObjectSize:16777216}
2022-08-16T16:55:29.356+0000 Error initializing mongomirror: could not initialize destination connection: could not connect to server: server selection error: server selection timeout, current topology: { Type: ReplicaSetNoPrimary, Servers: [{ Addr: mongo-1.mongo-svc.mongodb.svc.cluster.local:27017, Type: Unknown, Last error: connection() error occured during connection handshake: EOF }, { Addr: mongo-0.mongo-svc.mongodb.svc.cluster.local:27017, Type: Unknown, Last error: connection() error occured during connection handshake: EOF }, { Addr: mongo-2.mongo-svc.mongodb.svc.cluster.local:27017, Type: Unknown, Last error: connection() error occured during connection handshake: EOF }, ] }
I've tested the connection with mongosh. It has no issues while establishing the connection.
mongosh --host mongodb-1.mongodb-svc.mongodb.svc.cluster.local:27017 --username <username>
So what might I have missed here?
Thanks!

Problem on virtualize hyperledger network on multiple hosts

I have followed and finished the tutorial
https://medium.com/#malliksarvepalli/hyperledger-fabric-1-2-on-multiple-hosts-using-docker-swarm-and-compose-11c13635e69e
and I have 3 hosts connected and all services are up. I had the correct result when I run the ./script.sh in the PC2 host.
Now I am following the next tutorial which is:
https://medium.com/#malliksarvepalli/hyperledger-explorer-with-fabric-1-2-running-on-multiple-hosts-89c5af691b7e
Can anyone please enlighten me in this tutorial? I have figured out that I should create a new host with ubuntu 16.04 and install the following prerequites:
nodejs 8.11.x
PostgreSQL 9.5 or greater
Jq
Am I right?
And in the exploreconfig.json I updated postgres variables:
host:192.168.1.136(4th's vm ip),
port:5432,
username:postgres,
password:psql,
database:fabric
Are they correct?
I have also modified Orderer, Org1 [peer0 & peer1), Org2(peer0 & peer1) IP address in config.json file with the ips of first three Vms where the network is up and running.
And follow the rest instructions and tests but when I run the command ./start.sh I have logs in console which are:
false 'ssl-certs' '/home/database/blockchain-explorer/ssl-certs'
postgres://christy:christy#192.168.1.136:5432/fabric
error when connecting to db: { Error: connect ECONNREFUSED 192.168.1.136:5432
at Object._errnoException (util.js:992:11)
at _exceptionWithHostPort (util.js:1014:20)
at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1186:14)
code: 'ECONNREFUSED',
errno: 'ECONNREFUSED',
syscall: 'connect',
address: '192.168.1.136',
port: 5432 }
******* Initialization started for hyperledger fabric platform ******, {
'network-1':
{ version: '1.0',
clients: { 'client-1': [Object] },
channels: { mychannel: [Object] },
organizations: { Org1MSP: [Object], Org2MSP: [Object], OrdererMSP:
[Object] },
peers:
{ 'peer0.org1.ntua.com': [Object],
'peer1.org1.ntua.com': [Object],
'peer0.org2.ntua.com': [Object],
'peer1.org2.ntua.com': [Object] },
orderers: { 'orderer.ntua.com': [Object] } },
'network-2': {} }
client_configs.name undefined client_configs.profile undefined
FabricUtils.createFabricClient
<<<<<<<<<<<<<<<<<<<<<<<<<< Explorer Error >>>>>>>>>>>>>>>>>>>>>
Error : [ 'Invalid platform configuration, Please check the log' ]
error when connecting to db: TypeError: Cannot read property 'on' of
undefined
at Timeout.handleDisconnect [as _onTimeout] (/home/database/blockchain-
explorer/app/persistence/postgreSQL/PgService.js:68:16)
at ontimeout (timers.js:498:11)
at tryOnTimeout (timers.js:323:5)
at Timer.listOnTimeout (timers.js:290:5)
<<<<<<<<<<<<<<<<<<<<<<<<<< Explorer Error >>>>>>>>>>>>>>>>>>>>>
TypeError: "callback" argument must be a function
at setTimeout (timers.js:450:11)
at Timeout.handleDisconnect [as _onTimeout] (/home/database/blockchain-
explorer/app/persistence/postgreSQL/PgService.js:85:5)
at ontimeout (timers.js:498:11)
at tryOnTimeout (timers.js:323:5)
at Timer.listOnTimeout (timers.js:290:5)
Received kill signal, shutting down gracefully
Closed out connections
false 'ssl-certs' '/home/database/blockchain-explorer/ssl-certs'
postgres://christy:christy#192.168.1.136:5432/fabric
error when connecting to db: { Error: connect ECONNREFUSED 192.168.1.136:5432
at Object._errnoException (util.js:992:11)
at _exceptionWithHostPort (util.js:1014:20)
at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1186:14)
code: 'ECONNREFUSED',
errno: 'ECONNREFUSED',
syscall: 'connect',
address: '192.168.1.136',
port: 5432 }
******* Initialization started for hyperledger fabric platform ******,
{
'network-1':
{ version: '1.0',
clients: { 'client-1': [Object] },
channels: { mychannel: [Object] },
organizations: { Org1MSP: [Object], Org2MSP: [Object], OrdererMSP:
[Object] },
peers:
{ 'peer0.org1.ntua.com': [Object],
'peer1.org1.ntua.com': [Object],
'peer0.org2.ntua.com': [Object],
'peer1.org2.ntua.com': [Object] },
orderers: { 'orderer.ntua.com': [Object] } },
'network-2': {} }
client_configs.name undefined client_configs.profile undefined
FabricUtils.createFabricClient
<<<<<<<<<<<<<<<<<<<<<<<<<< Explorer Error >>>>>>>>>>>>>>>>>>>>>
Error : [ 'Invalid platform configuration, Please check the log' ]
error when connecting to db: TypeError: Cannot read property 'on' of
undefined
at Timeout.handleDisconnect [as _onTimeout] (/home/database/blockchain-
explorer/app/persistence/postgreSQL/PgService.js:68:16)
at ontimeout (timers.js:498:11)
at tryOnTimeout (timers.js:323:5)
at Timer.listOnTimeout (timers.js:290:5)
<<<<<<<<<<<<<<<<<<<<<<<<<< Explorer Error >>>>>>>>>>>>>>>>>>>>>
TypeError: "callback" argument must be a function
at setTimeout (timers.js:450:11)
at Timeout.handleDisconnect [as _onTimeout] (/home/database/blockchain-
explorer/app/persistence/postgreSQL/PgService.js:85:5)
at ontimeout (timers.js:498:11)
at tryOnTimeout (timers.js:323:5)
at Timer.listOnTimeout (timers.js:290:5)
Received kill signal, shutting down gracefully
Received kill signal, shutting down gracefully
Closed out connections
If the other logs are also needed please let me know. Any help will be helpful. Thanks a lot.
I can give you some aspects to be checked on your environment.
First you need to do check if you are able to access postgresql DB(4th's vm) from the vm where you are trying to launch Hyperledger Explorer. If not, you need to change your postgres configuration (postgresql.conf/pg_hba.conf) for enabling external access, I think.
$ sudo -u postgres psql -h 192.168.1.136 -d fabric -c 'table peer'
Second, please confirm that you did run ./createdb.sh on 4th VM. If you changed database name, you also need to align the script
$ cd app/persistence/fabric/postgreSQL/db/
$ ./createdb.sh