关于sphinx搜索进程searchd启动工作进程数及mysql查询方式

即sphinx的searchd服务模式可以是多处理模式MPM,如下为启动一个searchd之后,可以看到随即searchd即启动了10个工作进程。

[root@localhost test]# ps -ef | grep searchd
root     10232     1  0 14:23 pts/5    00:00:00 /usr/local/coreseek4.1/bin/searchd -c /usr/local/coreseek4.1/etc/news.conf
root     10233 10232 37 14:23 pts/5    00:00:02 /usr/local/coreseek4.1/bin/searchd -c /usr/local/coreseek4.1/etc/news.conf
root     10234 10232 44 14:23 pts/5    00:00:03 /usr/local/coreseek4.1/bin/searchd -c /usr/local/coreseek4.1/etc/news.conf
root     10235 10232 37 14:23 pts/5    00:00:02 /usr/local/coreseek4.1/bin/searchd -c /usr/local/coreseek4.1/etc/news.conf
root     10236 10232 25 14:23 pts/5    00:00:01 /usr/local/coreseek4.1/bin/searchd -c /usr/local/coreseek4.1/etc/news.conf
root     10237 10232 27 14:23 pts/5    00:00:01 /usr/local/coreseek4.1/bin/searchd -c /usr/local/coreseek4.1/etc/news.conf
root     10238 10232 37 14:23 pts/5    00:00:02 /usr/local/coreseek4.1/bin/searchd -c /usr/local/coreseek4.1/etc/news.conf
root     10239 10232 36 14:23 pts/5    00:00:02 /usr/local/coreseek4.1/bin/searchd -c /usr/local/coreseek4.1/etc/news.conf
root     10240 10232 34 14:23 pts/5    00:00:02 /usr/local/coreseek4.1/bin/searchd -c /usr/local/coreseek4.1/etc/news.conf
root     10241 10232 29 14:23 pts/5    00:00:02 /usr/local/coreseek4.1/bin/searchd -c /usr/local/coreseek4.1/etc/news.conf
root     10242 10232 21 14:23 pts/5    00:00:01 /usr/local/coreseek4.1/bin/searchd -c /usr/local/coreseek4.1/etc/news.conf

这是为什么呢?sphinx的searchd配置中有一项workers配置,这项配置决定了sphinx的searchd工作进程的启动方式和个数。workers配置的值有如下几种:
1,none: 所有请求串行处理。searchd进程启动后不会再启动其它工作进程。但因为它是串行处理,因此如果有一个mysql端连接上了sphinx.则其它端要连接mysql必须等待直到这个连接断开。
2,fork: 它会在你执行sphinx查询的时候创建一个新的子进程来处理每个传入请求。工作进程的个数受max_children控制。达到了这个限制之后再使用mysql连接sphinx就会报错:
ERROR 2013 (HY000): Lost connection to MySQL server at reading initial communication packet, system error: 0
3,prefork: searchd会在启动时,预先fork一个工作进程数,再将传入的请求传递给子进程处理。一般启动10个
4,threads: 将创建一个新的线程来处理每个传入请求
建议测试环境配置为fork,max_children配置3-5个即可,线上环境配置成prefork,max_children配置100即可,当然也还需要结合业务需要进行调整。上面的结果即是配置成了prefork。
另外sphinx的searchd服务提供轮换功能,即--rotate。为防止在需要预取大量数据的索引时停止搜索响应。有一项相关配置seamless_rotate,当然它默就是1(即启用无缝seamless轮换),还有一项配置关于索引轮换成功之后,是否删除以.old为扩展名的索引拷贝。默认是删除。如下:

seamless_rotate = 1
unlink_old = 1

提完sphinx的搜索服务后,记一点sphinx提供的mysql方式的连接查询。php也可以直接在程序代码里像连接mysql一样连接sphinx进行SQL查询。

searchd
{
    listen                  = 192.168.162.11:9310
    listen                  = 192.168.162.11:9311:mysql41 #添加此行配置
}

即只需要在searchd中增加一行配置文件,加入一个协议为mysql41的listener。不过sphinx并不完全支持所有SQL查询样式,只支持一些简单语法。如下:

[root@localhost controller]# mysql -h 192.168.162.11 -P 9311
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 1
Server version: 2.0.2-dev (r2922)
Copyright (c) 2000, 2013, 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> select title from news where match('中国人民') limit 5; 
+----------+--------+-----------------------------------------+
| id       | weight | title                                   |
+----------+--------+-----------------------------------------+
| 10123463 |  12590 | 中国人民大学:城市发展导论    |
| 12123153 |  12590 | 中国人民解放军进行曲          |
| 12222119 |  12590 | 中国人民解放军进行曲          |
| 12156663 |  12590 | 中国人民解放军进行曲          |
| 12789530 |  12590 | 中国人民解放军进行曲          |
+----------+--------+-----------------------------------------+
5 rows in set (0.03 sec)
mysql> 
点赞