安装过Redis的或许都经历过这种麻烦,就是每次想启动或者重启Redis服务的时候,都要到Redis的安装目录下/../redis_5.0.3/src通过./redis-server启动Redis服务,有时候还要指定配置文件,这样实在是太麻烦了。那么有什么简便的方法来启动Redis服务呢?就像我们使用systemctl命令启动一个服务的时候,三个单词就解决问题。
读者可能知道我们如果想要使用systemctl启动Redis服务,就要自己手动配置系统服务,这样听起来或许还是有点复杂,那么有没有什么更简单的方法呢。今天在学习Redis的时候正好看到了这部分内容,就是Redis内置了一个配置系统服务的脚本,运行脚本然后跟着脚本配置几个参数就好了,非常方便。下面演示一下我的配置:
第一步还是逃不过要进入Redis安装目录,在我的的服务器上是/usr/local/src/redis-5.0.3/。然后再进入utils目录,执行./install_server.sh
,然后脚本会提示你Redis的监听端口,如果你不想被黑客攻击建议你设置一个只有自己知道的端口:
Welcome to the redis service installer
This script will help you easily set up a running redis server
Please select the redis port for this instance: [6379]
如果你想使用默认的就直接回车就好了,中括号里就是默认值,我这里为了方便演示就用了默认的了(不然让不怀好意的人看到我使用什么端口就不好了哈)。
然后脚本提示你redis配置文件的名字,这个无所谓,默认就可以了。默认的会根据你上面的端口号在/etc/redis/目录下生成一个端口号.conf的配置文件:
Please select the redis config file name [/etc/redis/6379.conf]
Selected default - /etc/redis/6379.conf
后面会提示你选择redis日志和数据的存储路径,选默认的就好了:
Please select the redis log file name [/var/log/redis_6379.log]
Selected default - /var/log/redis_6379.log
Please select the data directory for this instance [/var/lib/redis/6379]
Selected default - /var/lib/redis/6379
再后面就是选择redis-server的可执行路径,这个需要自己手动输入,然后脚本会自动推测redis-cli的路径:
Please select the redis executable path [] /usr/local/src/redis-5.0.3/src/redis-server
配置完成之后需要你确认一下,没问题回车就好:
Selected config:
Port : 6379
Config file : /etc/redis/6379.conf
Log file : /var/log/redis_6379.log
Data dir : /var/lib/redis/6379
Executable : /usr/local/src/redis-5.0.3/src/redis-server
Cli Executable : /usr/local/src/redis-5.0.3/src/redis-cli
Is this ok? Then press ENTER to go on or Ctrl-C to abort.
这就是配置成功的提示:
Copied /tmp/6379.conf => /etc/init.d/redis_6379
Installing service...
Successfully added to chkconfig!
Successfully added to runlevels 345!
Starting Redis server...
Installation successful!
我们可以看到脚本自动给我们添加了系统服务和运行级别。
然后就可以愉快的使用systemctl start redis_6379
运行redis服务了!
[root@iz2ze9ibpdra8vnyqrclsdz ~]# systemctl start redis_6379
[root@iz2ze9ibpdra8vnyqrclsdz ~]# netstat -tlnp|grep redis
tcp 0 0 127.0.0.1:6379 0.0.0.0:* LISTEN 9434/./redis-server
本博客所有文章除特别声明外,均采用 CC BY-SA 3.0协议 。转载请注明出处!