redis使用

Redis常用命令网址:http://www.runoob.com/redis/redis-java.html

查看redis进程是否启动:ps -ef|grep redis 

Lsof –I :6379

修改文件权限 chmod 777 text

一:redis在Linux安装及报错处理

1.下载Redis-3.2.8.tar.gz 压缩包

解压 :

[plain] view plain copy

  1. # tar zxf redis-3.2.8.tar.gz  

解压以后 需要编译,切到redis解压目录下 ,(ll 是查看当前目录)

[plain] view plain copy

  1. [root@bogon local]# cd redis-3.2.8  
  2. [root@bogon redis-3.2.8]# ll  

编译命令是make 

[plain] view plain copy

  1. [root@bogon redis-3.2.8]# make  
  2. cd src && make all  
  3. make[1]: 进入目录“/usr/local/redis-3.2.8/src”  
  4. rm -rf redis-server redis-sentinel redis-cli redis-benchmark redis-check-rdb redis-check-aof *.o *.gcda *.gcno *.gcov redis.info lcov-html  
  5. (cd ../deps && make distclean)  
  6. make[2]: 进入目录“/usr/local/redis-3.2.8/deps”  

编译过程中出现报错

[html] view plain copy

  1. make[3]: 进入目录“/usr/local/redis-3.2.8/deps/hiredis”  
  2. gcc -std=c99 -pedantic -c -O3 -fPIC  -Wall -W -Wstrict-prototypes -Wwrite-strings -g -ggdb  net.c  
  3. make[3]: gcc:命令未找到  
  4. make[3]: *** [net.o] 错误 127  
  5. make[3]: 离开目录“/usr/local/redis-3.2.8/deps/hiredis”  
  6. make[2]: *** [hiredis] 错误 2  
  7. make[2]: 离开目录“/usr/local/redis-3.2.8/deps”  
  8. make[1]: [persist-settings] 错误 2 (忽略)  
  9.     CC adlist.o  
  10. /bin/sh: cc: 未找到命令  
  11. make[1]: *** [adlist.o] 错误 127  
  12. make[1]: 离开目录“/usr/local/redis-3.2.8/src”  
  13. make: *** [all] 错误 2  

提示gcc命令未找到,这是因为redis没有安装gcc编译器没安装这时候只要安装编译器即

[html] view plain copy

  1. [root@bogon redis-3.2.8]# yum install -y gcc g++ gcc-c++ make  

安装完成提

[html] view plain copy

  1. 已安装:  
  2.   gcc.x86_64 0:4.8.5-11.el7                                               gcc-c++.x86_64 0:4.8.5-11.el7                                                
  3.   
  4. 作为依赖被安装:  
  5.   cpp.x86_64 0:4.8.5-11.el7                           glibc-devel.x86_64 0:2.17-157.el7_3.1          glibc-headers.x86_64 0:2.17-157.el7_3.1           
  6.   kernel-headers.x86_64 0:3.10.0-514.6.2.el7          libmpc.x86_64 0:1.0.1-3.el7                    libstdc++-devel.x86_64 0:4.8.5-11.el7             
  7.   
  8. 更新完毕:  
  9.   make.x86_64 1:3.82-23.el7                                                                                                                             
  10.   
  11. 作为依赖被升级:  
  12.   glibc.x86_64 0:2.17-157.el7_3.1     glibc-common.x86_64 0:2.17-157.el7_3.1     libgcc.x86_64 0:4.8.5-11.el7     libgomp.x86_64 0:4.8.5-11.el7      
  13.   libstdc++.x86_64 0:4.8.5-11.el7      
  14.   
  15. 完毕!  

如果gcc编译器安装过程中报错,可以到百度搜索解决,欢迎评论中补充!!

------------------------------

编译器安装完成之后再redis-3.2.8目录下执行make命令

