How to Install and Configure Sphinx in CentOS

To Install and Configure Sphinx on CentOS

Sphinx is an open source search engine which can efficiently be used for full-text searches. Even very large data can be thoroughly searched with Sphinx. It can search on any data ranging from SQL databases, plain text files, HTML files, mailboxes, and so on. In this tutorial, you will be given an in-depth guidance on how to install and configure Sphinx on CentOS.


Installation of Sphinx

In this tutorial, Sphinx is setup with MySQL server with the sample SQL file included in the distribution package.

The installation of Sphinx requires prior installation of dependency packages. Use the following command for the same.

[root@linuxhelp1 ~]# yum install postgresql-libs unixODBC mariadb-server -y
Loaded plugins: fastestmirror, langpacks
Loading mirror speeds from cached hostfile
 * base: mirror.fibergrid.in
 * extras: mirror.fibergrid.in
 * updates: mirror.fibergrid.in
Resolving Dependencies
-->  Running transaction check
--->  Package mariadb-server.x86_64 1:5.5.50-1.el7_2 will be installed
-->  Processing Dependency: mariadb(x86-64) = 1:5.5.50-1.el7_2 for package: 1:mariadb-server-5.5.50-1.el7_2.x86_64
-->  Processing Dependency: perl-DBI for package: 1:mariadb-server-5.5.50-1.el7_2.x86_64
-->  Processing Dependency: perl-DBD-MySQL for package: 1:mariadb-server-5.5.50-1.el7_2.x86_64
-->  Processing Dependency: perl(Data::Dumper) for package: 1:mariadb-server-5.5.50-1.el7_2.x86_64
-->  Processing Dependency: perl(DBI) for package: 1:mariadb-server-5.5.50-1.el7_2.x86_64
--->  Package postgresql-libs.x86_64 0:9.2.15-1.el7_2 will be installed  
--->  Package unixODBC.x86_64 0:2.3.1-11.el7 will be installed
-->  Running transaction check
--->  Package mariadb.x86_64 1:5.5.50-1.el7_2 will be installed
--->  Package perl-DBD-MySQL.x86_64 0:4.023-5.el7 will be installed
.
.
.
Installing : perl-DBD-MySQL-4.023-5.el7.x86_64                                                                                       9/12
  Installing : 1:mariadb-server-5.5.50-1.el7_2.x86_64                                                                                 10/12
  Installing : unixODBC-2.3.1-11.el7.x86_64                                                                                           11/12
  Installing : postgresql-libs-9.2.15-1.el7_2.x86_64                                                                                  12/12
  Verifying  : perl-Compress-Raw-Bzip2-2.061-3.el7.x86_64                                                                              1/12
  Verifying  : perl-Net-Daemon-0.48-5.el7.noarch                                                                                       2/12
  Verifying  : 1:mariadb-server-5.5.50-1.el7_2.x86_64                                                                                  3/12
  Verifying  : perl-Data-Dumper-2.145-3.el7.x86_64                                                                                     4/12
  Verifying  : postgresql-libs-9.2.15-1.el7_2.x86_64                                                                                   5/12
  Verifying  : unixODBC-2.3.1-11.el7.x86_64                                                                                            6/12
  Verifying  : perl-PlRPC-0.2020-14.el7.noarch                                                                                         7/12
  Verifying  : 1:perl-Compress-Raw-Zlib-2.061-4.el7.x86_64                                                                             8/12
  Verifying  : perl-DBD-MySQL-4.023-5.el7.x86_64                                                                                       9/12
  Verifying  : 1:mariadb-5.5.50-1.el7_2.x86_64                                                                                        10/12
  Verifying  : perl-DBI-1.627-4.el7.x86_64                                                                                            11/12
  Verifying  : perl-IO-Compress-2.061-2.el7.noarch                                                                                    12/12

Installed:
  mariadb-server.x86_64 1:5.5.50-1.el7_2          postgresql-libs.x86_64 0:9.2.15-1.el7_2          unixODBC.x86_64 0:2.3.1-11.el7         

