Redis 笔记(01)— 安装、启动配置、开启远程连接、设置密码、远程连接
1. Redis 簡介
1月份將 《Redis入門指南》過了一遍,現將 Redis 五大類型的常用命令做一總結,留著后續備用。
Redis 是 RemoteDictionary Server (遠程字典服務器)的縮寫,它以字典結構存儲數據,并允許其它應用通過 TCP 的協議讀寫字典中的內容。Redis 字典中的鍵值除了可以是字符串,還可以是其它數據類型。支持的數據類型如下:
-
字符串類型
-
散列類型
-
列表類型
-
集合類型
-
有序集合類型
Redis 數據庫中的所有數據都存儲在內存中,由于內存的讀寫速度遠快于硬盤,因此 Redis 在性能上對比其他基于硬盤存儲的數據有非常明顯的優勢。在普通筆記本電腦上,Redis 可以在一秒內讀寫超過 10 萬以上的鍵值。
Redis 提供了對于持久化的支持,即可以將內存中的屬于異步寫入到硬盤中,同時不影響繼續提供服務。
除此之外,Redis 的列表類型鍵可以用來實現隊列,并且支持阻塞式讀取,可以很容易實現一個高性能的優先級隊列。同時還支持“發布/訂閱”的消息模式。
2. Redis 安裝
2.1 源碼安裝
- 下載,解壓,編譯:
wget http://download.redis.io/releases/redis-4.0.8.tar.gz
tar xzf redis-4.0.8.tar.gz
cd redis-4.0.8
make
cd src
- 二進制文件是編譯完成后在
src目錄下,通過下面的命令啟動redis服務:
./redis-server
# 或者后臺運行服務器,daemonize表示在后臺運行
./redis-server --daemonize yes
- 你可以使用內置的客戶端命令
redis-cli進行使用:
./redis-cli
redis> set foo bar
OK
redis> get foo
"bar"
2.2 工具管理包安裝
# mac
brew install redis
# ubuntu
sudo apt-get install -y redis-server
# redhat
yum install redis
# 運行客戶端
redis-cli
-y 表示確認安裝
安裝完成后,Redis 服務器會自動啟動,我們檢查 Redis 服務器程序
wohu@Z:/$ service redis status
● redis-server.service - Advanced key-value storeLoaded: loaded (/lib/systemd/system/redis-server.service; enabled; vendor preset: enabled)Active: active (running) since Thu 2018-12-20 21:51:23 CST; 2min 17s agoDocs: http://redis.io/documentation,man:redis-server(1)Main PID: 3559 (redis-server)CGroup: /system.slice/redis-server.service└─3559 /usr/bin/redis-server 127.0.0.1:6379
2.3 Docker 安裝
docker pull redis
docker run --name myredis -d -p6379:6379 redis
docker exec -it myredis redis-cli
3. Redis 配置
3.1 啟動配置
- 默認啟動
$ redis-server
Redis 安裝后默認就以這種方式啟動。
- 運行啟動
$ redis-server --configKey1 configValue1 --configKey2 configValue2
例如
$ redis-server --port 6380
- 配置文件啟動
將配置寫到指定文件里,例如我們將配置寫到了 /opt/redis/redis.conf 中,那么只需要執行如下命令即可啟動 Redis :
$ redis-server /opt/redis/redis.conf
redis.conf 文件參數說明:
圖片來源于 https://gitbook.cn/books/5a196cb24a97251edce2ae9a/index.html
3.2 開啟遠程連接
Redis 默認是不支持遠程連接的,要開啟這個功能,需要作如下修改,執行以下命令
sudo cat /etc/redis/redis.conf
找到如下節點
# By default Redis listens for connections from all the network interfaces
# available on the server. It is possible to listen to just one or multiple
# interfaces using the "bind" configuration directive, followed by one or
# more IP addresses.
#
# Examples:
#
# bind 192.168.1.100 10.0.0.1
bind 127.0.0.1
將 bind 127.0.0.1 前面加 # 號注釋掉
3.3 設置密碼
將 /etc/redis/redis.conf 文件下 requirepass 123456(密碼設置為 123456 )
# 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
注意:配置 Redis 復制的時候如果主數據庫設置了密碼,需要在從數據庫的配置文件中通過 masterauth 參數設置主數據庫的密碼,以使從數據庫連接主數據庫時自動使用 AUTH 命令認證。
4. Redis 測試
4.1 本地連接
輸入 redis-cli 命令可啟動 Redis 客戶端,輸入 ping 命令有如下返回值時,說明 Redis 服務正常
wohu@Z:/$ redis-cli
127.0.0.1:6379> ping
PONG
127.0.0.1:6379>
4.2 遠程連接
wohu@Z:/$ redis-cli -h host_ip -p port
其中 host_ip 為 redis 遠程服務器地址, port 為 redis 的端口地址,默認為 6379
總結
以上是生活随笔為你收集整理的Redis 笔记(01)— 安装、启动配置、开启远程连接、设置密码、远程连接的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: OpenCV 图像处理系列(6)—— 图
- 下一篇: Redis 笔记(02)— keys 键