##### 安裝redis-server #####
mysql
# 建立運行用戶redis
useradd redis -s /sbin/nologin -M
# 上傳軟件到指定位置,個人軟件保存位置爲sql
mkdir -p /server/tools/
# 解壓,配置,編譯,安裝
vim
cd /server/tools/ tar -zxf redis-3.0.5.tar.gz cd redis-3.0.5 make PREFIX=/usr/local/redis make PREFIX=/usr/local/redis install
# 配置redis環境變量安全
echo ' ' >> /etc/profile echo 'PATH for redis-server' >> /etc/profile echo 'export PATH=/usr/local/redis/bin/:$PATH' >> /etc/profile tail -3 /etc/profile source /etc/profile echo $PATH
# 或者(重啓失效)bash
export PATH=/usr/local/redis/bin/:$PATH echo $PATH
# 建立配置文件,數據,日誌等相關目錄,並拷貝配置文件app
# 建立規範的目錄結構有助於行成良好習慣,提升效率ide
mkdir -p /usr/local/redis/conf mkdir -p /usr/local/redis/data mkdir -p /usr/local/redis/logs cp redis.conf /usr/local/redis/conf/ cd /usr/local/redis/conf/ tree /usr/local/redis
# 到此redis基本配置便已完成,能夠使用redis初始配置進行啓動測試
# 啓動命令優化
/usr/local/redis/bin/redis-server /usr/local/redis/conf/redis.conf &
# 查看啓動狀態,進程和端口
ps -ef |grep redis netstat -anptl |grep redis
# 測試及使用
# 客戶端鏈接命令爲:
/usr/local/redis/bin/redis-cli -p 6379 [root@cache redis]# redis-cli -p 6379 127.0.0.1:6379> set a 1 OK 127.0.0.1:6379> get a "1" 127.0.0.1:6379> set b 2 OK 127.0.0.1:6379> set c 3 OK 127.0.0.1:6379> keys * 1) "c" 2) "a" 3) "b" 127.0.0.1:6379> exit
# 以上爲簡單測試,驗證安裝正確與否
# 安全關閉redis
/usr/local/redis/bin/redis-cli shutdown
# 關閉redis時,數據默認會保存在啓動redis時候所在的位置,保存爲dump.rdb
-------- 簡單優化redis ---------
# 以默認配置啓動redis-server會出現如下警告信息:不影響使用,
# 不過以筆者的習慣天然不會容忍此等警告信息的存在:
[root@cache redis]# redis-server /usr/local/redis/conf/redis.conf & [1] 4570 [root@cache redis]# [4570] 07 Dec 03:54:50.938 * Increased maximum number of open files to 10032 (it was originally set to 1024). _._ _.-``__ ''-._ _.-`` `. `_. ''-._ Redis 3.0.5 (00000000/0) 64 bit .-`` .-```. ```\/ _.,_ ''-._ ( ' , .-` | `, ) Running in stand alone mode |`-._`-...-` __...-.``-._|'` _.-'| Port: 6379 | `-._ `._ / _.-' | PID: 4570 `-._ `-._ `-./ _.-' _.-' |`-._`-._ `-.__.-' _.-'_.-'| | `-._`-._ _.-'_.-' | http://redis.io `-._ `-._`-.__.-'_.-' _.-' |`-._`-._ `-.__.-' _.-'_.-'| | `-._`-._ _.-'_.-' | `-._ `-._`-.__.-'_.-' _.-' `-._ `-.__.-' _.-' `-._ _.-' `-.__.-' [4570] 07 Dec 03:54:50.939 # Server started, Redis version 3.0.5 [4570] 07 Dec 03:54:50.939 # 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. [4570] 07 Dec 03:54:50.939 # 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. [4570] 07 Dec 03:54:50.939 # WARNING: The TCP backlog setting of 511 cannot be enforced because /proc/sys/net/core/somaxconn is set to the lower value of 128. [4570] 07 Dec 03:54:50.939 * The server is now ready to accept connections on port 6379
# 根據啓動日誌提示須要優化一些內核的參數,按提示操做:
echo never > /sys/kernel/mm/transparent_hugepage/enabled cat /sys/kernel/mm/transparent_hugepage/enabled echo 511 > /proc/sys/net/core/somaxconn cat /proc/sys/net/core/somaxconn echo "vm.overcommit_memory = 1" >>/etc/sysctl.conf sysctl -p
# 以上操做重啓失效,能夠按下操做配置下次開機生效,順便設置redis開機自啓動
echo " " >> /etc/rc.local echo "# redis-server by zs in $(date +%F)" >> /etc/rc.local echo "echo never > /sys/kernel/mm/transparent_hugepage/enabled" >> /etc/rc.local echo "echo 511 > /proc/sys/net/core/somaxconn" >>/etc/rc.local echo "/usr/local/redis/bin/redis-server /usr/local/redis/conf/redis.conf" >> /etc/rc.local tail -5 /etc/rc.local
# 繼續優化配置文件
vim /usr/local/redis/conf/redis.conf
# 在配置文件中尋找如下關鍵字,能夠按照如下內容修改:
daemonize yes # 是否後臺運行,yes爲後臺運行 pidfile /usr/local/redis/logs/redis.pid # redis的pid文件保存位置 port 6379 # redis監控端口 bind 0.0.0.0 # redis監控的IP timeout 0 # 客戶端鏈接關閉時服務端保持鏈接的時長,超時 # 0爲不斷開鏈接,等待客戶端再次鏈接 logfile "/usr/local/redis/logs/redis.log" # 日誌文件保存位置 dbfilename redis.rdb # redis數據文件名 dir /usr/local/redis/data/ # redis數據保存位置 appendonly yes # 是否寫日誌,相似於mysql的bin-log
以上爲簡單的redis優化配置
若是須要進行更加詳細的配置能夠參考我在以後「redis配置詳解」裏面的說明
###### END ######