【中间件】centos7 安装redis及加入系统服务自启动
1、下载或上传已有源码包
我用的版本是4.0.14
# 创建redis下载包文件夹
mkdir /opt/redis
cd /opt/redis
已有源码包,则上传源码包至/opt/redis
或者当场下载一个
wget http://download.redis.io/releases/redis-4.0.14.tar.gz
2、解压
tar zxvf redis-4.0.14.tar.gz
3、编译安装
cd redis-4.0.14
make prefix=/opt/redis/redis-4.0.14 install
编译后在目录下会有一个产生一个新的“src”目录
4、redis配置修改
# 备份reids.conf
cp redis.conf redis.conf.bak
# 修改redis配置
vim redis.conf
修改内容如下
# 所有机器都可以访问
bind 0.0.0.0
# 密码
requirepass fskj123@Redis
# 是否启动守护线程
daemonize yes
:wq 保存退出。
5、将redis服务相关文件拷贝到新的目录,用于添加系统服务
mkdir -p /usr/local/redis
cp src/redis-server /usr/local/redis/
cp src/redis-cli /usr/local/redis/
cp redis.conf /usr/local/redis/
6、添加系统服务,设置开机启动
[root@localhost redis-4.0.14]# vim /etc/systemd/system/redis.service
编辑内容如下:
[Unit]
Description=The redis-server Process Manager
After=syslog.target network.target
[Service]
ExecStart=/usr/local/redis/redis-server /usr/local/redis/redis.conf
ExecReload=/bin/kill -USR2 $MAINPID
ExecStop=/bin/kill -SIGINT $MAINPID
[Install]
WantedBy=multi-user.target
7、设置开机启动
# 使所有配置systemctl 生效
systemctl daemon-reload
# 加入开机自启动
systemctl enable redis
提示
Created symlink from /etc/systemd/system/multi-user.target.wants/redis.service to /etc/systemd/system/redis.service.
查看有没有加入成功
systemctl list-unit-files | grep redis
要移除的话,使用此命令
systemctl disable xxx
启动redis
systemctl start redis
加入系统自启动服务成功
8、检查服务是否启动
[root@localhost redis-4.0.14]# ps -ef| grep redis
root 10200 1 0 11:25 ? 00:00:00 /usr/local/redis/redis-server 127.0.0.1:6379
root 10453 9099 0 11:32 pts/2 00:00:00 grep --color=auto redis
[root@localhost redis-4.0.14]# lsof -i:6379
COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME
redis-ser 10200 root 6u IPv4 318635 0t0 TCP localhost:6379 (LISTEN)
9、创建redis客户端快捷方式(软连接)
ln -s /usr/local/redis/redis-cli /usr/bin/redis-cli
10、测试
# redis-cli连接
[root@localhost redis-4.0.14]# redis-cli
127.0.0.1:6379>
127.0.0.1:6379> set name yaomaomao
(error) NOAUTH Authentication required.
# 需要输入密码
127.0.0.1:6379> auth fskj123@Redis
OK
127.0.0.1:6379> get yaomaomao
(nil)
127.0.0.1:6379> set yaomaomao 123
OK
127.0.0.1:6379> get yaomaomao
"123"
127.0.0.1:6379>
# 查看redis所有key
127.0.0.1:6379> keys *
1) "yaomaomao"
127.0.0.1:6379>
# 退出 exit
127.0.0.1:6379> exit