Install Apache Solr 4.4 on Ubuntu 12.04 with Tomcat 7 and MySql Data Import

 

There are a lot of tutorials and questions out there about this setup process, most of them aren’t quite right for Solr 4.4 / Ubuntu 12.04 and Tomcat 7. I’m going to use the example core as a basis for this setup.

 

Install Tomcat & Solr

sudo apt-get install tomcat7 tomcat7-admin

Download and untar solr 4.4 to /usr/share/solr.

 

Next we setup the tomcat application, first issue the following command to copy solr.war into the application root

sudo cp /usr/share/solr/example/webapps/solr.war /usr/share/solr/example/solr/solr.war 

Important! One of the things about running Solr in a container other than the embedded Jetty is that we need to setup the logging. This step is crucial, without doing this solr will never start and you will pull all of your hair out trying to figure out why. Based on the instructions from the Apache Solr Wiki, copy all the jar files from solr/example/lib/ext to /usr/share/tomcat7/lib. Now copy solr/example/resources/log4j.properties to /usr/share/tomcat7/lib. At this point you should set your log path by changing solr.log. I have set mine to /usr/share/solr/logs/

 

Next we add solr to the Catalina config:

cd /etc/tomcat7/Catalina/localhost
sudo nano solr.xml

The config file should look like this:

<Context docBase="/usr/share/solr/example/solr/solr.war" debug="0" crossContext="true">
        <Environment name="solr/home" type="java.lang.String" value="/usr/share/solr/example/solr" override="true" />
</Context>

Let's test the install, we want to use the tomcat application manager but we need to configure users first. Add the following to /etc/tomcat7/tomcat-users.xml

<tomcat-users>
<role rolename="manager-gui"/>
<user username="tomcat" password="secret" roles="manager-gui"/>
</tomcat-users>

Navigating to http://example.com:8080/manager/html, you should see the solr application is running and all is good. If not, check the tomcat logs at /var/log/tomcat7 to see any error messages.

The Install part is pretty standard, so I won't go into any more detail. Though, if you haven't already, I do recommend going through the apache solr tutorial and make sure it’s working with jetty at least. If you did get errors when trying to run it in tomcat, trying the embedded jetty would be a good way to determine if the problem lies with tomcat or solr config.

MySQL Data Import

First we need to download the JDBC driver for MySQL from here http://dev.mysql.com/downloads/connector/j/, untar and place the jar file in the tomcat lib folder /usr/share/tomcat7/lib

Edit the /usr/share/solr/example/solr/collection1/conf/solrconfig.xml file, not too far down he file you'll find a few
tags, add the following:

<lib dir="../../../dist/" regex="solr-dataimporthandler-.*.jar" />
<lib dir="../../../dist/" regex="solr-dataimporthandler-extras-.*.jar" />
<requestHandler name="/dataimport" class="org.apache.solr.handler.dataimport.DataImportHandler">
    <lst name="defaults">
        <str name="config">data-config.xml</str>
    </lst>
</requestHandler>

In /usr/share/solr/example/solr/collection1/conf/ create the file data-config.xml with the following values, adjusting for your particular schema:

<dataConfig>
 <dataSource type="JdbcDataSource"
   driver="com.mysql.jdbc.Driver"
   url="jdbc:mysql://127.0.0.1/DATABASE"
   user="USERNAME"
   password="PASSWORD"
 />
 <document>
   <entity name="id"
    query="select id, title, content from TABLE;">
    <field column="id" name="id"/>
    <field column="title" name="title"/>
    <field column="content" name="description"/>
   </entity>
 </document>
</dataConfig>

At this point you should be able to navigate to example.com:8080/solr/#/collection1/dataimport//dataimport and import your data from MySql into Solr.

 

About Greg Somers

Greg Somers has been working in the web development field for over 10 years. He is currently a MASc candidate at the University of Ottawa in the field of Electrical and Computer Engineering.