方法一
1.编辑vi /etc/systemd/system/redis.service
[Unit]
Description=redis-server
After=network.target
[Service]
Type=forking
#redis的安装目录
ExecStart=/usr/local/redis-2.8.18/src/redis-server /usr/local/redis-2.8.18/redis.conf
PrivateTmp=true
[Install]
WantedBy=multi-user.target
2.重新加载参数systemctl daemon-reload
3.启动redis服务,systemctl start redis
4.开机自启动 systemctl enable redis
方法二
1.先配置redis.conf,将daemonize 配置为yes,可以进入后台运行,将bind改为0.0.0.0
2.复制redis配置文件
//1.创建文件
mkdir /etc/redis
//2.将redis安装好目录里面的redis.conf复制到/etc/redis
cp -r /usr/local/redis/redis.conf /etc/redis/
3.复制redis启动脚本
cp -r /usr/local/redis/utils/redis_init_script /etc/init.d/redis
4.修改脚本启动参数
[root@localhost redis]# vi /etc/init.d/redis
#在/etc/init.d/redis文件的头部添加下面两行注释代码,也就是在文件中#!/bin/sh的下方添加
# chkconfig: 2345 10 90
# description: Start and Stop redis
同时还要修改参数,指定redis的安装路径
EXEC=/usr/local/redis/src/redis-server
CLIEXEC=/usr/local/redis/src/redis-cli
5.启动redis
[root@pc1 src]# service redis start
Starting Redis server...
2336:C 04 Sep 16:56:26.149 # Fatal error, can't open config file '/etc/redis/6379.conf'
启动时报错,这是因为我拷贝redis.conf文件到/etc/redis/目录下忘记改名字的原因
将文件名字改过来
mv /etc/redis/redis.conf /etc/redis/6379.conf
再启动,启动成功
[root@pc1 redis]# service redis start
Starting Redis server...
2351:C 04 Sep 16:59:10.398 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
2351:C 04 Sep 16:59:10.398 # Redis version=4.0.10, bits=64, commit=00000000, modified=0, pid=2351, just started
2351:C 04 Sep 16:59:10.398 # Configuration loaded
6.看下redis端口是否起来
[root@pc1 redis]# netstat -ntlp|grep 6379
tcp 0 0 0.0.0.0:6379 0.0.0.0:* LISTEN 2535/redis-server 0
7.设置redis开机自启
//1.将redis添加到开机自启
[root@pc1 redis]# chkconfig redis on
//2.开启redis
[root@pc1 redis]# chkconfig redis on
//3.列出开机自启的服务(2 3 4 5 为是否开启状态,on开启 off未开启)
[root@pc1 redis]# chkconfig --list
Note: This output shows SysV services only and does not include native
systemd services. SysV configuration data might be overridden by native
systemd configuration.
If you want to list systemd services use 'systemctl list-unit-files'.
To see services enabled on particular target use
'systemctl list-dependencies [target]'.
aegis 0:off 1:off 2:on 3:on 4:on 5:on 6:off
netconsole 0:off 1:off 2:off 3:off 4:off 5:off 6:off
network 0:off 1:off 2:on 3:on 4:on 5:on 6:off
nginx 0:off 1:off 2:on 3:on 4:on 5:on 6:off
redis 0:off 1:off 2:on 3:on 4:on 5:on 6:off
chkconfig的级别:
0:关机
1:单用户模式
2:无网络支持的多用户模式
3:有网络支持的多用户模式
4:保留,未使用
5:有网络支持有X-Windows(图形界面)支持的多用户模式
6:重新引导系统,即重启