Dependency Installed:
  mariadb.x86_64 1:5.5.50-1.el7_2           perl-Compress-Raw-Bzip2.x86_64 0:2.061-3.el7     perl-Compress-Raw-Zlib.x86_64 1:2.061-4.el7    
  perl-DBD-MySQL.x86_64 0:4.023-5.el7       perl-DBI.x86_64 0:1.627-4.el7                    perl-Data-Dumper.x86_64 0:2.145-3.el7          
  perl-IO-Compress.noarch 0:2.061-2.el7     perl-Net-Daemon.noarch 0:0.48-5.el7              perl-PlRPC.noarch 0:0.2020-14.el7              

Complete!

Move into the tmp directory to download Sphinx' s files.

[root@linuxhelp1 ~]# cd /tmp

Now, inside the directory download the latest Sphinx version using wget command.

[root@linuxhelp1 tmp]# wget http://sphinxsearch.com/files/sphinx-2.2.11-1.rhel7.x86_64.rpm
--2016-11-14 14:59:52--  http://sphinxsearch.com/files/sphinx-2.2.11-1.rhel7.x86_64.rpm
Resolving sphinxsearch.com (sphinxsearch.com)... 78.47.162.181
Connecting to sphinxsearch.com (sphinxsearch.com)|78.47.162.181|:80... connected.
HTTP request sent, awaiting response... 200 OK
Length: 6548504 (6.2M) [application/octet-stream]
Saving to: ‘ sphinx-2.2.11-1.rhel7.x86_64.rpm’ 

100%[==================================================================================================> ] 6,548,504   88.3KB/s   in 63s    

2016-11-14 15:00:56 (101 KB/s) - ‘ sphinx-2.2.11-1.rhel7.x86_64.rpm’  saved [6548504/6548504]

Finally, use the following command to install Sphinx.

[root@linuxhelp1 tmp]# yum install sphinx-2.2.11-1.rhel7.x86_64.rpm -y
Loaded plugins: fastestmirror, langpacks
Examining sphinx-2.2.11-1.rhel7.x86_64.rpm: sphinx-2.2.11-1.rhel7.x86_64
Marking sphinx-2.2.11-1.rhel7.x86_64.rpm to be installed
Resolving Dependencies
-->  Running transaction check
--->  Package sphinx.x86_64 0:2.2.11-1.rhel7 will be installed
-->  Finished Dependency Resolution

Dependencies Resolved

============================================================================================================================================
 Package                  Arch                     Version                            Repository                                       Size
============================================================================================================================================
Installing:
 sphinx                   x86_64                   2.2.11-1.rhel7                     /sphinx-2.2.11-1.rhel7.x86_64                    22 M
.
.
.
To query search daemon using MySQL client:
    mysql -h 0 -P 9306
    mysql>  SELECT * FROM test1 WHERE MATCH(' test' ) 

See the manual at /usr/share/doc/sphinx-2.2.11 for details.

For commercial support please contact Sphinx Technologies Inc at
http://sphinxsearch.com/contacts.html

  Verifying  : sphinx-2.2.11-1.rhel7.x86_64                                                                                             1/1

Installed:
  sphinx.x86_64 0:2.2.11-1.rhel7                                                                                                            

Complete!

Now start the MariaDB service with the following command.

[root@linuxhelp1 tmp]# systemctl start mariadb.service

Use the following command to do the secure installation.

[root@linuxhelp1 tmp]# mysql_secure_installation
/usr/bin/mysql_secure_installation: line 379: find_mysql_client: command not found

NOTE: RUNNING ALL PARTS OF THIS SCRIPT IS RECOMMENDED FOR ALL MariaDB
      SERVERS IN PRODUCTION USE!  PLEASE READ EACH STEP CAREFULLY!

In order to log into MariaDB to secure it, we' ll need the current
password for the root user.  If you' ve just installed MariaDB, and
you haven' t set the root password yet, the password will be blank,
so you should just press enter here.

Enter current password for root (enter for none):
OK, successfully used password, moving on...

Setting the root password ensures that nobody can log into the MariaDB
root user without the proper authorisation.

