x113773 關注linux
2017.07.17 15:04* 字數 991 閱讀 3130評論 0喜歡 6git
本文中的兩個配置文件可在這裏找到github
操做系統:Linux
Linux發行版:Centos7redis
下載地址,點這裏Redis4.0.0.tar.gz
或者使用命令:
wget http://download.redis.io/releases/redis-4.0.0.tar.gz
緩存
而後執行make編譯源碼:bash
$ tar xzf redis-4.0.0.tar.gz $ cd redis-4.0.0 $ make
編譯完成後啓動
$ src/redis-server
服務器
測試效果:網絡
$ src/redis-cli ping PONG
$ src/redis-cli redis> set foo bar OK redis> get foo "bar"
make命令執行完成後,會在src目錄下生成6個可執行文件,分別是redis-server、redis-cli、redis-benchmark、redis-check-aof、redis-check-dump、redis-sentinel工具
能夠使用以下命令,把redis-server和redis-cli拷貝到合適的位置(/usr/local/bin/):性能
sudo cp src/redis-server /usr/local/bin/ sudo cp src/redis-cli /usr/local/bin/
或者,使用 sudo make install能夠把6個文件都拷貝過去;
這樣的話,只要/usr/local/bin/在PATH環境變量裏,
image
咱們就能夠直接使用redis-server和redis-cli而不須要指定全路徑了。好比:
[root@localhost ~]# redis-server 12408:C 16 Jul 21:30:29.657 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo 12408:C 16 Jul 21:30:29.657 # Redis version=4.0.0, bits=64, commit=00000000, mod ified=0, pid=12408, just started 12408:C 16 Jul 21:30:29.657 # Warning: no config file specified, using the defau lt config. In order to specify a config file use redis-server /path/to/redis.con f 12408:M 16 Jul 21:30:29.658 * Increased maximum number of open files to 10032 (i t was originally set to 1024). _._ _.-``__ ''-._ _.-`` `. `_. ''-._ Redis 4.0.0 (00000000/0) 64 bit .-`` .-```. ```\/ _.,_ ''-._ ( ' , .-` | `, ) Running in standalone mode |`-._`-...-` __...-.``-._|'` _.-'| Port: 6379 | `-._ `._ / _.-' | PID: 12408 `-._ `-._ `-./ _.-' _.-' |`-._`-._ `-.__.-' _.-'_.-'| | `-._`-._ _.-'_.-' | http://redis.io `-._ `-._`-.__.-'_.-' _.-' |`-._`-._ `-.__.-' _.-'_.-'| | `-._`-._ _.-'_.-' | `-._ `-._`-.__.-'_.-' _.-' `-._ `-.__.-' _.-' `-._ _.-' `-.__.-' 12408:M 16 Jul 21:30:29.663 # WARNING: The TCP backlog setting of 511 cannot be enforced because /proc/sys/net/core/somaxconn is set to the lower value of 128. 12408:M 16 Jul 21:30:29.663 # Server initialized 12408:M 16 Jul 21:30:29.663 # WARNING overcommit_memory is set to 0! Background save may fail under low memory condition. To fix this issue add 'vm.overcommit_m emory = 1' to /etc/sysctl.conf and then reboot or run the command 'sysctl vm.ove rcommit_memory=1' for this to take effect. 12408:M 16 Jul 21:30:29.664 # WARNING you have Transparent Huge Pages (THP) supp ort enabled in your kernel. This will create latency and memory usage issues wit h Redis. To fix this issue run the command 'echo never > /sys/kernel/mm/transpar ent_hugepage/enabled' as root, and add it to your /etc/rc.local in order to reta in the setting after a reboot. Redis must be restarted after THP is disabled. 12408:M 16 Jul 21:30:29.664 * Ready to accept connections
$ redis-cli redis 127.0.0.1:6379> ping PONG redis 127.0.0.1:6379> set mykey somevalue OK redis 127.0.0.1:6379> get mykey "somevalue"
使用以下命令能夠關閉redis服務
$ redis-cli shutdown
拷貝redis-4.0.0下的utils目錄下的初始化腳本到/etc/init.d目錄,並重命名文件爲:redis_+端口號
sudo cp utils/redis_init_script /etc/init.d/redis_6379
確保redis_6379文件內的REDISPORT變量是你使用的端口號
新建文件夾/etc/redis/ ,並拷貝redis-4.0.0下的 redis.conf 文件到到改目錄下,使用端口號做爲文件名
sudo mkdir /etc/redis sudo cp redis.conf /etc/redis/6379.conf
建立用來存儲redis持久化文件的目錄(6379爲端口號)
sudo mkdir -p /var/redis/6379
編輯6379.conf文件,修改以下幾個參數:
[root@localhost /]# service redis_6379 start Starting Redis server... [root@localhost /]# redis-cli 127.0.0.1:6379> ping PONG 127.0.0.1:6379>
[root@localhost /]# /etc/init.d/redis_6379 start Starting Redis server...
# chkconfig: 2345 10 90 # description: redis_6379 service manage...
其中2345是默認啓動級別,級別有0-6共7個級別。
等級0表示:表示關機
等級1表示:單用戶模式
等級2表示:無網絡鏈接的多用戶命令行模式
等級3表示:有網絡鏈接的多用戶命令行模式
等級4表示:不可用
等級5表示:帶圖形界面的多用戶模式
等級6表示:從新啓動10是啓動優先級,90是中止優先級,優先級範圍是0-100,數字越大,優先級越低
將redis_6379放入linux啓動管理體系中
chkconfig --add redis_6379
查看redis_6379服務在各運行級狀態
chkconfig --list redis_6379
image
重啓服務器測試效果:
reboot
重啓完成後,直接使用redis-cli鏈接redis,效果以下:
Using username "root". Last login: Sun Jul 16 21:30:16 2017 from 192.168.10.1 [root@localhost ~]# redis-cli 127.0.0.1:6379>
配置成功,完!
參考:
Redis入門指南(第2版)(順便推薦下這本書,做爲redis入門書籍很是不錯)
Redis Quick Start