设为首页 收藏本站
查看: 1598|回复: 0

[经验分享] CentOS6.4安装配置redis

[复制链接]
累计签到:1 天
连续签到:1 天
发表于 2015-7-27 08:43:53 | 显示全部楼层 |阅读模式
一、环境

系统        CentOS6.4x64最小化安装
redis-m      192.168.1.13
redis-s     192.168.1.14
二、安装redis
下载软件
1
[iyunv@redis-m ~]# wget http://download.redis.io/releases/redis-2.8.9.tar.gz



安装

1
2
3
4
5
6
7
8
9
10
11
12
13
14
[iyunv@redis-m ~]# tar xf redis-2.8.9.tar.gz
[iyunv@redis-m ~]# cd redis-2.8.9
[iyunv@redis-m redis-2.8.9]# make
[iyunv@redis-m redis-2.8.9]# make PREFIX=/usr/local/redis-2.8.9 install
[iyunv@redis-m redis-2.8.9]# ln -s /usr/local/redis-2.8.9/ /usr/local/redis

#默认文件
[iyunv@redis-m ~]# ll /usr/local/redis/bin
total 13912
-rwxr-xr-x 1 root root 4172184 Jul 26 18:08 redis-benchmark    #redis性能测试工具
-rwxr-xr-x 1 root root   22177 Jul 26 18:08 redis-check-aof    #对更新日志appendonly.aof检查,是否可用
-rwxr-xr-x 1 root root   45411 Jul 26 18:08 redis-check-dump    #用于本地数据库rdb文件的检查
-rwxr-xr-x 1 root root 4265375 Jul 26 18:08 redis-cli           #redis命令行操作工具
-rwxr-xr-x 1 root root 5728711 Jul 26 18:08 redis-server        #服务器的daemon启动程序



启动redis
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
#增加环境变量
[iyunv@redis-m ~]# echo 'PATH=$PATH:/usr/local/redis/bin' >>/etc/profile
[iyunv@redis-m ~]# source /etc/profile
[iyunv@redis-m ~]# which redis-server
/usr/local/redis/bin/redis-server

[iyunv@redis-m ~]# redis-server -h
Usage: ./redis-server [/path/to/redis.conf] [options]
       ./redis-server - (read config from stdin)
       ./redis-server -v or --version
       ./redis-server -h or --help
       ./redis-server --test-memory <megabytes>

Examples:
       ./redis-server (run the server with default conf)
       ./redis-server /etc/redis/6379.conf
       ./redis-server --port 7777
       ./redis-server --port 7777 --slaveof 127.0.0.1 8888
       ./redis-server /etc/myredis.conf --loglevel verbose

Sentinel mode:
       ./redis-server /etc/sentinel.conf --sentinel
        
#启动服务,由于默认是没有配置文件,所以我们从安装目录cp一个配置文件
[iyunv@redis-m ~]# mkdir /usr/local/redis/conf
[iyunv@redis-m ~]# cp /root/redis-2.8.9/redis.conf /usr/local/redis/conf/

#在后台启动
[iyunv@redis-m ~]# redis-server /usr/local/redis/conf/redis.conf &
[1] 5870
[iyunv@redis-m ~]# [5870] 26 Jul 18:24:24.771 * Increased maximum number of open files to 10032 (it was originally set to 1024).
                _._                                                  
           _.-``__ ''-._                                             
      _.-``    `.  `_.  ''-._           Redis 2.8.9 (00000000/0) 64 bit
  .-`` .-```.  ```\/    _.,_ ''-._                                   
