---
title: "How to connect a Discord bot to a database | StreetHosting"
description: "Give your Discord bot a memory by connecting it to MongoDB or a relational database. Covers the connection string, credential security and basic data modeling."
url: "https://streethosting.com.br/en/guides/discord-bots/connect-discord-bot-to-database"
type: "page"
language: "en-US"
---

Discord Bots · 3 min · Intermediate

Published on Jun 17, 2026 · Updated on Jun 17, 2026

# How to connect a Discord bot to a database

Without a database, the bot forgets everything on restart. Connecting to a database like MongoDB gives levels, economy and settings persistent memory. See a secure connection and basic modeling.

By [Equipe StreetHosting](https://streethosting.com.br/en/autores#equipe-streethosting) · StreetHosting infrastructure and support team

[Databases](https://streethosting.com.br/en/guides/topics/databases) [Getting started](https://streethosting.com.br/en/guides/topics/getting-started)

Summarize with:

[](https://chat.openai.com/?q=Summarize%20the%20key%20points%20of%20this%20StreetHosting%20guide%3A%20https%3A%2F%2Fstreethosting.com.br%2Fen%2Fguides%2Fdiscord-bots%2Fconnect-discord-bot-to-database.%20Highlight%20the%20step-by-step%20instructions%2C%20the%20prerequisites%20and%20the%20most%20common%20mistakes. "ChatGPT") [](https://claude.ai/new?q=Summarize%20the%20key%20points%20of%20this%20StreetHosting%20guide%3A%20https%3A%2F%2Fstreethosting.com.br%2Fen%2Fguides%2Fdiscord-bots%2Fconnect-discord-bot-to-database.%20Highlight%20the%20step-by-step%20instructions%2C%20the%20prerequisites%20and%20the%20most%20common%20mistakes. "Claude") [](https://www.google.com/search?udm=50&aep=11&q=Summarize%20the%20key%20points%20of%20this%20StreetHosting%20guide%3A%20https%3A%2F%2Fstreethosting.com.br%2Fen%2Fguides%2Fdiscord-bots%2Fconnect-discord-bot-to-database.%20Highlight%20the%20step-by-step%20instructions%2C%20the%20prerequisites%20and%20the%20most%20common%20mistakes. "Google AI Mode") [](https://x.com/i/grok?text=Summarize%20the%20key%20points%20of%20this%20StreetHosting%20guide%3A%20https%3A%2F%2Fstreethosting.com.br%2Fen%2Fguides%2Fdiscord-bots%2Fconnect-discord-bot-to-database.%20Highlight%20the%20step-by-step%20instructions%2C%20the%20prerequisites%20and%20the%20most%20common%20mistakes. "Grok") [](https://www.perplexity.ai/search/new?q=Summarize%20the%20key%20points%20of%20this%20StreetHosting%20guide%3A%20https%3A%2F%2Fstreethosting.com.br%2Fen%2Fguides%2Fdiscord-bots%2Fconnect-discord-bot-to-database.%20Highlight%20the%20step-by-step%20instructions%2C%20the%20prerequisites%20and%20the%20most%20common%20mistakes. "Perplexity")

Share:

[](https://x.com/intent/tweet?text=How%20to%20connect%20a%20Discord%20bot%20to%20a%20database&url=https%3A%2F%2Fstreethosting.com.br%2Fen%2Fguides%2Fdiscord-bots%2Fconnect-discord-bot-to-database "Share on X") [](https://www.facebook.com/sharer/sharer.php?u=https%3A%2F%2Fstreethosting.com.br%2Fen%2Fguides%2Fdiscord-bots%2Fconnect-discord-bot-to-database "Share on Facebook") [](https://www.linkedin.com/sharing/share-offsite/?url=https%3A%2F%2Fstreethosting.com.br%2Fen%2Fguides%2Fdiscord-bots%2Fconnect-discord-bot-to-database "Share on LinkedIn") [](https://wa.me/?text=How%20to%20connect%20a%20Discord%20bot%20to%20a%20database%20https%3A%2F%2Fstreethosting.com.br%2Fen%2Fguides%2Fdiscord-bots%2Fconnect-discord-bot-to-database "Share on WhatsApp")

For agents: Copy as Markdown [.md](https://streethosting.com.br/en/guides/discord-bots/connect-discord-bot-to-database.md)

In this guide 5 sections

* [Why a database](https://streethosting.com.br/en/guides/discord-bots/connect-discord-bot-to-database#por-que-banco)
* [Which database to choose](https://streethosting.com.br/en/guides/discord-bots/connect-discord-bot-to-database#qual-banco)
* [Secure connection](https://streethosting.com.br/en/guides/discord-bots/connect-discord-bot-to-database#conexao-segura)
* [Model the data](https://streethosting.com.br/en/guides/discord-bots/connect-discord-bot-to-database#modelar)
* [Resilience](https://streethosting.com.br/en/guides/discord-bots/connect-discord-bot-to-database#resiliencia)

Quick answer

To give your bot a memory, connect it to a **database** like MongoDB or a relational database. Store the connection string in an environment variable, model the data using the member ID as the key and handle failures with reconnection. That way levels, economy and settings survive restarts.

## Why a database[](https://streethosting.com.br/en/guides/discord-bots/connect-discord-bot-to-database#por-que-banco)

A bot without a database forgets everything on restart. Levels, balances for a [virtual economy](https://streethosting.com.br/en/guides/discord-bots/discord-economy-bot) and per-server settings need persistent memory. The database is that memory: it stores the data safely and handles many accesses at the same time.

## Which database to choose[](https://streethosting.com.br/en/guides/discord-bots/connect-discord-bot-to-database#qual-banco)

MongoDB stores flexible documents and is widely used in bots because it is easy to get started with. Relational databases, like the ones you can set up with [MariaDB or MySQL on a VPS](https://streethosting.com.br/en/guides/vps/install-mariadb-mysql-ubuntu-vps), are great when the data has clear relationships. Choose based on the shape of what you store.

## Secure connection[](https://streethosting.com.br/en/guides/discord-bots/connect-discord-bot-to-database#conexao-segura)

The connection string usually contains the database username and password. Never leave it in the code or in commits. Use an environment variable and treat it as a critical secret.

## Model the data[](https://streethosting.com.br/en/guides/discord-bots/connect-discord-bot-to-database#modelar)

1. Use the member ID as the primary key.
2. Store level, balance and settings tied to each user.
3. Separate data by server when the bot serves several.
4. Create indexes for the most frequent queries.

Think about modeling before you grow. Reorganizing the data of thousands of users later is much more work than getting the structure right at the start.

## Resilience[](https://streethosting.com.br/en/guides/discord-bots/connect-discord-bot-to-database#resiliencia)

* Connection string in an environment variable
* Automatic reconnection on failure
* Database access allowed only for the bot
* Regular backup of the database data

The database and the bot have to be up together. Keep everything online with [24/7 hosting](https://streethosting.com.br/en/guides/discord-bots/discord-bot-hosting-24-7) and, to run the bot and the database with room to spare, check out the [Ryzen VPS](https://streethosting.com.br/en/vps/ryzen) and the [bot plans](https://streethosting.com.br/en/bots).

In this guide

* [Why a database](https://streethosting.com.br/en/guides/discord-bots/connect-discord-bot-to-database#por-que-banco)
* [Which database to choose](https://streethosting.com.br/en/guides/discord-bots/connect-discord-bot-to-database#qual-banco)
* [Secure connection](https://streethosting.com.br/en/guides/discord-bots/connect-discord-bot-to-database#conexao-segura)
* [Model the data](https://streethosting.com.br/en/guides/discord-bots/connect-discord-bot-to-database#modelar)
* [Resilience](https://streethosting.com.br/en/guides/discord-bots/connect-discord-bot-to-database#resiliencia)

## Frequently asked questions

Why not store data in a plain file?

It works for tiny prototypes, but it scales poorly and corrupts easily with simultaneous access. A database handles concurrency, queries and growth far more safely.

MongoDB or a relational database?

It depends on the data. MongoDB stores flexible documents and is popular with bots. Relational databases shine when there are clear relationships and structured queries. Choose based on the shape of your data and what your team knows well.

Where do I store the connection string?

In an environment variable, never in the code or in commits. The string usually contains the database username and password, so treating it as a secret is essential for security.

Does the database have to be on the same machine as the bot?

No. It can run on the same VPS for small projects or separately as you grow. Either way, allow access only to the bot and never expose the database to the whole internet.

Next step

See bot plans

24/7 Discord bots with fast activation and support in Portuguese.

[See bot plans](https://streethosting.com.br/en/bots)

[See Ryzen VPS Ryzen 9 9950X VPS in São Paulo with root access, NVMe and gamer Anti-DDoS.](https://streethosting.com.br/en/vps/ryzen) [See VPS plans Root VPS in Brazil with NVMe and Anti-DDoS.](https://streethosting.com.br/en/vps)

## Related guides

[Discord bots Intermediate How to build a virtual economy on Discord with a bot Virtual currency keeps a community engaged when it is well balanced. Here is how to set up earnings for activity, a daily reward, a shop for roles and items, and why balances need a database. 3 min Read guide](https://streethosting.com.br/en/guides/discord-bots/discord-economy-bot) [VPS Intermediate How to install MariaDB or MySQL on an Ubuntu VPS Almost every application needs a database. See how to install MariaDB or MySQL, run the security script, create a database and user, and allow access without exposing the server. 3 min Read guide](https://streethosting.com.br/en/guides/vps/install-mariadb-mysql-ubuntu-vps) [Discord bots Beginner 24/7 Discord bot hosting in Brazil Discord bot hosting 24/7 combines a process manager with automatic restart, a token kept out of your code, logs and a server in Brazil for low latency to the API and your players. 6 min Read guide](https://streethosting.com.br/en/guides/discord-bots/discord-bot-hosting-24-7)

[← Back to the Guide Center](https://streethosting.com.br/en/guides)
