Unable to use mongoexport on remote server - mongodb

I am new with MongoDB and I have a test account on mongogb account. I am trying to export the database I have remotely and this has given me a lot of headaches. Can someone help me out on this please?
mongoexport \
--host="myDbUser:DataBas3Pwd#mongodb://cluster0.raiy3.mongodb.net" \
-d socialDeveloper \
-c posts \
--out social.json
When I run the code on my command line it returns the response:
2021-03-01T01:13:26.240+0100 could not connect to server: server selection error: server selection timeout, current topology: { Type: Single, Servers: [{ Addr: cluster0.raiy3.mongodb.net:27017, Type: Unknown, State: Connected, Average RTT: 0, Last error: connection() : dial tcp: lookup cluster0.raiy3.mongodb.net: no such host }, ] }
And here is where I got the connection details from on my mongoDb account
WHat do I need to change please?

Try:
mongoexport 'mongodb+srv://myDbUser:DataBas3Pwd#cluster0.raiy3.mongodb.net' --db=socialDeveloper --collection=posts --out=social.json

Related

MongoDB Cannot run tests when building Docker image - Server selection error: server selection timeout

I'm running a mongo image in a container with this config:
version: '3'
services:
mongodb:
image: mongo
ports:
- '27017:27017'
environment:
- MONGO_INITDB_ROOT_USERNAME=user
- MONGO_INITDB_ROOT_PASSWORD=password
- MONGO_INITDB_DATABASE=microservices
volumes:
- ./docker-entrypoint-initdb.d/mongo-init.js:/docker-entrypoint-initdb.d/mongo-init.js:ro
And I have started to build microservices with golang and I want to start dockerizing those as well to speed up the development process. However when I try to run this Dockerfile below it panics at the run test command with the error:
database url: mongodb://user:password#127.0.0.1:27017/microservices
server selection error: server selection timeout, current topology: { Type: Unknown, Servers: [{ Addr: 127.0.0.1:27017, Type: Unknown, Last error: connection() error occurred during connection handshake: dial tcp 127.0.0.1:27017: connect: connection refused }, ] }
panic: server selection error: server selection timeout, current topology: { Type: Unknown, Servers: [{ Addr: 127.0.0.1:27017, Type: Unknown, Last error: connection() error occurred during connection handshake: dial tcp 127.0.0.1:27017: connect: connection refused }, ] }
The db connection works since when I try to run the test from the go files, they pass. There only seems to be any problem when I try to run them from the container when building it up.
FROM golang:1.18 as build
WORKDIR /go/src/app
COPY . .
RUN go mod download
RUN go vet -v /go/src/app/...
RUN go test -v /go/src/app/...
RUN CGO_ENABLED=0 go build -o /go/bin/app/authsvc /go/src/app/authentication/main.go
FROM gcr.io/distroless/static-debian11
COPY --from=build /go/bin/app/authsvc /
COPY --from=build /go/src/app/authentication/.env /
CMD ["/authsvc"]
Change database url
try mongodb
database url: mongodb://user:password#mongodb:27017/microservices
So first of all you must check your default network ip with
docker network ls
Result
After get network id and use this command :
docker inspect <your network id>
Inside result schema get your Gateway ip
"IPAM": {
"Driver": "default",
"Options": null,
"Config": [
{
"Subnet": "111.11.0.0/11",
"Gateway": "111.11.0.1"
}
]
}
and use that ip
database url: mongodb://user:password# < your Gateway ip > :27017/microservices

Unable to import json into mongo db atlas

I'm attempting to import json into an atlas db.
The attempt uses this code:
mongoimport --uri "mongodb://usr:pass#myurlname.uia8q.mongodb.net/paintura?retryWrites=true&w=majority" --collection products --file pathtojson
all parameters in the above are correct via the connect docs.
exact error is:
error connecting to host: could not connect to server: server selection error: server selection timeout, current topology: { Type: Single, Servers: [{ Addr: myurlname.uia8q.mongodb.net:27017, Type: Unknown, Last error: connection() error occured during connection handshake: dial tcp: lookup paintura.uia8q.mongodb.net: no such host }, ] }
For the user I have atlas admin selected so I assume it should have full privelege.
I also have cleared my ip in the ip list.
Any ideas what I'm missing?

Mongoexport auth error using mechanism "SCRAM-SHA-1"