[html] view plain copy

  1. [root@bogon redis-3.2.8]# make  
  2. cd src && make all  
  3. make[1]: 进入目录“/usr/local/redis-3.2.8/src”  
  4.     CC adlist.o  
  5. In file included from adlist.c:34:0:  
  6. zmalloc.h:50:31: 致命错误:jemalloc/jemalloc.h:没有那个文件或目录  
  7.  #include <jemalloc/jemalloc.h>  
  8.                                ^  
  9. 编译中断。  
  10. make[1]: *** [adlist.o] 错误 1  
  11. make[1]: 离开目录“/usr/local/redis-3.2.8/src”  
  12. make: *** [all] 错误 2  

又出现错误,上网查了之后说

[html] view plain copy

  1. 原因分析  
  2. 在README 有这个一段话。  
  3.   
  4. Allocator    
  5. ---------    
  6.    
  7. Selecting a non-default memory allocator when building Redis is done by setting    
  8. the `MALLOC` environment variable. Redis is compiled and linked against libc    
  9. malloc by default, with the exception of jemalloc being the default on Linux    
  10. systems. This default was picked because jemalloc has proven to have fewer    
  11. fragmentation problems than libc malloc.    
  12.    
  13. To force compiling against libc malloc, use:    
  14.    
  15.     % make MALLOC=libc    
  16.    
  17. To compile against jemalloc on Mac OS X systems, use:    
  18.    
  19.     % make MALLOC=jemalloc  
  20.   
  21. 说关于分配器allocator, 如果有MALLOC  这个 环境变量, 会有用这个环境变量的 去建立Redis。  
  22.   
  23. 而且libc 并不是默认的 分配器, 默认的是 jemalloc, 因为 jemalloc 被证明 有更少的 fragmentation problems 比libc。  
  24.   
  25. 但是如果你又没有jemalloc 而只有 libc 当然 make 出错。 所以加这么一个参数。  

我应该就是这个问题

没有jemalloc 而只有 libc 当然 make 出错。

这时候在编译过程时增加一个参数

[html] view plain copy

  1. [root@bogon redis-3.2.8]# make MALLOC=libc  

编译成功:

其中出现了两个警告

[html] view plain copy

  1. ldo.c: 在函数‘f_parser’中:  
  2. ldo.c:496:7:<span style="color:#ff0000;background-color: rgb(204, 204, 204);"> <strong>警告</strong></span>:未使用的变量‘c’ [-Wunused-variable]  
  3.    int c = luaZ_lookahead(p->z);  

[html] view plain copy

  1. liblua.a(loslib.o):在函数‘os_tmpname’中:  
  2. loslib.c:(.text+0x28c): 警告:the use of `tmpnam' is dangerous, better use `mkstemp'  

说实在的 由于时间关系,警告没有考虑是怎么回事,如果有人愿意分享一下不胜感激

===============

现在redis可以说能用了,执行命令启动redis后 还需要再打开一个窗口执行server-cli测试

