Redis速度很快,所以在一臺比較好的服務起下,一個外部的用戶可以在1秒鐘進行150k次的密碼嘗試連接,所以我們需要設置非常強大的密碼來防止暴力破解。 ...
該文使用centos6.5 64位 redis3.2.8
一、redis常用命令
鍵值常用命令: 1、 keys 返回滿足pattern的所有key。 127.0.0.1:6379> keys my* 127.0.0.1:6379> keys * 2、exits 確認key是否存在。 返回1表示存在 0表示不存在 127.0.0.1:6379> exists name 3、del :刪除一個鍵 返回1:刪除成功 0:失敗 127.0.0.1:6379> del name (integer) 1 4、expire 設置key(該key必須存在)的過期時間 返回1表示設置成功 0 失敗 5、ttl 查看鍵的過期時間 如果該鍵已經過期(銷毀)則返回負數 127.0.0.1:6379> expire myset2 10 (integer) 1 127.0.0.1:6379> ttl myset2 (integer) 4 127.0.0.1:6379> ttl myset2 (integer) -2 6、選擇資料庫 Redis中一共有16個資料庫他們分別是0-15 select 0 表示當前資料庫 7、move 將當前資料庫中的key轉移到其他資料庫中 返回1表示成功 0 失敗 127.0.0.1:6379> move age 1 (integer) 1 8、persiste:移除給定key的過期時間 返回1表示取消成功 0 失敗 127.0.0.1:6379[1]> expire age 100 (integer) 1 127.0.0.1:6379[1]> ttl age (integer) 94 127.0.0.1:6379[1]> persist age (integer) 1 127.0.0.1:6379[1]> get age "25" 9、randomkey:隨機資料庫中的一個key 127.0.0.1:6379[1]> randomkey "age" 10、rename:重命名key 127.0.0.1:6379[1]> keys * 1) "age" 127.0.0.1:6379[1]> rename age age_now OK 127.0.0.1:6379[1]> keys * 1) "age_now" 11、返回key的數據類型 127.0.0.1:6379> type height string 127.0.0.1:6379> type myzset1 Zset 伺服器相關命令 1、Ping :測試鏈接redis是否存活 返回 PONG 表示鏈接存活 127.0.0.1:6379> ping PONG 2、echo :在命令行列印數據 127.0.0.1:6379> echo hello "hello" 3、select 選擇資料庫。Redis資料庫編號是0-15,我們可以選擇任意一個資料庫進行數據存儲 127.0.0.1:6379> select 0 OK 127.0.0.1:6379> select 15 OK 127.0.0.1:6379[15]> select 16 (error) ERR invalid DB index 127.0.0.1:6379[15]> 4、quit 退出客戶端與redis伺服器的連接 5、dbsize 返回當前資料庫中key的數量 127.0.0.1:6379> dbsize (integer) 15 6、info 獲取redis服務的相關信息和統計 127.0.0.1:6379> info 7、config get 查看redis伺服器相關配置參數 127.0.0.1:6379> config get * 127.0.0.1:6379> config get port 1) "port" 2) "6379" 7、flushdb 刪除當前資料庫中所有的key 127.0.0.1:6379[1]> keys * 1) "age_now" 127.0.0.1:6379[1]> flushdb OK 127.0.0.1:6379[1]> keys * (empty list or set) 8、刪除所有資料庫中所有的key 127.0.0.1:6379[1]> flushall
二、 redis安全機制
設置redis連接密碼
Redis速度很快,所以在一臺比較好的服務起下,一個外部的用戶可以在1秒鐘進行150k次的密碼嘗試連接,所以我們需要設置非常強大的密碼來防止暴力破解。
設置密碼:在redis的配置文件中 添加requirepass password
################################## SECURITY ################################### # Require clients to issue AUTH <PASSWORD> before processing any other # commands. This might be useful in environments in which you do not trust # others with access to the host running redis-server. # # This should stay commented out for backward compatibility and because most # people do not need auth (e.g. they run their own servers). # # Warning: since Redis is pretty fast an outside user can try up to # 150k passwords per second against a good box. This means that you should # use a very strong password otherwise it will be very easy to break. # # requirepass foobared requirepass jalja
重啟redis伺服器:[root@localhost bin]# ./redis-server /usr/local/redis/etc/redis.conf
1、測試密碼是否可用:
[root@localhost bin]# ./redis-cli
127.0.0.1:6379> keys *
(error) NOAUTH Authentication required.(操作被拒絕)
使用密碼(授權):auth password
127.0.0.1:6379> auth jalja
OK
2、使用密碼登錄redis伺服器:[root@localhost bin]# ./redis-cli -a jalja