前言
Redis是常用基於內存的Key-Value數據庫,比Memcache更先進,支持多種數據結構,高效,快速。用Redis可以很輕松解決高並發的數據訪問問題;作為實時監控信號處理也非常不錯。
環境
Ubuntu 18.04
安裝Redis服務器端
~ sudo apt-get install redis-server
安裝完成后,Redis服務器會自動啟動,我們檢查Redis服務器程序
檢查Redis服務器系統進程
~ ps -agx|grep redis
1 10382 10382 10382 ? -1 Ssl 124 0:07 /usr/bin/redis-server 127.0.0.1:6379
2677 10618 10617 2677 pts/0 10617 S+ 1000 0:00 grep --color=auto redis
通過啟動命令檢查Redis服務器狀態
~ netstat -nlt|grep 6379
tcp 0 0 127.0.0.1:6379 0.0.0.0:* LISTEN
tcp6 0 0 ::1:6379 :::* 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 Fri 2018-08-10 19:50:13 CST; 36min ago
Docs: http://redis.io/documentation,
man:redis-server(1)
Main PID: 10382 (redis-server)
Tasks: 4 (limit: 2294)
CGroup: /system.slice/redis-server.service
└─10382 /usr/bin/redis-server 127.0.0.1:6379
8月 10 19:50:12 zhangkun-virtual-machine systemd[1]: Starting Advanced key-v…..
8月 10 19:50:13 zhangkun-virtual-machine systemd[1]: redis-server.service: C…ry
8月 10 19:50:13 zhangkun-virtual-machine systemd[1]: Started Advanced key-va…e.
Hint: Some lines were ellipsized, use -l to show in full.
通過命令行客戶端訪問Redis
安裝Redis服務器,會自動地一起安裝Redis命令行客戶端程序。
在本機輸入redis-cli命令就可以啟動,客戶端程序訪問Redis服務器。
~ redis-cli
127.0.0.1:6379> help
redis-cli 4.0.9
To get help about Redis commands type:
"help @<group>" to get a list of commands in <group>
"help <command>" for help on <command>
"help <tab>" to get a list of possible help topics
"quit" to exit
To set redis-cli preferences:
":set hints" enable online hints
":set nohints" disable online hints
Set your preferences in ~/.redisclirc
127.0.0.1:6379> set key1 "hello"
OK
127.0.0.1:6379> get key1
"hello"
基本的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 "abc@gmail.com" (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) "abc@gmail.com"
增加一條哈希表記錄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:6379 變成 0 0.0.0.0:6379,表示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中的系統安裝完成。