Bulletproof Your Home Assistant: Migrating to MariaDB & External SSD for Performance & Durability
NGC 224
DIY Smart Home Creator
The Achilles' Heel of Home Assistant: Database Performance & SD Card Wear
If you've been running Home Assistant on a Raspberry Pi or similar single-board computer, you've likely encountered performance issues or even dreaded SD card corruption. The culprit is often Home Assistant's default SQLite database, especially when the 'Recorder' integration logs vast amounts of state changes, sensor data, and event history directly to the same storage medium as your operating system. This constant read/write activity can quickly wear out flash storage like SD cards, leading to slowdowns, UI unresponsiveness, and eventual data loss. For tech enthusiasts, practical homeowners, and professionals alike, a stable, performant, and durable backend is paramount. The solution lies in offloading this intensive database workload to a more robust and faster storage solution: MariaDB running on an external Solid State Drive (SSD).
This guide will walk you through the practical steps of migrating your Home Assistant recorder database to a dedicated MariaDB instance on an external SSD. This setup will not only dramatically improve the responsiveness of your Home Assistant interface, history graphs, and logbook but also significantly extend the lifespan of your primary boot media, providing a foundation for a truly bulletproof smart home ecosystem.
Step-by-Step Setup: MariaDB on External SSD
1. Backup Your Home Assistant Instance
Before making any significant changes, create a full backup of your Home Assistant instance. For Home Assistant OS or Supervised users, navigate to Settings > System > Backups and create a full backup. Download it to a safe location. This is your safety net!

2. Prepare Your External SSD
You'll need a USB to SATA adapter (ensure it's compatible and reliable, especially for Raspberry Pi) and an SSD. Connect the SSD to your Home Assistant host. For Home Assistant OS:
- Go to Settings > System > Storage.
- Locate your newly attached SSD. It might appear as
sdbor similar. - If it's not already formatted, select it and choose Format. It's recommended to format it with a Linux-compatible filesystem like EXT4, though HAOS can handle others.
- Home Assistant OS will typically automount USB drives under
/media/. Make a note of its mount path (e.g.,/media/my_ssd_name). We'll create a subdirectory for MariaDB's data. For example, if your SSD is mounted at/media/usb-SSD-1234, we'll use/media/usb-SSD-1234/mariadb_data.

3. Install and Configure the MariaDB Add-on
For Home Assistant OS and Supervised users, the official MariaDB add-on simplifies installation:
- Go to Settings > Add-ons > Add-on Store.
- Search for 'MariaDB' and install the add-on.
- Once installed, go to the MariaDB add-on's Configuration tab.
- Modify the configuration. Here's a recommended setup. Replace
<YOUR_HA_DB_PASSWORD>with a strong, unique password and<YOUR_SSD_MOUNT_PATH>with the path you identified in Step 2.{
"log_level": "info",
"databases": [
"homeassistant"
],
"username": "homeassistant",
"password": "<YOUR_HA_DB_PASSWORD>",
"data_path": "<YOUR_SSD_MOUNT_PATH>/mariadb_data"
} - Save the configuration.
- Go to the Info tab and enable Start on boot and Watchdog.
- Start the MariaDB add-on. Check the logs tab to ensure it starts without errors. It might take a moment to initialize the database if it's the first run.

