redis下载及安装运行

安装redis的方式有两种
第一种:用yum安装
在centos7系统上需要安装epel源

 [root@localhost ~]# yum install -y redis 

注意: 这种方式安装的redis版本过低
第二种:从redis官方网站下载
redis官方网站:https://redis.io
下载redis源码

 [root@localhost ~]# wget https://github.com/redis/redis/archive/7.2.0.tar.gz

解压至指定的目录

 [root@localhost ~]# tar -xvf redis-7.2.0.tar.gz -C /data/ 

安装相应的依赖包

 [root@localhost ~]# yum -y install gcc jemalloc-devel
  [root@localhost ~]# cd /data/redis-7.2.0

安装源码到指定的目录/data/redis

 [root@localhost ~]# make PREFIX=/data/redis install

设置环境变量

 [root@localhost ~]# echo "PATH=${PATH}:/data/redis/bin" >> /etc/bashrc 
  [root@localhost ~]#source /etc/bashrc

创建对应的用户,及文件夹

 [root@localhost ~]# useradd -r redis
 [root@localhost ~]# mkdir /data/redis/{conf,log,data}

复制配置文件到相应的目录

  [root@localhost ~]# cp /data/redis-7.2.0/redis.conf /data/redis/conf/

修改配置文件,建议备份后,用一个新的文件,然后添加以下内容

  [root@localhost ~]#  vim /data/redis/conf/redis.conf 

daemonize yes
pidfile /data/redis/conf/redis_6379.pid #这个6379可以根据实际情况指定
dir /data/redis/data/

#如果不想设置redis后台运行,可以将daemonize这一注释掉

更改目录属主属组

  [root@localhost ~]# chown -R redis.redis /data/redis

修改redis.service配置文件内容

 [root@localhost ~]# vim /usr/lib/systemd/system/redis.service

[Unit]
Description=Redis persistent key-value database
After=network.target
[Service]
User=redis
Group=redis
PIDFile=/data/redis/conf/redis_6379.pid
Type=forking
ExecStart=/data/redis/bin/redis-server /data/redis/conf/redis.conf
[Install]
WantedBy=multi-user.target

修改完service文件之后,重新载入

 [root@localhost ~]# systemctl daemon-reload
 [root@localhost ~]# systemctl start redis
正常情况下,操作完以上步骤之后,就可以直接登录到redis,如果输入redis      命令之后,显示未找到命令,需要做下面这一步:
   [root@localhost ~]# cd /data/redis-7.2.0/

将src目录里的redis-cli文件复制到/usr/local/bin/目录下,然后就可以正常按上边的步骤操作了

[root@localhost ~]#cp src/redis-cli /usr/local/bin

登录到redis

redis-cli -h 127.0.0.1

127.0.0.1:6379> set a 100
127.0.0.1:6379> get a
“100”

Redis默认开启了保护模式,只允许本地回环地址访问redis,不允许远程访问

 [root@localhost ~]#redis-cli -h 192.168.122.100

192.168.122.100:6379> set a 1
Error: Connection reset by peer
可以看见不能连接,所以需要我们在redis.conf配置文件中添加以下内容

[root@localhost ~]#  vim /data/redis/conf/redis.conf

daemonize yes
pidfile /data/redis/conf/redis_6379.pid
dir /data/redis/data/
bind 192.168.122.100
requirepass 123456

重启redis服务

[root@localhost ~]#  systemctl restart redis

然后重新登录redis

[root@localhost ~]# redis-cli -h 192.168.122.100 -a 123456
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
192.168.122.100:6379> get a
"haha"
192.168.122.100:6379> set c 100
OK
192.168.122.100:6379> get c
"100"

看到以上界面,并能操作,就说明redis已经安装并能正常运行。
以下redis的配置文件的参数,可以根据实际情况添加配置,如下:
bind 0.0.0.0 #监听地址,可以用空格隔开后多个监听IP

requirepass foobared #设置redis连接密码,之后需要AUTH pass

protected-mode yes #redis3.2之后加入的新特性,在没有设置bind IP和密码的时候,redis只允许访问127.0.0.1:6379,可以远程连接,但当访问将提示警告信息并拒绝远程访问

port 6379 #监听端口,默认6379/tcp

timeout 0 #客户端和Redis服务端的连接超时时间,默认是0,表示永不超时

loglevel notice #日志级别

logfile “/path/redis.log” #日志路径,示例:logfile “/data/redis/log/redis_6379.log” databases 16 #设置数据库数量,默认:0-15,共16个库
always-show-logo yes #在启动redis 时是否显示或在日志中记录记录redis的logo

maxclients 10000 #Redis最大连接客户端

maxmemory #redis使用的最大内存,单位为bytes字节,0为不限制,建议设为物理内存一半, 8G内存的计算方式8(G)*1024(MB)1024(KB)*1024(Kbyte),需要注意的是缓冲区是不计算在maxmemory 内.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值