Mac下使用Homebrew安装Sphinx和MySQL
来源:SegmentFault
时间:2023-02-23 19:45:05 484浏览 收藏
知识点掌握了,还需要不断练习才能熟练运用。下面golang学习网给大家带来一个数据库开发实战,手把手教大家学习《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 // 所连接的 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 [数据库密码]
如果没有出现什么错误就说明数据库已经创建成功了。接下来建立索引。
第三步:使用 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 well exact_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》的介绍就到此结束啦,但是学无止境,想要了解学习更多关于数据库的相关知识,请关注golang学习网公众号!
声明:本文转载于:SegmentFault 如有侵犯,请联系study_golang@163.com删除
相关阅读
更多>
-
499 收藏
-
244 收藏
-
235 收藏
-
157 收藏
-
101 收藏
最新阅读
更多>
-
278 收藏
-
126 收藏
-
414 收藏
-
320 收藏
-
247 收藏
-
149 收藏
-
392 收藏
-
268 收藏
-
162 收藏
-
349 收藏
-
363 收藏
-
345 收藏
课程推荐
更多>
-
- 前端进阶之JavaScript设计模式
- 设计模式是开发人员在软件开发过程中面临一般问题时的解决方案,代表了最佳的实践。本课程的主打内容包括JS常见设计模式以及具体应用场景,打造一站式知识长龙服务,适合有JS基础的同学学习。
- 立即学习 543次学习
-
- GO语言核心编程课程
- 本课程采用真实案例,全面具体可落地,从理论到实践,一步一步将GO核心编程技术、编程思想、底层实现融会贯通,使学习者贴近时代脉搏,做IT互联网时代的弄潮儿。
- 立即学习 514次学习
-
- 简单聊聊mysql8与网络通信
- 如有问题加微信:Le-studyg;在课程中,我们将首先介绍MySQL8的新特性,包括性能优化、安全增强、新数据类型等,帮助学生快速熟悉MySQL8的最新功能。接着,我们将深入解析MySQL的网络通信机制,包括协议、连接管理、数据传输等,让
- 立即学习 499次学习
-
- JavaScript正则表达式基础与实战
- 在任何一门编程语言中,正则表达式,都是一项重要的知识,它提供了高效的字符串匹配与捕获机制,可以极大的简化程序设计。
- 立即学习 487次学习
-
- 从零制作响应式网站—Grid布局
- 本系列教程将展示从零制作一个假想的网络科技公司官网,分为导航,轮播,关于我们,成功案例,服务流程,团队介绍,数据部分,公司动态,底部信息等内容区块。网站整体采用CSSGrid布局,支持响应式,有流畅过渡和展现动画。
- 立即学习 484次学习