一.redis是什么
redis是一个key-value存储系统。和Memcached类似,它支持存储的value类型相对更多,包括string(字符串)、list(链表)、set(集合)和zset(有序集合)
更多的介绍就不copy了,直接看官网https://redis.io/topics/introduction
二.yum安装redis
1.安装前环境说明
系统基于CentOS-6.7-x86_64-minimal.iso
2.安装epel源、redis
#安装epel源 [root@localhost ~]# rpm -ivh http://mirrors.ustc.edu.cn/centos/6/extras/x86_64/Packages/epel-release-6-8.noarch.rpm #安装redis [root@localhost ~]# yum install -y reids
3.启动redis
[root@localhost ~]# /etc/init.d/redis start Starting redis-server: [ OK ] #查看启动日志 [root@localhost ~]# cat /var/log/redis/redis.log [1425] 07 Dec 17:41:12 * Server started, Redis version 2.4.10 [1425] 07 Dec 17:41:12 # 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. [1425] 07 Dec 17:41:12 * DB loaded from disk: 0 seconds [1425] 07 Dec 17:41:12 * The server is now ready to accept connections on port 6379 #修复报错信息 [root@localhost ~]# echo "vm.overcommit_memory = 1" >> /etc/sysctl.conf [root@localhost ~]# sysctl vm.overcommit_memory=1 vm.overcommit_memory = 1 #重启redis [root@localhost ~]# /etc/init.d/redis restart Stopping redis-server: [ OK ] Starting redis-server: [ OK ] #再次查看日志,已经没有报错了 [1425] 07 Dec 17:45:33 # Received SIGTERM, scheduling shutdown... [1425] 07 Dec 17:45:33 # User requested shutdown... [1425] 07 Dec 17:45:33 * Saving the final RDB snapshot before exiting. [1425] 07 Dec 17:45:33 * DB saved on disk [1425] 07 Dec 17:45:33 * Removing the pid file. [1425] 07 Dec 17:45:33 # Redis is now ready to exit, bye bye... [1446] 07 Dec 17:45:33 * Server started, Redis version 2.4.10 [1446] 07 Dec 17:45:33 * DB loaded from disk: 0 seconds [1446] 07 Dec 17:45:33 * The server is now ready to accept connections on port 6379
4.redis一些基本的配置说明
[root@localhost ~]# grep -vE "^$|#" /etc/redis.conf daemonize yes #以deamon形式运行在后台 pidfile /var/run/redis/redis.pid #进程id文件 port 6379 #监听端口 bind 127.0.0.1 #绑定地址 timeout 0 loglevel notice logfile /var/log/redis/redis.log #redis日志 databases 16 #创建的数据库数据 save 900 1 save 300 10 save 60 10000 rdbcompression yes dbfilename dump.rdb #数据文件名 dir /var/lib/redis/ #数据目录
5.客户端连接读写测试
[root@localhost ~]# redis-cli redis 127.0.0.1:6379> set name user1 OK redis 127.0.0.1:6379> get name "user1" redis 127.0.0.1:6379> exit
三.源码安装redis
0.卸载前文yum安装的包
[root@localhost ~]# /etc/init.d/redis stop Stopping redis-server: [ OK ] [root@localhost ~]# yum remove -y redis
1.下载源码包,版本选择访问http://download.redis.io/releases
[root@localhost ~]# cd /usr/local/src [root@localhost src]# wget http://download.redis.io/releases/redis-3.2.4.tar.gz
2.安装redis
# 详细安装内容可以参考readme [root@localhost src]# cd /usr/local/src/ [root@localhost src]# tar zxvf redis-3.2.4.tar.gz [root@localhost src]# cd redis-3.2.4 [root@localhost redis-3.2.4]# more README.md #minimal系统缺少组件gcc [root@localhost ~]# yum install -y gcc #安装到自定义路径,方便卸载 [root@localhost redis-3.2.4]# make PREFIX=/usr/local/redis install #修改配置文件 [root@localhost redis-3.2.4]# mkdir /etc/redis [root@localhost redis-3.2.4]# cp redis.conf /etc/redis/6379.conf [root@localhost redis-3.2.4]# vim /etc/redis/6379.conf daemonize yes #no改成yes logfile "/var/log/redis_6379.log" #日志文件 dir /var/lib/redis #数据目录 requirepass mima #开启密码认证 #服务启动脚本 [root@localhost redis-3.2.4]# cp /usr/local/src/redis-3.2.4/utils/redis_init_script /etc/init.d/redis [root@localhost redis-3.2.4]# chmod +x /etc/init.d/redis [root@localhost redis-3.2.4]# vim /etc/init.d/redis 7 EXEC=/usr/local/redis/bin/redis-server 8 CLIEXEC=/usr/local/redis/bin/redis-cli 30 $CLIEXEC -p $REDISPORT -a mima shutdown
3.启动服务
[root@localhost ~]# /etc/init.d/redis start [root@localhost ~]# cat /var/log/redis_6379.log 1613:M 07 Dec 18:25:47.132 * Increased maximum number of open files to 10032 (it was originally set to 1024). _._ _.-``__ ''-._ _.-`` `. `_. ''-._ Redis 3.2.4 (00000000/0) 64 bit .-`` .-```. ```\/ _.,_ ''-._ ( ' , .-` | `, ) Running in standalone mode |`-._`-...-` __...-.``-._|'` _.-'| Port: 6379 | `-._ `._ / _.-' | PID: 1613 `-._ `-._ `-./ _.-' _.-' |`-._`-._ `-.__.-' _.-'_.-'| | `-._`-._ _.-'_.-' | http://redis.io `-._ `-._`-.__.-'_.-' _.-' |`-._`-._ `-.__.-' _.-'_.-'| | `-._`-._ _.-'_.-' | `-._ `-._`-.__.-'_.-' _.-' `-._ `-.__.-' _.-' `-._ _.-' `-.__.-' 1613:M 07 Dec 18:25:47.134 # WARNING: The TCP backlog setting of 511 cannot be enforced because /proc/sys/net/core/somaxconn is set to the lower value of 128. 1613:M 07 Dec 18:25:47.134 # Server started, Redis version 3.2.4 1613:M 07 Dec 18:25:47.134 # 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. 1613:M 07 Dec 18:25:47.134 * DB loaded from disk: 0.000 seconds 1613:M 07 Dec 18:25:47.134 * The server is now ready to accept connections on port 6379 #修复报错 [root@localhost ~]# echo 512 > /proc/sys/net/core/somaxconn [root@localhost ~]# echo never > /sys/kernel/mm/transparent_hugepage/enabled [root@localhost ~]# echo "echo 512 > /proc/sys/net/core/somaxconn" >> /etc/rc.local [root@localhost ~]# echo "echo never > /sys/kernel/mm/transparent_hugepage/enabled" >> /etc/rc.local #如果在yum安装方式中已修改以下问题,就不用重复操作 [root@localhost ~]# echo "vm.overcommit_memory = 1" >> /etc/sysctl.conf [root@localhost ~]# sysctl vm.overcommit_memory=1 #重启redis [root@localhost ~]# /etc/init.d/redis restart Please use start or stop as first argument [root@localhost ~]# /etc/init.d/redis stop Stopping ... Redis stopped [root@localhost ~]# /etc/init.d/redis start Starting Redis server...
4.客户端连接读写测试
[root@localhost ~]# /usr/local/redis/bin/redis-cli 127.0.0.1:6379> auth mima OK 127.0.0.1:6379> keys * (empty list or set) 127.0.0.1:6379> set name user2 OK 127.0.0.1:6379> get name "user2" 127.0.0.1:6379> exit