ubuntu安装sphinx以及配置php sphinx测试环境

在配置php sphinx的时候 遇到了很多坑,在此记录下。

我的环境:

ubuntu 12.04

php 5.6.16

mysql 5.5

apache 2.3

sphinx server 2.2.10-id64-release

sphinx client(php) 1.3.3

安装步骤:

切换成root用户进行安装: sudo su

一,安装服务端。Sphinx Server

以ubuntu为例。其他系统可以参考:

http://sphinxsearch.com/docs/current.html

1,执行命令

1
2
3
sudo add-apt-repository ppa:builds/sphinxsearch-rel22
sudo apt-get update
sudo apt-get install sphinxsearch

2,安装完之后,创建/etc/sphinxsearch/sphinx.conf 这个文件,这个目录有个sphinx.conf.sample文件,直接把他复制一份出来即可。

1
cp /etc/sphinxsearch/sphinx.conf.sample     /etc/sphinxsearch/sphinx.conf

3,配置该文件(暂时不配置)

默认情况下你也可以不配置,一般安装mysql的时候,都会创建test账户,这个配置文件中使用的就是test账户进行连接的。如果没有test用户可以创建其他用户,然后在sphinx.conf中设置。

注意:这里在设置数据库用户密码的时候尽量设置的不要带太多特殊字符。笔者在测试的过程中,如果设置有特殊字符的时候,在生成索引的时候,失败。

输入测试数据,sphinx本身有一个example.sql文件,我用apt-get安装后,没有找到(没仔细找),所以在它官网下载了个,在phpmyadmin中的test数据库中导入这个sql文件。

4、开启sphinxsearch功能

编辑/etc/default/sphinxsearch文件 将START=no 修改为 START=yes 用vi打开编辑就可以了

5,建立索引

执行

1
indexer --config /etc/sphinxsearch/sphinx.conf --all --rotate

6,启动sphinx

sudo service sphinxsearch start

 

二,安装客户端。php sphinx

1,先安装libsphinxclient

注意:如果不安装libsphinxclient,直接安装php sphinx的话会报错:

