How to Install MySQL/MariaDB in Termux
How to Install MySQL/MariaDB in Termux
Want to run a real database directly on your Android phone? You can. Setting up a local database used to mean carrying a laptop or paying for a cloud server — not anymore. With Termux, your phone becomes a genuinely capable development environment. This guide covers how to install MySQL/MariaDB in Termux, step by step, whether you want to practice SQL queries on the go or run a local testing database.
Quick Summary: Termux MySQL Setup at a Glance
Termux's package repository includes MariaDB, an open-source, drop-in replacement for MySQL. Because Android isn't built to run database servers out of the box, you need to initialize the data directory manually and start the server daemon yourself from the command line.
Quick Facts
- Package name:
mariadb - Required tool: the Termux app, installed from F-Droid or GitHub — the official, actively maintained sources
- What you'll run: an install command, a data directory initialization command, and a server start command
Understanding Termux MySQL and MariaDB
When people search for "Termux MySQL," they usually end up installing MariaDB instead — and that's correct. MariaDB is the MySQL-compatible package that ships in Termux's repository (and in most modern Linux distributions).
MariaDB is a community-developed fork of MySQL that maintains close compatibility with MySQL's commands, structure, and syntax. For practical purposes in Termux, MariaDB functions as your MySQL server.
If you're new to Termux itself, start with how to install Termux on Android and the basic Termux commands for beginners before working through this guide.
Prerequisites
- An Android device running Android 7.0 or higher.
- Termux installed from F-Droid or GitHub (avoid the Play Store build, which has historically lagged behind or gone unmaintained).
- A stable internet connection to download the packages.
- Roughly 200MB of free storage space.
Step-by-Step Guide: How to Install MySQL/MariaDB in Termux
Step 1: Update and Upgrade Termux Packages
Always update your package lists first to avoid dependency errors.
pkg update && pkg upgrade -y
Step 2: Install MariaDB
Install the database package:
pkg install mariadb -y
If the package won't resolve, check our guide on fixing "Unable to Locate Package" in Termux.
Step 3: Initialize the Database
Unlike desktop installers, Termux requires you to initialize the data directory manually:
mysql_install_db
If it completes without fatal errors, your base database directories and system tables have been created. On some current MariaDB versions this command may print a deprecation notice pointing you toward mariadb-install-db instead — both do the same job, so either works if offered.
Step 4: Start the MySQL Server
Start the database daemon in the background:
mysqld_safe &
The trailing & runs the process in the background so you can keep using your terminal. On newer MariaDB builds you may see a message saying mysqld_safe is deprecated — if that happens, use mariadbd-safe & instead, which is the current equivalent.
Step 5: Access the MySQL Shell
With the server running, connect as root — by default there's no password required for a fresh local install:
mysql -u root
If your prompt changes to MariaDB [(none)]>, your setup is working.
Real Use-Case Example: Testing Queries on Mobile
Say you're a student learning SQL, or a developer wanting to test a schema idea while away from a laptop. Open Termux, start MariaDB, and create a quick test database:
CREATE DATABASE test_db;
USE test_db;
CREATE TABLE users (id INT AUTO_INCREMENT PRIMARY KEY, name VARCHAR(50));
INSERT INTO users (name) VALUES ('Alice'), ('Bob');
SELECT * FROM users;
You'll see a formatted table output right in your terminal — a fully functional SQL playground wherever you are.
Termux MySQL vs. Alternative Tools
How does MariaDB in Termux compare to SQLite or a cloud-hosted database?
| Feature | Termux MySQL (MariaDB) | SQLite (in Termux) | Cloud MySQL (AWS/DigitalOcean) |
|---|---|---|---|
| Server architecture | Client-server model (daemon) | Serverless (file-based) | Remote server |
| Internet required? | No (runs fully offline) | No (runs fully offline) | Yes, always |
| Compatibility | High — matches typical web hosting databases | Standard SQL, limited features | Exact match |
| Resource usage | Moderate (uses RAM/CPU) | Very low | Depends on plan |
SQLite is great for tiny local apps, but running actual MySQL/MariaDB in Termux makes more sense if you're building something that will eventually deploy to a traditional LAMP or LEMP stack. Our guide on hosting a local website using Termux covers pairing this with Apache and PHP.
Troubleshooting: Common Errors and Fixes
1. Connection Refused / Can't Connect to Local MySQL Server
Cause: The database daemon isn't running.
Fix: Start it manually:
mysqld_safe &
Wait a few seconds, then try connecting again with mysql -u root.
2. Access Denied / Root Password Issues
Cause: Termux runs as its own Android app user, with no mapping to a MariaDB system user — this means the standard mysql_secure_installation wizard often fails to complete inside Termux, unlike on a typical Linux install.
Fix: Skip that wizard. Instead, stop any running instance, restart with grant tables disabled, and set the password manually:
mysqld_safe --skip-grant-tables &
mysql -u root
Then, inside the MySQL shell:
ALTER USER 'root'@'localhost' IDENTIFIED BY 'your_secure_password';
FLUSH PRIVILEGES;
EXIT;
Restart the daemon normally afterward with mysqld_safe & (or mariadbd-safe & if that's the command your version uses).
3. Storage Permission Errors
Cause: Termux lacks permission to write files during initialization.
Fix: Run termux-setup-storage to grant access — see our Termux storage permission fix guide if issues persist — and confirm you have enough free space.
Best Practices and Tips
- Secure your installation. Set a root password using the
ALTER USERcommand shown above rather than relying onmysql_secure_installation, which doesn't work reliably in Termux's environment. - Manage battery usage. A running
mysqld_safe/mariadbd-safedaemon consumes background battery. Stop it when you're done testing. - Back up regularly. Use the
mysqldumputility to export your local development databases to your device's storage.
Frequently Asked Questions
Can I connect to Termux MySQL from an external app?
By default, MariaDB in Termux listens only on the local loopback interface (127.0.0.1) for security. Connecting from another device or app requires additional network configuration.
Will installing MySQL/MariaDB drain my phone's battery?
Only while the server process is actively running. Stopping the daemon (or closing Termux entirely) brings background battery use back to zero.
Is MariaDB completely identical to MySQL?
For the vast majority of everyday queries, user management commands, and general development tasks, MariaDB behaves as a drop-in replacement for MySQL.
Conclusion
Running a database on mobile isn't a novelty anymore — with these steps, you can complete a working MySQL/MariaDB setup in Termux on your Android phone or tablet, giving you a portable sandbox for testing queries, learning SQL, and prototyping small projects anywhere. Pair it with our guides on installing PHP in Termux and hosting a local website using Termux to build a complete local development stack.
Ready to get started? Open Termux and run through the setup steps today.
Join the conversation