Using MySQL to Sync XBMC Libraries

Subscribe to Syndicate
Tags: 

I use XBMC as my Home Theater PC software solution. You can add media files to the system by pointing it at a location on some storage device. It will then scan that location and scrape metadata from the internet to populate it's library database with that media information.

If you have multiple XMBC systems in your house, and wish to keep this library of information synchronized, you can manually export the library on one XMBC pc, and import it into another, using some removable media. This can get to be a hassle. The solution is to point all of your XMBC machines to a central database. This assumes all of the XMBC machines have access to the same media files.

Most of this information can be found at XBMC Wiki

Requirements:

  • Multiple XMBC pcs using the same media repositories
  • A “server” to store the database (can be the same server serving up your media files)
  • MySQL database software which can be obtained either through your server os's distribution or from MySQL

 
Before you do anything, backup your XBMC by doing an export of the Library. Choose the XBMC machine with the most up-to-date version of the library you wish to place into the database. You can find this at System → Settings → Video and System → Settings - Video. Towards the bottom of this screen you will see the Export option. I exported the library to a removable usb stick.

Installing MySQL

I am installing MySQL on a RHEL/CentOS 6 system, so I will use be using yum to install it. This will also install any dependencies that are required. If using an Ubuntu system, just use apt-get as usual. If using Windows, download the MySQL installer from the link above.

[root@server ~]# yum install mysql mysql-libs mysql-server

Now we start the server:

[root@server ~]# service mysqld start
Initializing MySQL database:  Installing MySQL system tables...
OK
Filling help tables...
OK
 
To start mysqld at boot time you have to copy
support-files/mysql.server to the right place for your system
 
PLEASE REMEMBER TO SET A PASSWORD FOR THE MySQL root USER !
To do so, start the server, then issue the following commands:
/usr/bin/mysqladmin -u root password 'new-password'
/usr/bin/mysqladmin -u root -h server.domain password 'new-password'
 
Alternatively you can run:
/usr/bin/mysql_secure_installation
 
which will also give you the option of removing the test
databases and anonymous user created by default.  This is
strongly recommended for production servers.
 
See the manual for more instructions.
 
You can start the MySQL daemon with:
cd /usr ; /usr/bin/mysqld_safe &
 
You can test the MySQL daemon with mysql-test-run.pl
cd mysql-test ; perl mysql-test-run.pl
 
Please report any problems with the /usr/bin/mysqlbug script!
 
The latest information about MySQL is available on the web at
http://www.mysql.com
Support MySQL by buying support/licenses at http://shop.mysql.com
                                                           [  OK  ]
Starting mysqld:                                           [  OK  ]
[root@server ~]#

Be sure to tell the system to start this service upon reboot:

[root@server ~]# chkconfig mysqld on
[root@server ~]#

We should now set a password for the mysql admin user (root), and delete unnecessary users/databases. We can accomplish this by running the supplied script:

[root@server ~]# /usr/bin/mysql_secure_installation
 
NOTE: RUNNING ALL PARTS OF THIS SCRIPT IS RECOMMENDED FOR ALL MySQL
      SERVERS IN PRODUCTION USE!  PLEASE READ EACH STEP CAREFULLY!
 
In order to log into MySQL to secure it, we'll need the current
password for the root user.  If you've just installed MySQL, and
you haven't set the root password yet, the password will be blank,
so you should just press enter here.
 
Enter current password for root (enter for none):
OK, successfully used password, moving on...
 
Setting the root password ensures that nobody can log into the MySQL
root user without the proper authorisation.
 
Set root password? [Y/n] Y
New password:
Re-enter new password:
Password updated successfully!
Reloading privilege tables..
 ... Success!
 
By default, a MySQL installation has an anonymous user, allowing anyone
to log into MySQL without having to have a user account created for
them.  This is intended only for testing, and to make the installation
go a bit smoother.  You should remove them before moving into a
production environment.
 
Remove anonymous users? [Y/n] Y
 ... Success!
 
Normally, root should only be allowed to connect from 'localhost'.  This
ensures that someone cannot guess at the root password from the network.
 
Disallow root login remotely? [Y/n] Y
 ... Success!
 
By default, MySQL comes with a database named 'test' that anyone can
access.  This is also intended only for testing, and should be removed
before moving into a production environment.
 
Remove test database and access to it? [Y/n] Y
 - Dropping test database...
 ... Success!
 - Removing privileges on test database...
 ... Success!
 
Reloading the privilege tables will ensure that all changes made so far
will take effect immediately.
 
Reload privilege tables now? [Y/n] Y
 ... Success!
 
Cleaning up...
 
All done!  If you've completed all of the above steps, your MySQL
installation should now be secure.
 
Thanks for using MySQL!
 
[root@server ~]#

Configure the SQL Database For XBMC

We can now log into the mysql server:

[root@server ~]# mysql -u root -p
Enter password:
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 10
Server version: 5.1.67 Source distribution
 
Copyright (c) 2000, 2011, Oracle and/or its affiliates. All rights reserved.
 
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
 
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
 
mysql>

Now we can create a user for XBMC to use. We will grant this user NO privs to ANY database from any address. We do NOT want to allow the XBMC user to be a superuser and have the ability to change the mysql permissions database, or any other database you may be have or create at a later date.

