October DealsAmazon USOctober deal check: compare before you payAmazon US: current deals, useful picks and tech finds.Check DealsWindows FixRecommendedWindows errors stealing your time? Find the fix fastScan stability, cleanup and performance issues.Fix NowOctober DealsAmazon USDeal season is back - check today's better picksAmazon US: current deals, useful picks and tech finds.See Picks×
Skip to content
VGSources
Blog

How to Access the MongoDB Shell on a Linux VPS

Run mongosh over SSH for a local MongoDB connection, or supply the host, port, credentials, TLS option, and database path that your deployment requires. This guide shows the exact command forms and how to verify and close the session.
Length7 min Posted Quest giverVGSources Team
Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

SSH into the VPS, make sure the current MongoDB shell (mongosh) is installed, and run mongosh. With MongoDB listening on its default local endpoint, that opens a shell connected to localhost:27017. The equivalent explicit command is mongosh "mongodb://localhost:27017".

Use a connection string or command-line options when the server uses another host, port, database, authentication method, or TLS. Once connected, run db.getMongo() to verify the active deployment.

As an Amazon Associate I earn from qualifying purchases.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

What you need before connecting

The commands below assume you can open an SSH session to the Linux VPS and that mongosh is already installed there. MongoDB’s documented connection procedures begin from that point; installing the shell is a separate operating-system task.

  • SSH access: You need an account that can log in to the VPS and reach the shell prompt.
  • The endpoint: Identify whether MongoDB is on the VPS itself or on another host, and confirm its listening port. The default port is 27017.
  • Credentials: If access control is enabled, have the MongoDB username and the database used to authenticate that user.
  • Transport requirements: Know whether the deployment requires TLS and, for a managed or replica-set deployment, whether you were given a complete connection URI.

Being logged in to the VPS matters for a local connection: localhost means the machine on which mongosh is running. If you run the same command on your own computer, it targets your computer, not the VPS.

#1 Best Overall
Sale
Gaoye Blue Light Blocking Glasses
  • BLUE LIGHT BLOCKING GLASSES: Our blue light glasses for women boast UV400 protection, blocking harmful blue rays from computer screens. With these bluelight glasses women, you can enjoy gaming, reading, or watching TV with no worries of eye strain.
  • EYE RELIEF: Our blue light glasses are engineered to reduce blurry vision or strain caused by prolonged screen time. These womens blue light glasses aid in better sleep by preventing blue light from disrupting your natural sleep cycle, helping you fall asleep faster and enjoy a more restful night's sleep, making them the perfect christmas present.
  • COMFORTABLE: Crafted from durable material, our trendy blue light glasses for woman or blue light glasses men ensure long-lasting comfort. The high-transmittance lenses accessories, gamer gifts for men, help keep your eyes relaxed. Perfect stocking stuffers, gifts for gamers men, christmas gifts for men, halloween costumes.
  • LIGHTWEIGHT: These clear blue light glasses feature a lightweight frame, meaning they’ll stay comfy for long-term wear. Perfect as blue blockers glasses for women or mens blue light glasses, these non-prescription fake glasses for men can be worn throughout the day. The blue light blocking glasses women feature polycarbonate HD lenses, which are anti-reflective and restore true color.
  • WORRY-FREE PURCHASE: If you experience any issues, our support team is ready to assist you quickly. These are the perfect costume glasses for cosplay dressing up.

Open a local MongoDB shell on the default port

After SSH login, run the command with no options:

mongosh

This connects to MongoDB on localhost:27017. MongoDB documents this as the standard connection for a deployment running locally with the default port. The explicit equivalent is:

mongosh "mongodb://localhost:27017"

These two forms select the same host and port. If the URI does not include a database name, mongosh starts with the test database as the default database context. That does not change which MongoDB deployment you connected to; it only determines the initial database name used by shell operations that rely on the current database.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Confirm the connection

At the mongosh prompt, run:

db.getMongo()

The result shows the MongoDB connection object for the active session. Use it as a quick check that the shell connected to the endpoint you intended, especially when several MongoDB instances or ports are present on a VPS.

Connect when MongoDB uses another port

If the database is on the VPS but listens on a non-default port, specify that port either in the URI or with --port.

Use a port in the connection string

mongosh "mongodb://localhost:28015"

Here, 28015 replaces the default 27017. The port must match the MongoDB process you intend to reach.

Use the command-line option

mongosh --port 28015

