How do I connect my bot to a database?
Choose between a SQLite file and an external database, put the credentials in environment variables and test the connection.
Bot hosting does not include a managed database: the bot uses either a SQLite file kept with its files or a database hosted elsewhere. The credentials of that database are separate from the ACLClouds account.
Step-by-step procedure
-
Create the database
Create the database at the service that hosts it and allow remote connections there. With SQLite there is nothing to create: the library creates the file on first launch.
-
Copy the settings
Note the host, port, database name, user and password from the service that hosts the database.
-
Create variables
Store settings in separate variables or a DATABASE_URL, never in Git.
-
Test the connection
Run a simple query at startup and handle reconnections without an aggressive retry loop.
-
Back up
Regularly export data with the appropriate tool before migration or deletion.
Recommended variables
DB_HOST=db.example.com
DB_PORT=3306
DB_NAME=botdb
DB_USER=botuser
DB_PASSWORD=secret
MySQL connection in Node.js
const mysql = require('mysql2/promise');
const pool = mysql.createPool({
host: process.env.DB_HOST,
port: Number(process.env.DB_PORT || 3306),
user: process.env.DB_USER,
password: process.env.DB_PASSWORD,
database: process.env.DB_NAME,
connectionLimit: 5,
});
pool.query('SELECT 1').then(() => console.log('Database connected'));
Choose where to store the data
A bot page has no “Databases” tab and no MySQL database is created with the service. You have two options, depending on how much data you store and whether another program needs to read it.
The SQLite file counts towards the bot’s storage. Download a copy from the “Files” tab or over SFTP before any major change, and add it to .gitignore so your data is never published.
- SQLite: the database is a single file, for example data/bot.db, kept with the bot’s files. There is no server to install, and it is enough for levels, warnings or per-guild settings.
- External database: MySQL, MariaDB, PostgreSQL or MongoDB on a VPS or at a specialised provider. Prefer it when several programs read the same data, for example a bot and a website.
Connect to an external database
In the “Startup” tab, under “Environment variables”, add one variable per parameter with “Name” and “Value”: DB_HOST, DB_PORT, DB_NAME, DB_USER and DB_PASSWORD, or a single DATABASE_URL. Values are stored encrypted and the bot receives them at its next start.
The host must be a public address, a domain name or an IP reachable from the Internet. localhost and 127.0.0.1 point to the bot’s own container, where no database server runs. The bot reaches the Internet through an IP address shared with other bots, so an IP allow list alone does not protect the database. Create a user reserved for the bot, with a long password and rights limited to its database, and enable TLS if the provider offers it.
Also declare the driver in your dependencies: mysql2, pg or mongoose in package.json, aiomysql, asyncpg or motor in requirements.txt. It is installed when the bot starts.
Check connection
Restart the bot and read the console. Have your code print a clear line after a simple query such as SELECT 1, for example Database connected. If it does not appear, the error shown just before almost always names the cause: host, port, credentials or firewall.
Frequently asked questions
Why do I get connect ECONNREFUSED 127.0.0.1:3306?
The code is connecting to localhost: either DB_HOST is missing and the library falls back to its default, or the value comes from a local .env file. No database runs inside the bot’s container. Set DB_HOST to the public address of the database server, then restart.
What does ER_ACCESS_DENIED_ERROR: Access denied for user mean?
The MySQL server answered but rejects the credentials. Check DB_USER and DB_PASSWORD, with no spaces or quotes around the value. In MySQL a user is also tied to an origin host: an account created for localhost cannot connect remotely. Create it with the host % and a strong password.
Why do I get ETIMEDOUT when connecting?
No answer comes back: the port is filtered or the database does not listen on the Internet. On a VPS, check that MySQL listens on 0.0.0.0 rather than 127.0.0.1 (the bind-address option) and that the firewall lets port 3306 through. At a provider, allow external connections in its console.