A “QuickNote” about how to connect MATLAB to a remote MySQL server through an SSH tunnel.
(updated 12/21/2011 to avoid port issues with Cisco VPN)
The server only accepts encrypted connections and I couldn’t figure out an easy way to use SSL directly within the JDBC driver. Additional note: the server’s main role is the head node of a high-performance computing cluster and my goal with the laptop is to perform simple testing before spawning lots of parallel processes on the cluster. MySQL is used to store and retrieve data across these parallel processes without clobbering each other.
- MATLAB with Database Toolbox (tested using R2011b pre-release)
- An account on a server running MySQL (tested server running MySQL 5.1.58)
- A command-line SSH tool. I am running Mac OSX (10.6), so this is included and accessed through Terminal
- Download and extract the latest (platform independent) Connector/J driver from MySQL (tested using 5.1.17)
- Move the file
mysql-connector-java-VERSION-bin.jarto a suitable permanent home. I put mine at
- Tell MATLAB where to look for this file by editing the MATLAB specific Java classpath.txt file. Easiest way to find this file is to open MATLAB and type
This will bring up the file in the MATLAB editor. Ignore any comments about the file being autogenerator (so far so good). Somewhere in this file add the following line (modify with your path and version number)
Establishing the Connection
- Go to a command prompt (Terminal on OSX) and establish an SSH tunnel using something like the following:
ssh -f firstname.lastname@example.org -L 7800:server.edu:3306 -N
Notes: -L is the key. It sets up an ssh tunnel that connects port 7800 on the localhost (laptop) to port 3306 (default MySQL port) on the server; -f runs ssh in the background; and -N tells ssh to not enter any commands once the tunnel is open. Replace user with your username on the server (probably different than your database user name), and server.edu (in both places) with the appropriate MySQL server name. There is nothing special about port 7800 so feel free to pick your favorite number; but do use >1024 to avoid most critical ports, and check the tcp/udp port number list on wikipedia to make sure some other application doesn’t use it.
- Enter your password (server password not MySQL password). Now your SSH tunnel is setup and you can connect anything to localhost:7800 and have it look like you are connected the server’s MySQL service at server.edu:3306
- Now get MATLAB to use this connection using:
conn = database('test', 'username', 'passwd', 'com.mysql.jdbc.Driver', 'jdbc:mysql://localhost:7800/test')
in this case you use your MySQL username and password. ‘test’ specifies the database (schema) to access within the server, replace it in both places with the appropriate database to access.
- Finally to get data from this connection first establish a cursor by issuing an SQL command:
curs = exec(conn,'SELECT * FROM test_table');
and then fetch the data from it (here we only get 10 results at a time)
curs = fetch(curs, 10);
and finally look at the data (as a cell array)
See the MATLAB database toolbox for more ways to use the connection such as writing data, altering the table structure, etc.