Set root password? [Y/n] y
New password:
Re-enter new password:
Password updated successfully!
Reloading privilege tables..
 ... Success!

By default, a MariaDB installation has an anonymous user, allowing anyone
to log into MariaDB without having to have a user account created for
them.  This is intended only for testing, and to make the installation
go a bit smoother.  You should remove them before moving into a
production environment.

Remove anonymous users? [Y/n]
 ... Success!

Normally, root should only be allowed to connect from ' localhost' .  This
ensures that someone cannot guess at the root password from the network.

Disallow root login remotely? [Y/n]
 ... Success!

By default, MariaDB comes with a database named ' test'  that anyone can
access.  This is also intended only for testing, and should be removed
before moving into a production environment.

Remove test database and access to it? [Y/n]
 - Dropping test database...
 ... Success!
 - Removing privileges on test database...
 ... Success!

Reloading the privilege tables will ensure that all changes made so far
will take effect immediately.

Reload privilege tables now? [Y/n]
 ... Success!

Cleaning up...

All done!  If you' ve completed all of the above steps, your MariaDB
installation should now be secure.

Thanks for using MariaDB!

Let' s import the sample SQL file into the database. First, log in to the MySQL server shell with the following command.

In order to test the efficiency of Sphinx search, a database is setup using the sample SQL file available with the package.

[root@linuxhelp1 tmp]# mysql -u root -p

Enter password:
Welcome to the MariaDB monitor.  Commands end with   or g.
Your MariaDB connection id is 10
Server version: 5.5.50-MariaDB MariaDB Server

Copyright (c) 2000, 2016, Oracle, MariaDB Corporation Ab and others.

Type ' help '  or ' h'  for help. Type ' c'  to clear the current input statement.

You can create a dummy database here.
You can name the database as you wish.

MariaDB [(none)]>  CREATE DATABASE test 
Query OK, 1 row affected (0.00 sec)

Import the sample SQL file and then exit MariaDB as follows.

MariaDB [(none)]>  SOURCE /usr/share/doc/sphinx-2.2.11/example.sql 
Query OK, 0 rows affected, 1 warning (0.00 sec)

Query OK, 0 rows affected (0.01 sec)

Query OK, 4 rows affected (0.00 sec)
Records: 4  Duplicates: 0  Warnings: 0

Query OK, 0 rows affected, 1 warning (0.00 sec)

Query OK, 0 rows affected (0.00 sec)

Query OK, 10 rows affected (0.00 sec)
Records: 10  Duplicates: 0  Warnings: 0

MariaDB [(none)]>  q
Bye

Create a new sphinx.conf with the following command.

There are three main components to the Sphinx.conf file - index, search id and source block. Each of them is explained here.

[root@linuxhelp1 ~]# vim /etc/sphinx/sphinx.conf

The Source block contains all the necessary information for MySQL server such as source type, username and password. Make sure that the first column of the sql_query has a unique id. The SQL query will run on every index and dump the data to Sphinx index file.

In the above source file the index has the source and path which can store the data. The source contains the name of the source block which in this case is src 1. The index is saved through path.

The port and other variables which are needed to run the Sphinx daemon is present in the Searchd component. Following are the attributes of the Searchd components.

Attributes

Description

Listen The port number for the Sphinx Daemon to run: protocol used
query_log The path in which the query log is saved
pid_file PID file path of the Sphinx daemon
seamless_rotate restricts searchd stalls when indexes with huge data is rotated to precache
preopen_indexes Preopens all the indexes on startup
unlink_old deletes old indexes copies on successful rotation


Now add data to the Sphinx index.

 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.046 sec, 4151 bytes/sec, 86.04 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 necessary to check if the index is up to date. Use the crontab command to do that as follows.

[root@linuxhelp1 ~]# crontab &ndash e

If you want to start and check the status of the Sphinx service, use the following command.

[root@linuxhelp1 ~]# systemctl start searchd
[root@linuxhelp1 ~]# systemctl status searchd
 searchd.service - SphinxSearch Search Engine
