Thursday, October 11, 2012

Mediatomb handles my music collection with Mysql

Recently I've started streaming audio and video from my music collection to my ASUS Transformer tablet. For the most part this works really well, except that my music collection is rather huge (7355 tracks so far).

In particular, I'm using MediaTomb to index and stream the media over the network. What's really nice about MediaTomb is that there's no client configuration needed to find the server; the client discovers the server using uPNP. A close second is that MediaTomb uses the inotify facility on the system to detect and index files as they're added to the filesystem. This means that resource-intensive periodic scans of the filesystem are not necessary.

MediaTomb worked really well for streaming videos, but my video collection is rather small. When I tried to index and stream my music collection, it would choke. As it turns out, the default installation of MediaTomb on Debian uses SQLite3 to store the backend database. SQLite, put simply, doesn't scale. I've seen this in other applications as well; I seem to recall trying to deploy Bacula with a SQLite3 backend, with similar catastrophic results.

Anyway, because MediaTomb was trying to use SQLite, the database would get corrupted when trying to add more than a few hundred audio tracks. I tried adding them piecemeal, but it was to no avail; tracks would be missing, or would be in the list, but wouldn't play. Finally I decided it was time to get my hands dirty; MediaTomb will use MySQL, if you tell it to, and I already had a MySQL backend running on my server for other things.

So here's what I did (Thanks to Socrateos for the instructions):

  • Start by stopping mediatomb on the server. Then delete the old SQLite3 database, which by default is stored at /var/lib/mediatomb/mediatomb.db
  • Edit /etc/mediatomb/config.xml
    • Find the <storage> section
    • Change "sqlite3 enabled" to "no"
    • Change "mysql enabled" to "yes"
    • Set the "host", "username" and "password" fields in the mysql section. On my installation, the password attribute was missing, so I created it.
  • Save the file and exit
  • Next, create a database user:

    • $ mysql -u root -p 
    • mysql> create user 'mediatomb'@'localhost' identified by 'secret';
    • mysql> grant all on mediatomb.* to 'mediatomb'@'localhost';
    • mysql> quit
  • Now create the mysql database...:
    • $ mysql -u mediatomb -p 
    • mysql> create database mediatomb;
    • mysql> quit
  • ...and populate it
    • $ mysql mediatomb -u mediatomb -p < /usr/share/mediatomb/mysql.sql
  • Lastly, start the server
    • $ sudo /etc/init.d/mediatomb start
After this, I was able to connect to my MediaTomb server and scan my entire music library in at once. After MySQL hammered on my CPU for a few minutes doing thousands upon thousands of INSERTs, the database was complete and functioning.

No comments:

Post a Comment