Wannan shafin bai samu a cikin Hausa tukuna ba. Ana nuna shi da Turanci.
Reach a MongoDB server through Localtonet
A wannan shafin 9 sassa
- How it works
- Before you start
- Create the tunnel
- Open the TCP - UDP page
- Fill in the form
- Start the tunnel and copy the address
- Connect from outside
- MongoDB settings that matter behind a tunnel
- Enable access control
- bindIp can stay local
- Replica sets need a direct connection
- Keep the same address
- Secure it
- Common problems
- Related pages
Connect to a MongoDB server on your machine from another network with mongosh, MongoDB Compass or a driver: a TCP tunnel gives port 27017 a public host name and port.
How it works #
The Localtonet client runs on the machine that hosts MongoDB, or on another device in the same network. A TCP tunnel points to the MongoDB port, 27017. Localtonet gives the tunnel a public address made of a host name and a port. A MongoDB client that connects to that address reaches your server. The details of TCP tunnels are on the TCP page.
Turn on authentication first. A self-managed MongoDB server accepts every connection without a user name or password until you enable access control. A started tunnel is reachable by anyone who has its address, so a tunnel to such a server gives strangers full access to your data.
Before you start #
- A Localtonet account and the Localtonet client connected with your AuthToken. See Quick start.
- MongoDB running on the device with access control enabled. See MongoDB settings below.
- A MongoDB user for remote use, with a strong password and only the roles it needs.
Create the tunnel #
-
Open the TCP - UDP page #
Open My Tunnels › TCP - UDP and expand Create TCP - UDP Tunnel.
-
Fill in the form #
Field What to enter Protocol TCP Device token The token the Localtonet client on the device uses. Server region Choose a region close to your device for lower latency. Local IP address 127.0.0.1when MongoDB runs on the same device as the client. For a server on another machine in the network, enter that machine's address.Local port 27017, or thenet.portvalue inmongod.conf.URL type Random subdomain. Custom subdomain lets you choose the name and needs Balance. Select Create tunnel.
-
Start the tunnel and copy the address #
The tunnel appears under Your tunnels. Select Start. The URL column shows the public address as
host:port, and the Server Port column shows the port on its own.
Connect from outside #
MongoDB clients take a connection string. Use the mongodb:// form with the tunnel's host and port. authSource names the database the user was created in, usually admin.
mongodb://<DB_USER>:<DB_PASSWORD>@<TUNNEL_HOST>:<TUNNEL_PORT>/<DB_NAME>?authSource=adminWith mongosh, leave the password out of the command so it isn't stored in your shell history. mongosh asks for it:
mongosh "mongodb://<TUNNEL_HOST>:<TUNNEL_PORT>/<DB_NAME>" --username <DB_USER> --authenticationDatabase adminIn MongoDB Compass, paste the connection string into the URI field of a new connection.
Don't use the mongodb+srv:// form with a tunnel address. That form looks up DNS records that a tunnel host name doesn't have.
MongoDB settings that matter behind a tunnel #
Enable access control #
Create an administrator first, then turn authorization on in mongod.conf and restart MongoDB:
security:
authorization: enabledAfter the restart, a connection without credentials can no longer read or change data. The MongoDB manual describes the full procedure under "Enable Access Control".
bindIp can stay local #
The Localtonet client connects to MongoDB from your own device. MongoDB doesn't have to listen on a public interface: the default net.bindIp: 127.0.0.1 is enough when the client runs on the same machine. Leave it that way, so the tunnel is the only path from outside.
Replica sets need a direct connection #
When a driver connects to a replica set, the server answers with the host names of its members, and the driver then tries to reach those names. They are the addresses inside your network, so the connection hangs or fails from outside. Add directConnection=true to the connection string to stay on the tunnel's address:
mongodb://<DB_USER>:<DB_PASSWORD>@<TUNNEL_HOST>:<TUNNEL_PORT>/<DB_NAME>?authSource=admin&directConnection=trueKeep the same address #
The host name stays with the tunnel. The port is assigned by the server when the tunnel starts. With Is Reserved? on in the tunnel's Settings, the port stays the same when the tunnel or the Localtonet client restarts, which is what a connection string in an application needs. With it off, the port changes on a restart. Tunnels that you create while your account has Balance start with Is Reserved? on. Turning it on needs Balance and a started tunnel.
With Balance you can also choose the port under Server Port › Change, for example 27017 if no other tunnel on that server uses it. See Ports.
Free plan. A TCP tunnel stops after 30 minutes on the Free plan and can be started again. The port can be different after each start. See Plans and Balance.
Secure it #
- Access control in MongoDB, as described above. This is the one step you can't skip.
- IP Restriction. In the tunnel's Settings, select IP Restriction › Manage, choose Allow only these and add the addresses of the applications and people that connect. Use my IP fills in your current address. Changes apply right away. IP Restriction needs Balance. See Access control.
- A dedicated user with roles on the one database it needs, for example
readWrite, instead of an administrator. - TLS in MongoDB. Tunnels are not encrypted end to end by default. Configure
net.tlson the server and addtls=trueto the connection string so credentials and data are encrypted all the way. - Limits. Expiration Date, Bandwidth Limit and Speed Limits in the tunnel's Settings cap how long and how much the tunnel can be used. See Limits and expiration.
- Request Log › Show lists the connections made to the tunnel with the visitor's IP address. MongoDB itself logs the Localtonet client as the source of every connection.
Common problems #
MongoServerSelectionError or MongoNetworkError: connection timed out
Check that the tunnel is started and its Connection column shows ON, that you use the current port from Server Port, and that MongoDB answers on the device at the tunnel's Local IP address and Local port. If the server is a replica set, add directConnection=true. If you use IP Restriction, check that your current address is in the list.
MongoServerError: Authentication failed
The tunnel works and MongoDB answered. Check the password and the authSource: it must be the database the user was created in. Special characters in the user name or password must be percent-encoded in a connection string.
I can read data without signing in
Access control is off. Stop the tunnel, enable security.authorization, restart MongoDB, then start the tunnel again.
The application lost its connection after a restart
The port is not reserved, so it changed when the tunnel or the client restarted. Turn on Is Reserved?, or update the port in the connection string.
The tunnel stopped by itself after half an hour
On the Free plan a TCP tunnel stops after 30 minutes. Select Start to begin another session, or add Balance to remove the session limit.
"You need to purchase balance to use this feature."
Is Reserved?, Server Port, IP Restriction and Custom subdomain need Balance. Add it under Balance.