4. Configure Home Assistant's Recorder to Use MariaDB
Now, tell Home Assistant to use your new MariaDB database. You'll need access to your Home Assistant configuration files (e.g., via the File Editor add-on or Samba Share).
- Edit your
configuration.yamlfile. - Add or modify the
recordersection to point to your MariaDB instance. Use the same password you set in the MariaDB add-on configuration.recorder:
db_url: mysql://homeassistant:<YOUR_HA_DB_PASSWORD>@core-mariadb/homeassistant?charset=utf8mb4mysql://specifies the database type.homeassistantis the username for HA to connect.<YOUR_HA_DB_PASSWORD>is the password you set.core-mariadbis the hostname for the MariaDB add-on within the Home Assistant OS network (it's the service name)./homeassistantspecifies the database name.?charset=utf8mb4ensures proper character encoding.
- Save the
configuration.yamlfile. - Go to Settings > System > Developer Tools > YAML Configuration and click CHECK CONFIGURATION. Resolve any errors.
- Restart Home Assistant (Settings > System > Restart).
After restarting, Home Assistant will begin writing all new recorder data to the MariaDB instance on your external SSD. Your old SQLite database file (home-assistant_v2.db) will no longer be used or grow in size.
Troubleshooting Section
MariaDB Add-on Fails to Start
- Check Logs: The first step is always to check the MariaDB add-on's logs (Add-ons > MariaDB > Logs). Look for specific error messages.
- Configuration Syntax: Ensure your MariaDB add-on configuration (JSON) is perfectly valid. Even a misplaced comma or quote can prevent it from starting.
- SSD Path: Double-check the
data_path. If the path doesn't exist or isn't writable by the add-on, it will fail. Verify the SSD is correctly mounted and the path is exact. - Port Conflicts: Less common with the add-on, but ensure no other service is trying to use MariaDB's default port (3306).
Home Assistant Fails to Connect to Database
- Recorder Configuration: Verify the
db_urlinconfiguration.yamlmatches exactly, including username, password, hostname (core-mariadb), and database name. - MariaDB Running: Ensure the MariaDB add-on is running and its logs show no errors.
- Password Mismatch: The most common issue. The password in
configuration.yamlmust match the password set in the MariaDB add-on config. - Firewall: (Less likely for HAOS/Supervised) If running on a different system or Docker, check firewall rules between HA and MariaDB.
Slow Performance Post-Migration
- MariaDB Tuning: The default MariaDB settings are generic. See the 'Advanced Config' section for tuning `innodb_buffer_pool_size`.
- Recorder Includes/Excludes: You might still be recording too much data. Fine-tune your recorder settings to only log what you truly need.
- SSD Speed: While an SSD is faster than an SD card, a very low-end USB 2.0 adapter or an extremely cheap SSD can still bottleneck performance.
Advanced Configuration & Optimization
MariaDB Tuning Parameters
For demanding Home Assistant instances, optimizing MariaDB's memory usage can provide significant gains. You can add custom options to MariaDB via the add-on configuration's options section. Here's an example for my.cnf parameters:
{
"log_level": "info",
"databases": [
"homeassistant"
],
"username": "homeassistant",
"password": "<YOUR_HA_DB_PASSWORD>",
"data_path": "<YOUR_SSD_MOUNT_PATH>/mariadb_data",
"options": {
"innodb_buffer_pool_size": "256M",
"query_cache_size": "64M",
"max_connections": 200
}
}innodb_buffer_pool_size: This is critical. It defines the memory area where InnoDB (MariaDB's storage engine) caches data and indexes. Set it to roughly 50-70% of your available RAM if MariaDB is the primary service on the machine (e.g., 256M for a 1GB Raspberry Pi, 512M for 2GB).query_cache_size: Caches query results. Can be helpful, but less impactful than the buffer pool.max_connections: Allows more simultaneous connections. Default is usually fine for Home Assistant.
Fine-tuning the Recorder Integration
Reducing the amount of data written to the database is the most effective way to maintain long-term performance. Use include and exclude in your recorder configuration:
recorder:
db_url: mysql://homeassistant:<YOUR_HA_DB_PASSWORD>@core-mariadb/homeassistant?charset=utf8mb4
commit_interval: 10
purge_keep_days: 7
exclude:
domains:
- automation
- script
- scene
- updater
entities:
- sensor.last_boot
- sensor.cpu_temperature # If you don't need historical data
entity_globs:
- sensor.weather_*_forecast # Exclude all weather forecast sensors
include:
domains:
- sensor
- switch
- light
- binary_sensor
entities:
- sensor.power_consumption_main
- switch.living_room_lightpurge_keep_days: Adjust how long historical data is kept. Keeping it lean (e.g., 7-14 days) drastically reduces database size.commit_interval: How often Home Assistant writes changes to the database (in seconds). A slightly higher value (e.g., 10-15s) can reduce disk writes.exclude/include: Be very selective. Start by excluding domains you rarely need history for (automations, scripts, scenes). Then, exclude specific noisy sensors or entities you don't care to track over time. Conversely, you can useincludeto create a whitelist, only recording specified domains/entities (more restrictive but very effective).
Real-World Example: Accelerating a Power-Hungry Smart Home
Consider a smart home with 70+ Zigbee devices, 15 ESPHome devices monitoring power consumption at multiple points, ambient light sensors updating every 5 seconds, and motion sensors reporting frequently. With the default SQLite on an SD card:
- Initial Setup: UI is snappy for a few weeks.
- 3 Months In: History graphs take 5-10 seconds to load. The logbook is sluggish. Raspberry Pi CPU usage is consistently high due to I/O waits. SD card integrity checks become more frequent after power cycles.
- 6 Months In: Home Assistant startup takes several minutes. The UI often shows a 'Connecting' message. Database corruption occurs, requiring manual intervention or even a full restore.
After migrating to MariaDB on an external 250GB SSD, with the recorder configured to keep 14 days of data and MariaDB's innodb_buffer_pool_size set to 512MB (on a RPi 4 with 4GB RAM):
- History Graphs: Load in 1-2 seconds, even for a full 14-day period.
- Logbook: Instantly responsive.
- System Load: Raspberry Pi CPU idle significantly increases, and I/O wait times drop to near zero.
- Stability: Months without database issues or SD card corruption. The entire system feels more robust and enterprise-grade.
This transformation is especially noticeable for energy monitoring setups, where constantly updating power sensors generate a massive amount of data. MariaDB's indexing and transaction handling capabilities, combined with the SSD's high IOPS, make querying this data a breeze.
Best Practices & Wrap-up
- Quality Hardware: Invest in a reputable brand SSD and a reliable USB 3.0 (if your host supports it) to SATA adapter. Cheap adapters can cause stability issues.
- Regular Purging: Don't keep unnecessary historical data. Use
purge_keep_daysto manage database size. - MariaDB Backups: While Home Assistant full backups include MariaDB, consider using an add-on like 'phpMyAdmin' or 'Terminal & SSH' to perform periodic
mysqldumpbackups of your MariaDB database. This gives you another layer of protection. - Monitor Performance: Keep an eye on your system's resource usage (CPU, RAM, I/O wait) before and after the migration using tools like 'Glances' (another HA add-on) or simple SSH commands like
htoporiostat. - Security: Always use strong, unique passwords for your MariaDB user. Avoid using 'root' for Home Assistant's connection.
- Consider Docker (Advanced): For those running Home Assistant Core or in a more complex Docker environment, you might run MariaDB in a separate Docker container, potentially even on another machine for ultimate separation of concerns and scalability.
Migrating Home Assistant's database to MariaDB on an external SSD is one of the most impactful upgrades you can make for system stability, performance, and longevity. It transforms Home Assistant from a tinkering project susceptible to storage failures into a reliable, enterprise-grade smart home hub capable of handling extensive sensor data and complex automations without breaking a sweat.
NGC 224
Author bio: DIY Smart Home Creator
