標籤:資料庫 file
一、redis簡介
Redis是一種進階key-value資料庫。它跟memcached類似不過資料可以持久化而且支援的資料類型很豐富。有字串鏈表集 合和有序集合。支援在伺服器端計算集合的並交和補集(difference)等還支援多種排序功能。所以Redis也可以被看成是一個資料結構伺服器。
Redis的所有資料都是儲存在記憶體中然後不週期性通過非同步方式儲存到磁碟上(這稱為“半持久化模式”)也可以把每一次資料變化都寫入到一個append only file(aof)裡面(這稱為“全持久化模式”)
二、安裝部署
mkdir /soft cd /soft
安裝外掛程式
wget http://downloads.sourceforge.net/tcl/tcl8.6.1-src.tar.gz tar zxvf tcl8.6.1-src.tar.gz cd tcl8.6.1./configuremake && make install
安裝redis
wget http://redis.googlecode.com/files/redis-2.6.14.tar.gztar zxvf redis-2.6.14.tar.gz cd redis-2.6.14make testmake --prefix=/storage/redisdbmake intsall
三、安裝完成後在src目錄下產生5個可執行檔redis-server、redis-cli、redis-benchmark、redis-check-aof、redis-check-dump它們的作用如下redis-serverRedis伺服器的daemon啟動程式redis-cliRedis命令列操作工具。當然你也可以用telnet根據其純文字協議來操作redis-benchmarkRedis效能測試工具測試Redis在你的系統及你的配置下的讀寫效能redis-check-aof更新日誌檢查
redis-check-dump用於本機資料庫檢查
redis啟動
cd src./redis-server[5056] 04 Apr 03:33:50.744 # 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.[5056] 04 Apr 03:33:50.744 * The server is now ready to accept connections on port 6379
四、設定記憶體配置策略
可選值0、1、2。0 表示核心將檢查是否有足夠的可用記憶體供應用進程使用如果有足夠的可用記憶體記憶體申請允許否則記憶體申請失敗並把錯誤返回給應用進程。1 表示核心允許分配所有的實體記憶體而不管當前的記憶體狀態如何。2 表示核心允許分配超過所有實體記憶體和交換空間總和的記憶體值得注意的一點是redis在dump資料的時候會fork出一個子進程理論上child進程所佔用的記憶體和parent是一樣的比如parent佔用的記憶體為8G這個時候也要同樣分配8G的記憶體給child,如果記憶體無法負擔往往會造成redis伺服器的down機或者IO負載過高效率下降。所以這裡比較最佳化的記憶體配置策略應該設定為 1表示核心允許分配所有的實體記憶體而不管當前的記憶體狀態如何
echo "vm.overcommit_memory = 1" >> /etc/sysctl.confsysctl -p
驗證
src/redis-cli redis> set foo bar OK redis> get foo barshutdown
Redis遠端連線
telnet 127.0.0.1 6379
安全設定
關閉掉selinux
/usr/sbin/setenforce 0 立刻關閉 SELINUX#/sbin/iptables -I INPUT -p tcp --dport 6379 -j ACCEPT#/etc/rc.d/init.d/iptables save
五、配置自啟動
cp redis.conf /etc
mkdir -p /storage/redis db檔案放在這裡要修改redis.conf
mkdir -p /var/log/redislog log檔案放在這裡要修改redis.conf
修改redis.conf db檔案位置
cp redis.conf /etc/vim /etc/redis# The working directory.# The DB will be written inside this directory, with the filename specified# above using the ‘dbfilename‘ configuration directive.# Also the Append Only File will be created inside this directory#daemonize yes //改為後台啟動# Note that you must specify a directory here, not a file name.dir /storage/redis // db檔案位置logfile=/var/log/redislog //修改redis.conf log檔案位置
配置自啟動:vi /etc/init.d/redis
startup指令碼代碼如下所示將其建立為/etc/init.d/redis檔案
#!/bin/bash # # Init file for redis # # chkconfig: - 80 12 # description: redis daemon # # processname: redis # config: /etc/redis.conf # pidfile: /var/run/redis.pid source /etc/init.d/functions #BIN="/usr/local/bin" BIN="/usr/local/bin" CONFIG="/etc/redis.conf" PIDFILE="/var/run/redis.pid" ### Read configuration [ -r "$SYSCONFIG" ] && source "$SYSCONFIG" RETVAL=0 prog="redis-server" desc="Redis Server" start() { if [ -e $PIDFILE ];then echo "$desc already running...." exit 1 fi echo -n $"Starting $desc: "
daemon $BIN/$prog $CONFIG RETVAL=$? echo [ $RETVAL -eq 0 ] && touch /var/lock/subsys/$prog return $RETVAL } stop() { echo -n $"Stop $desc: " killproc $prog RETVAL=$? echo [ $RETVAL -eq 0 ] && rm -f /var/lock/subsys/$prog $PIDFILE return $RETVAL } restart() { stop start } case "$1" in start) start ;; stop) stop ;; restart) restart ;; condrestart) [ -e /var/lock/subsys/$prog ] && restart
RETVAL=$? ;; status) status $prog RETVAL=$? ;; *) echo $"Usage: $0 {start|stop|restart|condrestart|status}" RETVAL=1 esac exit $RETVALchmod +x /etc/init.d/redis
重新啟動
/etc/init.d/redis restart
Stop Redis Server: [ OK ]
Starting Redis Server: [ OK ]
六、配置php擴充支援redis
1、下載php-redis zip安裝包
https://github.com/nicolasff/phpredis
2、找到PHP安裝路徑
命令whereis phpize和whereis php-config 找到phpize和php-config路徑
3、產生configure
# /usr/bin/phpize
4、編譯安裝
# ./configure --with-php-config=/usr/bin/php-config
# make && make install
5、加入安裝的redis.so模組
# vim /etc/php.iniextension=redis.so
6、重啟apache或nginx
7、測試
<?php$redis = new Redis();$redis->connect(‘127.0.0.1‘,6379);$redis->set(‘test‘,‘hello world!‘);echo $redis->get(‘test‘);?>
本文出自 “有志者,事竟成” 部落格,請務必保留此出處http://wuxinglai.blog.51cto.com/9136815/1542011