[html] view plain copy

  1. [root@bogon src]# ./redis-server  
  2. 28198:C 24 Feb 14:04:55.227 # Warning: no config file specified, using the default config. In order to specify a config file use ./redis-server /path/to/redis.conf  
  3. 28198:M 24 Feb 14:04:55.230 * Increased maximum number of open files to 10032 (it was originally set to 1024).  
  4.                 _._                                                    
  5.            _.-``__ ''-._                                               
  6.       _.-``    `.  `_.  ''-._           Redis 3.2.8 (00000000/0) 64 bit  
  7.   .-`` .-```.  ```\/    _.,_ ''-._                                     
  8.  (    '      ,       .-`  | `,    )     Running in standalone mode  
  9.  |`-._`-...-` __...-.``-._|'` _.-'|     Port: 6379  
  10.  |    `-._   `._    /     _.-'    |     PID: 28198  
  11.   `-._    `-._  `-./  _.-'    _.-'                                     
  12.  |`-._`-._    `-.__.-'    _.-'_.-'|                                    
  13.  |    `-._`-._        _.-'_.-'    |           http://redis.io          
  14.   `-._    `-._`-.__.-'_.-'    _.-'                                     
  15.  |`-._`-._    `-.__.-'    _.-'_.-'|                                    
  16.  |    `-._`-._        _.-'_.-'    |                                    
  17.   `-._    `-._`-.__.-'_.-'    _.-'                                     
  18.       `-._    `-.__.-'    _.-'                                         
  19.           `-._        _.-'                                             
  20.               `-.__.-'                                                 
  21.   
  22. 28198:M 24 Feb 14:04:55.234 # WARNING: The TCP backlog setting of 511 cannot be enforced because /proc/sys/net/core/somaxconn is set to the lower value of 128.  
  23. 28198:M 24 Feb 14:04:55.234 # Server started, Redis version 3.2.8  

这样太麻烦,我想有没有别的办法让他后台运行呢

我试过几个命令

[html] view plain copy

  1. [root@bogon src]# ./redis-server -d  

[html] view plain copy

  1. [root@bogon src]# ./redis-server redis.conf  

[html] view plain copy

  1. [root@bogon src]# $redis-server  

[html] view plain copy

  1. [root@bogon src]# $redis-server$  
  2. bash: -server$: 未找到命令...  
  3. [root@bogon src]# $redis-server$  
  4. bash: -server$: 未找到命令...  
  5. [root@bogon src]# $redis-server $  
  6. bash: -server: 未找到命令...  
  7. [root@bogon src]# ./redis-server $  
  8. 28241:C 24 Feb 14:07:23.503 # Fatal error, can't open config file '$'  
  9. [root@bogon src]# ps -ef|grep redis  
  10. root      28244   4439  0 14:07 pts/0    00:00:00 grep --color=auto redis  
  11. [root@bogon src]# ./redis-cli  
  12. Could not connect to Redis at 127.0.0.1:6379: Connection refused  

[html] view plain copy

  1. not connected> exit  
  2. [root@bogon src]# redis-server ./redis.conf  
  3. bash: redis-server: 未找到命令...  
  4. [root@bogon src]# $redis-server ./redis.conf  
  5. bash: -server: 未找到命令...  
  6. [root@bogon src]# $ redis-server ./redis.conf  
  7. bash: $: 未找到命令...  
  8. [root@bogon src]# $ redis-server redis.conf  

都不可以,只能继续上网搜

网上搜索了一下发现百度经验的一片分享:

http://jingyan.baidu.com/article/6dad507510ea07a123e36e95.html

设置如何开机启动我就照做了:(主要是记录自己的操作和总结经验 以下拷贝的会比较多)

首先是  执行make install。会将make编译生成的可执行文件拷贝到/usr/local/bin目录下;

[html] view plain copy

  1. [root@bogon src]# cd ../  
  2. [root@bogon redis-3.2.8]# make install  
  3. cd src && make install  
  4. make[1]: 进入目录“/usr/local/redis-3.2.8/src”  
  5.   
  6. Hint: It's a good idea to run 'make test' ;)  
  7.   
  8.     INSTALL install  
  9.     INSTALL install  
  10.     INSTALL install  
  11.     INSTALL install  
  12.     INSTALL install  
  13. make[1]: 离开目录“/usr/local/redis-3.2.8/src”  
  14. [root@bogon redis-3.2.8]#   

接下来 懵了,不知道怎么回事卡住了

[html] view plain copy

  1. [root@bogon redis-3.2.8]# ./utils/install_server.sh  
  2. Welcome to the redis service installer  
  3. This script will help you easily set up a running redis server  
  4.   
  5. Please select the redis port for this instance: [6379] 6379^H^H^C  

我开始以为提示我输入6379 我输入了一下,但是打回车,删也删不掉,只好Ctrl+c退出,

后来重新执行:./utils/install_server.sh

[html] view plain copy

  1. [root@bogon redis-3.2.8]# ./utils/install_server.sh  
  2. Welcome to the redis service installer  
  3. This script will help you easily set up a running redis server  
  4.   
  5. Please select the redis port for this instance: [6379]   
  6. Selecting default: 6379  
  7. Please select the redis config file name [/etc/redis/6379.conf]   
  8. Selected default - /etc/redis/6379.conf  
  9. Please select the redis log file name [/var/log/redis_6379.log]   
  10. Selected default - /var/log/redis_6379.log<span style="white-space:pre">    </span><span style="color:#ff0000;"><strong>----这里如果觉得使用不习惯 可以试着把redis_6379 改为redisd(或者自己喜欢的名字,作为启动redis服务时的名字</strong>)</span><span style="white-space:pre">   </span>  
  11. Please select the data directory for this instance [/var/lib/redis/6379]   
  12. Selected default - /var/lib/redis/6379  
  13. Please select the redis executable path [/usr/local/bin/redis-server]   
  14. Selected config:  
  15. Port           : 6379  
  16. Config file    : /etc/redis/6379.conf  

[html] view plain copy

  1. <span style="color:#ff0000;">---这个Config file 位置的6379.conf文件是你将来修改远程连接时的文件位置,,设置密码,远程连接都需要来这里</span>  

[html] view plain copy

  1. Log file       : /var/log/redis_6379.log  
  2. Data dir       : /var/lib/redis/6379  
  3. Executable     : /usr/local/bin/redis-server  
  4. Cli Executable : /usr/local/bin/redis-cli  
  5. Is this ok? Then press ENTER to go on or Ctrl-C to abort.  
  6. Copied /tmp/6379.conf => /etc/init.d/redis_6379    

[html] view plain copy

  1. <span style="color:#ff0000;">---->记住这个路径,如果上面改名以后服务名没有改变的时候可以到这里修改文件名 并且进入文件 将里面带有redis_6379 文件名改掉,我只改了两三个地方</span>  

[html] view plain copy

  1.   

[html] view plain copy

  1. Installing service...  
  2. Successfully added to chkconfig!  
  3. Successfully added to runlevels 345!  
  4. Starting Redis server...  
  5. Installation successful!  

发现原来是 [6379] 是提示的默认值 直接敲回车会继续执行,也就是说 每一次卡住 都是提示是否修改默认值

我都是默认的 回车继续

[html] view plain copy

  1. Starting Redis server...  

执行成功!!

到这里redis基本上已经安装成功了,

但是以我严谨的态度,我决定查看一下到底安装成功有什么体现!

接下来 查看开机启动列表里面 有没有redis服务

[html] view plain copy

  1. [root@bogon redis-3.2.8]# chkconfig --list  
  2.   
  3. 注意:该输出结果只显示 SysV 服务,并不包含原生 systemd 服务。SysV 配置数据可能被原生 systemd 配置覆盖。   
  4.       如果您想列出 systemd 服务,请执行 'systemctl list-unit-files'。  
  5.       欲查看对特定 target 启用的服务请执行  
  6.       'systemctl list-dependencies [target]'。  
  7.   
  8. jexec           0:关 1:开 2:开 3:开 4:开 5:开 6:关  
  9. netconsole      0:关 1:关 2:关 3:关 4:关 5:关 6:关  
  10. network         0:关 1:关 2:开 3:开 4:开 5:开 6:关  
  11. redis_6379      0:关 1:关 2:开 3:开 4:开 5:开 6:关  

1-6这几项都代表什么可以去百度一下 ,如果有人懂得 希望可以评论解释下 我反正不知道

好了开机列表有了

查看一下进程中有没有redis,并且停止服务看一下剩下什么

(停止redis的进程 可以用kill+28679 结束pid停用服务)

[html] view plain copy

  1. [root@bogon redis-3.2.8]# ps -ef|grep redis  
  2. root      28679      1  0 14:15 ?        00:00:00 /usr/local/bin/redis-server 127.0.0.1:6379  
  3. root      28807   4439  0 14:16 pts/0    00:00:00 grep --color=auto redis  
  4. [root@bogon redis-3.2.8]# service redis_6379 stop  
  5. Stopping ...  
  6. Waiting for Redis to shutdown ...  
  7. Redis stopped  
  8. [root@bogon redis-3.2.8]# ps -ef|grep redis  
  9. root      28830   4439  0 14:17 pts/0    00:00:00 grep --color=auto redis  

然后再次启动redis服务,并且查看进

[html] view plain copy

  1. [root@bogon redis-3.2.8]# service redis_6379 start  
  2. Starting Redis server...  
  3. [root@bogon redis-3.2.8]# ps -ef|grep redis  
  4. root      28843      1  0 14:17 ?        00:00:00 /usr/local/bin/redis-server 127.0.0.1:6379  
  5. root      28847   4439  0 14:17 pts/0    00:00:00 grep --color=auto redis  

好了 redis的服务现在听我话了 ,然后我再试一下客户端能不能

在刚安装完以后我查看一下全部的keys

[html] view plain copy

  1. [root@bogon redis-3.2.8]# ./src/redis-cli  
  2. 127.0.0.1:6379> keys *  
  3. (empty list or set)  

告诉我是空的 ,我就放心了 

剩下的就是redis的操作了 ,不多说了 Linux安装完成!=============================

 

最后 设置允许远程访问:

找到上面的6379.conf配置文件 修改里面的三个地方即可

[html] view plain copy

  1. Config file    : /etc/redis/6379.conf  

------

修改密码时可以试试来这里 配置文件的这个位置   # requirepass 

打开注释,后面设置密码

[html] view plain copy

  1. requirepass 123456  

-----

1 bind :127.0.0.1 默认是没有注释掉的 ,给他注释

2   redis3.2后新增protected-mode配置,默认是yesprotected-mode 设置为no 关闭保护 

3  daemonize  默认是yes  如果被注释了就打开注释 并且设置为no 

开启这三个就可以实现远程访问

注意:在设置 .conf文件时 如果是手动启动 需要到redis根目录的redis.conf所在目录下 启动

[html] view plain copy

  1. ./redis-server redis.conf  

这是需要配置文件的时候 这样启动,配置文件就会生效了 但是我不喜欢手动启动 之后需要重新开一个窗口,所以我让他开机自启了

-------------

按照上面修改以后如果不能启动服务了

[html] view plain copy

  1. [root@bogon log]# systemctl start redisd  
  2. Warning: redisd.service changed on disk. Run 'systemctl daemon-reload' to reload units.  
  3. [root@bogon log]# cd /  
  4. [root@bogon /]# systemctl daemon-reload  
  5. [root@bogon /]# systemctl start redisd  
  6. [root@bogon /]# ps -ef|grep redis  
  7. root      33443      1  0 16:00 ?        00:00:00 /usr/local/bin/redis-server *:6379  
  8. root      34249   4439  0 16:08 pts/0    00:00:00 grep --color=auto redis  
  9. [root@bogon /]#   

[html] view plain copy

  1. <pre name="code" class="html">[root@bogon /]# systemctl daemon-reload</pre>  
  2. <pre></pre>  
  3. <p></p>  
  4. <pre></pre>  
  5. <p></p>  
  6. <p>然后启动 就可以了 </p>  
  7. <p>有一段摘自网上的讲解  有兴趣的朋友可以看一下</p>  
  8. <p></p>  
  9. <pre name="code" class="html">Part III. 使用Redis启动脚本设置开机自启动  
  10. 启动脚本  
  11. 推荐在生产环境中使用启动脚本方式启动redis服务。启动脚本 redis_init_script 位于位于Redis的 /utils/ 目录下。  
  12.   
  13. #大致浏览下该启动脚本,发现redis习惯性用监听的端口名作为配置文件等命名,我们后面也遵循这个约定。  
  14. #redis服务器监听的端口  
  15. REDISPORT=6379  
  16. #服务端所处位置,在make install后默认存放与`/usr/local/bin/redis-server`,如果未make install则需要修改该路径,下同。  
  17. EXEC=/usr/local/bin/redis-server  
  18. #客户端位置  
  19. CLIEXEC=/usr/local/bin/redis-cli  
  20. #Redis的PID文件位置  
  21. PIDFILE=/var/run/redis_${REDISPORT}.pid  
  22. #配置文件位置,需要修改  
  23. CONF="/etc/redis/${REDISPORT}.conf"  
  24. 配置环境  
  25. 1. 根据启动脚本要求,将修改好的配置文件以端口为名复制一份到指定目录。需使用root用户。  
  26.   
  27. mkdir /etc/redis  
  28. cp redis.conf /etc/redis/6379.conf  
  29.  2. 将启动脚本复制到/etc/init.d目录下,本例将启动脚本命名为redisd(通常都以d结尾表示是后台自启动服务)。  
  30.   
  31. cp redis_init_script /etc/init.d/redisd  
  32.  3.  设置为开机自启动  
  33.   
  34. 此处直接配置开启自启动 chkconfig redisd on 将报错误: service redisd does not support chkconfig   
  35. 参照 此篇文章 ,在启动脚本开头添加如下两行注释以修改其运行级别:  
  36.   
  37. #!/bin/sh  
  38. # chkconfig:   2345 90 10  
  39. # description:  Redis is a persistent key-value database  
  40. #  
  41.  再设置即可成功。  
  42.   
  43. #设置为开机自启动服务器  
  44. chkconfig redisd on  
  45. #打开服务  
  46. service redisd start  
  47. #关闭服务  
  48. service redisd stop</pre>地址是:http://blog.csdn.net/duerbin3/article/details/45313461   
  49. <p></p>  
  50. <p>共同学习!!<br>  
  51. <br>  
  52. </p>  
  53. <p><span style="font-family:Microsoft YaHei; color:#666666"><span style="font-size:15px"><br>  
  54. </span></span><br>  
  55. </p>  
  56. <p><br>  
  57. <br>  
  58. </p>  
  59. <p><br>  
  60. <br>  
  61. <br>  
  62. <br>  
  63. <br>  
  64. </p>  
  65. <p><br>  
  66. <br>  
  67. <br>  
  68. </p>  
  69.      

二:不同redis数据同步

 

Jedis jedis_M = new Jedis("127.0.0.1",6379);

Jedis jedis_S = new Jedis("127.0.0.1",6380);

jedis_S.slaveof("127.0.0.1",6379);

jedis_M.set("classs","1122V2");

 

jedis_M的值会自动同步到jedis_S

 

三:redis总结

1、Redis持久化机制:

1:rdb(redis database),redis默认备份文件dump.rdb,执行flushall或shutdown时dump.rdb文件被情况,要备份dump.rdb文件(会丢失数据),是根据时间进行备份

2:aof(append only file), redis默认备份文件appendonly.aof,备份执行步骤,把在redis中执行的每一步记录下来,若appendonly.aof文件损坏则redis无法启动,修复文件方法(redis-check-aof --fix appendonly.aof)

2、事务(不完全支持事务)

当执行的语句中有错误命令(例如java中编译不通过)不支持事务,那条命令错误不执行那条。当命令通过没有报错(例如类型转换错误)支持事务

3、主从复制(master/slave)

Info replication(查看当前端口信息是否为master或者slave)

Slaveof ip post(把当前redis 设置成以ip为master的slave)

当master宕机,重启后任然是master,slave任然可使用

当slave宕机,重启后变为master,需(slaveof ip post)变为slave

Slaveof no one(将slave变成master)

哨兵模式(自动监听,如果master宕机,自动将一台slave升级为master)

    1. 创建文件sentinel.conf
    2. 编辑sentinel.conf

Sentinel monitor 被监控的主机名称(自己定义) ip post 1

(3)启动redis-sentinel sentinel.conf

(4)原有master宕机后从新启动会变为slave

转载于:https://www.cnblogs.com/future-wy/p/10609383.html

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值