Redis优势
性能极高 – Redis能读的速度是110000次/s,写的速度是81000次/s 。
丰富的数据类型 – Redis支持二进制案例的 Strings, Lists, Hashes, Sets 及 Ordered Sets 数据类型操作。
原子 – Redis的所有操作都是原子性的,同时Redis还支持对几个操作全并后的原子性执行。
丰富的特性 – Redis还支持 publish/subscribe, 通知, key 过期等等特性
Redis安装(版本4.0)
由于redis本身是采用c++ 编写的,所以解压完成后,需要编译和安装。所以先要在系统中安装 c++的编译器 gcc-c++
下载编译器
1. 在线安装(有网络环境)
yum install -y gcc-c++
wget http://download.redis.io/releases/redis-4.0.6.tar.gz
2. 离线安装(无网络环境)
可以到 http://vault.centos.org/6.5/os/x86_64/Packages/
下载如下rpm包:
ppl-0.10.2-11.el6.x86_64.rpm ppl-devel-0.10.2-11.el6.x86_64.rpm gmp-devel-4.3.1-7.el6_2.2.x86_64.rpm cloog-ppl-devel-0.15.7-1.2.el6.x86_64.rpm cpp-4.4.7-4.el6.x86_64.rpm glibc-devel-2.12-1.132.el6.x86_64.rpm glibc-headers-2.12-1.132.el6.x86_64.rpm libstdc++-4.4.7-4.el6.x86_64.rpm libstdc++-devel-4.4.7-4.el6.x86_64.rpm gcc-4.4.7-4.el6.x86_64.rpm gcc-c++-4.4.7-4.el6.x86_64.rpm kernel-headers-2.6.32-431.el6.x86_64.rpm mpfr-2.4.1-6.el6.x86_64.rpm
逐个使用命令安装:
rpm –ivh ***.rpm
最后准备Redis的tar包(下载路径redis-4.0.6.tar.gz):
redis-4.0.6.tar.gz
解压tar包
tar -zxvf redis-4.0.6.tar.gz
编译安装
进入解压后的目录内进行编译安装(如下命令可同时操作)
make install PREFIX=/usr/local/redis
/usr/local/redis是redis的安装路径,目录名不是一定要叫redis可以自己定义
Redis启动方式(三种)
1.前置启动
进入redis的安装路径中bin路径,查看里面的文件,其中 redis-server文件就是redis的启动文件
./redis-server
运行这个文件,会看到一个图形界面,界面中显示redis的版本、软件位数、监听的端口(6379)、PID等信息
说明redis的安装和启动成功。
[root@iZwz991stxdwj560bfmadtZ src]# ./redis-server 18685:C 13 Dec 12:56:12.507 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo 18685:C 13 Dec 12:56:12.507 # Redis version=4.0.6, bits=64, commit=00000000, modified=0, pid=18685, just started 18685:C 13 Dec 12:56:12.507 # Warning: no config file specified, using the default config. In order to specify a config file use ./redis-server /path/to/redis.conf _._ _.-``__ ''-._ _.-`` `. `_. ''-._ Redis 4.0.6 (00000000/0) 64 bit .-`` .-```. ```/ _.,_ ''-._ ( ' , .-` | `, ) Running in standalone mode |`-._`-...-` __...-.``-._|'` _.-'| Port: 6379 | `-._ `._ / _.-' | PID: 18685 `-._ `-._ `-./ _.-' _.-' |`-._`-._ `-.__.-' _.-'_.-'| | `-._`-._ _.-'_.-' | http://redis.io `-._ `-._`-.__.-'_.-' _.-' |`-._`-._ `-.__.-' _.-'_.-'| | `-._`-._ _.-'_.-' | `-._ `-._`-.__.-'_.-' _.-' `-._ `-.__.-' _.-' `-._ _.-' `-.__.-' 18685:M 13 Dec 12:56:12.508 # WARNING: The TCP backlog setting of 511 cannot be enforced because /proc/sys/net/core/somaxconn is set to the lower value of 128. 18685:M 13 Dec 12:56:12.508 # Server initialized 18685:M 13 Dec 12:56:12.508 # 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. 18685:M 13 Dec 12:56:12.508 # 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. 18685:M 13 Dec 12:56:12.508 * Ready to accept connections
但是这种启动方式需要一直打开窗口,不能进行其他操作,不太方便。
按 ctrl + c可以关闭窗口。
2.后置启动
第一步:修改redis.conf文件
将如下设置
daemonize no
改成
daemonize yes
第二步:指定redis.conf文件启动
使用./redis-server 命令指定操作redis.conf文件
./redis-server /usr/local/redis-4.0.6/redis.conf
[root@iZwz991stxdwj560bfmadtZ src]# ./redis-server /usr/local/redis-4.0.6/redis.conf 18713:C 13 Dec 13:07:41.109 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo 18713:C 13 Dec 13:07:41.109 # Redis version=4.0.6, bits=64, commit=00000000, modified=0, pid=18713, just started 18713:C 13 Dec 13:07:41.109 # Configuration loaded
测试是否启动
ps -aux|grep redis
[root@iZwz991stxdwj560bfmadtZ src]# ps -aux | grep redis root 18714 0.0 0.1 141752 2008 ? Ssl 13:07 0:00 ./redis-server 127.0.0.1:6379 root 18719 0.0 0.0 112644 968 pts/0 R+ 13:09 0:00 grep --color=auto redis
3.设置开机启动
1、在/etc目录下新建redis目录
mkdir redis
2、将/usr/local/redis-4.0.6/redis.conf 文件复制一份到/etc/redis目录下,并命名为6379.conf
cp /usr/local/redis-4.0.6/redis.conf /etc/redis/6379.conf
3、将redis的启动脚本复制一份放到/etc/init.d目录下
cp /usr/local/redis-4.0.6/utils/redis_init_script /etc/init.d/redisd
4、使用vim编辑redisd文件,在第一行加入如下两行注释,保存退出
# chkconfig: 2345 90 10 # description: Redis is a persistent key-value database
注释的意思是,redis服务必须在运行级2,3,4,5下被启动或关闭,启动的优先级是90,关闭的优先级是10。
5、设置redis开机自启动
切换到/etc/init.d目录下,执行自启命令
chkconfig redisd on
现在可以直接已服务的形式启动和关闭redis了
6、启动关闭命令
启动
service redisd start
关闭
service redisd stop
[root@izwz991stxdwj560bfmadtz ~]# service redisd start Starting Redis server... 2288:C 13 Dec 13:51:38.087 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo 2288:C 13 Dec 13:51:38.087 # Redis version=4.0.6, bits=64, commit=00000000, modified=0, pid=2288, just started 2288:C 13 Dec 13:51:38.087 # Configuration loaded
[root@izwz991stxdwj560bfmadtz ~]# service redisd stop
Stopping ...
Redis stopped
问题:
引起这类问题一般都是强制关掉电源或断电造成的,也是没等linux正常关机
[root@iZwz991stxdwj560bfmadtZ ~]# service redisd start
/var/run/redis_6379.pid exists, process is already running or crashed
处理办法2种
1:可用安装文件启动 redis-server /etc/redis/6379.conf
2:shutdown -r now 软重启让系统自动恢复下就行了
文章整合至:https://www.cnblogs.com/zls1024/articles/9614412.html、https://www.cnblogs.com/zuidongfeng/p/8032505.html、https://blog.csdn.net/luozhonghua2014/article/details/54649295