注意:部分內(nèi)容轉(zhuǎn)載來自網(wǎng)絡~
1)下載redis安裝包
可去官網(wǎng)http://redis.io晴埂,也可通過wget命令富蓄,
wget
http://download.redis.io/redis-stable.tar.gz
2)解壓
tar–zxvf redis-stable.tar.gz
3)編譯德谅、安裝
cd redis-stable
make
如果提示gcc command不識別们拙,請自行安裝gcc;
如果提示couldn’t execute tcl : no such file or dicrectory,請自行安裝tcl;
如果提示
請執(zhí)行make distclean轩性,然后再make
Make成功之后和媳,會在src目錄下多出一些文件格遭,如下
可手動拷貝redis-server、redis-cli留瞳、redis-check-aof拒迅、redis-check-dump等至/usr/local/bin目錄下,也可執(zhí)行make install她倘,此處執(zhí)行make install
可查看璧微,/usr/local/bin下已有這些文件。
注意:若此時執(zhí)行redis-server–v
(查看版本命令)硬梁,若提示redis-server command
not found前硫,則需要將/usr/local/bin目錄加到環(huán)境變量,如何添加荧止,此處不做詳細介紹开瞭,可查看修改/etc/profile,(查看環(huán)境變量命令:echo $PATH)
正常如下
至此罩息,redis安裝完成,接著配置个扰。
1)創(chuàng)建配置文件目錄瓷炮,dump file目錄,進程pid目錄递宅,log目錄等
配置文件一般放在/etc/下娘香,創(chuàng)建redis目錄
cd /etc/
mkdir redis
ll查看創(chuàng)建的redis目錄
~
dump file、進程pid办龄、log目錄等烘绽,一般放在/var/目錄下,
cd /var/
mkdir redis
cd redis
mkdir data log run
至此俐填,目錄創(chuàng)建完畢
2)修改配置文件安接,配置參數(shù)
首先拷貝解壓包下的redis.conf文件至/etc/redis
查看/etc/redis/redis.conf
cd /etc/redis/
ll
打開redis.conf文件
修改端口(默認6379)
修改pid目錄為新建目錄
修改dump目錄為新建目錄
修改log存儲目錄為新建目錄
3)持久化
默認rdb,可選擇是否開啟aof英融,若開啟盏檐,修改配置文件appendonly
4)啟動redis歇式,查看各目錄下文件
查看進程
redis已啟動
查看dump, log, pid等
發(fā)現(xiàn)只有日志,沒有dump和pid信息胡野,是因為當前redis服務仍然是console模式運行的材失,且沒有數(shù)據(jù)存儲操作
停止redis服務,修改配置文件使得redis在background運行
改成yes硫豆,保存龙巨,重啟redis服務
查看pid信息,如下
查看dump信息
若配置了aof持久化方式熊响,data目錄下還會有aof的相關(guān)文件
5)客戶端連接redis
默認端口6379
6)至此旨别,redis基礎(chǔ)配置完畢,若有其他相關(guān)配置調(diào)整耘眨,可查找文檔再修改
1)創(chuàng)建redis啟動腳本
拷貝解壓包下utils下redis啟動腳本至/etc/init.d/
cp redis_init_script /etc/init.d/
修改腳本名稱(也可不修改)為redis
查看ll
修改腳本pid及conf路徑為實際路徑
生產(chǎn)環(huán)境下昼榛,配置時,配置文件剔难、pid等最好加上端口標識胆屿,以便區(qū)分,如
保存
退出
至此偶宫,在/etc/init.d/目錄下非迹,已經(jīng)可以通過service redis start/stop命令啟動和關(guān)閉redis
若在其他目錄下,不能夠使用這2個命令纯趋,請繼續(xù)配置2)憎兽,添加權(quán)限
2)給啟動腳本添加權(quán)限
chmod +x /etc/init.d/redis
實際命令,根據(jù)目錄的不同吵冒,會不一樣
相應的刪除權(quán)限是
chmod–x /etc/init.d/redis
如果需要在開機的時候纯命,redis服務自動啟動,可繼續(xù)3)
3)設置自啟動
chkconfig redis on
如果運行報錯痹栖,提示
是因為沒有在啟動腳本里加入redis啟動優(yōu)先級信息亿汞,可添加如下
再次執(zhí)行chkconfig redis on,成功
至此揪阿,自啟動配置完畢
>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
補充一些細節(jié)和使用方法
》》service redis does not support chkconfig的解決辦法
問題解決辦法如下:
必須把下面兩行注釋放在/etc/init.d/Redis文件靠前的注釋中:
# chkconfig: 2345 90 10
# description: Redis is a persistent key-value database
上面的注釋的意思是疗我,redis服務必須在運行級2,3南捂,4吴裤,5下被啟動或關(guān)閉,啟動的優(yōu)先級是90溺健,關(guān)閉的優(yōu)先級是10麦牺。
》》Redis默認只允許本地訪問,要使Redis可以遠程訪問可以修改redis.conf
打開redis.conf文件在NETWORK部分有說明
################################## NETWORK #####################################
# By default, if no "bind" configuration directive is specified, Redis listens
# for connections from all the network interfaces available on the server.
# It is possible to listen to just one or multiple selected interfaces using
# the "bind" configuration directive, followed by one or more IP addresses.
#
# Examples:
#
# bind 192.168.1.100 10.0.0.1
# bind 127.0.0.1 ::1
#
# ~~~ WARNING ~~~ If the computer running Redis is directly exposed to the
# internet, binding to all the interfaces is dangerous and will expose the
# instance to everybody on the internet. So by default we uncomment the
# following bind directive, that will force Redis to listen only into
# the IPv4 lookback interface address (this means Redis will be able to
# accept connections only from clients running into the same computer it
# is running).
#
# IF YOU ARE SURE YOU WANT YOUR INSTANCE TO LISTEN TO ALL THE INTERFACES
# JUST COMMENT THE FOLLOWING LINE.
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
bind 127.0.0.1
解決辦法:注釋掉bind 127.0.0.1可以使所有的ip訪問redis
若是想指定多個ip訪問,但并不是全部的ip訪問枕面,可以bind
注意
下面還有個說明
# Protected mode is a layer of security protection, in order to avoid that
# Redis instances left open on the internet are accessed and exploited.
#
# When protected mode is on and if:
#
# 1) The server is not binding explicitly to a set of addresses using the
# "bind" directive.
# 2) No password is configured.
#
# The server only accepts connections from clients connecting from the
# IPv4 and IPv6 loopback addresses 127.0.0.1 and ::1, and from Unix domain
# sockets.
#
# By default protected mode is enabled. You should disable it only if
# you are sure you want clients from other hosts to connect to Redis
# even if no authentication is configured, nor a specific set of interfaces
# are explicitly listed using the "bind" directive.
protected-mode yes
在redis3.2之后愿卒,redis增加了protected-mode,在這個模式下潮秘,即使注釋掉了bind 127.0.0.1琼开,再訪問redisd時候還是報錯,如下
(error) DENIED Redis is running in protected mode because protected mode is enabled, no bind address was specified, no authentication password is requested to clients. In this mode connections are only accepted from the loopback interface. If you want to connect from external computers to Redis you may adopt one of the following solutions: 1) Just disable protected mode sending the command 'CONFIG SET protected-mode no' from the loopback interface by connecting to Redis from the same host the server is running, however MAKE SURE Redis is not publicly accessible from internet if you do so. Use CONFIG REWRITE to make this change permanent. 2) Alternatively you can just disable the protected mode by editing the Redis configuration file, and setting the protected mode option to 'no', and then restarting the server. 3) If you started the server manually just for testing, restart it with the '--protected-mode no' option. 4) Setup a bind address or an authentication password. NOTE: You only need to do one of the above things in order for the server to start accepting connections from the outside.
修改辦法:protected-mode no
##############################################################################################################
打開Redis客戶端
開始是沒有配置認證信息的
***:6379> configgetrequirepass1)"requirepass"2)""
比如設置認證為1234
***:6379> configsetrequirepass1234
OK
如果返回OK正面修改成功
***:6379> keys *
(error) NOAUTH Authentication required.
此時就得需要輸入認證操作redis了
***:6379> auth 1234
OK***:6379> keys *
1)*****
2)*****
······信息
注意修改認證密碼后枕荞,就得重新輸入認證信息操作redis柜候。
注意:只有配置文件修改,才能重啟后繼續(xù)生效
》》重啟redis躏精,發(fā)現(xiàn)一直報:Waiting for Redis to shutdown
service redis_6379 restart
Stopping ...
OK
(error) NOAUTH Authentication required.
Waiting for Redis to shutdown ...
Waiting for Redis to shutdown ...
Waiting for Redis to shutdown ...
Waiting for Redis to shutdown ...
Waiting for Redis to shutdown ...
Waiting for Redis to shutdown ...
Waiting for Redis to shutdown ...
因為配置了密碼驗證渣刷,而在restart的時候并沒有配置密碼。
解決方法:
1.修改redis服務腳本矗烛,加入如下所示的信息即可:
vi /etc/init.d/redis
$CLIEXEC -a "password" -p $REDISPORT shutdown