Linux环境 solr-7.4.0安装【一篇就够】

一、环境说明

三台安装了CenOS7操作系统的虚拟机

master(192.168.11.128)、slave1(192.168.11.129)、slave2(192.168.11.30)

二、下载solr安装包

地址:http://mirrors.shu.edu.cn/apache/lucene/solr/7.4.0/solr-7.4.0.zip

三、解压solr安装包

[root@slave1 usr]# unzip solr-7.4.0.zip

四、配置环境变量

[root@slave1 usr]# vi /etc/profile
编辑profile文件末尾追加:

# solr environment
export SOLR_INSTALL_HOME=/usr/solr-7.4.0
export PATH=$PATH:$SOLR_INSTALL_HOME/bin

执行如下命令使环境变量生效

[root@slave1 usr]# source /etc/profile

五、以SolrCloud模式启动solr

[root@slave1 usr]# solr start -c
*** [WARN] *** Your open file limit is currently 1024.  
 It should be set to 65000 to avoid operational disruption. 
 If you no longer wish to see this warning, set SOLR_ULIMIT_CHECKS to false in your profile or solr.in.sh
*** [WARN] ***  Your Max Processes Limit is currently 3818. 
 It should be set to 65000 to avoid operational disruption. 
 If you no longer wish to see this warning, set SOLR_ULIMIT_CHECKS to false in your profile or solr.in.sh
WARNING: Starting Solr as the root user is a security risk and not considered best practice. Exiting.
         Please consult the Reference Guide. To override this check, start with argument '-force'

将/usr/solr-7.4.0/bin/solr.in.sh文件中,SOLR_ULIMIT_CHECKS设置为false,消除WARN

# Settings for common system values that may cause operational imparement when system defaults are used.
# Solr can use many processes and many file handles. On modern operating systems the savings by leaving
# these settings low is minuscule, while the consequence can be Solr instability. To turn these checks off, set
# SOLR_ULIMIT_CHECKS=false either here or as part of your profile.

# Different limits can be set in solr.in.sh or your profile if you prefer as well.
#SOLR_RECOMMENDED_OPEN_FILES=
#SOLR_RECOMMENDED_MAX_PROCESSES=
SOLR_ULIMIT_CHECKS=false

再次启动(根据WARNING提示因为是root用户,需要加 -force)

[root@slave1 bin]# solr start -c -force
NOTE: Please install lsof as this script needs it to determine if Solr is listening on port 8983.

Started Solr server on port 8983 (pid=2772). Happy searching!

[root@slave1 bin]# 
 

六、访问solr页面

需要先执行命令(systemctl stop firewalld.service)关闭防火墙,否则页面不能正常打开

http://192.168.11.129:8983/solr/#/

Linux环境 solr-7.4.0安装【一篇就够】_第1张图片

七、扩展-配置solr.in.sh

     51 # Set the ZooKeeper connection string if using an external ZooKeeper ensemble
     52 # e.g. host1:2181,host2:2181/chroot
     53 # Leave empty if not using SolrCloud
     54 ZK_HOST="master:2181,slave1:2181,slave2:2181"     <==前提:zookeeper安装完毕,安装在各个机器上的solr通过
     55                                                       ZK共享collection信息,即一台服务器上创建好collection之后,其它服务器可见
     56 # Set the ZooKeeper client timeout (for SolrCloud mode)
     57 #ZK_CLIENT_TIMEOUT="15000"
     58 
     59 # By default the start script uses "localhost"; override the hostname here
     60 # for production SolrCloud environments to control the hostname exposed to cluster state
     61 SOLR_HOST="slave1"        <==solr安装所在机器名/IP地址
     62 
     63 # By default Solr will try to connect to Zookeeper with 30 seconds in timeout; override the timeout if needed
     64 #SOLR_WAIT_FOR_ZK="30"
     65 
     66 # By default the start script uses UTC; override the timezone if needed
     67 #SOLR_TIMEZONE="UTC"
     68 SOLR_TIMEZONE="UTC+8"

     89 # Path to a directory for Solr to store cores and their data. By default, Solr will use server/solr
     90 # If solr.xml is not stored in ZooKeeper, this directory needs to contain solr.xml
     91 SOLR_HOME=/usr/solr-7.4.0/solr_home            <==需先创建相应目录
     92 
     93 # Path to a directory that Solr will use as root for data folders for each core.
     94 # If not set, defaults to /data. Overridable per core through 'dataDir' core property
     95 SOLR_DATA_HOME=/usr/solr-7.4.0/solr_data_home            <==需先创建相应目录
     96 
     97 # Solr provides a default Log4J configuration xml file in server/resources
     98 # however, you may want to customize the log settings and file appender location
     99 # so you can point the script to use a different log4j2.xml file
    100 #LOG4J_PROPS=/var/solr/log4j2.xml
    101 
    102 # Changes the logging level. Valid values: ALL, TRACE, DEBUG, INFO, WARN, ERROR, FATAL, OFF. Default is INFO
    103 # This is an alternative to changing the rootLogger in log4j2.xml
    104 #SOLR_LOG_LEVEL=INFO
    105 
    106 # Location where Solr should write logs to. Absolute or relative to solr start dir
    107 SOLR_LOGS_DIR=/usr/solr-7.4.0/solr_logs                   <==需先创建相应目录
    108 
    109 # Enables log rotation before starting Solr. Setting SOLR_LOG_PRESTART_ROTATION=true will let Solr take care of pre
    110 # start rotation of logs. This is false by default as log4j2 handles this for us. If you choose to use another log
    111 # framework that cannot do startup rotation, you may want to enable this to let Solr rotate logs on startup.
    112 SOLR_LOG_PRESTART_ROTATION=true             <==每次重启启动solr都会新生成一个log文件
    113 
    114 # Sets the port Solr binds to, default is 8983
    115 SOLR_PORT=8983
 

