本文介绍下如何在ubuntu系统中,利用apt工具,搭建mysql+sphinx的全文检索环境。
一 安装mysql
1 更新源列表 打开"终端窗口",输入"sudo apt-get update"-->回车-->"输入root用户的密码"-->回车,就可以了。如果不运行该命令,直接安装mysql,会出现"有几个软件包无法下载,您可以运行apt-get update------"的错误提示,导致无法安装。 2 安装mysql 打开"终端窗口",输入"sudo apt-get install mysql-server mysql-client"-->回车-->输入"y"-->回车-->在"软件包设置对话框"中输入mysql中"root"用户的密码-->回车-->再输一次密码-->回车,安装完成。 3 如何判断mysql是否安装成功 打开"终端窗口",输入"sudo service mysql restart"-->回车-->如果mysql启动成功,处于运行状态说明mysql安装成功。 4 让apache支持mysql 打开"终端窗口",输入"sudo apt-get install libapache2-mod-auth-mysql"-->回车-->安装成功,安装这个模块后,apache才能支持mysql。 5 让php支持mysql 打开"终端窗口",输入"sudo apt-get install php5-mysql"-->回车-->安装成功,安装这个模块后,php才能支持mysql。 6 登录mysql 打开"终端窗口",输入"mysql -u root -p"-->回车-->输入mysql中"root"用户的密码-->回车-->登录成功。
二安装sphinx
01、安装全文搜索引擎 Sphinx 前,必须先安装 MySQL server 并设置数据库 root 用户(参考步骤一) 02、使用 apt-get 方法直接安装 Sphinx:
复制代码
03、创建全文搜索引擎 Sphinx 的配置文件:
复制代码
配置全文搜索引擎 Sphinx 的配置文件:
复制代码
将以上代码改为
复制代码
04、开启 sphinxsearch 功能:
复制代码
将其中的
复制代码
改为
复制代码
05、创建 test 数据库:
root:~# mysql -u root -p
Enter password: 键入您的数据库 root 用户密码
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 20177
Server version: 5.5.40-0ubuntu0.14.04.1 (Ubuntu)
Copyright (c) 2000, 2014, Oracle and/or its affiliates. All rights reserved.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql>create database test;
Query OK, 1 row affected (0.00 sec)
mysql> use test;
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A
Database changed
mysql> exit
Bye
root:~#
复制代码
06、将 test 数据库范例文件
复制代码
下载到本地计算机 07、导入example.sql
mysql -u root -p你的root密码 -Dtest < /etc/sphinxsearch/example.sql
说明 example.sql 文件导入 test 数据库成功 08、建立全文搜索引擎 Sphinx 的索引:
复制代码
09、启动全文搜索引擎 Sphinx 服务:
root:~# service sphinxsearch start
Starting sphinxsearch: Sphinx 2.0.4-id64-release (r3135)
Copyright (c) 2001-2012, Andrew Aksyonoff
Copyright (c) 2008-2012, Sphinx Technologies Inc (http://sphinxsearch.com)
using config file '/etc/sphinxsearch/sphinx.conf'...
listening on all interfaces, port=9312
listening on all interfaces, port=9306
precaching index 'test1'
precaching index 'test1stemmed'
WARNING: multiple addresses found for 'localhost', using the first one (ip=127.0.0.1)
precaching index 'rt'
precached 3 indexes in 0.001 sec
sphinxsearch.
root:~#
复制代码
root:~# /usr/bin/search test
Sphinx 2.0.4-id64-release (r3135)
Copyright (c) 2001-2012, Andrew Aksyonoff
Copyright (c) 2008-2012, Sphinx Technologies Inc (http://sphinxsearch.com)
using config file '/etc/sphinxsearch/sphinx.conf'...
index 'test1': query 'test ': returned 3 matches of 3 total in 0.000 sec
displaying matches:
1. document=1, weight=2421, group_id=1, date_added=Mon Dec 22 15:30:18 2014
id=1
group_id=1
group_id2=5
date_added=2014-12-22 15:30:18
title=test one
content=this is my test document number one. also checking search within phrases.
2. document=2, weight=2421, group_id=1, date_added=Mon Dec 22 15:30:18 2014
id=2
group_id=1
group_id2=6
date_added=2014-12-22 15:30:18
title=test two
content=this is my test document number two
3. document=4, weight=1442, group_id=2, date_added=Mon Dec 22 15:30:18 2014
id=4
group_id=2
group_id2=8
date_added=2014-12-22 15:30:18
title=doc number four
content=this is to test groups
words:
1. 'test': 3 documents, 5 hits
index 'test1stemmed': query 'test ': returned 3 matches of 3 total in 0.000 sec
displaying matches:
1. document=1, weight=2421, group_id=1, date_added=Mon Dec 22 15:30:18 2014
id=1
group_id=1
group_id2=5
date_added=2014-12-22 15:30:18
title=test one
content=this is my test document number one. also checking search within phrases.
2. document=2, weight=2421, group_id=1, date_added=Mon Dec 22 15:30:18 2014
id=2
group_id=1
group_id2=6
date_added=2014-12-22 15:30:18
title=test two
content=this is my test document number two
3. document=4, weight=1442, group_id=2, date_added=Mon Dec 22 15:30:18 2014
id=4
group_id=2
group_id2=8
date_added=2014-12-22 15:30:18
title=doc number four
content=this is to test groups
words:
1. 'test': 3 documents, 5 hits
index 'rt': search error: failed to open /var/lib/sphinxsearch/data/rt.sph: No such file or directory.
root:~#
复制代码
11、好啦,一切 OK
三测试