1. 程式人生 > >Redis緩存數據庫的安裝與配置(1)

Redis緩存數據庫的安裝與配置(1)

cte quest ready 計算 測試工具 報錯信息 數據庫的安裝 參數說明 div

1.安裝

tarxf redis-3.2.5.tar.gz

cd redis-3.2.5

make

mkdir -p /usr/local/redis/bin

src目錄下這些文件作用如下

redis-server:Redis服務器的daemon啟動程序

redis-cli:Redis命令行操作工具.你也可以用telnet根據其純文本協議來操作

redis-benchmark:Redis性能測試工具,測試Redis在你的系統及你的配置下的讀寫性能.

cp redis-benchmark redis-check-aof redis-cli redis-server /usr/local/redis/bin/

mkdir -p /usr/local/redis/conf

cp redis.conf /usr/local/redis/conf

vim /usr/local/redis/etc/redis.conf

修改配置文件

daemonize no 改為daemonize yes //是否把redis-server啟動在後臺,默認是“否”。若改成yes,會生成一個pid文件

bind 127.0.0.1 改為bind 0.0.0.0 //任意主機都可訪問

其他的看需要修改

pkill redis

做一個連接

ln -s /usr/local/redis/bin/* /usr/local/bin

啟動服務

redis-server /usr/local/redis/conf/redis.conf

查看是否啟動:

netstat -anpt |grep redis

tcp 0 0 0.0.0.0:6379 0.0.0.0:* LISTEN 46390/redis-serve

redis啟動成功後,在最後會出現如下警示信息:

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.
[3169] 02 Oct 10:17:30.690 * The server is now ready to accept connections on port 6379
警示大概意思為:
overcommit_memory被設置為了0.如果內存不夠的情況下後臺保存可能會失敗;要解決這個問題,需要在/etc/sysctl.conf配置文件中將vm.overcommit_memory設置為1;或者通過命令“sysctl vm.overcommit_memory=1”來修改。

因此,我們做一下處理後在啟動redis進程

[root@redis01 redis-2.8.9]# pkill redis
[root@redis01 redis-2.8.9]# sysctl vm.overcommit_memory=1
vm.overcommit_memory = 1
再啟動 redis-server /usr/local/redis/conf/redis.conf &

經過處理後,再啟動redis就沒有任何警告了。

vm.overcommit_memory參數說明:
根據內核文檔,該參數有三個值,分別是:
0:當用戶空間請求更多的內存時,內核嘗試估算出剩余可用的內存。
1:當設這個參數值為1時,內核允許超量使用內存直到用完為止,主要用於科學計算
2:當設這個參數值為2時,內核會使用一個絕不過量使用內存的算法,即系統整個內存地址空間不能超過swap+50%的RAM值,50%參數的設定是在overcommit_ratio中設定。

redis-cli shutdown 關閉redis進程

2.通過客戶端操作redis數據庫

下面我們來簡單操作一下數據庫。 插入數據:設置一個key-value對

[root@redis01 redis-2.8.9]# redis-cli   #通過客戶端連接本地redis 
127.0.0.1:6379> set id 001  #寫入一條數據key(id),value(001)
OK
127.0.0.1:6379> get id  #取值key(id)
"001"   #顯示key對應的值
127.0.0.1:6379> del id  #刪除key(id)
(integer) 1     #1表示成功
127.0.0.1:6379> exists id   #驗證key是否存在
(integer) 0     #0表示不存在
127.0.0.1:6379> get id  #取key的值
(nil)           #報錯信息
127.0.0.1:6379> set user001 benet
OK
127.0.0.1:6379> set user002 yunjisuan
OK
127.0.0.1:6379> set user003 yun123
OK
127.0.0.1:6379> get user001
"benet"
127.0.0.1:6379> get user002
"yunjisuan"
127.0.0.1:6379> keys *  #查看redis裏所有的key
1) "user003"
2) "user002"
3) "user001"

redis-cli客戶端的遠程連接及非交互式操作數據庫
[root@redis01 redis-2.8.9]# redis-cli -h 10.0.0.135 -p 6379
10.0.0.135:6379> quit
[root@redis01 redis-2.8.9]# redis-cli -h 10.0.0.135 -p 6379 set aaa 111
OK
[root@redis01 redis-2.8.9]# redis-cli -h 10.0.0.135 -p 6379 get aaa
"111"

也可通過telnet連接redis數據庫

telnet 10.0.0.135 6379

3.redis安全

(1)為redis客戶端設置外部鏈接密碼

警告: 因為redis速度相當快,所以在一臺比較好的服務器下,一個外部的用戶可以在1秒內進行上萬次的密碼嘗試,這意味著你需要指定非常非常強大的密碼來防止暴力破解。

[root@redis01 redis-2.8.9]# grep -n requirepass /usr/local/redis/conf/redis.conf    #修改redis配置文件,添加密碼
198:# If the master is password protected (using the "requirepass" configuration
339:# requirepass foobared
[root@redis01 redis-2.8.9]# sed -i ‘339 s@# requirepass foobared@requirepass yunjisuan@g‘   #密碼是yunjisuan /usr/local/redis/conf/redis.conf
[root@redis01 redis-2.8.9]# grep -n requirepass /usr/local/redis/conf/redis.conf 
198:# If the master is password protected (using the "requirepass" configuration
339:requirepass yunjisuan

重啟redis後測試

#重啟redis進程
[root@redis01 redis-2.8.9]# ps -ef | grep redis | grep -v grep
root       3442   1288  0 13:40 pts/0    00:00:17 redis-server *:6379                          
[root@redis01 redis-2.8.9]# redis-cli shutdown
[3442] 02 Oct 18:17:03.370 # User requested shutdown...
[3442] 02 Oct 18:17:03.370 * Saving the final RDB snapshot before exiting.
[3442] 02 Oct 18:17:03.380 * DB saved on disk
[3442] 02 Oct 18:17:03.380 # Redis is now ready to exit, bye bye...
[1]+  Done                    redis-server /usr/local/redis/conf/redis.conf
[root@redis01 redis-2.8.9]# ps -ef | grep redis | grep -v grep
[root@redis01 redis-2.8.9]# redis-server /usr/local/redis/conf/redis.conf &
[root@redis01 redis-2.8.9]# ps -ef | grep redis | grep -v grep
root       3843   1288  0 18:18 pts/0    00:00:00 redis-server *:6379  

#測試驗證效果
#第一種登陸驗證方式
[root@redis01 redis-2.8.9]# redis-cli   #登陸本地redis
127.0.0.1:6379> set name 3333   #存數據
(error) NOAUTH Authentication required. #沒有驗證權限
127.0.0.1:6379> keys *  #查看所有key
(error) NOAUTH Authentication required. #沒有驗證權限
127.0.0.1:6379> auth yunjisuan  #提交驗證密碼
OK          #驗證通過
127.0.0.1:6379> keys *  #查看所有keys
1) "user003"
2) "ab"
3) "user002"
4) "aaa"
5) "user001"

#第二種登錄驗證方式
[root@redis01 redis-2.8.9]# redis-cli -a yunjisuan  #登陸時提交密碼
127.0.0.1:6379> keys *
1) "user003"
2) "ab"
3) "user002"
4) "aaa"
5) "user001"

特別提示: redis沒有用戶的概念,只能設置連接密碼,並且redis的連接速度非常快。因此密碼需要設置的很復雜才安全。

Redis緩存數據庫的安裝與配置(1)