Sphinx安装
首先你要有mysql环境,我使用的是lnmp环境
下载并安装Sphinx,版本可自由选择
wget http://sphinxsearch.com/files/sphinx-2.2.11-release.tar.gz
tar -zxvf sphinx-2.2.11-release.tar.gz
cd sphinx-2.2.11-release.tar.gz
./configure --prefix=/usr/local/sphinx --with-mysql=/usr/local/mysql
make && make install
创建数据
在mysql中建表
CREATE TABLE `article` (
`id` int(11) NOT NULL,
`content` varchar(200) NOT NULL DEFAULT '',
`createtime` datetime NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
插入数据
INSERT INTO `article` VALUES ('1', 'hello world', '2016-11-11 00:36:23');
INSERT INTO `article` VALUES ('2', 'php is greater than others', '2016-11-10 00:36:59');
配置sphinx
进入Sphinx配置文件所在目录
cd /usr/local/sphinx/ect/
你能看到三个文件:
example.sql
sphinx.conf.dist
sphinx-min.conf.dist
我们使用第三个,最简单的配置
cp sphinx-min.conf.dist sphinx.conf
vim sphinx.conf
配置文件如下:
source article_src
{
type = mysql
sql_host = localhost
sql_user = root
sql_pass = root
sql_db = sphinx
sql_port = 3306 # optional, default is 3306
sql_query = \
SELECT id, content,createtime FROM article
#sql_attr_uint = group_id
#sql_attr_timestamp = date_added
}
index article
{
source = article_src
path = /usr/local/sphinx/var/data/article
}
indexer
{
mem_limit = 128M
}
searchd
{
listen = 9312
listen = 9306:mysql41
log = /usr/local/sphinx/var/log/searchd.log
query_log = /usr/local/sphinx/var/log/query.log
read_timeout = 5
max_children = 30
pid_file = /usr/local/sphinx/var/log/searchd.pid
seamless_rotate = 1
preopen_indexes = 1
unlink_old = 1
workers = threads # for RT to work
binlog_path = /usr/local/sphinx/var/data
}
建立索引
/usr/local/sphinx/bin/indexer --all
索引建立成功之后,就可以使用search
命令进行查找
/usr/local/sphinx/bin/search keyword
不过新版本取消了这个命令,如果你安装的是旧版本就有这个命令
如果你安装的版本不支持search,可以使用php连接Sphinx进行测试,请参考在php中使用Sphinx
启动服务
在/usr/local/sphinx/bin/
目录下还有一个searchd
命令,用于启动sphinx服务
/usr/local/sphinx/bin/searchd
停止服务
/usr/local/sphinx/bin/searchd --stop