摘要:第二步在数据库中新建一个需要被索引的测试数据库登录数据库创建名为的数据库退出导入测试数据数据库用户名数据库密码版本号如果没有出现什么错误就说明数据库已经创建成功了。
安装
假设你的系统上已经安装好饿 Homebrew,执行以下命令:
➜ ~ brew install mysql
启动MySQL:
➜ ~ mysql.server start
然后关闭MySQL:
➜ ~ mysql.server stop
安装Sphinx并将其支持MySQL:
➜ ~ brew install sphinx --with-mysql # Sphinx 默认安装在 /usr/local/Celler/sphinx/[版本号]/
安装PHP的Sphinx扩展
➜ ~ brew install homebrew/php/php56-sphinx # PHP-Sphinx 扩展默认安装在 /usr/local/Cellar/php56-sphinx/[版本号]/检查 Sphinx 及扩展安装是否成功
第一步:配置 Sphinx 与数据库连接
配置文件:/usr/local/Celler/sphinx/sphinx.conf
# 如果配置文件不存在,复制 sphinx.conf.dist 至 sphinx.conf # 下面是配置: source src1 { type = mysql // 数据库类型 sql_host = localhost // 所连接的 ip sql_user = user // 数据库用户名 sql_pass = pass // 数据库密码 sql_db = test // 数据库名称 sql_port = 3306 // 数据库端口 ....
默认情况下只需要修改数据库用户名和密码就可以了。
第二步:在数据库中新建一个需要被 Sphinx 索引的测试数据库
➜ ~ mysql -u root -p // 登录数据库 mysql> create database test; // 创建名为 test 的数据库 mysql> exit; // 退出mysql // 导入测试数据 mysql -u [数据库用户名] -p [数据库密码] < /usr/local/Cellar/sphinx/[版本号]/etcexample.sql
如果没有出现什么错误就说明数据库已经创建成功了。接下来建立索引。
第三步:使用 Indexer 建立索引
➜ ~ /usr/local/Cellar/sphinx/[版本号]/bin/indexer --all
输出如下信息(版本号可能会有出入):
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/Cellar/sphinx/2.2.10/etc/sphinx.conf"... indexing index "test1"... collected 4 docs, 0.0 MB sorted 0.0 Mhits, 100.0% done total 4 docs, 193 bytes total 0.160 sec, 1198 bytes/sec, 24.84 docs/sec indexing index "test1stemmed"... collected 4 docs, 0.0 MB sorted 0.0 Mhits, 100.0% done total 4 docs, 193 bytes total 0.005 sec, 32339 bytes/sec, 670.24 docs/sec skipping non-plain index "dist1"... skipping non-plain index "rt"... total 8 reads, 0.000 sec, 0.1 kb/call avg, 0.0 msec/call avg total 24 writes, 0.000 sec, 0.1 kb/call avg, 0.0 msec/call avg rotating indices: successfully sent SIGHUP to searchd (pid=1342).
第四步:启动 searchd
➜ ~ searchd
输入如下信息:
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/Cellar/sphinx/2.2.10/etc/sphinx.conf"... listening on all interfaces, port=9312 listening on all interfaces, port=9306 precaching index "test1" precaching index "test1stemmed" precaching index "rt" precached 3 indexes in 0.003 sec
出现上面这些信息,说明启动成功!
第五步:使用 PHP 检测 Sphinx 及扩展是否安装成功
"", "after_match" => "", "chunk_separator" => " ... ", "limit" => 60, "around" => 3, ); foreach ( array(0,1) as $exact ) { $opts["exact_phrase"] = $exact; print "exact_phrase=$exact "; $cl = new SphinxClient (); $res = $cl->BuildExcerpts ( $docs, $index, $words, $opts ); if ( !$res ) { die ( "ERROR: " . $cl->GetLastError() . ". " ); } else { $n = 0; foreach ( $res as $entry ) { $n++; print "n=$n, res=$entry "; } print " "; } }
以上源码输出:
exact_phrase=0 n=1, res=this is my test text to be highlighted, ... n=2, res=another test text to be highlighted, below limit n=3, res=test number three, without phrase match n=4, res=final test, not only ... with swapped phrase text test as well exact_phrase=1 n=1, res=this is my test text to be highlighted, ... n=2, res=another test text to be highlighted, below limit n=3, res=test number three, without phrase match n=4, res=final test, not only without phrase match, but also above ...
搞定!
文章版权归作者所有,未经允许请勿转载,若此文章存在违规行为,您可以联系管理员删除。
转载请注明本文地址:https://www.ucloud.cn/yun/46586.html
摘要:本开发环境,全部基于安装。制作全新安装启动盘。插上盘,在终端执行是你的盘盘符,根据实际情况来。安装开发常用的包软件安装开发包升级一下系统自带的安装常用软件是个很不错的东西,推荐必须安装。 用了一年的Mac OS X了,之前不熟悉这个系统,用的是系统自带的PHP 以及DMG包安装的MySQL,时间长了,慢慢觉得MacBook的速度跟不上了,虽然关机次数不多,但是每次开机,或者唤醒电...
摘要:本开发环境,全部基于安装。制作全新安装启动盘。插上盘,在终端执行是你的盘盘符,根据实际情况来。安装开发常用的包软件安装开发包升级一下系统自带的安装常用软件是个很不错的东西,推荐必须安装。 用了一年的Mac OS X了,之前不熟悉这个系统,用的是系统自带的PHP 以及DMG包安装的MySQL,时间长了,慢慢觉得MacBook的速度跟不上了,虽然关机次数不多,但是每次开机,或者唤醒电...
摘要:本开发环境,全部基于安装。制作全新安装启动盘。插上盘,在终端执行是你的盘盘符,根据实际情况来。安装开发常用的包软件安装开发包升级一下系统自带的安装常用软件是个很不错的东西,推荐必须安装。 用了一年的Mac OS X了,之前不熟悉这个系统,用的是系统自带的PHP 以及DMG包安装的MySQL,时间长了,慢慢觉得MacBook的速度跟不上了,虽然关机次数不多,但是每次开机,或者唤醒电...
摘要:安装之前,需要确定是否安装过然后安装命令行工具。安装命令行工具如果该方法你不愿用或者各种原因,可以登录然后下载安装注一定要选择和系统版本,版本一致的命令行工具。安装好了之后,便可以使用命令来安装相应的包了。 之前换电脑装了个Mnmp,有遇到一些小坑,写在这,希望能帮到一些初次搭建Mnmp的phper。 ... 安装 Mac 的包管理器 - homebrew Homebrew是一款Mac...
摘要:安装之前,需要确定是否安装过然后安装命令行工具。安装命令行工具如果该方法你不愿用或者各种原因,可以登录然后下载安装注一定要选择和系统版本,版本一致的命令行工具。安装好了之后,便可以使用命令来安装相应的包了。 之前换电脑装了个Mnmp,有遇到一些小坑,写在这,希望能帮到一些初次搭建Mnmp的phper。 ... 安装 Mac 的包管理器 - homebrew Homebrew是一款Mac...
阅读 2358·2021-11-22 15:27
阅读 337·2021-11-22 12:05
阅读 1305·2021-11-17 09:33
阅读 3239·2021-11-11 16:54
阅读 2468·2021-10-14 09:49
阅读 3519·2021-09-06 15:01
阅读 1681·2019-08-29 17:23
阅读 583·2019-08-29 14:09