各solr服务器通过zookeeper共享的内容如下:

[root@slave1 solr_logs]# zkCli.sh             <==登录zookeeper
log4j:WARN No such property [maxFileSize] in org.apache.log4j.DailyRollingFileAppender.
Connecting to localhost:2181
Welcome to ZooKeeper!
JLine support is enabled

WATCHER::

WatchedEvent state:SyncConnected type:None path:null
[zk: localhost:2181(CONNECTED) 0] ls /
[configs, zookeeper, overseer, aliases.json, live_nodes, collections, overseer_elect, security.json, clusterstate.json, autoscaling, autoscaling.json]
[zk: localhost:2181(CONNECTED) 1] 
solr页面也可以看到,如下:

Linux环境 solr-7.4.0安装【一篇就够】_第2张图片

 

 

八、扩展-配置solr.in.sh后,再次以SolrCloud模式启动
启动时需要保证solr_home下有solr.xml文件,否则会失败。solr.xml内容如下:

     29
     30 
     31  
     32 
     33     ${host:slave1}
     34     ${jetty.port:8983}
     35     ${hostContext:solr}
     36 
     37     ${genericCoreNodeNames:true}
     38 
     39     ${zkClientTimeout:30000}
     40     ${distribUpdateSoTimeout:600000}
     41     ${distribUpdateConnTimeout:60000}
     42     ${zkCredentialsProvider:org.apache.solr.common.cloud.DefaultZkCredentialsProvider}
     43     ${zkACLProvider:org.apache.solr.common.cloud.DefaultZkACLProvider}
     44 
     45  

     46 
     47        48     class="HttpShardHandlerFactory">
     49     ${socketTimeout:600000}
     50     ${connTimeout:60000}
     51  

     52 
     53


九、扩展-以SolrCloud模式启动后,创建/删除collection

[root@slave1 solr-7.4.0]# solr create_collection -c collection1 -shards 2 -replicationFactor 2 -p 8983 -force
WARNING: Using _default configset with data driven schema functionality. NOT RECOMMENDED for production use.
         To turn off: bin/solr config -c collection1 -p 8983 -action set-user-property -property update.autoCreateFields -value false
INFO  - 2018-09-02 23:31:02.105; org.apache.solr.util.configuration.SSLCredentialProviderFactory; Processing SSL Credential Provider chain: env;sysprop
Created collection 'collection1' with 2 shard(s), 2 replica(s) with config-set 'collection1'

默认会把 /usr/solr-7.4.0/server/solr/configsets/_default/conf 下内容,上传到zookeeper 的 /configs/collection1 目录下

[root@slave1 conf]# pwd
/usr/solr-7.4.0/server/solr/configsets/_default/conf
[root@slave1 conf]# ls
lang  managed-schema  params.json  protwords.txt  solrconfig.xml  stopwords.txt  synonyms.txt

[root@slave1 solr_logs]# zkCli.sh                     <==登录zookeeper
log4j:WARN No such property [maxFileSize] in org.apache.log4j.DailyRollingFileAppender.
Connecting to localhost:2181
Welcome to ZooKeeper!
JLine support is enabled

WATCHER::

WatchedEvent state:SyncConnected type:None path:null
[zk: localhost:2181(CONNECTED) 0] ls /configs/collection1
[managed-schema, protwords.txt, solrconfig.xml, synonyms.txt, stopwords.txt, lang, params.json]
[zk: localhost:2181(CONNECTED) 1] 
 

删除collection1,同时zookeeper上对应的配置文件目录(/configs/collection1)也会被删除

[root@slave1 conf]# solr delete -c collection1
INFO  - 2018-09-02 23:46:26.788; org.apache.solr.util.configuration.SSLCredentialProviderFactory; Processing SSL Credential Provider chain: env;sysprop
{
  "responseHeader":{
    "status":0,
    "QTime":1364},
  "success":{
    "master:8983_solr":{"responseHeader":{
        "status":0,
        "QTime":264}},
    "slave2:8983_solr":{"responseHeader":{
        "status":0,
        "QTime":274}},
    "slave1:8983_solr":{"responseHeader":{
        "status":0,
        "QTime":370}}}}


