資安

CentOS-7 安裝 Redis

wget安裝

[root@RedisSrv1 ~]# yum install wget

安裝gcc依賴

[root@RedisSrv1 ~]# yum install gcc -y
-- 請先檢查gcc的版本是否低於5,如果是請先升級,可以使用以下命令:
[root@RedisSrv1 redis-stable]# gcc -v
CentOS7默認安裝的是4.8.5,而redis6.0只支持5.3以上版本,這裡將gcc升級到9
[root@RedisSrv1 redis-stable]# yum -y install centos-release-scl
[root@RedisSrv1 redis-stable]# yum install devtoolset-9-gcc*
gcc版本切換
臨時切換:scl enable devtoolset-9 bash
永久切換:echo “source /opt/rh/devtoolset-9/enable” >> /etc/profile

下載最新穩定版 Redis

[root@RedisSrv1 /]# cd /opt/
[root@RedisSrv1 /]# wget http://download.redis.io/releases/redis-stable.tar.gz

解壓redis安裝包

[root@RedisSrv1 opt]# tar -zxvf redis-stable.tar.gz

進到解壓後的redis目錄中進行編譯

[root@RedisSrv1 opt]# cd redis-stable

[root@RedisSrv1 redis-stable]# make MALLOC=libc
make[1]: Leaving directory `/opt/redis-stable/src'

編譯完成後,進入到src目錄下,可以看到,生成了可執行文件
生成了src目錄文件之後,進入src(源文件目錄)繼續編譯
[root@RedisSrv1 redis-stable]# cd src/

#安裝到 /usr/local/redis-stable 目錄
[root@RedisSrv1 src]# make install PREFIX=/usr/local/redis-stable

Hint: It's a good idea to run 'make test' ;)

    INSTALL install
    INSTALL install
    INSTALL install
    INSTALL install
    INSTALL install

 --將配置文件移動到/home/redis/目錄
cp /opt/redis-stable/redis.conf /usr/local/redis-stable
--修改文件權限  
chmod -R 700 /usr/local/redis-stable/
[root@RedisSrv1 redis-stable]# cd /usr/local/redis-stable/bin
[root@RedisSrv1 bin]# ./redis-server /usr/local/redis-stable/redis.conf


--關閉redis進程
[root@server01 src]# ps -ef | grep redis
root       1177      1  0 09:09 ?        00:00:00 ./redis-server 0.0.0.0:6379
root       1185   1147  0 09:11 pts/0    00:00:00 grep --color=auto redis
[root@server01 src]# 
[root@server01 src]# ps -aux | grep redis
root       1177  0.0  0.2 144008  2028 ?        Ssl  09:09   0:00 ./redis-server 0.0.0.0:6379
root       1187  0.0  0.0 112708   976 pts/0    R+   09:11   0:00 grep --color=auto redis
[root@server01 src]# 
[root@server01 src]# kill -9 1177
[root@server01 src]# 
[root@server01 src]# ps -aux | grep redis
root       1189  0.0  0.0 112708   980 pts/0    R+   09:12   0:00 grep --color=auto redis
[root@server01 src]# 

開放防火牆6379端口
firewall-cmd --zone=public --add-port=6379/tcp --permanent

重新加載防火牆設置
firewall-cmd --reload

查看是否生效
firewall-cmd --list-ports

卸載Redis

查看進程
[root@RedisSrv1 src]# ps aux |grep redis

殺掉進程
kill -9 進程號

查看相關文件
[root@RedisSrv1 src]# find / -name "redis*"

刪除文件
rm -rf 文件

Redis 自啟動

[root@RedisSrv1 ~]# vim /usr/lib/systemd/system/redis.service
[Unit]
Description=Redis persistent key-value database
After=network.target
After=network-online.target
Wants=network-online.target

[Service]
Type=forking
PIDFile=/var/run/redis_6379.pid
ExecStart=/usr/local/redis-stable/bin/redis-server /usr/local/redis-stable/redis.conf --supervised systemd
ExecReload=/bin/kill -USR2 $MAINPID
ExecStop=/usr/local/redis-stable/bin/redis-cli -p 6379 shutdown

[Install]
WantedBy=multi-user.target

[root@RedisSrv1 ~]# systemctl daemon-reload
[root@RedisSrv1 ~]# systemctl start redis
[root@RedisSrv1 ~]# systemctl status redis
[root@RedisSrv1 ~]# systemctl stop redis

Leave a Reply

Your email address will not be published. Required fields are marked *