环境:centos 7 x86_64,php 7.1.7, MariaDB 10.1.23
一,下载sphinx,scws相关包
sphinx下载地址:http://sphinxsearch.com/downloads/release/
sphinx php扩展下载地址:http://pecl.php.net/package/sphinx
scws下载地址:http://www.xunsearch.com/scws/download.php
scws词库下载地址:http://www.xunsearch.com/scws/down/scws-dict-chs-utf8.tar.bz2
二,安装sphinx,scws,以及php扩展
- 1,安装sphinx
1
2
3
4
5# curl http://sphinxsearch.com/files/sphinx-2.3.1-beta.tar.gz
# tar zxvf sphinx-2.3.1-beta.tar.gz
# cd sphinx-2.3.1-beta
# ./configure --prefix=/usr/local/sphinx --with-mysql=/usr/local/mariadb LIBS=-liconv
# make && make install - 2,安装sphinx客户端
1
2
3# cd api/libsphinxclient //sphinx-2.3.1-beta目录下
# ./configure --prefix=/usr/local/sphinx/libsphinxclient
# make && make install - 3,安装sphinx php扩展
1 | # git clone https://github.com/php/pecl-search_engine-sphinx.git |
- 4,安装scws
1 | # cd /usr/local/src |
- 5,安装scws php扩展
1 | # cd /usr/local/src/sphinx/scws-1.2.3/phpext |
- 6 安装scws词库
1 | # wget http://www.xunsearch.com/scws/down/scws-dict-chs-utf8.tar.bz2 |
在这里一定要加权限,也就是说让php-fpm或者php-cgi的运行用户,拥有dict.utf8.xdb的所有权限。如果不这么做的话,php 扩展调用词库会报如下错误:
1 | Warning: SimpleCWS::add_dict(): Failed to add the dict file |
怎么查看php-fpm,php-cgi的运行用户呢?
1 | # ps aux |grep php-fpm |
三,配置sphinx,scws,php等
- 1,创建测试表和数据 test.sql
1 | test.sql |
导入数据 source test.sql
数据源
1 | select |
1 | +----+--------+----------+------------+--------------+---------+ |
2,配置sphinx.conf,加上以下内容
其中searchd 以下3个配置比较重要。mysql -h0 -P 9306访问。9312可以给php访问。query_log_format 记录日志的格式。1 # php
listen = 93122 # mysql
listen = 9306:mysql413 # log_format
query_log_format = sphinxqlsphinx.conf
1 | # vim /usr/local/sphinx/etc/sphinx-test.conf |
4,配置php.ini
1 | # vim /etc/php.ini |
四,启动sphinx,php-fpm
- 1,启动sphinx
1
2# /usr/local/sphinx/bin/indexer --config /usr/local/sphinx/etc/sphinx-test.conf --all
# /usr/local/sphinx/bin/searchd --config /usr/local/sphinx/etc/sphinx-test.conf - 2,重启php-fpm
1
# /etc/init.d/php-fpm restart
五,测试sphinx全文检索
- 1,命令行的测试
1 | # mysql -h127.0.0.1 -P9306 |
1 |
|
- 2,利用php 扩展
1 | <?php |
结果如下:
1 | <p>输入:洪荒之力</p> |
查询query.log
参考:
http://blog.csdn.net/clevercode/article/details/52204124
http://www.cnblogs.com/chenpingzhao/p/4712345.html
http://www.ibm.com/developerworks/cn/opensource/os-sphinx/
http://ourmysql.com/archives/965
http://www.cnblogs.com/yjf512/p/3581869.html
http://blog.51yip.com/mysql/1658.html