1. 程式人生 > 其它 >linux之redis6.2環境安裝與配置

linux之redis6.2環境安裝與配置

1. redis 6.2 系列

1.1 安裝

進入官網,下載redis-6.2.4.tar.gz
將下載好的 redis 檔案壓縮包,上傳到 linux 系統中
# 通過終端命令解壓
[root@localhost local]# tar -zxvf redis-6.2.4.tar.gz

# 重命令 redis,改不改問題不大,個人習慣
[root@localhost local]# tar -zxvf redis-6.2.4.tar.gz

# 注意 redis C語言開發 安裝需要 gcc 環境,才能正常執行
[root@localhost local]# yum -y install gcc

# 進入 redis 中,執行命令
[root@localhost local]# cd redis
[root@localhost redis]# make

# 在進入到 src 中
[root@localhost redis]# cd src
[root@localhost src]# make install

# 開啟 redis 服務
[root@localhost src]# ./redis-server
注意:這樣開啟服務,會有問題,它以前臺模式執行服務,這樣還需要另外一臺連線伺服器來對 redis 操作
# 修改 redis.conf 配置檔案
[root@localhost src]# vim /usr/local/redis/redis.conf
儲存並開啟服務測試
# 進入到 redis 中的 src 裡,開啟服務時,帶上 redis 配置檔案
[root@localhost src]# ./redis-server ../redis.conf

# 連線 redis 客戶端,進行操作,顯示地址和埠表示 redis 連線成功,可以使用
[root@localhost src]# ./redis-cli
127.0.0.1:6379>
遠端連線
# 綜上,能夠正常開啟和連線服務,但是在windows中使用 RDM 或者 IDEA,就無法連線成功
# 原因是沒有設定遠端連線
# 修改 redis 配置檔案
[root@localhost src]# vim /usr/local/redis/redis.conf
# 重啟 redis 服務
[root@localhost src]# ./redis-server ../redis.conf
防火牆
# 綜上得到配置好還是遠端連線不上,最後排查防火牆
[root@localhost local]# systemctl status firewalld.service
# 關閉防火牆
[root@localhost local]# systemctl stop firewalld.service
自啟動
# 編寫自啟動指令碼
# 注意在 /etc/init.d 中 編寫 redis 檔案,/etc/init.d 就是開機初始化資料夾
[root@localhost src]# vi /etc/init.d/redis
配置資訊
#!/bin/sh
# chkconfig: 2345 10 90
# description: Start and Stop redis

# 到本機安裝redis後,存放redis命令的目錄
PATH=/usr/local/bin:/sbin:/usr/bin:/bin

# redis的預設埠, 要和下文中的redis.conf中一致
REDISPORT=6379

# redis服務端的命令
EXEC=/usr/local/redis/src/redis-server

# redis客戶端的命令  這兩個一般都在 PATH目錄下
REDIS_CLI=/usr/local/redis/src/redis-cli

# reids的程序檔案生成的位置
PIDFILE=/var/run/redis.pid

# redis的配置檔案所在的目錄
CONF="/usr/local/redis/redis.conf"

case "$1" in
        start)
                if [ -f $PIDFILE ]
                then
                        echo "$PIDFILE exists, process is already running or crashed."
                else
                        echo "Starting Redis server..."
                        $EXEC $CONF
                fi
                if [ "$?"="0" ]
                then
                        echo "Redis is running..."
                fi
                ;;
        stop)
                if [ ! -f $PIDFILE ]
                then
                        echo "$PIDFILE exists, process is not running."
                else
                        PID=$(cat $PIDFILE)
                        echo "Stopping..."
                       $REDIS_CLI -p $REDISPORT  SHUTDOWN
                        sleep 2
                       while [ -x $PIDFILE ]
                       do
                                echo "Waiting for Redis to shutdown..."
                               sleep 1
                        done
                        echo "Redis stopped"
                fi
                ;;
        restart|force-reload)
                ${0} stop
                ${0} start
                ;;
        *)
               echo "Usage: /etc/init.d/redis {start|stop|restart|force-reload}" >&2
                exit 1
esac
# 儲存後,進入到 /etc/init.d 中
[root@localhost src]# cd /etc/init.d

# 檢視檔案許可權
[root@localhost init.d]# ll
-rw-r--r--. 1 root root 18281 May 22  2020 functions
-rwxr-xr-x. 1 root root 10613 Jul  9 19:32 mysql
-rwxr-xr-x. 1 root root  4569 May 22  2020 netconsole
-rwxr-xr-x. 1 root root  7928 May 22  2020 network
-rw-r--r--. 1 root root  1160 Oct  2  2020 README
-rw-r--r--. 1 root root  1898 Jul 15 16:20 redis

# 修改 redis 檔案許可權
[root@localhost init.d]# chmod 755 redis

# 再次檢視 redis 許可權
-rw-r--r--. 1 root root 18281 May 22  2020 functions
-rwxr-xr-x. 1 root root 10613 Jul  9 19:32 mysql
-rwxr-xr-x. 1 root root  4569 May 22  2020 netconsole
-rwxr-xr-x. 1 root root  7928 May 22  2020 network
-rw-r--r--. 1 root root  1160 Oct  2  2020 README
-rwxr-xr-x. 1 root root  1898 Jul 15 16:20 redis

# 測試是否有效
[root@localhost init.d]# /etc/init.d/redis start
Starting Redis server...
Redis is running...

# 最後新增自啟
[root@localhost init.d]# chkconfig --add redis
安裝完畢!

1.2 解除安裝

檢視是否安裝了 redis 資料庫
# 檢視是否存在 redis
[root@localhost local]# rpm -qa | grep redis
[root@localhost local]# find / -name redis
/etc/selinux/targeted/active/modules/100/redis
/usr/local/redis

# 檢視服務是否開啟狀態
[root@localhost local]# ps -ef | grep 6379
root       2854      1  0 14:31 ?        00:00:01 ./redis-server 0.0.0.0:6379
root       6970   1465  0 14:41 pts/0    00:00:00 grep --color=auto 6379

# 存在服務程序,將其殺死
[root@localhost local]# kill -9 2854

# 解除安裝 redis 服務
[root@localhost local]# rm -rf /etc/selinux/targeted/active/modules/100/redis
[root@localhost local]# rm -rf /usr/local/redis
解除安裝完畢!
從入門到崩潰