mysql> GRANT USAGE ON *.* TO 'xbmc'@'%' IDENTIFIED BY 'xbmc';
Query OK, 0 rows affected (0.00 sec)
 
mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)
 
mysql>

Now, we need to allow the xbmc user to create and manipulate any database XMBC will need access to. The databases, by default, are:

  • MyVideos*
  • MyMusic*

 
XBMC will always append the database format version number to the end of the databases it creates, so the MyMusic database may get created as MyMusic32 and MyVideos as MyVideos75.

We may, at some point, wish to use profiles. In this case, you would modify the advancedsettings.xml file to point to something like profile1MyVideos. The database will again get created with a number appended to the name. Therefore, when we setup the database user, we want to give him enough privileges to create these databases, as well as manipulate the tables within them.

Since the GRANT command does not allow wildcard database names, we will use the INSERT command.

mysql> INSERT INTO `mysql`.`db` (`Host`, `Db`, `User`, `Select_priv`, `Insert_priv`,
    -> `Update_priv`, `Delete_priv`, `Create_priv`, `Drop_priv`, `Grant_priv`,
    -> `References_priv`, `Index_priv`, `Alter_priv`, `Create_tmp_table_priv`,
    -> `Lock_tables_priv`, `Create_view_priv`, `Show_view_priv`, `Create_routine_priv`,
    -> `Alter_routine_priv`, `Execute_priv`, `Event_priv`, `Trigger_priv`)
    -> VALUES ('%', '%MyVideos%', 'xbmc', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y',
    -> 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y');
Query OK, 0 rows affected (0.00 sec)
 
mysql> INSERT INTO `mysql`.`db` (`Host`, `Db`, `User`, `Select_priv`, `Insert_priv`,
    -> `Update_priv`, `Delete_priv`, `Create_priv`, `Drop_priv`, `Grant_priv`,
    -> `References_priv`, `Index_priv`, `Alter_priv`, `Create_tmp_table_priv`,
    -> `Lock_tables_priv`, `Create_view_priv`, `Show_view_priv`, `Create_routine_priv`,
    -> `Alter_routine_priv`, `Execute_priv`, `Event_priv`, `Trigger_priv`)
    -> VALUES ('%', '%MyMusic%', 'xbmc', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y',
    -> 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y');
Query OK, 0 rows affected (0.00 sec)
 
mysql>

Now we update the privilege tables so mysql knows about the new user and it's privileges:

mysql> FLUSH PRIVILEGES;
Query OK, 0 rows affected (0.00 sec)
 
mysql>quit
Bye
[root@server ~]#

Setting Up XBMC to Access the MySQL Database

Now, we need to tell XBMC where it's metadata is stored for Videos and Music. We do this by editing the advancedsettings.xml file. This is located in the userdata folder. If one does not exist, we will create one. If it does exist, we will just append the settings to the end of the file.

[root@server ~]# ssh root@xbmc
##############################################
# OpenELEC - The living room PC for everyone #
# ...... visit http://www.openelec.tv ...... #
##############################################
 
OpenELEC (official) Version: 3.0.3
root@xbmc-pi's password: 
openelec:~ # cd .xbmc/userdata/
openelec:~/.xbmc/userdata # cat <<EOF >>advancedsettings.txt
 
<advancedsettings>
    <videodatabase>
        <type>mysql</type>
        <host>192.168.1.100</host>
        <port>3306</port>
        <user>xbmc</user>
        <pass>xbmc</pass>
    </videodatabase> 
 
    <musicdatabase>
        <type>mysql</type>
        <host>192.168.1.100</host>
        <port>3306</port>
        <user>xbmc</user>
        <pass>xbmc</pass>
    </musicdatabase>
 
     <videolibrary>
          <importwatchedstate>true</importwatchedstate>
     </videolibrary>
</advancedsettings>
EOF
openelec:~ #

Note: Change the IP address to the IP address of your MySQL server. If you do not wish to import the watched status of content, change the importwatchedstate to false.

Note: If you have multiple profiles, you should add a <name></name> line in each of the Video and Music sections. Do this for each profile.. so it looks like this:

<advancedsettings>
    <videodatabase>
        <type>mysql</type>
        <host>192.168.1.100</host>
        <port>3306</port>
        <user>xbmc</user>
        <pass>xbmc</pass>
        <name>profile1MyVideos</name>
    </videodatabase> 
 
    <musicdatabase>
        <type>mysql</type>
        <host>192.168.1.100</host>
        <port>3306</port>
        <user>xbmc</user>
        <pass>xbmc</pass>
        <name>profile1MyMusic</name>
    </musicdatabase>

Each profile has its OWN advancedsettings.xml file and will get placed in the cooresponding userdata/profiles/[profilename] folder. The master advancedsettings.xml file will not have the <name></name> tag line, and will still reside in the userdata folder.

Exit the shell and reboot the XBMC box.

Import your library from an export if you have one, or just rescan your library. I found the import/export of the library information was a bit flaky. Some of the metadata would import, some of it would not. I ended up just rescrapping all of my media. Of course, now I only need to do this from one XBMC machine.

You should now be able to update your library from any XBMC box in your house, and see the updated metadata on your other machines.