Loaded: loaded (/usr/lib/systemd/system/searchd.service  disabled  vendor preset: disabled)
Active: active (running) since Mon 2016-11-14 15:07:44 IST  18s ago
Process: 1791 ExecStart=/usr/bin/searchd --config /etc/sphinx/sphinx.conf (code=exited, status=0/SUCCESS)
Process: 1787 ExecStartPre=/bin/chown sphinx.sphinx /var/run/sphinx (code=exited, status=0/SUCCESS)
.
.
.
Nov 14 15:07:44 linuxhelp1 searchd[1791]: [212B blob data]
Nov 14 15:07:44 linuxhelp1 systemd[1]: searchd.service: Supervising process 1795 which is not our child. We' ll most likely not no...t exits.
Nov 14 15:07:44 linuxhelp1 systemd[1]: Started SphinxSearch Search Engine.
Hint: Some lines were ellipsized, use -l to show in full.

You can test search functionality of Sphinx with the following command.

[root@linuxhelp1 ~]# mysql -h0 -P9306
Welcome to the MariaDB monitor.  Commands end with   or g.
Your MySQL connection id is 1
Server version: 2.2.11-id64-release (95ae9a6)

Copyright (c) 2000, 2016, Oracle, MariaDB Corporation Ab and others.

Type ' help '  or ' h'  for help. Type ' c'  to clear the current input statement.

MySQL [(none)]> 

You can search a sentence by making use of the following command. The result of the given search is displayed below.

MySQL [(none)]>  SELECT * FROM test1 WHERE MATCH (' test document' )  SHOW META 
+------+----------+------------+
| id   | group_id | date_added |
+------+----------+------------+
|    1 |        1 | 1479116046 |
|    2 |        1 | 1479116046 |
+------+----------+------------+
2 rows in set (0.00 sec)

+---------------+----------+
| Variable_name | Value    |
+---------------+----------+
| total         | 2        |
| total_found   | 2        |
| time          | 0.001    |
| keyword[0]    | test     |
| docs[0]       | 3        |
| hits[0]       | 5        |
| keyword[1]    | document |
| docs[1]       | 2        |
| hits[1]       | 2        |
+---------------+----------+
9 rows in set (0.01 sec)

You can also search for the keywords. Use the following command for the same.

MySQL [(none)]>  CALL KEYWORDS (' test one three' , ' test1' , 1) 
+------+-----------+------------+------+------+
| qpos | tokenized | normalized | docs | hits |
+------+-----------+------------+------+------+
| 1    | test      | test       | 3    | 5    |
| 2    | one       | one        | 1    | 2    |
| 3    | three     | three      | 0    | 0    |
+------+-----------+------------+------+------+
3 rows in set (0.00 sec)

Once you are done searching, you can leave MySQL shell with the following command.

MySQL [(none)]>  q
Bye

Comment
muhammadalizia
Aug 28 2017
i am using latest cpanel v66.0.15 on centos 6.9 x64 final on a vps and want to use sphinx for my existing site's mariadb 10 mysql databases. I would really appreciate if you can post a tutorial for cpanel v66.0.15 on centos 6.9 x64. thank you for the awesome tutorial.
Add a comment
FAQ
Q
How to start mariadb service on Sphinx in CentOS?
A
To start mariadb service on Sphinx in CentOS use the following command

#systemctl start mariadb.service
Q
How to install Sphinx in CentOS CLi?
A
To install Sphinx in CentOS CLi use the following command?

yum install sphinx-2.2.11-1.rhel7.x86_64.rpm -y
Q
What is Sphinx?
A
Sphinx is an open source search engine which can efficiently be used for full-text searches. Even very large data can be thoroughly searched with Sphinx
Q
What are the alternatives available for Sphinx ?
A
the alternatives available for Sphinx were Doxygen ,
GitBook,
Q
When installing Sphinx i got some error. How to continue with installation in ubuntu ?
A
To install Sphinx in ubuntu follow this link : " https://www.linuxhelp.com/how-to-install-and-configure-sphinx-in-ubuntu/ "