~~~~~~~~~~~~~~Step 1 — Installing Sphinx~~~~~~~~~~~~~~~~~~
Installing Sphinx on Ubuntu is easy because it's in the native package repository. Install it using apt-get.
sudo apt-get install sphinxsearch
~~~~~~~~~~~~~~~~Step 2 – Creating the Test Database~~~~~~~~~~~~~~~
Next, we'll set up a database using the sample data in the SQL file provided with the package. This will allow us to test that Sphinx search is working later.
Let's import the sample SQL file into the database. First, log in to the MySQL server shell.
mysql -u root -p
Enter the password for the MySQL root user when asked. Your prompt will change to mysql>.
Create a dummy database. Here, we're calling it test, but you can name it whatever you want.
CREATE DATABASE test;
Import the example SQL file.
SOURCE /etc/sphinxsearch/example.sql;
Then leave the MySQL shell.
quit
~~~~~~~~~~~~~~~~~~~~~Step 3 – Configuring Sphinx~~~~~~~~~~~~~~~~~~
First, create the sphinx.conf file.
sudo nano /etc/sphinxsearch/sphinx.conf
Each of these index, searchd, and source blocks are described below. Then, at the end of this step, the entirety of sphinx.conf is included for you to copy and paste into the file.
The source block contains the type of source, username and password to the MySQL server. The first column of the sql_query should be a unique id. The SQL query will run on every index and dump the data to Sphinx index file. Below are the descriptions of each field and the source block itself.
type: Type of data source to index. In our example, this is mysql. Other supported types include pgsql, mssql, xmlpipe2, odbc, and more.
sql_host: Hostname for the MySQL host. In our example, this is localhost. This can be a domain or IP address.
sql_user: Username for the MySQL login. In our example, this is root.
sql_pass: Password for the MySQL user. In our example, this is the root MySQL user's password.
sql_db: Name of the database that stores data. In our example, this is test.
sql_query: The query thats dumps data from the database to the index.
// full conf file sample given. You have to change all things as your requirment i.e. source name, host,user,pass,db table name etc...
{
source src1
type = mysql
sql_host = localhost
sql_user = root //////////////////// database username
sql_pass = your_root_mysql_password //////////////////// password
sql_db = test //////////////////// database name
sql_port = 3306
sql_query = \
SELECT id, group_id, UNIX_TIMESTAMP(date_added) AS date_added, title, content \
FROM documents
sql_attr_uint = group_id
sql_attr_timestamp = date_added
}
index test1
{
source = src1 /////////same as above source
path = /var/lib/sphinxsearch/data/test1 ////////// test1 is indexname. change as your requirement
docinfo = extern
}
searchd
{
listen = 9306:mysql41
log = /var/log/sphinxsearch/searchd.log
query_log = /var/log/sphinxsearch/query.log
read_timeout = 5
max_children = 30
pid_file = /var/run/sphinxsearch/searchd.pid
seamless_rotate = 1
preopen_indexes = 1
unlink_old = 1
binlog_path = /var/lib/sphinxsearch/data
}
~~~~~~~~~~~~~~~~~Step 4 — Managing the Index~~~~~~~~~~~~~~
In this step, we'll add data to the Sphinx index and make sure the index stays up to date using cron.
First, add data to the index using the configuration we created earlier.
sudo indexer --all
You should get something that looks like the following.
Output
Sphinx 2.2.9-id64-release (rel22-r5006)
Copyright (c) 2001-2015, Andrew Aksyonoff
Copyright (c) 2008-2015, Sphinx Technologies Inc (http://sphinxsearch.com)
using config file '/etc/sphinxsearch/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.010 sec, 18552 bytes/sec, 384.50 docs/sec
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
In production environments, it is necessary to keep the index up to date. To do that let's create a cronjob. First, open crontab.
crontab -e
The follow cronjob will run on every hour and add new data to the index using the configuration file we created earlier. Copy and paste it at the end of the file, then save and close the file.
@hourly /usr/bin/indexer --rotate --config /etc/sphinxsearch/sphinx.conf --all
~~~~~~~~~~~~~~~~~~~~Step 5 — Starting Sphinx~~~~~~~~~~~~~~
By default, the Sphinx daemon is tuned off. First, we'll enable it by changing the line START=no to START=yes in /etc/default/sphinxsearch.
sudo sed -i 's/START=no/START=yes/g' /etc/default/sphinxsearch
Then, use systemctl to restart the Sphinx daemon.
sudo systemctl restart sphinxsearch.service
To check if the Sphinx daemon is running correctly, run.
sudo systemctl status sphinxsearch.service
ALL STEPS AT A GLANCE
1 sudo apt-get install sphinxsearch
2 mysql -u root -p // have to give mysql password
3 CREATE DATABASE test;
4 SOURCE /etc/sphinxsearch/example.sql;
5 quit
6 sudo nano /etc/sphinxsearch/sphinx.conf // copy given sample.conf file to this file
7 sudo indexer --all
8 crontab -e //copy below line and paste it in cronjob and save it.
@hourly /usr/bin/indexer --rotate --config /etc/sphinxsearch/sphinx.conf --all
9 sudo sed -i 's/START=no/START=yes/g' /etc/default/sphinxsearch
10 sudo systemctl restart sphinxsearch.service
11 sudo systemctl status sphinxsearch.service
No comments :
Post a Comment