(    '      ,       .-`  | `,    )     Running in stand alone mode
|`-._`-...-` __...-.``-._|'` _.-'|     Port: 6379
|    `-._   `._    /     _.-'    |     PID: 5870
  `-._    `-._  `-./  _.-'    _.-'                                   
|`-._`-._    `-.__.-'    _.-'_.-'|                                 
|    `-._`-._        _.-'_.-'    |           http://redis.io        
  `-._    `-._`-.__.-'_.-'    _.-'                                   
|`-._`-._    `-.__.-'    _.-'_.-'|                                 
|    `-._`-._        _.-'_.-'    |                                 
  `-._    `-._`-.__.-'_.-'    _.-'                                   
      `-._    `-.__.-'    _.-'                                       
          `-._        _.-'                                          
              `-.__.-'                                               

[5870] 26 Jul 18:24:24.774 # Server started, Redis version 2.8.9
[5870] 26 Jul 18:24:24.774 # 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.
[5870] 26 Jul 18:24:24.774 * The server is now ready to accept connections on port 6379

[iyunv@redis-m ~]# netstat -tunlp |grep redis
tcp        0      0 0.0.0.0:6379                0.0.0.0:*                   LISTEN      5870/redis-server *
tcp        0      0 :::6379                     :::*                        LISTEN      5870/redis-server *

#解决warning警告
[iyunv@redis-m ~]# sysctl vm.overcommit_memory=1
vm.overcommit_memory = 1
[iyunv@redis-m ~]# echo "vm.overcommit_memory = 1" >>/etc/sysctl.conf
[iyunv@redis-m ~]# sysctl -p

#关闭redis服务
[iyunv@redis-m ~]# redis-cli shutdown
[5870] 26 Jul 18:32:13.908 # User requested shutdown...
[5870] 26 Jul 18:32:13.908 * Saving the final RDB snapshot before exiting.
[5870] 26 Jul 18:32:13.926 * DB saved on disk
[5870] 26 Jul 18:32:13.926 # Redis is now ready to exit, bye bye...
[1]+  Done                    redis-server /usr/local/redis/conf/redis.conf
[iyunv@redis-m ~]# netstat -tunlp |grep redis



redis启动脚本
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
[iyunv@redis-m ~]# cat redis.sh
#!/bin/bash
# chkconfig: 2345 50 30
#
# description: Redis service
#
#Script:Redis command
  
Redisserver=/usr/local/redis/bin/redis-server
Rediscli=/usr/local/redis/bin/redis-cli
Redisconf=/usr/local/redis/conf/redis.conf
  
function_start()
{
    printf "start redis-server..."
    $Redisserver $Redisconf &>/dev/null  &
    if [ $? -eq 0 ];then
        echo "runing"
    fi
}
  
function_stop()
{
    printf "stop redis-server..."
    $Rediscli -p 6379 shutdown
    if [ $? -eq 0 ];then
        echo "stop"
    fi
}
  
function_restart()
{
    function_start
    function_stop
}
  
function_kill()
{
    killall redis-server
}
  
function_status()
{
    a=`ps -A|grep "redis-server\>" -c`
    if [ $a -ge 1 ];then
        echo -e "The Redis is [\e[0;32;5m runing \e[0m]"
    else
        echo -e "The Redis is [\e[0;31;5m not run \e[0m]"
    fi
}
  
case "$1" in
        start)
                function_start
                ;;
        stop)
                function_stop
                ;;
        restart)
                function_stop
                function_start
                ;;
        kill)
                function_kill
                ;;
        status)
                function_status
                ;;
              *)
              echo "Usage: /etc/init.d/redis {start|stop|restart|kill|status}"
              
esac
  
exit



三、操作redis
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
[iyunv@redis-m ~]# redis-cli
#添加一个值set  key value
127.0.0.1:6379> set id 001
OK
127.0.0.1:6379> get id
"001"
127.0.0.1:6379> del id
(integer) 1        #返回1,表示操作成功
127.0.0.1:6379> get id
(nil)
127.0.0.1:6379> exists id        #判断一个key是否存在
(integer) 0        #返回0表示不存在

#切换数据库,默认有16个库,标号从0-15,这里我们切换到1号库
127.0.0.1:6379> select 1
OK
127.0.0.1:6379[1]> keys *
(empty list or set)
127.0.0.1:6379[1]> set name lyao
OK
127.0.0.1:6379[1]> get name
"lyao"

#切回0号库
127.0.0.1:6379[1]> select 0
OK
127.0.0.1:6379> get name        #这个key只在1号库中才有
(nil)

#其它的连接方法
[iyunv@redis-m ~]# redis-cli  -h 192.168.1.13 -p 6379
192.168.1.13:6379>
-h    #指定主机地址
-p    #指定端口号

#redis使用帮助
[iyunv@redis-m ~]# redis-cli
127.0.0.1:6379> help
redis-cli 2.8.9
Type: "help @<group>" to get a list of commands in <group>
      "help <command>" for help on <command>
      "help <tab>" to get a list of possible help topics
      "quit" to exit
127.0.0.1:6379> help set        #查询某个命令的使用帮助

  SET key value [EX seconds] [PX milliseconds] [NX|XX]
  summary: Set the string value of a key
  since: 1.0.0
  group: string
   
#查询某类命令
127.0.0.1:6379> help @string

  APPEND key value
  summary: Append a value to a key
  since: 2.0.0

  BITCOUNT key [start] [end]
  summary: Count set bits in a string
  since: 2.6.0

  BITOP operation destkey key [key ...]
  summary: Perform bitwise operations between strings
  since: 2.6.0

  DECR key
  summary: Decrement the integer value of a key by one
  since: 1.0.0

  DECRBY key decrement
  summary: Decrement the integer value of a key by the given number
  since: 1.0.0

  GET key
  summary: Get the value of a key
  since: 1.0.0

  GETBIT key offset
  summary: Returns the bit value at offset in the string value stored at key
  since: 2.2.0

  GETRANGE key start end
  summary: Get a substring of the string stored at a key
  since: 2.4.0

  GETSET key value
  summary: Set the string value of a key and return its old value
  since: 1.0.0

  INCR key
  summary: Increment the integer value of a key by one
  since: 1.0.0

  INCRBY key increment
  summary: Increment the integer value of a key by the given amount
  since: 1.0.0

  INCRBYFLOAT key increment
  summary: Increment the float value of a key by the given amount
  since: 2.6.0

  MGET key [key ...]
  summary: Get the values of all the given keys
  since: 1.0.0

  MSET key value [key value ...]
  summary: Set multiple keys to multiple values
  since: 1.0.1

  MSETNX key value [key value ...]
  summary: Set multiple keys to multiple values, only if none of the keys exist
  since: 1.0.1

  PSETEX key milliseconds value
  summary: Set the value and expiration in milliseconds of a key
  since: 2.6.0

  SET key value [EX seconds] [PX milliseconds] [NX|XX]
  summary: Set the string value of a key
  since: 1.0.0

  SETBIT key offset value
  summary: Sets or clears the bit at offset in the string value stored at key
  since: 2.2.0

  SETEX key seconds value
  summary: Set the value and expiration of a key
  since: 2.0.0

  SETNX key value
  summary: Set the value of a key, only if the key does not exist
  since: 1.0.0

  SETRANGE key offset value
  summary: Overwrite part of a string at key starting at the specified offset
  since: 2.2.0

  STRLEN key
  summary: Get the length of the value stored in a key
  since: 2.2.0



四、redis权限
redis为客户端设置外部连接密码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
[iyunv@redis-m ~]# vim /usr/local/redis/conf/redis.conf
requirepass lyao36843    #相关说明大概在326行,每秒能进行150k次数的密码尝试

#重启redis服务
[iyunv@redis-m ~]# redis-cli shutdown
[5892] 26 Jul 19:49:03.756 # User requested shutdown...
[5892] 26 Jul 19:49:03.756 * Saving the final RDB snapshot before exiting.
[5892] 26 Jul 19:49:03.789 * DB saved on disk
[5892] 26 Jul 19:49:03.789 # Redis is now ready to exit, bye bye...
[1]+  Done                    redis-server /usr/local/redis/conf/redis.conf
[iyunv@redis-m ~]# redis-server /usr/local/redis/conf/redis.conf &
[iyunv@redis-m ~]# redis-cli
127.0.0.1:6379> set k v
(error) NOAUTH Authentication required.        #提示权限不够

#设置密码后,在进行命令操作之前,需要进行auth认证
127.0.0.1:6379> auth lyao36843
OK
127.0.0.1:6379> set k v
OK
127.0.0.1:6379> get k
"v"                            #能够正常增加值

#另外一种连接方式
[iyunv@redis-m ~]# redis-cli -a lyao36843
127.0.0.1:6379> set k1 v1
OK
127.0.0.1:6379> get k1
"v1"



五、redis数据类型
数据类型有string,list,set,hash这几种
string类型
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
127.0.0.1:6379> set mykey "my binary safe value"
OK
127.0.0.1:6379> get mykey
"my binary safe value"

#用来计数
127.0.0.1:6379> set counter 1
OK
127.0.0.1:6379> incr counter        #自增
(integer) 2
127.0.0.1:6379> incr counter
(integer) 3
127.0.0.1:6379> incr counter
(integer) 4
127.0.0.1:6379> decr counter        #自减
(integer) 3
127.0.0.1:6379> decr counter
(integer) 2

#getset用法
127.0.0.1:6379> set name lyao
OK
127.0.0.1:6379> getset name lyao36843    #先获取name的值,然后将name的值更新为lyao36843
"lyao"
127.0.0.1:6379> get name
"lyao36843"

#批量操作
127.0.0.1:6379> mset name1 tom age 26 sex male
OK
127.0.0.1:6379> mget name1 age sex
1) "tom"
2) "26"
3) "male"

#给值追加内容
127.0.0.1:6379> get name1
"tom"
127.0.0.1:6379> append name1 "teacher"
(integer) 10
127.0.0.1:6379> get name1
"tomteacher"



六、redis多实例
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
[iyunv@redis-m ~]# mkdir -p /data/6380/data
[iyunv@redis-m ~]# mkdir -p /data/6381/data
[iyunv@redis-m ~]# cp /usr/local/redis/conf/redis.conf /data/6380
[iyunv@redis-m ~]# cp /usr/local/redis/conf/redis.conf /data/6381

#编辑6380的配置文件
[iyunv@redis-m ~]# vim /data/6380/redis.conf
dir /data/6380/data                #修改dump文件的数据目录
port 6380                          #修改端口号
pidfile /data/6380/redis.pid       #修改pid文件路径

#编辑6381的配置文件
[iyunv@redis-m ~]# vim /data/6381/redis.conf
pidfile /data/6381/redis.pid
port 6381
dir /data/6381/data

#启动多实例
[iyunv@redis-m ~]# redis-server /data/6380/redis.conf &
[iyunv@redis-m ~]# redis-server /data/6381/redis.conf &

#查看端口
[iyunv@redis-m ~]# netstat -tunlp | grep redis
tcp        0      0 0.0.0.0:6379                0.0.0.0:*                   LISTEN      5956/redis-server *
tcp        0      0 0.0.0.0:6380                0.0.0.0:*                   LISTEN      6156/redis-server *
tcp        0      0 0.0.0.0:6381                0.0.0.0:*                   LISTEN      6161/redis-server *
tcp        0      0 :::6379                     :::*                        LISTEN      5956/redis-server *
tcp        0      0 :::6380                     :::*                        LISTEN      6156/redis-server *
tcp        0      0 :::6381                     :::*                        LISTEN      6161/redis-server *
#从上面结果我们能看到,6379,6380,6381这3个redis实例都正常运行起来了

#连接到6380实例
[iyunv@redis-m ~]# redis-cli -p 6380
127.0.0.1:6380> auth lyao36843
OK
127.0.0.1:6380> set name k1
OK
127.0.0.1:6380> get name
"k1"
127.0.0.1:6380> quit

#连接到6381实例
[iyunv@redis-m ~]# redis-cli -p 6381
127.0.0.1:6381> auth lyao36843
OK
127.0.0.1:6381> set web httpd
OK
127.0.0.1:6381> get web
"httpd"
127.0.0.1:6381> quit





运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-90965-1-1.html 上篇帖子: Redis系统管理 下篇帖子: supervisor 启动redis服务
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表