Apr 15

Hey all,

A different subject to normal. The anti-haskell: Javaish stuff!!

Well, I got a Mac recently and was trying to get a Tomcat server with MySQL integration going, and had a built of difficulty on the way!!

Note: For this tutorial I use the Bash terminal. To set your Mac to use Bash by default, as it uses Zsh by default, open a terminal, and then click:

Terminal->Preferences->Startup->Shell opens with: custom command

and make sure that the input box reads:

/bin/bash

I found this tutorial: Ben Slade’s tutorial. This is a good starting point but I think it’s a bit out-dated. So my tutorial here is a modernised version of that.

Firstly, you have to have the Java JDK installed, to find out type this into a terminal:

javac -version

If you get an error message, then you don’t have it installed, so head over to the Apple Developer Center and get the latest XCode. That should install it for you. :D

Next, go and get MySQL for OSX here and get the OSX version. This will download a disk image that will be mounted for you. In that double-click the .dmg file that starts with “mysql-……”. This will install MySQL for you. Next double-click the MySQLStartupItem.pkg, and confirm the prompts. This will enable an auto-start of MySQL when you boot into OSX.

Now a bit of setup for MySQL:

Restart your terminal (so that it’s definately a bash terminal), and type the following:

nano ~/.bash_profile

If that file doesn’t exist, thats fine. Just create it.

In the editor that opens up, type the following:

PATH=$PATH:/usr/local/mysql/bin

This loads the MySQL command line utilities when you open a bash terminal.

Now would be a good time to reboot your Mac, just to make sure everything loads alright. After rebooting, you need to set user passwords for the root account. Open up a terminal and type the following:

mysql -u root

To remove anonymous logins now type the following:

DELETE FROM mysql.user WHERE User = ' ';
FLUSH PRIVILEGES;

Now, find your computers names:

SELECT DISTINCT(Host) AS Host FROM mysql.user WHERE Host != "localhost";

You should see something like:

————————-
| Host |
————————-
| 127.0.0.1 |
| username’s-macbook.local |
————————-

Now type:

SET PASSWORD FOR 'root'@'localhost' = PASSWORD('new_pass');
SET PASSWORD FOR 'root'@'username's-macbook.LOCAL' = PASSWORD('new_pass');

Where new_pass is your new password.

Now make a test login, exactly like below…

CREATE DATABASE javatest;
GRANT ALL ON javatest.* TO 'some_login'@'localhost' IDENTIFIED BY 'some_password';
FLUSH PRIVILEGES;

Where some_login is the username you will be using earlier and some_password is the password you will use to log some_login in.
flush privileges;

Next, get Jakarta Tomcat and download the latest stable release, for OSX. Download it to ~/. Next you need to copy the tar to your local directory. Do the following:

sudo mv apache-tomcat-<i>version-number</i>.tar.gz /usr/local
cd /usr/local
sudo tar -xf apache-tomcat-<i>version-number</i>.tar.gz
sudo rm apache-tomcat-<i>version-number</i>.tar.gz

Now create a symlink for Tomcat for easy access:

sudo ln -s apache-tomcat-<i>version-number</i> tomcat

Now take ownership of the files:

sudo chown -R $LOGNAME: apache-tomcat-<i>version-number</i>

Now set up the $CATALINA_HOME variable to point to the tomcat installation:

nano ~/.bash_profile

And add the following line:

CATALINA_HOME=/usr/local/tomcat

Now, create two scripts - one for starting the tomcat server and one for stopping it:

 nano ~/bin/tomcat_start.sh

And add the following to that:

#!/bin/sh
$CATALINA_HOME/bin/startup.sh

Then:

nano ~/bin/tomcat_stop.sh

And add the following to that:

#!/bin/sh
$CATALINA_HOME/bin/shutdown.sh

Make the two scripts you just created executable:
$ chmod u+x ~/bin/*_tomcat

And add the ~/bin directory to your $PATH variable:

nano ~/.bash_profile

And add this to it:

PATH=$PATH:/Users/$LOGNAME/bin

Now, you can use tomcat_start to start the server:

$ tomcat_start

Now load up Safari and go to http://localhost:8080 and you should see the tomcat default page.

Next, run your tomcat_stop script, and type the following into terminal:

nano $CATALINA_HOME/conf/tomcat-users.xml

Add the following into the file:

<user username="myusername" password="mypassword" roles="tomcat,admin,manager"/>

Now to sort out the connectivity between tomcat and SQL download the latest ConnectorJ. When that's downloaded, copy it into the java extensions directory:

cp connectorJFileName /Library/Java/Extension
Tagged with:
preload preload preload