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