首页 > 解决方案 > 无法从 Docker 应用程序连接到 Sphinx(全文搜索)

问题描述

所以我有一个在 docker 容器中运行的项目,如下所示:

$ docker-compose up -d nginx mysql redis workspace

Docker 运行良好(所有容器都已启动),我现在正在尝试将 Sphinx 全文搜索集成到工作区容器中。我有这样的 /etc/sphinxsearch/sphinx.conf 文件设置:

source tablename
{
    type            = mysql
    sql_host        = mysql
    sql_user        = user
    sql_pass        = pass
    sql_db          = database_name
    sql_port        = 3306
    sql_query       = SELECT id, name FROM tablename
    sql_attr_uint   = id
}

index tablename
{
    source          = tablename
    path            = /var/lib/sphinxsearch/data/tablename
    min_prefix_len  = 2
    morphology      = stem_en
}

searchd
{
    listen              = 127.0.0.1:9312
    log                 = /var/log/sphinxsearch/searchd.log
    query_log           = /var/log/sphinxsearch/query.log
    query_log_format    = sphinxql
    pid_file            = /var/run/searchd.pid
}

我已经按如下方式运行索引器,这似乎很好:

root@1ec9add2d1dc:/etc/sphinxsearch# /usr/bin/indexer tablename
Sphinx 2.2.9-id64-release (rel22-r5006)
Copyright (c) 2001-2015, Andrew Aksyonoff
Copyright (c) 2008-2015, Sphinx Technologies Inc (http://sphinxsearch.com)
using config file '/etc/sphinxsearch/sphinx.conf'...
indexing index 'tablename'...
WARNING: index 'tablename': dict=keywords and prefixes and morphology enabled, forcing index_exact_words=1
WARNING: attribute 'id' not found - IGNORING
WARNING: Attribute count is 0: switching to none docinfo
collected 17 docs, 0.0 MB
sorted 0.0 Mhits, 100.0% done
total 17 docs, 171 bytes
total 0.322 sec, 529 bytes/sec, 52.68 docs/sec
total 3 reads, 0.000 sec, 0.3 kb/call avg, 0.0 msec/call avg
total 9 writes, 0.000 sec, 0.2 kb/call avg, 0.0 msec/call avg

以及随后对 /usr/bin/searchd -rotate 的调用也在修补索引。我还在工作区容器中运行了 searchd:

root@1ec9add2d1dc:/etc/sphinxsearch# /usr/bin/searchd 
Sphinx 2.2.9-id64-release (rel22-r5006)
Copyright (c) 2001-2015, Andrew Aksyonoff
Copyright (c) 2008-2015, Sphinx Technologies Inc (http://sphinxsearch.com)
using config file '/etc/sphinxsearch/sphinx.conf'...
listening on 127.0.0.1:9312

但我似乎无法从应用程序连接到这个。示例代码:

$query = 'keyword';
$sphinx = new SphinxClient();
$sphinx->setServer('127.0.0.1', 9312);
$sphinx->setMatchMode(SPH_MATCH_ALL);
$sphinx->addQuery($query, 'tablename');
$sphinx->setLimits(0, 10);
$result = $sphinx->runQueries();

但是 $result 总是错误的,并出现以下错误:

SPHINX Query failed: connection to 127.0.0.1:9312 failed (errno=111, msg=Connection refused)
bool(false)

我还尝试更改 .conf 和 setServer() 以使用“localhost”、“127.0.0.1”、“mysql”(sql 容器名称),但实际上没有任何东西从应用程序代码连接到 sphinx。

问题:我在这里遗漏了什么吗?连接 sphinx 并检索实际索引数据还需要哪些其他步骤?任何帮助深表感谢。


在 Mac OSX 上运行 Docker Desktop,Ubuntu 容器 PHP 7.2,“gigablah/sphinxphp”:“^2.0.8”,“laravel/framework”:“^6.4.0”,

标签: phplaravelsphinx

解决方案


鉴于您的 docker-compose 文件是这样的:

....
sphinx:
  -p 9311:9312
....

你的 sphinx.conf 文件应该在你的 searchd 中有这个

searchd {
  listen                    = 9312
  listen                    = 9306:mysql41
  .....
}

使用 PHP SphinxQL 连接到 sphinx 时

$conn = new Connection();
$conn->setParams(array('host' => 'sphinx', 'port' => 9306));
$querybuilder = new SphinxQL($conn);
$querybuilder->insert()->into('rtidx')->set($records)->execute();

推荐阅读