Redis快取資料庫的安裝與配置(1)

來源:互聯網
上載者:User

標籤:grep -n   attr   出現   啟動服務   grep   issue   連結   問題   yun   

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進程

[[email protected] redis-2.8.9]# pkill redis[[email protected] redis-2.8.9]# sysctl vm.overcommit_memory=1vm.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對

[[email protected] redis-2.8.9]# redis-cli   #通過用戶端串連本地redis 127.0.0.1:6379> set id 001  #寫入一條資料key(id),value(001)OK127.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 benetOK127.0.0.1:6379> set user002 yunjisuanOK127.0.0.1:6379> set user003 yun123OK127.0.0.1:6379> get user001"benet"127.0.0.1:6379> get user002"yunjisuan"127.0.0.1:6379> keys *  #查看redis裡所有的key1) "user003"2) "user002"3) "user001"

redis-cli用戶端的遠端連線及非互動式操作資料庫
[[email protected] redis-2.8.9]# redis-cli -h 10.0.0.135 -p 637910.0.0.135:6379> quit[[email protected] redis-2.8.9]# redis-cli -h 10.0.0.135 -p 6379 set aaa 111OK[[email protected] 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秒內進行上萬次的密碼嘗試,這意味著你需要指定非常非常強大的密碼來防止暴力破解。

[[email protected] redis-2.8.9]# grep -n requirepass /usr/local/redis/conf/redis.conf    #修改redis設定檔,添加密碼198:# If the master is password protected (using the "requirepass" configuration339:# requirepass foobared[[email protected] redis-2.8.9]# sed -i ‘339 [email protected]# requirepass foobared@requirepass yunjisuan@g‘   #密碼是yunjisuan /usr/local/redis/conf/redis.conf[[email protected] redis-2.8.9]# grep -n requirepass /usr/local/redis/conf/redis.conf 198:# If the master is password protected (using the "requirepass" configuration339:requirepass yunjisuan

重啟redis後測試

#重啟redis進程[[email protected] redis-2.8.9]# ps -ef | grep redis | grep -v greproot       3442   1288  0 13:40 pts/0    00:00:17 redis-server *:6379                          [[email protected] 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[[email protected] redis-2.8.9]# ps -ef | grep redis | grep -v grep[[email protected] redis-2.8.9]# redis-server /usr/local/redis/conf/redis.conf &[[email protected] redis-2.8.9]# ps -ef | grep redis | grep -v greproot       3843   1288  0 18:18 pts/0    00:00:00 redis-server *:6379  #測實驗證效果#第一種登陸驗證方式[[email protected] redis-2.8.9]# redis-cli   #登陸本地redis127.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 *  #查看所有keys1) "user003"2) "ab"3) "user002"4) "aaa"5) "user001"#第二種登入驗證方式[[email protected] 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)

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.