Sphinx安装与使用

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
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值