meta data for this page
  •  

This is an old revision of the document!


= MATLAB database connectivity for Mac (and also Linux, it is basically the same) ==

This page has instructions for how to connect MATLAB to the ICE-D MySQL database

Basically, this has two parts.

First, configure the database connectivity aspects of MATLAB. Once you have done this, MATLAB can function as a MySQL client just as HeidiSQL, Sequel Pro, MySQL workbench, etc., with the added benefit that you can include database queries in MATLAB scripts.

Second, connect MATLAB to the remote database. The main difference between using a typical MySQL client and MATLAB is that MATLAB doesn't internally handle the case where you are connecting through a database using an SSH tunnel. Thus, you have to configure the SSH tunnel externally to MATLAB and then point MATLAB to it.

Configuring MATLAB database toolbox

The first step here is to install the MATLAB 'Database Toolbox'. How to do this depends on your personal or institutional licensing setup for MATLAB. If you have an individual license, you will have to buy the toolbox. If you are working through a university license, typically it is included but not installed, so you have to figure out how to download and install it on your machine. Precisely how to do this varies with the version of MATLAB that you are using.

The second step is to locate and open the MATLAB help documentation for the database toolbox. This is also available online at mathworks.com. This will include instructions for how to install a 'JDBC driver' for MySQL. The online version of this as of 20220526 is here. Most likely, this will point you to this page, where you will download the 'platform independent'driver.

Once you have downloaded the JDBC driver (a '.jar' file) and stored it somewhere on your computer, you need to tell MATLAB where to find it. This procedure varies with MATLAB version. On current MATLAB (R2022), the procedure is here, although so far you don't have enough information to fill out all the boxes in the 'create data source' dialog. On older versions, this procedure involves adding to the MATLAB 'javaclasspath' variable to tell MATLAB where to locate the file.

Setting up SSH tunnel

Unlike other GUI MySQL clients, the setup dialog in the MATLAB 'database explorer' doesn't have an internal ability to connect through an SSH tunnel. Thus, you have to set up the SSH tunnel externally. To do this, open a Terminal window. You are going to run a terminal command to start the tunnel. Suppose the following:

โ€“ The ICE-D MySQL server is located at 123.456.789.10 (this is a made-up address, not the real one) โ€“ The SSH tunnel host has the domain name ssh.tunnel.host.org (also made-up, not the real one) โ€“ your login to the SSH tunnel host is 'iced' (this is the real one)

Then issue the following command in a terminal window:

ssh -f iced@ssh.tunnel.host.org -L 12345:123.456.789.10:3306 -N &

What this does is forward port 3306 (the default MySQL port) on the remote MySQL server, through the SSH tunnel host, to localhost port 12345 on the local machine. Thus, it will appear to a database client (such as MATLAB in this case) that the database is located at localhost:12345. The '&' tells the command to run in the background and return the command line.

Note that this won't work because the SSH host and database IP address don't exist. You'll need to put in the correct values. If you have already been using a different MySQL client, you know what these are. If not, ask Greg or Joe.

One additional item of importance is that when you are done with this, you will have to shut down the SSH process that is keeping the tunnel open. Even if you do nothing, it will eventually die next time your computer goes to sleep, but it is bad practice to leave zombie processes all over the place. Unfortunately, shutting it off is a bit complicated. First, you have to identify the process that is running SSH. To do this, type in the terminal:

ps aux | grep 3306

This will generate a bunch of information that will look something like this:

Connecting MATLAB to the database through the SSH tunnel

Now that you have an ssh tunnel open to the database, you need to point MATLAB to it. On current MATLAB, it appears the easiest way to do this is to use the 'JDBC data source configuration' dialog that you have already opened. What this does is create a persistent description of a database connection that you can refer to by name from the command line. So, for example, this:

will create a data source description with the name 'new_ICED_reader_through_tunnel' (the name is arbitrary, you can call it whatever you want) and the correct connection parameters to point to the local end of the SSH tunnel. The 'Driver location' box will be wherever you put the .jar driver file you installed earlier.

The last thing is that after you are done with this you need to stop the SSH tunnel. You don't really need to, it will die by itself when your laptop goes to sleep after a while, but it is good practice. First, locate the ssh process by typing in the terminal:

ps aux | grep 3306

This will return something like:

balcs 7966 0.0 0.0 4268344 728 s001 S+ 11:01PM 0:00.00 grep 3306 balcs 7652 0.0 0.0 4352368 852 ?? Ss 10:51PM 0:00.01 ssh -f iced@ssh.tunnel.host.org -L 12345:3123.446.789.10:3306 -N -v

This tells you that the ssh tunnel is process 7652. You can kill this process by typing 'kill 7652'. Careful not to mistype and start killing off other random processes.