linux 安装redis

1,什么是redis
redis是当前比较热门的NOSQL系统之一,它是一个key-value存储系统。和Memcached类似,但很大程度补偿了memcached的不足,它支持存储的value类型相对更多,包括string、list、set、zset和hash。这些数据类型都支持push/pop、add/remove及取交集并集和差集及更丰富的操作。在此基础上,redis支持各种不同方式的排序。Redis数据都是缓存在计算机内存中,并且会周期性的把更新的数据写入磁盘或者把修改操作写入追加的记录文件。

2,安装redis

[root@localhost redis]# wget http://download.redis.io/releases/redis-4.0.6.tar.gz
--2018-07-02 15:52:29--  http://download.redis.io/releases/redis-4.0.6.tar.gz
正在解析主机 download.redis.io (download.redis.io)... 109.74.203.151
正在连接 download.redis.io (download.redis.io)|109.74.203.151|:80... 已连接。
已发出 HTTP 请求,正在等待回应... 200 OK
长度:1723533 (1.6M) [application/x-gzip]
正在保存至: “redis-4.0.6.tar.gz”

100%[=================================================================================================>] 1,723,533    211KB/s 用时 8.3s   

2018-07-02 15:52:38 (202 KB/s) - 已保存 “redis-4.0.6.tar.gz” [1723533/1723533])

 

解压安装包

[root@localhost redis]# ll
总用量 1684
-rw-r--r--. 1 root root 1723533 12月  5 2017 redis-4.0.6.tar.gz
[root@localhost redis]# tar -xzvf redis-4.0.6.tar.gz 

 

安装依赖

[root@localhost ]# yum install gcc-c++
已加载插件:fastestmirror, langpacks
Loading mirror speeds from cached hostfile
 * base: mirrors.aliyun.com
 * extras: mirrors.aliyun.com
 * updates: mirrors.aliyun.com
正在解决依赖关系
--> 正在检查事务
---> 软件包 gcc-c++.x86_64.0.4.8.5-28.el7_5.1 将被 安装

 

编译安装

[root@localhost redis-4.0.6]# cd src && make install
    CC Makefile.dep
rm -rf redis-server redis-sentinel redis-cli redis-benchmark redis-check-rdb redis-check-aof *.o *.gcda *.gcno *.gcov redis.info lcov-html Makefile.dep dict-benchmark
(cd ../deps && make distclean)

 

进入src

[root@localhost redis-4.0.6]# cd src/

 

启动redis