Deleted collection 'collection1' using command:
http://slave2:8983/solr/admin/collections?action=DELETE&name=collection1

zookeeper上对应Node不再存在

[zk: localhost:2181(CONNECTED) 1] ls /configs/collection1
Node does not exist: /configs/collection1

 

从solr服务器上传配置文件到zookeeper(方法一)

[root@master conf]# solr zk                   <==查看solr zk所有命令

ERROR: Zookeeper operation (one of 'upconfig', 'downconfig', 'rm', 'mv', 'cp', 'ls', 'mkroot') is required!

  Usage: solr zk upconfig|downconfig -d -n [-z zkHost]
         solr zk cp [-r] [-z zkHost]
         solr zk rm [-r] [-z zkHost]
         solr zk mv [-z zkHost]
         solr zk ls [-r] [-z zkHost]
         solr zk mkroot [-z zkHost]

[root@master conf]# solr zk upconfig -d /usr/solr-7.4.0/solr_confdir/coll1_conf/conf -n collection1
INFO  - 2018-09-03 00:15:42.578; org.apache.solr.util.configuration.SSLCredentialProviderFactory; Processing SSL Credential Provider chain: env;sysprop
Uploading /usr/solr-7.4.0/solr_confdir/coll1_conf/conf for config collection1 to ZooKeeper at master:2181,slave1:2181,slave2:2181

从solr服务器上传配置文件到zookeeper(方法二)

sh /usr/solr-7.4.0/server/scripts/cloud-scripts/zkcli.sh -zkhost master:2181,slave1:2181,slave2:2181 -cmd upconfig -confdir /usr/solr-7.4.0/solr_confdir/coll1_conf/conf(solr服务器conf目录) -confname collection1(zookeeper对应collection目录)

 

从zookeeper上下载配置文件到solr服务器(方法一)

[root@master coll1_conf]# solr zk downconfig -d /usr/solr-7.4.0/solr_confdir/coll1_conf -n collection1
INFO  - 2018-09-03 00:09:08.594; org.apache.solr.util.configuration.SSLCredentialProviderFactory; Processing SSL Credential Provider chain: env;sysprop
Downloading configset collection1 from ZooKeeper at master:2181,slave1:2181,slave2:2181 to directory /usr/solr-7.4.0/solr_confdir/coll1_conf/conf
[root@master coll1_conf]# ls
conf
[root@master coll1_conf]#
cd conf
[root@master conf]# ls
lang  managed-schema  params.json  protwords.txt  solrconfig.xml  stopwords.txt  synonyms.txt
[root@master conf]# 

从zookeeper上下载配置文件到solr服务器(方法二)
sh /usr/solr-7.4.0/server/scripts/cloud-scripts/zkcli.sh -cmd downconfig -zkhost master:2181 -confdir /usr/solr-7.4.0/solr_confdir/coll1_conf/conf(solr服务器conf目录) -confname collection1(zookeeper对应collection目录)

 

十、扩展-以SolrCloud模式启动后,将MySQL中的表数据import到collection中

Linux环境 solr-7.4.0安装【一篇就够】_第3张图片

需要在配置文件solrconfig.xml中增加requestHandler,否则点击Dataimport会显示:

The solrconfig.xml file for this index does not have an operational DataImportHandler defined.

1,修改配置文件solrconfig.xml(增加蓝字部分)

     60              
     61 
     62  
     76  
     77  
     78 
     79  
     80  
     81 
     82  
     83  
     84 
     85  
     86  
     87 
     88  
     89        

     90  
    694    
    695    
    696       solr-data-config.xml
    697    

    698  

 

2,增加solr-data-config.xml配置文件,内容如下:


                        url="jdbc:mysql://192.168.0.4:3306/test?useUnicode=true&characterEncoding=utf8"
                user="root" password="root"/>
 
          query="select id,name,directed_by,genre,initial_release_date from films">
     
     
     
     
     
   

 

3,修改配置文件managed-schema(增加蓝字部分)

    431  
    432  
    433  
    434  
    435        <==默认存在
    436  
 

4,将mysql-connector-java-5.1.44.jar放到/usr/solr-7.4.0/server/solr-webapp/webapp/WEB-INF/lib下

5,使用root用户访问mysql可能会发生错误: Host 'SJMZ' is not allowed to connect to this MySQL server

这个错误,是因为MySQL不允许远程登录,所以远程登录失败了,解决方法如下:
a) 登录mysql
b) 执行use mysql;
c) 执行update user set host = '%' where user = 'root';
d) 执行FLUSH PRIVILEGES
注: d)步是刷新MySQL的权限相关表,一定不要忘了。

6,solr页面执行Dataimport

7,solr执行Query即可查看到导入数据

Linux环境 solr-7.4.0安装【一篇就够】_第4张图片

 

你可能感兴趣的:(solr)