Linux 下安裝 Redis


1、准備 redis 安裝包,可以進入官網,自行選擇需要的版本下載,我下載的是 redis-6.2.4.tar.gz

image-20210730204801426

2、將本地的安裝包上傳到 linux 服務器上,我這里放在 /home/software 目錄下


3、在 /usr/local/ 下創建 redis 文件夾

mkdir /usr/local/redis

4、解壓安裝包

tar zxvf redis-6.2.4.tar.gz -C /usr/local/redis

解壓完之后, /usr/local/redis 目錄中應該有一個相關目錄

[root@xxx software]# ls /usr/local/redis
redis-6.2.4

5、編譯並安裝 redis

cd /usr/local/redis/redis-6.2.4
make && make install

6、將 redis 安裝為系統服務並后台啟動

# 進⼊ utils ⽬錄,並執⾏如下腳本即可
cd utils/
./install_server.sh

如果出現報錯,那么編輯這個文件

image-20210731171900592

vim ./install_server.sh

注釋下面的代碼

#bail if this system is managed by systemd
#_pid_1_exe="$(readlink -f /proc/1/exe)"
#if [ "${_pid_1_exe##*/}" = systemd ]
#then
#       echo "This systems seems to use systemd."
#       echo "Please take a look at the provided example service unit files in this directory, and adapt and install them. Sorry!"
#       exit 1
#fi

image-20210731172018766

再次執行腳本即可

./install_server.sh

image-20210731172126852

7、查看 redis 服務啟動情況

systemctl status redis_6379.service

8、啟動自帶的 redis-cli 客戶端,測試 redis

[root@xxx utils]# redis-cli
127.0.0.1:6379> ping
PONG
127.0.0.1:6379> set k1 v1
OK
127.0.0.1:6379> get k1
"v1"

9、設置允許遠程連接

編輯配置文件

vim /etc/redis/6379.conf

將原來的 bind 127.0.0.1 這行注釋掉,改為 0.0.0.0

# bind 127.0.0.1
bind 0.0.0.0

image-20210731172353517

重啟 redis 服務

systemctl restart redis_6379.service

10、設置訪問密碼

vim /etc/redis/6379.conf

找到 # requirepass foobared,在這個注釋下加一行,為 requirepass 自己的密碼

# requirepass foobared
requirepass distance

image-20210731172724221

保存,重啟 redis 服務

systemctl restart redis_6379.service

11、再次測試

[root@xxx ~]# redis-cli
127.0.0.1:6379> ping
(error) NOAUTH Authentication required.
127.0.0.1:6379> auth distance
OK
127.0.0.1:6379> ping
PONG

12、 redis 服務管理

查看 redis 服務

ps -ef | grep redis

通過配置文件啟動 redis

redis-server /etc/redis/6379.conf

image-20210731174249027

redis-cli 客戶端連接

[root@xxx redis]# redis-cli -p 6379
127.0.0.1:6379> auth distance
OK
127.0.0.1:6379> ping
PONG

image-20210731174337626

在客戶端中可以關閉 redis 服務端

127.0.0.1:6379> shutdown
not connected> exit

image-20210731174515927

13、嘗試遠程連接,用本地 windows 的 redis-cli 來連接 linux 服務器上的 redis server,需要本地也安裝了 redis

# linux 先開啟 redis 服務端
redis-server /etc/redis/6379.conf

# windows 本地打開 cmd
redis-cli.exe -h 192.168.0.102 -p 6379 -a redis

image-20210731172937779

遠程連接成功!



免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM