Mac下使用Homebrew安装Sphinx和MySQL
生活随笔
收集整理的這篇文章主要介紹了
Mac下使用Homebrew安装Sphinx和MySQL
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
安裝
假設你的系統上已經安裝好餓 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 // 所連接的 ipsql_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 及擴展是否安裝成功
<?php header('Content-type: text/html; charset=utf-8');// 檢測 PHP-Spinx 模塊是否安裝成功 if (!in_array('sphinx', get_loaded_extensions())) {die('模塊不存在,請檢查!'); }$docs = array ("this is my test text to be highlighted, and for the sake of the testing we need to pump its length somewhat","another test text to be highlighted, below limit","test number three, without phrase match","final test, not only without phrase match, but also above limit and with swapped phrase text test as well", ); $words = "test text"; $index = "test1"; $opts = array ("before_match" => "<b>","after_match" => "</b>","chunk_separator" => " ... ","limit" => 60,"around" => 3, );foreach ( array(0,1) as $exact ) {$opts["exact_phrase"] = $exact;print "exact_phrase=$exact\n";$cl = new SphinxClient ();$res = $cl->BuildExcerpts ( $docs, $index, $words, $opts );if ( !$res ){die ( "ERROR: " . $cl->GetLastError() . ".\n" );} else{$n = 0;foreach ( $res as $entry ){$n++;print "n=$n, res=$entry\n";}print "\n";} }以上源碼輸出:
exact_phrase=0 n=1, res=this is my <b>test</b> <b>text</b> to be highlighted, ... n=2, res=another <b>test</b> <b>text</b> to be highlighted, below limit n=3, res=<b>test</b> number three, without phrase match n=4, res=final <b>test</b>, not only ... with swapped phrase <b>text</b> <b>test</b> as wellexact_phrase=1 n=1, res=this is my <b>test text</b> to be highlighted, ... n=2, res=another <b>test text</b> 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 ...搞定!
總結
以上是生活随笔為你收集整理的Mac下使用Homebrew安装Sphinx和MySQL的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 小度智能音箱开发
- 下一篇: 网上补卡一般多久可以到 在网上补卡需要多