Skip to main content

Howto setup a MySQL Connector/J 5.1 for Tomcat on Linux

Again, I'm not switching to Java. :) For clarity, I'm helping one of my online buddies to setup and use Ruby even as I write this. This work was something I had to do for a Rails project which used JSPs and stuff with a MySQL database over JDBC. The application setup was quite interesting calling JSPs to work with a Rails webapp.

Actually the following things are found on the Internet. I cannot remember all the sources I looked at, but one was the MySQLs own documentation and Apache Tomcat documentations. So if this works (which in my case did), credit should not be mine. :)

Here's the setup.
  • GNU/Linux (in my case CentOS 5, although should work with any Linux distro)
  • Apache Tomcat (5.5.25, should work with Tomcat 5.5 range)
  • Sun JDK (1.6.0_04)
  • MySQL (5.0.22)
  • MySQL Connector/J (5.1)

1. I assume that Java is setup (See my previous post for more details on setting up Java manually), and your MySQL is running on the same host on port 3306. Please replace your actual settings if they are different.

2. First, lets set up Apache Tomcat 5.5.
If you already have Tomcat up and running, feel free to skip to step 2.
You can download it from (http://tomcat.apache.org/). For my case I downloaded Apache Tomcat version 5.5.25 (Eg: apache-tomcat-5.5.25.tar.gz).

Extract the downloaded archive to get the tomcat (Eg: tar xzvf apache-tomcat-5.5.25.tar.gz). This will give a directory with a name similar to apache-tomcat-5.5.25.

Move this directory to a place where you'd run it as the Tomcat server.
Eg: cp -R ~/apache-tomcat-5.5.25 /var/tomcat

Now set the variables, CATALINA_HOME, JAVA_HOME, JDK_HOME. One way of doing this is by adding those to /etc/profile file (Eg: sudo vi /etc/profile).
Eg: Add these lines to the end of /etc/profile:
CATALINA_HOME=/var/tomcat
JAVA_HOME=/usr/java/jdk1.6.0_04
JDK_HOME=/usr/java/jdk1.6.0_04
export CATALINA_HOME JAVA_HOME JDK_HOME

To make sure it takes effect, you can log out and log in, or just close the terminal and start a new thing.

Then you can start the Tomcat server by running the startup.sh script which comes with Tomcat. Shutdown script is called shutdown.sh. They are in the bin directory of your Tomcat directory.
Eg: $/var/tomcat/bin/startup.sh


2. Now lets move to Connector/J setup. Download MySQL Connector/J from (http://www.mysql.com/products/connector/j/). In my case I downloaded version 5.1. You'll get a .tar.gz file
Eg: mysql-connector-java-5.1.5.tar.gz

This writeup assumes this connector version. It is the latest as of this writing, but if the version differs, the following configuration instruction may not work.

3. Extract the connector.tar.gz archive to get a .jar file, which is the actual connector.
Eg: tar xzvf mysql-connector-java-5.1.5.tar.gz

This will most probably create a directory with a name something like mysql-connector-java-5.1.5. In that you'll find a directory structure where the .jar file (Eg: mysql-connector-java-5.1.5-bin.jar) will be in the topmost level.

4. Copy the .jar file (Eg: mysql-connector-java-5.1.5-bin.jar) to your $CATALINA_HOME/common/lib (i.e: common/lib directory within your Tomcat directory.)
Eg: sudo cp ~/mysql-connector-java-5.1.5/mysql-connector-java-5.1.5-bin.jar /var/tomcat/common/lib/

5. Create a context configuration file for Tomcat.
Create a configuration file in your $CATALINA_HOME/conf/ which has the file name apps-yourapp.xml
Eg: If your applications name is myapp then,
$ vi /var/tomcat/conf/apps-myapp.xml

6. Enter the things found here or in this file. When you copy and paste these code, please remember to replace values for Context path, docBase, Resource name, ResourceParams name, username, password and url with your values.

Eg: If your application is located under /var/tomcat/webapps/myapp and it's accesible via http://yourdomain.tld/myapp then your Context path would be "/myapp" (where it's accessible in URL) and docBase would be "webapps/myapp" (where it's available on file system).

7. Restart your Tomcat can now you are good to go. In your Java code you can use the JDBC connection in something like:
Connection conn = DriverManager.getConnection("jdbc:mysql://yourdomain.tld/my_database?" + "user=myuser&password=mypassword");

Comments

  1. Thank you so much, that helped me immensely!

    ReplyDelete
  2. @Anja: I'm glad it helped. Honestly, I had even forgotten about this post. Basic concepts like setting the proper env variables must me the same, but now there should better ways to get things running I think. Plus new Linux distros could have OpenJDK ready and tomcat in their package repositories.

    Anyway, thanks for stopping by to post a comment. :)

    ReplyDelete
  3. Anonymous11:36 PM

    Great post, this filled a big documentation / knowledge gap, thanks!

    ReplyDelete
  4. HI , I dont see any commn dir in my tomcat.
    Please help.

    ReplyDelete
  5. Gmail support service is available 24x7. Contact Gmail Toll-Free number + 1855-558-1999 and get help quickly. If you have any queries related to Gmail account or you are unable to login. Our technical team contacts to you and resolve your issue in a minimal time.

    Gmail tech support number
    Gmail Toll-Free number
    Gmail help support number
    Gmail customer Service Number
    Gmail customer care phone number
    Gmail tech support number
    Gmail customer Service Number
    Gmail help support number
    Yahoo email Toll-Free number
    Yahoo mail help support number
    Yahoo mail customer service number

    ReplyDelete

Post a Comment

Popular posts from this blog

Howto Install Docky on Fedora

If you know me personally, then you know that I'm a big fan of GNOME Do . As a keyboard savvy person I use Do extensively. Do is an application launcher similar to the Mac app Quicksilver. However the GNOME Do team has been putting a lot of research and development into it from the initiation. Result: probably the best application launcher out there for any platform. Some months ago, Do included an interesting theme called Docky which made the launcher acts as a dock (a la Mac, Avant, Cairo Dock, etc.). With the integration of GNOME Do, there's no need to say that Docky was super cool. And it started gaining features in a high speed. Ultimately Docky was getting so developed that it became a separate project. Installing GNOME Do on a Fedora system is as easy as: $ sudo yum install gnome-do There are some packages with the names starting from gnome-do-plugins*. With the addition of these GNOME Do can truly enhance your desktop experience. Give it a fair try, I'm pretty

Howto Migrate from Thunderbird to Evolution

I know some of you are asking why , rather than how , regarding migration from Mozilla Thunderbird to Evolution. Maybe that's why there are lot of Evolution to Thunderbird migration guides, but not many vice-versa. Fear not, here is a guide, to assist who dare to migrate from Thunderbird to Evolution. The techniques described here are tested with the newer versions of both the software, namely Thunderbird 2.0.0.4 and Evolution 2.10.2. On higher versions also this should work without an issue. I think Mozilla people are doing a wonderful job with both Firefox and Thunderbird . From my point of view Firefox is the best general purpose web browser around. It beats most proprietary browser in speed, stability, security, modularity, etc. (and don't start commenting the so and so browsers are greater or so and so is cool too. I know they may be, Fx is simply my choice. This also applies to any comparisons with Evolution too :) However Fxs' counterpart in e mail business, is not y

Howto Setup a Subversion (svn) Repository for a Rails Project + Bonus

Setting up a Subversion (svn) repository is something development teams have to do fairly regularly, not that I want to use Subversion. :) If you listen to me, go use Git . Subversion is undoubtedly very good. But after using Git for about a year, you can't simply get me to switch back. Git is that good. :) I've written about Git before . In cases where you can't use Git (or you feel too castrated by TortoiseSVN, pardon me for the pun) you can use Subversion. In this post I'll go through the steps you have to follow to get a basic Subversion setup up and running on a CentOS 5 Linux host. However I think you should be able to use this on other Linux distros too. There's more than one way to host a Subversion repo. I'm going to stick with one way involving WebDAV. Don't mind the buzz word. It's the most common usage for this purpose. If you want a repository where you want to checkout and commit remotely, this is an easy way of getting it done. In addition