I have taken over undocumented Mongo 4.4.8 cluster (PSA). I am trying to tidy it up and test thouroughly.
An original connection string:
MONGODB_URI=mongodb://${USER}:${PASS}#10.0.0.3:27017,10.0.0.6:27017,10.0.0.2:27017/bud?replicaSet=bud-replica&authSource=admin
I have enabled localhost and socket connection. I can log in from cmdline with
mongo -u ${USER} -p ${PASS}
MongoDB shell version v4.4.8
connecting to: mongodb://127.0.0.1:27017/?compressors=disabled&gssapiServiceName=mongodb
Implicit session: session { "id" : UUID("492e331b-417e-458a-83c7-9db6eaae0869") }
MongoDB server version: 4.4.8
I can switch db to bud and perform the queries. But if I run just
mongo
then the authentication with the same credentials does not work:
bud-replica:PRIMARY> db.auth('admin','admin');
Error: Authentication failed.
0
I tried to search for users but shows there arent any:
bud-replica:PRIMARY> db.getUsers()
[ ]
bud-replica:PRIMARY> use bud
switched to db bud
bud-replica:PRIMARY> db.getUsers()
[ ]
This is mongod.conf security part:
security:
authorization: enabled
keyFile: "/etc/bud-rs"
Finally I need to export my data before doing experiments. Though the cmd line interface looks similar, mongoexport cannot fetch the data, regardless I set user/password or skip these arguments.
mongoexport -h localhost --db=bud -u ${USER} -p ${PASS} -c=accidents --jsonArray > accidents.json
2021-08-25T19:30:30.631+0200 could not connect to server: connection() error occured during connection handshake: auth error: sasl conversation error: unable to authenticate using mechanism "SCRAM-SHA-1": (AuthenticationFailed) Authentication failed.
mongoexport -h localhost --db=bud -u ${USER} -p ${PASS} -c=accidents --jsonArray --authenticationDatabase “admin” > accidents.json
2021-08-25T19:36:18.738+0200 could not connect to server: connection() error occured during connection handshake: auth error: sasl conversation error: unable to authenticate using mechanism "SCRAM-SHA-1": (AuthenticationFailed) Authentication failed.
root#10:~# mongoexport -h localhost --db=bud -u ${USER} -p ${PASS} -c=accidents --jsonArray --authenticationDatabase “bud” > accidents.json
2021-08-25T19:38:21.174+0200 could not connect to server: connection() error occured during connection handshake: auth error: sasl conversation error: unable to authenticate using mechanism "SCRAM-SHA-1": (AuthenticationFailed) Authentication failed.
I am really confused and I failed to find a solution on Google or SO.
Second relevant question:
If I need to create new user, shall I do it on all replicas or it is automatically synchronized?
1st update
This is the workaround, but my questions are still valid. I want to understand.
root#10:~# mongoexport --db=bud -u ${USER} -p ${PASS} -c=accidents --jsonArray "mongodb://admin:admin#10.0.0.3:27017/bud?authSource=admin" > accidents.json
2021-08-25T20:46:54.777+0200 connected to: mongodb://[**REDACTED**]#10.0.0.3:27017/bud?authSource=admin
2021-08-25T20:46:55.778+0200 [........................] bud.accidents 0/4379 (0.0%)
2021-08-25T20:46:56.497+0200 [########################] bud.accidents 4379/4379 (100.0%)
2021-08-25T20:46:56.497+0200 exported 4379 records
2nd update
bud-replica:PRIMARY> use admin
bud-replica:PRIMARY> show collections
system.keys
system.users
system.version
bud-replica:PRIMARY> db.system.users.find()
{ "_id" : "admin.admin", "userId" : UUID("769e4f5c-6f46-4153-857e-47d7d8730066"), "user" : "admin", "db" : "admin", "credentials" : { "SCRAM-SHA-1" : { "iterationCount" : 10000, "salt" : "32/AP4019eome36j8n
The user credential was created in the admin database.
When connecting with the mongo shell, switch with use admin before running db.auth
The mongoexport command that worked used authSource=admin in the connection string.
Add --authenticationDatabase=admin to the other command line to direct it to use the admin database for auth as well.
whole example command as below worked for me.
Mongodb version: 5.x.x, also for Mongodb version: 8.x.x
mongodump --authenticationDatabase=admin --uri mongodb://username:password#mongodb-host/db-name?ssl=false&authSource=admin

Error importing file in MongoDB Cluster error validating settings: incompatible options: --file and positional argument(s)

I am trying to use mongoimport my existing .json file into the cloud cluster. But I keep getting this error
error validating settings: incompatible options: --file and positional argument(s)
I have been on the cat with mongo for an hour and they have been no help to solve my issue. I think it has to do with --file drivers.json and it finding that file.
./mongoimport --host muleapp-shard-00-02-bxny3.mongodb.net:27017 --ssl --username srusso -password MuleApp2019 --authenticationDatabase admin --db Drivers --collection driver --type json --file drivers.json
error validating settings: incompatible options: --file and positional argument(s)
now I am getting a time out error:
2019-11-08T14:12:38.533-0500 error connecting to host: could not connect to server: server selection error: server selection timeout
current topology: Type: Single
Servers:
Addr: muleapp-shard-00-02-bxny3.mongodb.net:27017, Type: Unknown, State: Connected, Average RTT: 0, Last error: connection() : dial tcp 3.229.7.228:27017: i/o timeout
The first error could be due to long option password being provided using - rather than --. So the option could be provided as either of --password MuleApp2019 or -p MuleApp2019.

using mongdb in docker with Go

i tried to run MongoDB in docker-compose and connect my Go server with MongoDB in docker seem not to work for me, thought i already use mongodb:MyContanerName:27017
when i run my go server and then i tired to request on my client, the error something like this
2019/09/08 14:10:37 Unexpected error: rpc error: code = Internal desc = internal error: server selection error: server selection timeout
current topology: Type: Unknown
Servers:
Addr: mongo:27017, Type: Unknown, State: Connected, Average RTT: 0, Last error: connection(mongo:27017[-125]) connection is closed
i already did docker-compose up -d for my docker, and it was running on my docker ,
the full code is trigger here golang and mongodb