When no host is supplied, this form still targets localhost and changes only the port. A port option without a host therefore means localhost:28015.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
Rank #2
Windex Gaming Cleaning Wipes, Pre-Moistened Wipes to Instantly Clean all Gaming Gear and Electronics, Resealable Packaging for a Freshness Lock, 25 count (Pack of 1)
  • WINDEX GAMING WIPES: effortlessly clean all your gaming gear and gaming electronics in one easy step with our new Windex Gaming Wipes
  • AMMONIA FREE: our ammonia-free formula is safe to use on all of your favorite gaming equipment like consoles, headsets, controllers, screens, keyboards, and more
  • PRE-MOISTENED WIPES: each wipe instantly removes dust, dirt, and stubborn smudges and fingerprints leaving a streak-free shine
  • ANTI-STATIC: great for cleaning and wiping down your gaming gear and electronics leaving behind no visible residue
  • COMPACT RESEALABLE PACKAGE: store your pack of Windex Gaming Wipes anywhere in your home for whenever a quick and easy wipedown is needed

Connect to MongoDB on another host

For a database running on a different server, provide its hostname and port. For example:

mongosh "mongodb://mongodb0.example.com:28015"

The same endpoint can be written with separate options:

mongosh --host mongodb0.example.com --port 28015

If --port is omitted, mongosh uses 27017:

mongosh --host mongodb0.example.com

Use the hostname that belongs to your deployment, not the placeholder in the example. A remote connection requires both the correct host and the correct port; changing only one can send the shell to a different service or to an endpoint where MongoDB is not listening.

Local versus remote endpoint selection

Situation Command form Endpoint selected Initial database
MongoDB on the VPS, default port mongosh localhost:27017 test when no database is specified
MongoDB on the VPS, custom port mongosh --port 28015 localhost:28015 test when no database is specified
Named host and custom port mongosh --host mongodb0.example.com --port 28015 mongodb0.example.com:28015 test when no database is specified
Named host with the default port mongosh --host mongodb0.example.com mongodb0.example.com:27017 test when no database is specified

Authenticate with a username and password

When MongoDB access control is enabled, add the username and the authentication database. This example connects to a custom endpoint as user alice and authenticates against the admin database:

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
mongosh "mongodb://mongodb0.example.com:28015" --username alice --authenticationDatabase admin

mongosh then prompts for the password and hides the characters while you type. This interactive prompt is preferable to placing a password directly in a command line, because command text can be retained in shell history or exposed while processes are inspected.

Understand the authentication database

The database that stores a user’s authentication record is not necessarily the database you want to work in. In the example, admin is the authentication database, while the shell’s starting database remains governed by the URI. If you need to work in a particular database, include its name in the URI:

Rank #3
Sale
LISEN Cell Phone Stand Phone Holder for Desk, Office Decor Gadgets for Men
  • Ergonomic & Adjustable Design: Fight "text neck" and improve your posture. Designed for comfort, it offers an adjustable height (7.1in-8.5in) (1.4in adjustable range) and angle (5°-85°) Please note: cell phone stand is only 9 inches tall—the length of a standard pencil
  • Without blocking subtitles Perfect for office desk Tiktok Face Time Zoom video—this is the ultimate kitchen desk essential for phones! Perfect viewing experience while reducing neck back strain, A must-have for gamers streamers desk, and anyone who values both style and support!
  • Sleek & Stable Design fits Otterbox Case: Say goodbye to bent cables, tangles, and tip-overs! LISEN’s stylish adjustable desk phone holder features a built-in charging hole and smart cable management, keeping your phone secure and your workspace clutter-free cool gadgets for men
  • ANTI-SLIP DESIGN free hands: The under 10.00 items easy gadgets back and the bottom of this kitchen essentials for women are fully covered by anti-skid silicone, which LISEN stand up desk decor can provide maximum protection device from any scratches and slides
  • Your Perfect Viewing Companion:If your product arrives damaged or breaks within 365 days, reach out to LISEN for a quick and hassle-free solution. Struggling with assembly, like tightening the screw? We’ve got your back! Your experience matters to us. (Product including: phone Holder stand*1, screw*1, hexagon screwdriver*1, specification*1)
mongosh "mongodb://mongodb0.example.com:28015/qa" --username alice --authenticationDatabase admin

This connects to the specified host and port, authenticates alice against admin, and starts with qa as the database in the shell context.

Do not confuse credentials with endpoint details

  • The hostname and port identify where the MongoDB server is.
  • --username identifies the account.
  • --authenticationDatabase identifies where MongoDB should validate that account.
  • The hidden password prompt supplies the secret without putting it in the displayed command.

Check all three named settings when authentication fails. A valid username paired with the wrong authentication database can fail even when the server and port are correct.

What’s actually slowing this PC down?

Pick the symptom - the matching free tool is one click away.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Start in a specific database

Append a database name to the URI path when you want the shell to start there:

mongosh "mongodb://localhost:27017/qa"

This still connects to localhost on port 27017, but the initial database is qa rather than the implicit test. The same pattern works with a custom port or a remote host:

