# PMB can not connect to Mongodb

**URL:** <https://forums.percona.com/t/pmb-can-not-connect-to-mongodb/22647>\
**Category:** Percona Backup for MongoDB\
**Created:** [June 7, 2023, 8:49pm UTC](https://forums.percona.com/t/pmb-can-not-connect-to-mongodb/22647 "2023-06-07T20:49:35Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![Me1](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.percona.com/me1/32/10971_2.png) [@Me1](https://forums.percona.com/u/Me1)\
**Post date:** [June 7, 2023, 8:49pm UTC](https://forums.percona.com/t/pmb-can-not-connect-to-mongodb/22647/1 "2023-06-07T20:49:35Z")

</div>

Steps:  
I am following the setup for PBM from here for percona-server-for-mongodb:

> **[Percona Server for MongoDB 4.4 - Run Percona Server for MongoDB in a Docker...](https://docs.percona.com/percona-server-for-mongodb/4.4/install/docker.html#connecting-from-another-docker-container)**
>
> Docker images of Percona Server for MongoDB are hosted publicly on Docker Hub at
> https://hub.docker.com/r/percona/percona-server-mongodb/.

And here for PBM:

> **[Percona Backup for MongoDB - Set up and configure](https://docs.percona.com/percona-backup-mongodb/install/initial-setup.html#set-the-mongodb-connection-uri-for-pbm-agent)**
>
> After you install Percona Backup for MongoDB on every server with the mongod node that is not an arbiter node, the setup steps are the following:

…  
export PBM\_MONGODB\_URI=“mongodb://pbmuser:secretpwd@localhost:27018/?authSource=admin”

When trying pbm status or any backup commands with pbm agent, it emitted a connection error.  
For example:  
pbm status

> Error: connect to mongodb: create mongo connection: mongo ping: server selection error: server selection timeout, current topology: { Type: ReplicaSetNoPrimary, Servers: [{ Addr: 049bba6c2cc3:27017, Type: Unknown, Last error: dial tcp: lookup 049bba6c2cc3: Temporary failure in name resolution },] }

I can connect to the primary mongo node normally with:  
mongo mongodb://pbmuser:secretpwd@localhost:27018/?authSource=admin

To confirm it is a replicaSet:  
rs:PRIMARY\> db.isMaster().primary  
049bba6c2cc3:27017

Please anyone help advise how to check further why it could not connect.  
Thanks.

---

<div class="post-metadata">

**Author:** ![Dmytro\_Zghoba](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.percona.com/dmytro_zghoba/32/4721_2.png) [@Dmytro\_Zghoba](https://forums.percona.com/u/Dmytro_Zghoba)\
**Post date:** [June 8, 2023, 3:13pm UTC](https://forums.percona.com/t/pmb-can-not-connect-to-mongodb/22647/2 "2023-06-08T15:13:34Z")

</div>

hey @Me1

are you running `pbm-agent` on the host machine or in a container? if in a container, try to run `pbm status` in the same container.

---

<div class="post-metadata">

**Author:** ![Me1](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.percona.com/me1/32/10971_2.png) [@Me1](https://forums.percona.com/u/Me1)\
**Post date:** [June 8, 2023, 3:39pm UTC](https://forums.percona.com/t/pmb-can-not-connect-to-mongodb/22647/3 "2023-06-08T15:39:06Z")

</div>

hi @Dmytro_Zghoba,  
I am using pbm-agent in the host machine and PSMD in a container. It looks like pbm does not work the same way with other applications, such as mongo shell, when connecting to a containerised mongo server instance.

My setup only currently is using 1 PSMD node which is also the primary node for replica set ‘rs’.

With this usage scenario, is there any additional mappings or configuration needed so that PBM can connect properly?  
Thanks.

---

<div class="post-metadata">

**Author:** ![Dmytro\_Zghoba](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.percona.com/dmytro_zghoba/32/4721_2.png) [@Dmytro\_Zghoba](https://forums.percona.com/u/Dmytro_Zghoba)\
**Post date:** [June 8, 2023, 3:50pm UTC](https://forums.percona.com/t/pmb-can-not-connect-to-mongodb/22647/4 "2023-06-08T15:50:52Z")

</div>

in this case, set hostname for each PSMDB container. on the host’s “/etc/hosts” add alias each hostnames to 127.0.0.1 and flush the DNS cache

---

<div class="post-metadata">

**Author:** ![Dmytro\_Zghoba](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.percona.com/dmytro_zghoba/32/4721_2.png) [@Dmytro\_Zghoba](https://forums.percona.com/u/Dmytro_Zghoba)\
**Post date:** [June 8, 2023, 3:58pm UTC](https://forums.percona.com/t/pmb-can-not-connect-to-mongodb/22647/5 "2023-06-08T15:58:19Z")

</div>

btw do you run sharded cluster?  
I see `049bba6c2cc3:27017` instance but URI `localhost:27018` (with default port to shardsvr)

---

<div class="post-metadata">

**Author:** ![Me1](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.percona.com/me1/32/10971_2.png) [@Me1](https://forums.percona.com/u/Me1)\
**Post date:** [June 8, 2023, 4:24pm UTC](https://forums.percona.com/t/pmb-can-not-connect-to-mongodb/22647/6 "2023-06-08T16:24:31Z")

</div>

No. I just made a test PCMDB node like this:  
podman run --rm -d --name rs101 -p 27018:27017 percona/percona-server-mongodb:4.4 --port=27017 --replSet rs  
And then initialise the replica set and made it as the primary node within the container. I am not sure how or why PBM working that way when it mentioned the host as the container ID and port within the error.  
I am going to start the container again with the alias hostname as advised to see how it will work.  
Thanks.

---

<div class="post-metadata">

**Author:** ![Me1](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.percona.com/me1/32/10971_2.png) [@Me1](https://forums.percona.com/u/Me1)\
**Post date:** [June 8, 2023, 6:31pm UTC](https://forums.percona.com/t/pmb-can-not-connect-to-mongodb/22647/7 "2023-06-08T18:31:04Z")

</div>

hi @Dmytro_Zghoba,  
I re-create the PSMBD node again with -hostname and added this host to the host’s /etc/hosts. But it still can not connect.  
$ podman run --rm -d --name rs101 -p 27018:27017 --hostname=psmongo1 percona/percona-server-mongodb:4.4 --port=27017 --replSet rs

$ podman ps

CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES  
07f7e9841c40 [docker.io/percona/percona-server-mongodb:4.4](http://docker.io/percona/percona-server-mongodb:4.4) --port=27017 --re… 39 minutes ago Up 39 minutes 0.0.0.0:27018-\>27017/tcp rs101

$ ping psmongo1  
PING psmongo1 (127.0.0.1) 56(84) bytes of data.  
64 bytes from psmongo1 (127.0.0.1): icmp\_seq=1 ttl=64 time=0.008 ms  
64 bytes from psmongo1 (127.0.0.1): icmp\_seq=2 ttl=64 time=0.013 ms

$ export PBM\_MONGODB\_URI=“mongodb://pbmuser:secretpwd@psmongo1:27018/?authSource=admin&replSet=rs”

$ pbm status  
Error: connect to mongodb: create mongo connection: mongo ping: server selection error: server selection timeout, current topology: { Type: ReplicaSetNoPrimary, Servers: [] }

May be PBM does not support containers started by PodMan?  
[rs101\_inspect.txt](https://forums.percona.com/uploads/short-url/vAM0tJJXMiqniZNxuR3h7F5Vyls.txt) (12.3 KB)  
[rs.status.txt](https://forums.percona.com/uploads/short-url/brtaAxXE5jGLXfs7SQ6rlDcfZVm.txt) (2.8 KB)

---

<div class="post-metadata">

**Author:** ![Dmytro\_Zghoba](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.percona.com/dmytro_zghoba/32/4721_2.png) [@Dmytro\_Zghoba](https://forums.percona.com/u/Dmytro_Zghoba)\
**Post date:** [June 8, 2023, 8:20pm UTC](https://forums.percona.com/t/pmb-can-not-connect-to-mongodb/22647/8 "2023-06-08T20:20:06Z")

</div>

PBM usually runs on the same host as mongod or on the same container, or in a sidecar (but with the same linux namespaces).

The error comes from the mongo driver. PBM should have the same hostname and port in the connection string as the replset deployed.  
In short, `psmongo1:27018` won’t work because MongoDB identifies itself as `psmongo1:27017`.

---

<div class="post-metadata">

**Author:** ![Me1](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.percona.com/me1/32/10971_2.png) [@Me1](https://forums.percona.com/u/Me1)\
**Post date:** [June 9, 2023, 5:35pm UTC](https://forums.percona.com/t/pmb-can-not-connect-to-mongodb/22647/9 "2023-06-09T17:35:52Z")

</div>

Thanks @Dmytro_Zghoba. Now that I am trying pbm with the local mongod instance. My storage config using the local filesystem storage (for testing):  
storage:  
type: filesystem  
filesystem:  
path: /home/user/local\_backups

The permission on the folder:  
drwxrwxr-x 2 user user 4096 Jun 9 12:50 /home/user/local\_backups

But then running pbm status there is an error:

> ERROR with storage: storage: no init file, attempt to create failed: create destination file \</home/user/local\_backups/.pbm.init\>: open /home/user/local\_backups/.pbm.init: permission denied

In this case, which permissions are set for the folder so pbm can write to?

Thanks

---

<div class="post-metadata">

**Author:** ![Parag\_Bhayani](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.percona.com/parag_bhayani/32/3710_2.png) [@Parag\_Bhayani](https://forums.percona.com/u/Parag_Bhayani)\
**Post date:** [June 11, 2023, 10:40am UTC](https://forums.percona.com/t/pmb-can-not-connect-to-mongodb/22647/10 "2023-06-11T10:40:22Z")

</div>

Hi @Me1 ,

The above issue is related to ownership of storage path.

1. Start the `pbm-agent` as the `mongod` user.
2. Give mongod ownership to the storage path as well.

Regards,  
Parag
