Sphinx is a free and open source full-text search engine which can be integrated into various web applications to provide rapid and high quality search results.
This article will show you how to setup a Sphinx search engine to index and search an example MariaDB database on a Vultr CentOS 7 server instance. It will give you some preliminary ideas of how to use Sphinx in your project.
- Deploy a fresh CentOS 7 server instance from the Vultr control panel.
- Use a non-root sudo user to log in from your SSH terminal. See how to create a sudo user in this article.
Step 1: Update your system
sudo yum update -y && sudo reboot
After the system reboot finishes, use the same sudo user to log in again.
Step 2: Install and configure MariaDB
Sphinx can be used to search various data sources, like SQL databases, plain text files, HTML files, etc. Here, let’s have a look at how to use Sphinx to perform searches upon a MariaDB database.
Install MariaDB using YUM:
sudo yum install mariadb mariadb-server
Start the MariaDB service:
sudo systemctl start mariadb.service sudo systemctl enable mariadb.service
For security purposes, perform the secure MySQL installation:
Finish this procedure in accordance with the following instructions:
Enter current password for root (enter for none): Press Enter Set root password? [Y/n]: Input Y, then press Enter New password: Input a new password for root, press Enter Re-enter new password: Input the same password again, then press Enter Remove anonymous users? [Y/n]: Input Y, then press Enter Disallow root login remotely? [Y/n]: Input Y, then press Enter Remove test database and access to it? [Y/n]: Input Y, then press Enter Reload privilege tables now? [Y/n]: Input Y, then press Enter
After the secure MySQL installation finishes, you will have to set a new password for the MySQL user “root”. In this tutorial, I’ll refer to it as “yourpassword”.
Step 3: Install the latest Sphinx
In order to install the latest version of Sphinx, you need to download the proper rpm package from the Sphinx official website:
cd ~ wget http://sphinxsearch.com/files/sphinx-2.2.10-1.rhel7.x86_64.rpm sudo yum install sphinx-2.2.10-1.rhel7.x86_64.rpm
Step 4: Create a database for testing
Use the following MySQL commands to create a database named “test”:
mysql -u root -p -e "CREATE DATABASE test"
When you are asked to provide a password, input the one you set earlier and then press Enter.
Now, import test data from a Sphinx example sql file:
mysql -u root -p test < /usr/share/doc/sphinx-2.2.10/example.sql
Again, input the same password and then press Enter to finish the import.
Step 5: Configure Sphinx
Open the Sphinx configuration file
sudo vi /etc/sphinx/sphinx.conf
For now, you only need to setup the MySQL username
sql_user and the password
sql_pass, which would be:
sql_user = root sql_pass = yourpassword
Save and quit:
Step 6: Start the indexer and searchd daemons
Both indexer and searchd are important components of Sphinx. Indexer is in charge of gathering data from the data source, and searchd is the part of the search engine which actually handles searches.
Be sure to run the following commands orderly, or you will encounter errors.
sudo indexer --all sudo searchd
To update the index status regularly, create a cron job:
sudo crontab -e
Input the following entry:
0 * * * * /usr/bin/indexer --rotate --config /etc/sphinx/sphinx.conf --all
Save and quit:
This cron job will update the index status hourly.
Step 7: Test your Sphinx search engine
You can use a predefined python script to test your setup of Sphinx:
python /usr/share/sphinx/api/test.py this is my test document
This command will search “this is my test document” in the example MySQL database and then show you the search result.
That concludes this tutorial. Sphinx also includes a search API, which can be used from your own PHP, Perl, Python, Ruby or Java projects. More information is available on the Sphinx official website.
Want to contribute?
You could earn up to $300 by adding new articles
Suggest an update
Request an article
Leave a Reply