May 172010

How to install JDBC driver on Linux

To use MySQL with your java programs, you need to download the MySQL Connector-J from their website.

Download the tar.gz file and place it on a directory that you can access. (Suggested directory is /usr/local). Remeber the name that you used when you downloaded it.

You have access to /usr/local

Downloaded file name is on /usr/local/mysql-jdbc.tar.gz

Installing MySQL Connector-J

To use the Connector-J, you need to unzip and untar it. To do this, you have to enter this command on Linux:

cd /usr/local

tar -zxvf mysql-jdbc.tar.gz

At the end of these commands, the files would be placed in a folder called: mysql-connector-java-version.
Where version is the connector version number.
You can then copy the mysql-connector-java-version-bin.jar file in this directory to the java jre/lib/ext directory. Doing this will allow the java interpreter to find the driver.
Then your JDBC will work fine.

I still could not get my JDBC connector to work. JDBC looks for the mysql-connector-java-x.x.x-bin.jar in jre/lib/ext. I had to find where I installed java which was in (/usr/java/jre1.6.0_20). I copied the .jar file to /usr/java/jre1.6.0_20/lib/ext/) and I still discovered a few problems or things that I was missing:
1. I did not have a $JAVA_HOME variable set
2. I typed “which java” and I discovered that the default java location my system used was (/usr/bin/java). This was not where I installed java.
3. Finally, the directory where I installed java did not have /jre/lib/ext. I only had /usr/java/jre1.6.0_20/lib/ext NOT /usr/java/jre/lib/ext which is what JDBC needed to work correctly.

Problem 1 Solution: To set the global JAVA_HOME variable (Need to be root or have sudo privileges):
From the command line type: echo $JAVA_HOME
You should see the path to your java installation. If you don’t see anything then you don’t have the JAVA_HOME variable set.
To set this up you need to edit or create /etc/profile.d/ and export JAVA_HOME to the path of your java installation. As I stated earlier my java installation path was /usr/java/jre1.6.0_20/. So my /etc/profile.d/ file looks like this:

export JAVA_HOME=/usr/java/jre1.6.0_20
export PATH=$JAVA_HOME/bin:$PATH

After you save this file you need to implement the JAVA_HOME variable by typing: source /etc/profile.d/
Then you need to reload your profile by typing: . ./.bash_profile
Now when you type “echo $JAVA_HOME” you should see /usr/java/jre1.6.0_20.

Problem 2 Solution: This should have been fixed when you sourced /etc/profile.d/ If you type “which java” you should see /usr/java/jre1.6.0_20/bin/java or the equivalent if your java installation is different from mine.

Problem 3 Solution: Change directory to /usr/java/jre1.6.0_20 and create a symbolic link to jre by typing “ln -s /usr/java/jre1.6.0_20 jre”. This will force the JDBC connector to look in the correct path. If you want to test it type: cd $JAVA_HOME/jre/lib/ext. You should be in /usr/java/jre1.6.0_20/jre/lib/ext. Your .jar files should be in here too.