[root@localhost src]# ./redis-server 
59191:C 02 Jul 16:11:05.921 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
59191:C 02 Jul 16:11:05.921 # Redis version=4.0.6, bits=64, commit=00000000, modified=0, pid=59191, just started
59191:C 02 Jul 16:11:05.921 # Warning: no config file specified, using the default config. In order to specify a config file use ./redis-server /path/to/redis.conf
59191:M 02 Jul 16:11:05.922 * Increased maximum number of open files to 10032 (it was originally set to 1024).
                _._                                                  
           _.-``__ ''-._                                             
      _.-``    `.  `_.  ''-._           Redis 4.0.6 (00000000/0) 64 bit
  .-`` .-```.  ```\/    _.,_ ''-._                                   
 (    '      ,       .-`  | `,    )     Running in standalone mode
 |`-._`-...-` __...-.``-._|'` _.-'|     Port: 6379
 |    `-._   `._    /     _.-'    |     PID: 59191
  `-._    `-._  `-./  _.-'    _.-'                                   
 |`-._`-._    `-.__.-'    _.-'_.-'|                                  
 |    `-._`-._        _.-'_.-'    |           http://redis.io        
  `-._    `-._`-.__.-'_.-'    _.-'                                   
 |`-._`-._    `-.__.-'    _.-'_.-'|                                  
 |    `-._`-._        _.-'_.-'    |                                  
  `-._    `-._`-.__.-'_.-'    _.-'                                   
      `-._    `-.__.-'    _.-'                                       
          `-._        _.-'                                           
              `-.__.-'                                               

59191:M 02 Jul 16:11:06.007 # WARNING: The TCP backlog setting of 511 cannot be enforced because /proc/sys/net/core/somaxconn is set to the lower value of 128.
59191:M 02 Jul 16:11:06.007 # Server initialized
59191:M 02 Jul 16:11:06.008 # WARNING overcommit_memory is set to 0! Background save may fail under low memory condition. To fix this issue add 'vm.overcommit_memory = 1' to /etc/sysctl.conf and then reboot or run the command 'sysctl vm.overcommit_memory=1' for this to take effect.
59191:M 02 Jul 16:11:06.008 # WARNING you have Transparent Huge Pages (THP) support enabled in your kernel. This will create latency and memory usage issues with Redis. To fix this issue run the command 'echo never > /sys/kernel/mm/transparent_hugepage/enabled' as root, and add it to your /etc/rc.local in order to retain the setting after a reboot. Redis must be restarted after THP is disabled.
59191:M 02 Jul 16:11:06.008 * Ready to accept connections

redis启动成功,但是这种启动方式需要一直打开窗口,不能进行其他操作,不太方便。

 

修改redis的配置文件

[root@localhost redis-4.0.6]# vim redis.conf

注释掉 bind 127.0.0.1,让所有ip都可以访问redis服务

# bind 127.0.0.1 或改成 bind 0.0.0.0

然后修改protected-mode为no, 才能完成暴漏redis的服务

protected-mode no

修改daemonize为yes,使启动服务时为后台启动

daemonize yes

可以不改下面的

#requirepass foobared去掉注释,foobared改为自己的密码,我在这里改为

requirepass 123456

 

指定redis.conf文件启动

[root@localhost src]# ./redis-server  ../redis.conf 
61187:C 02 Jul 16:42:40.561 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
61187:C 02 Jul 16:42:40.561 # Redis version=4.0.6, bits=64, commit=00000000, modified=0, pid=61187, just started
61187:C 02 Jul 16:42:40.561 # Configuration loaded
[root@localhost src]# ps -ef | grep redis
root      61188      1  0 16:42 ?        00:00:00 ./redis-server 0.0.0.0:6379
root      61231   3161  0 16:43 pts/0    00:00:00 grep --color=auto redis

杀掉redis进程

[root@localhost src]# kill -9 61188

 

设置redis开机自启动

[root@localhost etc]# mkdir redis

 

进入到安装目录,文件复制一份到/etc/redis目录下

[root@localhost redis-4.0.6]# cp redis.conf /etc/redis/redis_6379.conf

 

将redis的启动脚本复制一份放到/etc/init.d目录下

[root@localhost redis-4.0.6]# cp ./utils/redis_init_script /etc/init.d/redis_6379

 

切换到/etc/init.d目录下

[root@localhost redis-4.0.6]# cd /etc/init.d/

执行自启命令

[root@localhost init.d]# chkconfig redis_6379 on

如果报错要修改启动脚本

service redisd does not support chkconfig

 

需要对Redis初始化脚本进行简单修改,执行命令:

[root@localhost redis-4.0.6]# vim /etc/init.d/redis_6379

修改启动脚本

#!/bin/sh
# chkconfig:   2345 90 10
# description:  Redis is a persistent key-value database
#
# Simple Redis init.d script conceived to work on Linux systems
# as it does use of the /proc filesystem.

REDISPORT=6379

注释的意思是,redis服务必须在运行级2,3,4,5下被启动或关闭,启动的优先级是90,关闭的优先级是10。

在设置自启动

[root@localhost init.d]# chkconfig redis_6379 on

启动,发现报错

[root@localhost init.d]# service redis_6379 start
/var/run/redis_6379.pid exists, process is already running or crashed

解决方法:

1:可用安装文件启动   ./redis-server  /etc/redis/6379.conf

2:shutdown -r now  软重启让系统自动恢复下就行了

 

如果启动出现下面错误:


Starting Redis server...
3161:C 02 Jul 17:28:42.560 # Fatal error, can't open config file '/etc/redis/6379.conf'

解决办法:

REDISPORT=6379
EXEC=/usr/local/bin/redis-server
CLIEXEC=/usr/local/bin/redis-cli

PIDFILE=/var/run/redis_${REDISPORT}.pid
CONF="/etc/redis/${REDISPORT}.conf"

将redis配置文件复制到/etc/redis/${REDISPORT}.conf 

[root@xxx]# mkdir /etc/redis

[root@xxx]# cp /opt/redis/redis-2.8.21/redis.conf  /etc/redis/6379.conf

 

测试

[root@localhost xinetd.d]# telnet 192.168.129.128 6379
Trying 192.168.129.128...
Connected to 192.168.129.128.
Escape character is '^]'.
set a 10
+OK
get a 
$2
10

 

redis 常用命令


incr a(a加一) 
decr a (a减一) 
del a (删除)

常用数据类型 
String 
Hash 
List 
Set 
SortedSet

最后让redis能远程访问开通防火墙端口

[root@localhost xinetd.d]# firewall-cmd --add-port=6379/tcp --permanent
success
[root@localhost xinetd.d]# firewall-cmd --reload
success
[root@localhost xinetd.d]# 

 

远程客户端连接redis


远程客户端连接redis
redis-cli -h 127.0.0.1 -p 6379


查询所有key
127.0.0.1:6379> keys *

查询所有keys 中带TEST的key
127.0.0.1:6379> keys TEST*

查看key 的value值
127.0.0.1:6379> get key


删除Key
127.0.0.1:6379> del mykey
(integer) 1

查看一下刚刚删除的Key是否还存在
127.0.0.1:6379> exists mykey 
(integer) 0 

将mykey改名为mykey1 
127.0.0.1:6379> rename mykey mykey1 
OK 

将该键的超时设置为100秒。 
127.0.0.1:6379> expire mykey 100 
(integer) 1 

通过ttl命令查看一下还剩下多少秒。 
127.0.0.1:6379> ttl mykey 
(integer) 97 

立刻执行persist命令,该存在超时的键变成持久化的键,即将该Key的超时去掉。 
127.0.0.1:6379> persist mykey 
(integer) 1 

ttl的返回值告诉我们,该键已经没有超时了,变成持久化了
127.0.0.1:6379> ttl mykey 
(integer) -1 


mykey的值是字符串类型,因此返回string
127.0.0.1:6379> type mykey 
string 

 

转载于:https://my.oschina.net/saulc/blog/1839258

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值