安装前请先确定安装了常用的组件,然后在官方网站下载最新的Sphinx,
yum install -y python python-develhttp://sphinxsearch.com/downloads/release/ 请使用稳定版,线上机器就别玩beta版啦! 安装sphinx
tar zxvf sphinx-2.1.4-release.tar.gz cd sphinx-2.1.4-release ./configure --prefix=/usr/local/sphinx –-with-mysql make && make install
在make时如果出现undefined reference to libiconv的错,
错误描述
/usr/local/sphinx/src/sphinx.cpp:20060:undefined reference to `libiconv_open'
/usr/local/sphinx/src/sphinx.cpp:20078: undefined reference to `libiconv'
/usr/local/sphinx/src/sphinx.cpp:20084: undefined reference to `libiconv_close'
collect2: ld returned 1exit status
make[2]:***[indexer]Error1
make[2]:Leaving directory `/home/sphinx/src'
make[1]: *** [all] Error 2
make[1]: Leaving directory `/home/sphinx/src'
make: *** [all-recursive] Error 1
解决办法:打开configure文件,找到“#define USE_LIBICONV 1”,将注释去掉,并将1改成0。
libsphinxclient 安装(PHP模块需要)
cd api/libsphinxclient ./configure --prefix=/usr/local/sphinx make && make install
安装PHP的Sphinx模块
下载地址:http://pecl.php.net/package/sphinx
wget http://pecl.php.net/get/sphinx-1.3.0.tgz tar zxf sphinx-1.3.0.tgz cd sphinx-1.3.0 /usr/local/PHP/bin/phpize ./configure --with-php-config=/usr/local/php/bin/php-config --with-sphinx=/usr/local/sphinx/ make && make install
安装好后,在安装目录下etc目录下,有份测试数据和配置的样本
cd /usr/local/sphinx/etc cp sphinx.conf.dist sphinx.conf
将sphinx.conf里面的数据库地址,账号密码改下就好了,再将lvtao.sql导进数据库lvtao
测试执行
/usr/local/sphinx/bin/search lvtao
就可以看到一些查询的相关信息了
创建索引
创建索引命令:indexer
-c 指定配置文件
--all 对所有索引重新编制索引
--rotate 用于轮换索引,在不停止服务的时候(searchd运行时)增加索引;searchd运行时不加会报错。
--merge 合并索引,增量索引合并到主索引的时候用
生成全部索引: /usr/local/sphinx/bin/indexer -c /usr/local/sphinx/etc/sphinx.conf --all
或指定索引(例如main): /usr/local/sphinx/bin/indexer -c /usr/local/sphinx/etc/sphinx.conf main
如果出现下面的报错
- 1
- 2
- 3
- 4
我是直接把sphinx下面的config/config.h 改成了他提示的这个 #define USE_LITTLE_ENDIAN = 1,之后再make的,
(1)如果这里出现报错:
【ERROR: index 'main': sql_connect: Can't connect to local MySQL server through socket '/tmp/mysql.sock'】
没找到/tmp/mysql.sock, 通过find / -name mysql.sock -print查找到位置,在配置sphinx.conf里更改正确。
如:mysql_sock = /var/lib/mysql/mysql.sock 保存退出。
(2)继续创建索引,警告:
【WARNING: Attribute count is 0: switching to none docinfo】
改sphinx.conf里的docinfo = none就没有警告了。(http://sphinxsearch.com/docs/current.html#conf-docinfo)
创建索引出现如下提示,表示生成成功:
3. 启动Sphinx
重建索引:./searchd -c /usr/local/sphinx/etc/sphinx.conf
轮换索引: ./searchd -c /usr/local/sphinx/etc/sphinx.conf goods_list --rotate
./searchd -c /usr/local/sphinx/etc/sphinx.conf store_list --rotate
停止服务:./searchd -c /usr/local/sphinx/etc/sphinx.conf --stop