MySQL is currently the most popular open-source database server. With recent developments, it provides numerous features that make it competitive with the big commercial packages. It is probably the easiest database to get started on, and there is no end of examples in using MySQL with various Java applications like Tomcat.
Start by going to the main download page, http://www.mysql.com, and downloading the latest version, 4.0.16 as of this writing, for the appropriate platform. The page http://www.mysql.com/downloads/mysql-4.0.html lists all the various options. For my Windows XP machine, I chose the file mysql-4.0.16-win.zip, and for my Linux server, I used mysql-standard-4.0.16-pc-linuxi686.tar.gz.
You also need the Java Database Connectivity (JDBC) drivers for MySQL, which is a separate download from http://www.mysql.com/downloads/api-jdbc-stable.html. The current version, 3.0.9, comes in a gzipped or zipped file. Pick whatever is easier.
Installing MySQL is pretty easy. For Windows, just double-click the downloaded zip file to open it and run the setup.exe file inside. You get a few prompts, but you can just take the defaults, which means MySQL is installed in c:\mysql.
For Unix, you need to be user root to install MySQL. You can unpack the distribution anywhere you want. I typically use the /opt directory, but you can choose something else. After you install it, you need to go through a few steps to get it up and running. First, add a mysql user and group by typing
groupadd mysql useradd -g mysql mysql
Now, cd into the distribution and execute the script to set up all the system tables:
cd /opt/mysql-standard-4.0.16-pc-linux-i686 scripts/mysql_install_db chown -R mysql data chgrp -R mysql .
Again, this is only for Unix installations. Note that my directory reflects my installation of version 4.0.16; adjust as appropriate for your location and version.
For the JDBC driver package, you have to unpack it as well and locate the actual jar file, which is called mysql-connector-java-3.0.9-stable-bin.jar. Remember the discussion of $CATALINA_HOME/common and $CATALINA_HOME/shared in the last chapter? There you learned that all objects in these directories are available to all Web applications, but that the former are also available to Tomcat, whereas the latter are not. Here is an example of a situation where you need to use the common directory. Because you want Tomcat to maintain the database connection pool, you must put the MySQL JDBC driver jar into $CATALINA_HOME/common/lib.
Before you start MySQL, I should point out that by default, the default login to MySQL is the admin user, root, and there is no password. Obviously, you need to productionalize this area, but that is not the point in this chapter. If you do decide to change any of it now, or if you are using an existing MySQL installation that has modified these defaults, then remember to substitute your login information in my examples.
Starting MySQL on Linux
Starting MySQL on Linux is easy: just go to the distribution directory and type
bin/mysqld_safe --user=mysql &
You get a couple of startup messages and then you should be able to type the following to enter the command-line utility:
If you are successful, the command-line utility should respond with a message like the following:
Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 2 to server version: 4.0.16-standard Type 'help;' or '\h' for help. Type '\c' to clear the buffer. mysql>
Then you know you are ready to go.
If for some reason you don't get this response, look in the data directory for an *.err file. Usually, this file tells you why the server could not start.
Starting MySQL on Windows
On Windows, it is easiest to install MySQL as a service and then start and stop the service. To do so, go to the bin directory of the MySQL installation, which is c:\mysql by default, and type the following:
You should get the message Service successfully installed. Now, go into Services, under Control Panel, Administrative Tools; find the mysql service; and start it. If you'd like, you can set it to autostart when Windows starts.
Finally, you can test your installation by going back to the MySQL bin directory and typing
You should see the same response shown earlier, in the section "Starting MySQL on Linux."
Creating the Table
By default, MySQL comes with a system database called mysql. The first thing you have to do is create one for yourself, called unleashed. You do so by starting the client program and typing
create database unleashed
Now you can create and populate a table to hold various Tomcat-related resources. To simplify things, I've provided a sample SQL script, shown in Listing 3.3.
Listing 3.3 Table Create Script
use unleashed; create table TomcatResources ( url varchar(255) not null, title varchar(255) not null ); insert into TomcatResources values ( 'http://www.onjava.com/pub/a/onjava/2003/06/25/tomcat_tips.html? page=2&x-showcontent=off', 'ONJava.com: Top Ten Tomcat Configuration Tips [Jun. 25, 2003]'); insert into TomcatResources values ( 'http://jakarta.apache.org/tomcat', 'The Jakarta Site - Apache Tomcat'); insert into TomcatResources values ( 'http://www.moreservlets.com/Using-Tomcat-4.html', 'Apache Jakarta Tomcat 4 and 5: Configuration and Usage Tutorial'); insert into TomcatResources values ( 'http://www.jguru.com/faq/Tomcat', 'Java Guru: Tomcat FAQ Home Page');
Because the point is to test database connectivity, I'm not loading up the table (yet) with the many considerable Tomcat-related resources on the Net!
To run this script, all you have to do is save it as something like unleashed.sql and type
mysql < unleashed.sql
This creates and populates your table. If you want to make sure, go back into the command-line utility, change databases, and select from the table:
use unleashed; select * from TomcatResources;
With our database and table ready, we can now hook it up to Tomcat.