mongosh "mongodb://mongodb0.example.com:28015/qa"

Only the database path changes the starting database. It does not replace the host, port, authentication settings, or TLS requirements. Add those options as needed for the deployment.

Enable TLS for an encrypted connection

If the deployment requires TLS, add --tls to the shell command or add tls=true to the URI query string.

Rank #4
Ledagic IC+RGBW USB Powered Led Light Bar with App & Remote Control, 2 Pack
  • Easy Installation: Our gaming lights have been designed to enhance your entertainment experience. Simply position the led light bar next to your TV or game console to create a game room environment with ease. They come with USB power and a 60 inch cord for direct mounting and mobility
  • Audio Syncing & Extensive Mode: The built-in high-sensitivity music sensor allows you to sync the TV led lights to your favorite music. Lights react and move to the beat of music, film and game audio, creating an incredible immersive sound and light experience – and all without the need for an additional hub. You can choose from 64+ patterns, including strobe, illusion, jump, energy, gradient, running, chasing, fluttering mode etc. Each mode can be applied to different scenes
  • 64+ Chasing IC+RGB & W Effect: The independent control design of IC+RGB and white all-in-one multifunctional rgbw led bar lights monitor backlight allows you to easily switch the led lights for desk between IC+RGB and white to enjoy a variety of lighting effects, you can choose white lighting mode with high brightness for better illumination when working, etc. Choose IC+RGB lighting mode when you need to create an entertaining atmosphere for the light show, 16 million colours of Color changing rgb light bar let you dance to the beat and create a dynamic party atmosphere
  • Dynamic and Dimmable Lights: 1%-full brightness free brightness adjustment gives you the flexibility to adjust dimmable led light bar for room decorations or a variety of occasions
  • USB Powered: USB light bars for room can be powered by powernank, usb charger, TV, Monitor, laptop PC mainframe, making behind TV led lighting bar easy to install in any location.TV led lights are powered by the TV usb interface / PC mainframe, can be turned on/off synchronously with TV / PC

Use the command-line flag

mongosh "mongodb://mongodb0.example.com:28015" --tls

The flag can be combined with authentication and a database path:

mongosh "mongodb://mongodb0.example.com:28015/qa" --username alice --authenticationDatabase admin --tls

Use a URI option

mongosh "mongodb://mongodb0.example.com:28015/qa?tls=true"

Use one approach consistently with the connection details supplied for your deployment. A TLS-required server and a client that omits TLS will not have matching transport settings; conversely, adding TLS to a server that is configured for a non-TLS connection can prevent the session from opening.

Atlas and SRV connection strings

A mongodb+srv:// URI represents a DNS seed-list connection and automatically enables TLS. If your provider gives you a complete mongodb+srv:// string, pass that URI to mongosh rather than converting it into a single host-and-port example:

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
mongosh "mongodb+srv://your-deployment.example/qa"

Use the exact URI supplied for the deployment, including any required database name or options. The example above illustrates the form only; replace it with the real connection string for your server.

Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Support on Ko-Fi

Choose the connection form that matches your deployment

The following summary separates the decisions that commonly get mixed together: locality, port, credentials, transport security, topology, and starting database.

Need Use What it controls
Local default connection mongosh Localhost on port 27017
Local custom port mongosh --port 28015 Localhost on the selected port
Remote single host mongosh --host mongodb0.example.com --port 28015 Named host and port; omitted port defaults to 27017
Username authentication --username alice --authenticationDatabase admin Account name and database used for authentication; password is prompted interactively
TLS on a standard URI --tls or ?tls=true Encrypted transport setting
SRV-based deployment mongodb+srv://... Provider-supplied DNS seed-list URI; TLS is enabled automatically
Specific starting database Database path such as /qa Initial shell database instead of the default test

For a replica set or a managed deployment, prefer the complete URI supplied for that topology. It can carry the host discovery and connection options that a hand-written single-host command may omit.