1
2
3
checking for libsphinxclient headers in /usr/local/libsphinxclient... not found
configure: error: Cannot find libsphinxclient headers
ERROR: `/tmp/pear/temp/sphinx/configure --with-php-config=/usr/bin/php-config --with-sphinx=/usr/local/libsphinxclient' failed

下载源码:

http://sphinxsearch.com/downloads/release/

版本号就是上面Sphinx Server的版本(2.2.10).版本一定要匹配,不然的话会报错。

1
2
3
4
5
下载后解压,比如放在目录/home/zou/sphinx-2.2.10-release下。
cd /home/zou/sphinx-2.2.10-release/api/libsphinxclient
./configure
make
make install

2,再安装php sphinx

1
2
3
4
pecl install sphinx
echo "extension=sphinx.so" >> /etc/php5/conf.d/sphinx.ini
echo "extension=sphinx.so" >> /etc/php5/apache2/conf.d/sphinx.ini
service apache2 restart

phpinfo-sphinx

三,测试

可以直接在之前下载的sphinx源码里测试

1
2
cd /home/zou/sphinx-2.2.10-release/api
php test.php t

如果显示:

1
2
3
4
5
6
7
root@aassa:/home/zou/sphinx-2.2.10-release/api# php test.php t
PHP Deprecated: DEPRECATED: Do not call this method or, even better, use SphinxQL instead of an API in /mnt/home/zou/sphinx-2.2.10-release/api/sphinxapi.php on line 771
Query 't ' retrieved 0 of 0 matches in 0.015 sec.
Query stats:
't' found 0 times in 0 documents

PHP Notice: Undefined index: matches in /mnt/home/zou/sphinx-2.2.10-release/api/test.php on line 138

说明是没问题的。

 

四:遇到的几个报错

1,”failed to send client protocol version”

这是因为守护进程没有启动,是配置文件sphinx.conf的问题。

解决办法:

命令行执行:searchd –status

1
2
3
4
5
6
7
8
9
10
11
12
root@adaa:/etc/sphinxsearch# searchd --status
Sphinx 2.2.10-id64-release (2c212e0)
Copyright (c) 2001-2015, Andrew Aksyonoff
Copyright (c) 2008-2015, Sphinx Technologies Inc (http://sphinxsearch.com)

using config file './sphinx.conf'...
WARNING: key 'sql_query_info' was permanently removed from Sphinx configuration. Refer to documentation for details.
WARNING: key 'charset_type' was permanently removed from Sphinx configuration. Refer to documentation for details.
WARNING: key 'max_matches' was permanently removed from Sphinx configuration. Refer to documentation for details.
WARNING: failed to connect to 0.0.0.0:9312: Connection refused

FATAL: failed to connect to daemon: please specify listen with sphinx protocol in your config file

查看日志: vim /var/log/sphinxsearch/searchd.log

定位到最后面一行,会显示有报错:

1
[Sun Feb 28 22:04:18.340 2016] [ 6926] FATAL: binlog meta file /var/lib/sphinxsearch/data/binlog.meta is v.4, binary is v.5; recovery requires previous binary version

说明是binlog.meta的问题,我们直接把这个文件删除

1
rm /var/lib/sphinxsearch/data/binlog.meta -rf

再命令行执行: searchd –status

1
2
3
4
5
6
root@aaaa:/var/lib/sphinxsearch/data# searchd --status
Sphinx 2.2.10-id64-release (2c212e0)
Copyright (c) 2001-2015, Andrew Aksyonoff
Copyright (c) 2008-2015, Sphinx Technologies Inc (http://sphinxsearch.com)

<span style="color: #ff0000;">FATAL: no readable config file</span> (looked in /usr/local/etc/sphinx.conf, ./sphinx.conf).

说明是没找到/usr/local/etc/sphinx.conf的原因。

/etc/sphinxsearch/sphinx.conf复制进去即可

1
cp /etc/sphinxsearch/sphinx.conf /usr/local/etc

 

再一次查看状态: searchd –status

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
root@aaaa:/usr/local/etc# searchd --status
Sphinx 2.2.10-id64-release (2c212e0)
Copyright (c) 2001-2015, Andrew Aksyonoff
Copyright (c) 2008-2015, Sphinx Technologies Inc (http://sphinxsearch.com)

using config file '/usr/local/etc/sphinx.conf'...
WARNING: key 'sql_query_info' was permanently removed from Sphinx configuration. Refer to documentation for details.
WARNING: key 'charset_type' was permanently removed from Sphinx configuration. Refer to documentation for details.
WARNING: key 'max_matches' was permanently removed from Sphinx configuration. Refer to documentation for details.

searchd status
--------------
uptime: 28
connections: 1
maxed_out: 0
command_search: 0
command_excerpt: 0
command_update: 0
command_delete: 0
command_keywords: 0
command_persist: 0
command_status: 1
command_flushattrs: 0
agent_connect: 0
agent_retry: 0
queries: 0
dist_queries: 0
query_wall: 0.000
query_cpu: OFF
dist_wall: 0.000
dist_local: 0.000
dist_wait: 0.000
query_reads: OFF
query_readkb: OFF
query_readtime: OFF
avg_query_wall: 0.000
avg_query_cpu: OFF
avg_dist_wall: 0.000
avg_dist_local: 0.000
avg_dist_wait: 0.000
avg_query_reads: OFF
avg_query_readkb: OFF
avg_query_readtime: OFF

很正常的状态,没报错了.

最后再重启一下sphinx server

1
2
3
root@aaaa:/usr/local/etc# service sphinxsearch restart
sphinxsearch stop/waiting
sphinxsearch start/running, process 7092

这样就搞定了。

 

2,”Query failed: searchd error: client version is higher than daemon version (client is v.1.30, daemon is v.1.25).

这个原因很奇葩,本意是客户端版本太高了,要降级。

1
2
3
pecl uninstall sphinx
pecl install sphinx-1.2.0
service apache2 restart

但是悲剧的是我降到1.2之后 还是报这个错。

试了很久,最后搞烦了 直接重启(reboot)服务器了。

妈蛋,重启之后 居然好了。

 

五:sphinx.conf文件说明

一个最简单的例子:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
source mysql
{
type = mysql
sql_host = localhost
sql_user = root
sql_pass =
sql_db = test
sql_port = 3306
sql_query_pre = SET NAMES utf8
sql_query = \
SELECT ID, TopicID , IsTopic , IF(IsTopic, Subject, '') as Subject, Content, PostTime \
FROM carbon_posts \
WHERE IsDel=0
sql_attr_uint = TopicID
sql_attr_bool = IsTopic
sql_attr_timestamp = PostTime
sql_ranged_throttle = 500
sql_query_info = SELECT * FROM carbon_posts WHERE ID=$id
}

index mysql
{
# 索引源声明
source = mysql
# 索引文件的存放位置
path = /var/lib/sphinxsearch/data/mysql
# 数据编码(设置成utf8才能索引中文)
charset_dictpath = /etc/
charset_type = zh_cn.utf-8
# 文件存储模式(默认为extern)
docinfo = extern
# 缓存数据内存锁定
mlock = 0
# 马氏形态学(对中文无效)
morphology = none
# 索引词最小长度 模糊匹配
min_word_len = 0
enable_star = 1
#prefix_fields = domain
#ignore_chars = -
# 最小索引前缀长度
min_prefix_len = 0
# 最小索引中缀长度 模糊匹配
min_infix_len = 0
# 对于非字母型数据的长度切割(for CJK indexing)
ngram_len = 1
ngram_chars = U+3000..U+2FA1F
# 对否对去除用户输入查询内容的html标签
html_strip = 0

charset_table = 0..9, A..Z->a..z, _, a..z, U+410..U+42F->U+430..U+44F, U+430..U+44F
}
indexer
{
mem_limit = 32M
}
searchd
{
listen = 9312
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
max_matches = 10000
seamless_rotate = 1
preopen_indexes = 1
unlink_old = 1
workers = threads # for RT to work
binlog_path = /var/lib/sphinxsearch/data
}

讲下这个配置文件中每项的含义。

source mysql{} 定义源名称为mysql,也可以叫其他的,比如:source xxx{}

type 数据源类型

sql_* 数据相关的配置,比如sql_host,sql_pass什么的,这些不解释鸟

sql_query 建立索引时的查询命令,在这里尽可能不使用where或group by,将where与groupby的内容交给sphinx,由sphinx进行条件过滤与groupby效率会更高,注意:select 的字段必须包括一个唯一主键以及要全文检索的字段,where中要用到的字段也要select出来

sql_query_pre 在执行sql_query前执行的sql命令, 可以有多条

sql_attr 以这个开头的配置项,表示属性字段,在where,orderby,groupby中出现的字段要分别定义一个属性,定义不同类型的字段要用不同的属性名,比如上面的sql_attr_timestamp就是时间戳类型。

 

index mysql{} 定义索引名称为mysql,也可以叫其他的,比如:index xxx{}

source 关联源,就是source xxx定义的。

path 索引文件存放路径,比如:E:/coreseek/var/data/mysql 实际存放在E:/coreseek/var/data/目录,然后创建多个名称为mysql后缀却不同的索引文件

charset_dictpath 指明分词法读取词典文件的位置,当启用分词法时,为必填项。在使用LibMMSeg作为分词 库时,需要确保词典文件uni.lib在指定的目录下

charset_type 字符集,比如charset_type = zh_cn.gbk

 

searchd{} sphinx守护进程配置

listen 监听端口

max_matches最大匹配数,也就是查找的数据再多也只返回这里设置的1000条

pid_file pid文件路径

log全文检索日志

query_log查询日志

 

好了,配置文件就这样,配置的参数还有很多,大家可以自己查文档。

六:参考链接

http://pecl.php.net/package/sphinx

http://php.net/manual/zh/book.sphinx.php

http://blog.nengzuo.com/?p=1267

坚持原创技术分享,您的支持将鼓励我继续创作!