How to Install and Configure Sphinx Search on CentOS 7

Table of Contents

Introduction

Sphinx is a free and open source search engine written in C++ that allows you to search full text. Sphinx can be easily integrated into various web applications to provide high quality search results. Sphinx runs on various Linux distributions such as Ubuntu, RHEL, CentOS, Fedora, and many more. It supports various databases like MySQL, PostgreSQL, ODBC. Sphinx allows you to distribute searches across multiple servers.

In this tutorial we will learn how to install and configure Sphinx to index and search a database on CentOS 7.

Requirements

  • A server running CentOS 7.
  • A non-root user with sudo privileges.

Getting Started

Update your system with the latest available packages by running the following command:

sudo yum update -y

Once your system finishes processing any available updates, you can proceed to the next step.

Install and Configure MariaDB

MariaDB is not available in the CentOS default repository. You will need to install and enable the EPEL repository.

You can install the EPEL repository with the following command:

sudo yum install epel-release -y

Once the EPEL repository is installed, you can install MariaDB with the following command:

sudo yum --enablerepo=epel install mariadb mariadb-server

Start the MariaDB service and enable it to automatically start at boot time with the following commands:

sudo systemctl start mariadb
sudo systemctl enable mariadb

The default MariaDB installation is not secure, so you will also need to secure it:

To do so, run the following command:

sudo mysql_secure_installation

Answer all the questions as shown below, making sure to set a password:

Enter current password for root (enter for none): Enter root password
Change the root password? [Y/n] Y
Remove anonymous users? [Y/n] Y
Disallow root login remotely? [Y/n] Y
Remove test database and access to it? [Y/n] Y
Reload privilege tables now? [Y/n] Y

Once you are finished, you can proceed to the next step.

Create a Database for Sphinx

You will need to create a database for testing purpose.

Log in to MariaDB with the following command:

mysql -u root -p

Enter the root password when asked, then create a database named test:

MariaDB [(none)]> create database test;
MariaDB [(none)]> use test;

Next, import the example SQL file to this database:

MariaDB [(none)]> source /usr/share/doc/sphinx-2.2.11/example.sql;

Next exit from the MariaDB shell:

MariaDB [(none)]> exit

Install and Configure Sphinx

You will need to download the latest version of the Sphinx rpm package from the Sphinx official website:

wget http://sphinxsearch.com/downloads/sphinx-2.2.11-1.rhel7.x86_64.rpm
sudo yum install sphinx-2.2.11-1.rhel7.x86_64.rpm

Next, you will need to configure Sphinx.

You can do this by editing the sphinx.conf file:

sudo nano /etc/sphinx/sphinx.conf

Change the values to match the MariaDB user, password, and database as shown below:

    sql_user                = root
    sql_pass                = your-MariaDB-root-password
    sql_db                  = test

Save the file when you are finished making the changes.

Next you will need to create the full-text index.

You can add data to the index using the configuration you created earlier with the following command:

sudo indexer --all

You should see the following output:

Sphinx 2.2.11-id64-release (95ae9a6)
Copyright (c) 2001-2016, Andrew Aksyonoff
Copyright (c) 2008-2016, Sphinx Technologies Inc (http://sphinxsearch.com)

using config file '/etc/sphinx/sphinx.conf'...
indexing index 'test1'...
collected 4 docs, 0.0 MB
sorted 0.0 Mhits, 100.0% done
total 4 docs, 193 bytes
total 0.009 sec, 20091 bytes/sec, 416.40 docs/sec
skipping non-plain index 'testrt'...
total 4 reads, 0.000 sec, 0.1 kb/call avg, 0.0 msec/call avg
total 12 writes, 0.000 sec, 0.1 kb/call avg, 0.0 msec/call avg

It is recommended to keep the index up to date in a production environment.

To do so, create a cron job for this:

crontab -e

Add the following line:

@hourly /usr/bin/indexer --rotate --config /etc/sphinx/sphinx.conf --all

Once Sphinx is configured, you can start the Sphinx service with the following command:

sudo systemctl start searchd

Once everything is set up properly, it is time to test the Sphinx search functionality.

Connect to the SphinxQL using the following command:

mysql -h0 -P9306

Next, search for the word "test" in the test1 database:

MySQL [(none)]> SELECT * FROM test1 WHERE MATCH('test'); SHOW META;

You should see the following results:

+------+----------+------------+
| id   | group_id | date_added |
+------+----------+------------+
|    1 |        1 | 1485715143 |
|    2 |        1 | 1485715143 |
|    4 |        2 | 1485715143 |
+------+----------+------------+
3 rows in set (0.00 sec)

+---------------+-------+
| Variable_name | Value |
+---------------+-------+
| total         | 3     |
| total_found   | 3     |
| time          | 0.000 |
| keyword[0]    | test  |
| docs[0]       | 3     |
| hits[0]       | 5     |
+---------------+-------+
6 rows in set (0.00 sec)

You can see that Sphinx found 3 matches in the test1 database.

Summary

I hope you found this tutorial helpful in getting Sphinx installed on your CentOS server and learning how to perform a simple search. You can find more information on the Sphinx official website. You are welcome to add a comment below or start a new discussion in the community section of this site if you have any questions related to Sphinx search.