Ubuntu16.04安裝Redis

前言

Redis是常用基於內存的Key-Value數據庫,比Memcache更先進,支持多種數據結構,高效,快速。用Redis可以很輕鬆解決高併發的數據訪問問題;作爲實時監控信號處理也非常不錯。

環境

Ubuntu 16.04

安裝Redis服務器端

~ sudo apt-get install redis-server

安裝完成後,Redis服務器會自動啓動,我們檢查Redis服務器程序

檢查Redis服務器系統進程

~ ps -aux|grep redis
redis     4162  0.1  0.0  10676  1420 ?        Ss   23:24   0:00 /usr/bin/redis-server /etc/redis/redis.conf
conan     4172  0.0  0.0  11064   924 pts/0    S+   23:26   0:00 grep --color=auto redis

通過啓動命令檢查Redis服務器狀態

~ netstat -nlt|grep 6379
tcp        0      0 127.0.0.1:6379          0.0.0.0:*               LISTEN

通過啓動命令檢查Redis服務器狀態

複製代碼

~$ sudo /etc/init.d/redis-server status
● redis-server.service - Advanced key-value store
   Loaded: loaded (/lib/systemd/system/redis-server.service; enabled; vendor preset: enabled)
   Active: active (running) since 四 2017-11-09 12:22:09 CST; 59s ago
     Docs: http://redis.io/documentation,
           man:redis-server(1)
 Main PID: 5394 (redis-server)
   CGroup: /system.slice/redis-server.service
           └─5394 /usr/bin/redis-server 127.0.0.1:6379

11月 09 12:22:09 zzf systemd[1]: Starting Advanced key-value store...
11月 09 12:22:09 zzf run-parts[5388]: run-parts: executing /etc/redis/redi...le
11月 09 12:22:09 zzf run-parts[5395]: run-parts: executing /etc/redis/redi...le
11月 09 12:22:09 zzf systemd[1]: Started Advanced key-value store.
Hint: Some lines were ellipsized, use -l to show in full.

複製代碼

通過命令行客戶端訪問Redis

安裝Redis服務器,會自動地一起安裝Redis命令行客戶端程序。

在本機輸入redis-cli命令就可以啓動,客戶端程序訪問Redis服務器。

複製代碼

~ redis-cli
redis 127.0.0.1:6379>

# 命令行的幫助

redis 127.0.0.1:6379> help
redis-cli 2.2.12
Type: "help @" to get a list of commands in 
      "help " for help on 
      "help " to get a list of possible help topics
      "quit" to exit


# 查看所有的key列表

redis 127.0.0.1:6379> keys *
(empty list or set)

複製代碼

基本的Redis客戶端命令操作

增加一條記錄key1

複製代碼

redis 127.0.0.1:6379> set key1 "hello"
OK

# 打印記錄
redis 127.0.0.1:6379> get key1
"hello"

複製代碼

增加一條數字記錄

複製代碼

set key2 1
OK

# 讓數字自增
redis 127.0.0.1:6379> INCR key2
(integer) 2
redis 127.0.0.1:6379> INCR key2
(integer) 3

# 打印記錄
redis 127.0.0.1:6379> get key2
"3"

複製代碼

增加一個列表記錄key3

複製代碼

redis 127.0.0.1:6379> LPUSH key3 a
(integer) 1

# 從左邊插入列表
redis 127.0.0.1:6379> LPUSH key3 b
(integer) 2

# 從右邊插入列表
redis 127.0.0.1:6379> RPUSH key3 c
(integer) 3

# 打印列表記錄,按從左到右的順序
redis 127.0.0.1:6379> LRANGE key3 0 3
1) "b"
2) "a"
3) "c"

複製代碼

增加一個哈希記表錄key4

複製代碼

redis 127.0.0.1:6379> HSET key4 name "John Smith"
(integer) 1

# 在哈希表中插入,email的Key和Value的值
redis 127.0.0.1:6379> HSET key4 email "[email protected]"
(integer) 1

# 打印哈希表中,name爲key的值
redis 127.0.0.1:6379> HGET key4 name
"John Smith"

# 打印整個哈希表
redis 127.0.0.1:6379> HGETALL key4
1) "name"
2) "John Smith"
3) "email"
4) "[email protected]"

複製代碼

增加一條哈希表記錄key5

複製代碼

# 增加一條哈希表記錄key5,一次插入多個Key和value的值
redis 127.0.0.1:6379> HMSET key5 username antirez password P1pp0 age 3
OK

# 打印哈希表中,username和age爲key的值
redis 127.0.0.1:6379> HMGET key5 username age
1) "antirez"
2) "3"

# 打印完整的哈希表記錄
redis 127.0.0.1:6379> HGETALL key5
1) "username"
2) "antirez"
3) "password"
4) "P1pp0"
5) "age"
6) "3"

複製代碼

刪除記錄

複製代碼

# 查看所有的key列表
redis 127.0.0.1:6379> keys *
1) "key2"
2) "key3"
3) "key4"
4) "key5"
5) "key1"

# 刪除key1,key5
redis 127.0.0.1:6379> del key1
(integer) 1
redis 127.0.0.1:6379> del key5
(integer) 1

# 查看所有的key列表
redis 127.0.0.1:6379> keys *
1) "key2"
2) "key3"
3) "key4"

複製代碼

修改Redis的配置

使用Redis的訪問賬號

默認情況下,訪問Redis服務器是不需要密碼的,爲了增加安全性我們需要設置Redis服務器的訪問密碼。設置訪問密碼爲redisredis。

用vi打開Redis服務器的配置文件redis.conf

~ sudo vi /etc/redis/redis.conf

#取消註釋requirepass
requirepass redisredis

讓Redis服務器被遠程訪問

默認情況下,Redis服務器不允許遠程訪問,只允許本機訪問,所以我們需要設置打開遠程訪問的功能。

用vi打開Redis服務器的配置文件redis.conf

~ sudo vi /etc/redis/redis.conf

#註釋bind
#bind 127.0.0.1

修改後,重啓Redis服務器。

~ sudo /etc/init.d/redis-server restart
Stopping redis-server: redis-server.
Starting redis-server: redis-server.

未使用密碼登陸Redis服務器

~ redis-cli

redis 127.0.0.1:6379> keys *
(error) ERR operation not permitted

發現可以登陸,但無法執行命令了。

登陸Redis服務器,輸入密碼

複製代碼

~  redis-cli -a redisredis

redis 127.0.0.1:6379> keys *
1) "key2"
2) "key3"
3) "key4"

複製代碼

登陸後,一切正常。

我們檢查Redis的網絡監聽端口

檢查Redis服務器佔用端口

~ netstat -nlt|grep 6379
tcp        0      0 0.0.0.0:6379            0.0.0.0:*               LISTEN

我們看到網絡監聽從之前的 127.0.0.1:3306 變成 0 0.0.0.0:3306,表示Redis已經允許遠程登陸訪問。

我們在遠程的另一臺Linux訪問Redis服務器

複製代碼

~ redis-cli -a redisredis -h 192.168.1.199

redis 192.168.1.199:6379> keys *
1) "key2"
2) "key3"
3) "key4"

複製代碼

遠程訪問正常。通過上面的操作,我們就把Redis數據庫服務器,在Linux Ubuntu中的系統安裝完成。

 

原文鏈接:http://blog.csdn.net/qq_30242609/article/details/52913145

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章