给docker博客环境装上coreseek扩展

2017-11-03 13:02:47

    因为博客原来的搜索使用的是coreseek扩展的,犹记得当时装扩展的时候,可是费了一番功夫,当时就在想,唉呀,装这玩意真的是麻烦,下次如果换服务器的话,不免又要头疼了。

    现在使用docker测试装了一下,体验相当不错,比原先安装方便多了,特此记录一下。

    进入laradock目录,执行

    git clone https://github.com/muzilong/CoreseekDocker.git sphinx

    然后进入sphinx文件夹,建立csft.conf配置文件,下面是本博客的配置文件,仅供参考

        #
    # Minimal Sphinx configuration sample (clean, simple, functional)
    #
    
    source articles
    {
            type                    = mysql
    
            sql_host                = mysql
            sql_user                = #user
            sql_pass                = #pass
            sql_db                  = #db
            sql_port                = 3306  # optional, default is 3306
            sql_query_pre           = SET NAMES utf8
    
            sql_query               = SELECT id,title from #table
    
    
            sql_query_info          = SELECT id FROM #table WHERE id=$id
    }
    
    source autoArticles
    {
            type                    = mysql
    
            sql_host                = mysql
            sql_user                = #user
            sql_pass                = #pass
            sql_db                  = #db
            sql_port                = 3306  # optional, default is 3306
            sql_query_pre           = SET NAMES utf8
    
            sql_query               = SELECT id,title from #table    
    
            sql_query_info          = SELECT id FROM #table WHERE id=$id
    }
    
    index articles
    {
            source                  = articles
            path                    = /usr/local/coreseek/var/data/articles
            charset_type        = zh_cn.utf-8
            charset_dictpath = /usr/local/mmseg3/etc/
    }
    
    index autoArticles
    {
            source                  = autoArticles
            path                    = /usr/local/coreseek/var/data/autoArticles
            charset_type        = zh_cn.utf-8
            charset_dictpath = /usr/local/mmseg3/etc/
    }
        indexer
    {
            mem_limit               = 32M
    }
    
    
    searchd
    {
            listen                  = 9312
            listen                  = 9306:mysql41
            log                     = /usr/local/coreseek/var/log/searchd.log
            query_log               = /usr/local/coreseek/var/log/query.log
            read_timeout            = 5
            max_children            = 30
            pid_file                = /usr/local/coreseek/var/log/searchd.pid
            max_matches             = 1000
            seamless_rotate         = 1
            preopen_indexes         = 1
            unlink_old              = 1
            workers                 = threads # for RT to work
    }

    然后编辑Dockerfile文件,代码如下

        # Author: Huijie Wei
    FROM ubuntu:14.04.2
    MAINTAINER Huijie Wei huijiewei@outlook.com
    ADD ./assets/sources.list /etc/apt/sources.list
    RUN apt-get update
    RUN apt-get upgrade -y
    RUN DEBIAN_FRONTEND=noninteractive apt-get install -y \
        make \
        gcc \
        g++ \
        automake \
        libtool \
        mysql-client \
        libmysqlclient15-dev \
        libxml2-dev \
        libexpat1-dev \
        git
    RUN mkdir -p /usr/src/coreseek/
    RUN git clone https://github.com/huijiewei/Coreseek-Fix.git /usr/src/coreseek/
    RUN chmod 777 -R /usr/src/coreseek
    WORKDIR /usr/src/coreseek/mmseg-3.2.14
    RUN ./bootstrap
    RUN ./configure --prefix=/usr/local/mmseg3
    RUN make && make install
    WORKDIR /usr/src/coreseek/csft-4.1
    RUN sh buildconf.sh
    RUN ./configure --prefix=/usr/local/coreseek --without-unixodbc --with-mmseg --with-mmseg-includes=/usr/local/mmseg3/include/mmseg/ --with-mmseg-libs=/usr/local/mmseg3/lib/ --with-mysql
    RUN make && make install
    WORKDIR /
    ADD ./bin/index.sh /
    RUN chmod a+x index.sh
    ADD ./bin/searchd.sh /
    RUN chmod a+x searchd.sh
    ADD ./bin/run.sh /
    RUN chmod a+x run.sh && cp run.sh /usr/local/bin
    ADD csft.conf /opt/coreseek/sphinx.conf
    CMD  ["sh","run.sh"]
    EXPOSE 9312

    最后编辑docker-compose.yml文件,添加sphinx支持,添加内容如下

        ### Sphinx Console Container ############################
    
        sphinx:
          build: ./sphinx
          ports:
            - "9312:9312"
          volumes:
            - ${DATA_SAVE_PATH}/sphinx:/usr/local/coreseek/var/data
          depends_on:
            - mysql
          links:
            - mysql
          networks:
            - backend

    配置完毕后,尝试执行

    docker-compose up -d sphinx

    使用 docker-compose ps,发现报错了,报错信息为

    Coreseek Fulltext 4.1 [ Sphinx 2.0.2-dev (r2922)]
    Copyright (c) 2007-2011,
    Beijing Choice Software Technologies Inc (http://www.coreseek.com)
    
     precaching index 'articles'
    [Fri Nov  3 03:56:48.964 2017] [    9] WARNING: index 'articles': preload: failed to open /usr/local/coreseek/var/data/articles.sph: No such file or directory; NOT SERVING
    precaching index 'autoArticles'
    [Fri Nov  3 03:56:48.964 2017] [    9] WARNING: index 'autoArticles': preload: failed to open /usr/local/coreseek/var/data/autoArticles.sph: No such file or directory; NOT SERVING
    [Fri Nov  3 03:56:48.964 2017] [    9] FATAL: no valid indexes to serve

    查看了一下错误原因,是因为连接不到数据库。解决方法

    docker-compose down
    docker-compose -d nginx mysql redis
    docker-compose -d sphinx

     首次启动的话,这样就可以了,暂时不知道什么原因,特备忘一下。记得在程序中连接sphinx,host要使用sphinx而非127.0.0.1或localhost