Best Value
Sale
HUANUO FlowLift™ Dual Monitor Stand, Fully Adjustable Gaming Monitor Desk Mount for 13–32″ Computer Screens, Full Motion VESA 75x75/100x100 with C-Clamp & Grommet Base, Each Arm Holds 4.4 to 19.8 lbs
  • Compatible with Wide Screens - To ensure compatibility with the dual monitor mount, your each monitor must meet three conditions at the same time: First, computer screens size range: 13 to 32 inches. Second, screen weight range: 4.4 to 19.8 lbs. Third, the back of the monitor screen must have VESA mounting holes with a pitch of 75x75mm or 100x100mm.
  • Regarding the compatibility with desks - Your desk must meet three conditions at the same time: First, desk material: Only wooden desks are recommended, plastic or glass desks cannot be used. Second, desk thickness range: 0.59" - 3.54". Third, the bottom of the desk should not have any cross beams or panels, as this will interfere with installation. We recommend carefully checking that your desk and monitors meets all above conditions before purchasing.
  • Dual C-Clamp Hold - Worried your dual monitors might wobble or slip? Our upgraded base uses a larger platform plus a dual C-clamp structure to lock the dual monitor arm firmly to your desk. Each arm safely keeps your screens steady while you type, click and game—no shaking, no sliding, just a clean and secure setup you can trust every day. It also provides Grommet Mounting installation choice, both options ensure stable and secure fixation for your 0.59" - 3.54" desk.
  • Full-Motion Adjustment For Comfortable View - Pull the screen closer when you’re deep in a spreadsheet, push it back to watch videos, or rotate to portrait for coding — moving everything smoothly with just one hand. The monitor stand offers +85°/-50° tilt, ±90° swivel and 360° rotation. Raise your monitor up to 15.75″ to support a healthy sitting posture. Whether you’re working from home, gaming through the night, or switching between video calls and documents, getting the screens to your natural line of sight helps relieve neck, shoulder and back strain so you can stay focused longer with less fatigue.
  • Keep Your Desk Organized: By lifting both screens off the desktop, this dual monitor stand opens up valuable space for your keyboard, notebook, docking station or a simple, clutter-free work area. Built-in cable management guides wires along the arms, keeping cords out of sight and out of the way. Enjoy a tidy, modern workstation that looks as good as it feels to use.

A reliable connection sequence on a VPS

  1. Open SSH: Log in to the VPS with your normal SSH client and wait for the remote shell prompt.
  2. Check the client: Run mongosh or otherwise confirm that the command is available before troubleshooting the database endpoint.
  3. Start with the simplest matching endpoint: Use mongosh for local port 27017, or add the known host and port when they differ.
  4. Add authentication: If access control is enabled, append --username and --authenticationDatabase; enter the password at the hidden prompt.
  5. Add TLS: Include --tls, ?tls=true, or the provider’s mongodb+srv:// URI when encrypted transport is required.
  6. Select the working database: Add a database path such as /qa if the session should begin outside test.
  7. Verify: Run db.getMongo() and check that the active connection corresponds to the intended host and port.

Diagnose the most common connection problems

mongosh is unavailable

If the VPS reports that the command cannot be found, the shell client is not available in that session. Confirm installation and the executable’s availability before changing MongoDB connection options. A URI cannot work until the client itself runs.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

The shell reaches the wrong server

Recheck whether you used localhost intentionally. From the VPS, it means the VPS; from another computer, it means that other computer. Then check the port: omitting --port or a URI port selects 27017, not a custom port such as 28015.

Authentication is rejected

Confirm the username and the authentication database separately. Keep the password out of the command line and let mongosh prompt for it. If the deployment uses a supplied URI, preserve its authentication options rather than replacing them with assumptions about admin or another database.

TLS negotiation fails

Compare the command with the deployment’s transport requirement. Add --tls or ?tls=true when TLS is required; use the provider’s mongodb+srv:// URI as supplied, since that URI enables TLS automatically. Do not treat a TLS setting as a substitute for the correct hostname, port, or credentials.

The shell opens but the database is not the one expected

Inspect the URI path. No path selects test initially; adding /qa, for example, starts in qa. This setting affects the initial database context, while db.getMongo() confirms the underlying connection.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Leave the MongoDB shell

Use any of the following at the mongosh prompt:

  • .exit
  • exit()
  • quit()
  • Press Ctrl-D.
  • Press Ctrl-C twice.

These commands close the shell session and return you to the VPS’s normal terminal prompt. They do not by themselves stop MongoDB or change the server configuration.

Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.

Leave a Reply

Your email address will not be published. Required fields are marked *

Free tools Windows power users keep installed

One-click scans. No signup required.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

More quests from Patch Notes

  1. How to Set Up a RedM (RDR2) Server in 2026: License Key, txAdmin and server.cfg, Step by StepBlog11min
  2. Best RedM (RDR2) Server Hosting in 2026: Comparing Four Hosts on Slots, Memory and PriceBlog10min
  3. How to Host a Mindustry Server in 2026: server-release.jar, Port 6567 and the Commands That MatterBlog7min
Recommended PC Tool
Recommended PC Tool
Crashes, No Sound, or Screen Glitches?Free driver scan
Windows Errors? Fix Them Before They SpreadFree repair scan

Two free Windows tools

One Free Minute Could Fix That PC

Before you go - each of these free tools takes about a minute and tackles what quietly slows a Windows PC down.

Special offer. View Outbyte info, uninstall instructions, EULA, and Privacy Policy.