MySQL and Matlab

Our data lab runs an open-source database server called MySQL, designed for robustness and speed. On the MySQL home page you can read all about it. To interface to the server, you need a client program which implements the specific MySQL protocol. Then you can execute queries using standard Structured Query Language (SQL).

For mathematical and statistical analysis, the best way to use the data is to download it directly into Matlab. We have written a MySQL client for Matlab that handles the interaction. Here are binary versions for Linux and Windows, and source code and instructions in case you want to look at it or modify it.

Author: Robert Almgren.


Binary

Be sure your browser is correctly set to download binary files!
mysql.m
This M-file contains the help text, displayed if you type help mysql. It is a short summary of how to use the client program.

Linux x86

mysql.mexglx
This is compiled against Matlab Version 7 (Release 14), with gcc 3.3.4 on a Pentium 4. It may or may not work with other software and hardware; it almost certainly will not work with Matlab Version 6 (Release 13). Put this and mysql.m either into your working directory, or into a subdirectory matlab in your Unix home directory, or into $MATLAB/toolbox/local ($MATLAB is Matlab's home directory, usually /usr/local/matlab.) It has been statically linked against the MySQL libraries as described below, so you shouldn't need any other libraries.

Windows

mysql.dll
This is compiled against Matlab Version 4 (Release 14) and MySQL 4.1.11, on x86 using Microsoft Visual C++ 2003. Put this and mysql.m into $MATLAB\toolbox\local, or keep them in your working directory. ($MATLAB means Matlab's home directory, usually something like C:\Program Files\MATLAB704.)
libmysql.dll
This is the MySQL client library software, version 4.1.11 from the MySQL Download page. Put this into C:\WINDOWS\system32.

Test it from inside Matlab with

mysql('status')
If it says "Not connected" then you are up and running!

Additional useful files

datelabel.m
Make axis labels look like reasonable dates. It is like Matlab's dateaxis except that it works better (you can zoom in, then type datelabel again to get new labels).

Compiling from source code

As far as I know, these source versions can be compiled against any reasonably recent versions of MySQL and of Matlab, and I don't know of any particular platform dependences although I have tried only Linux and Windows

mysql.cpp
The C++ source code.
mysql.m
The help text.

Linux/Unix

From the MySQL Download page, get either a full version, or the "Include files and libraries for development" if you use RPMs. Then compile with
mex -I/usr/include/mysql -L/usr/lib/mysql -lmysqlclient mysql.cpp
from either the shell command line or the Matlab prompt. I have the MySQL include files and libraries in /usr/include/mysql and /usr/lib/mysql respectively; modify as necessary if your system is different. (Look for the include file mysql.h and the library libmysqlclient.a.) This will produce the file mysql.mexglx which you can install as described above.

Windows

From the MySQL Download page get the whole package including server, 18 Mb. Any recent version should be all right but this is most heavily tested against MySQL 4.0.x. I recommend the option "Without installer" for manual unpacking. Unpack and install; I'll assume you're putting it in the default location C:\mysql. Copy C:\mysql\lib\opt\libmySQL.dll into C:\WINDOWS\system32.

Be sure that you have a C++ compiler installed (I'm using Microsoft Visual Studio 7.0 .NET), and that you have auto-configured Matlab with "mex -setup".

Now from a Matlab command line, cd to the directory containing the source program, and compile it with

mex -I"C:\mysql\include" -DWIN32 mysql.cpp "C:\mysql\lib\opt\libmySQL.lib"
This will produce the file mysql.dll as above, which you may install as described there.