Centos7安裝Redis6,Linux redis 安裝和開機啟動


 

================================

©Copyright 蕃薯耀 2022-01-20

https://www.cnblogs.com/fanshuyao/

 

一、下載redis文件

https://redis.io/download

 


二、上傳redis文件(redis-6.2.6.tar.gz)到服務器進行編譯安裝


1、解壓文件:

tar -zxvf redis-6.2.6.tar.gz

 

2、修改文件夾名稱,簡短一點

mv redis-6.2.6 redis6

 

3、進入目錄:

cd redis6/

 

4、編譯文件:

make

 

如果make命令報錯(沒錯可跳過):
make[3]: cc:命令未找到
make[3]: *** [alloc.o] 錯誤 127
make[3]: 離開目錄“/java/redis6/deps/hiredis”
make[2]: *** [hiredis] 錯誤 2
make[2]: 離開目錄“/java/redis6/deps”
make[1]: [persist-settings] 錯誤 2 (忽略)
CC adlist.o
/bin/sh: cc: 未找到命令
make[1]: *** [adlist.o] 錯誤 127
make[1]: 離開目錄“/java/redis6/src”
make: *** [all] 錯誤 2


解決方案(cc:命令未找到):

yum -y install gcc automake autoconf libtool make

 


如果make命令還報錯(沒錯可跳過):
zmalloc.h:50:31: 致命錯誤:jemalloc/jemalloc.h:沒有那個文件或目錄
#include <jemalloc/jemalloc.h>
^
編譯中斷。
make[1]: *** [adlist.o] 錯誤 1
make[1]: 離開目錄“/java/redis6/src”
make: *** [all] 錯誤 2


解決方案:make增加參數(致命錯誤:jemalloc/jemalloc.h):

make MALLOC=libc

 


5、編譯后安裝Redis
PREFIX為指定安裝的目錄,自動創建bin目錄

make install PREFIX=/java/redis6

 

查看(多出一個bin目錄):

ll

 

 


三、Redis配置文件復制和數據存放目錄

1、創建一個redis數據存放目錄datas,以后其它所有應用程序都將數據存放這里,方便備份

sudo mkdir -p /var/datas/redis

 

2、修改目錄擁有者

sudo chown -R java:java /var/datas

 

3、復制redis.conf配置文件到bin目錄

cp /java/redis6/redis.conf /java/redis6/bin/

 

4、進入bin目錄:

cd /java/redis6/bin/

 

 

 

四、redis.conf配置文件屬性修改

修改redis.conf配置文件

vi /java/redis6/bin/redis.conf

 

1、Redis開啟守護進程模式

找到daemonize配置項,修改為yes,開啟守護進程模式

daemonize yes

 

2、Redis取消IP綁定

注釋bind 127.0.0.1這行,只能本地連接redis,不然無法使用遠程連接。

# bind 127.0.0.1

 

3、Redis關閉保護模式

將protected-mode的yes改為no,也是開啟遠程連接。

protected-mode no

 

4、修改Redis默認端口(可省略)

默認端口是6378,修改成6677

port 6677

 

位置和示例:
# Accept connections on the specified port, default is 6379 (IANA #815344).
# If port 0 is specified Redis will not listen on a TCP socket.
port 6677


5、Redis修改rdb文件名、存放目錄、日志文件名

文件名及路徑相關配置

pidfile /var/run/redis_101_6677.pid

logfile "redis-101-6677.log"

dbfilename dump-101-6677.rdb

dir /var/datas/redis/

 

dir 配置日志和rdb文件存放的目錄,后面要帶/

位置:
# The working directory.
#
# The DB will be written inside this directory, with the filename specified
# above using the 'dbfilename' configuration directive.
#
# The Append Only File will also be created inside this directory.
#
# Note that you must specify a directory here, not a file name.
dir /var/datas/redis/

 

6、Redis修改密碼:requirepass 密碼(不需要可省略)

打開注釋,修改密碼為:123456

requirepass 123456

 

位置:
# IMPORTANT NOTE: starting with Redis 6 "requirepass" is just a compatibility
# layer on top of the new ACL system. The option effect will be just setting
# the password for the default user. Clients will still authenticate using
# AUTH <password> as usually, or more explicitly with AUTH default <password>
# if they follow the new protocol: both will work.
#
# The requirepass is not compatable with aclfile option and the ACL LOAD
# command, these will cause requirepass to be ignored.
#
# requirepass foobared

 

 

三、Redis啟動和測試

1、啟動Redis

/java/redis6/bin/redis-server /java/redis6/bin/redis.conf

 

2、查看redis進程:

ps -ef | grep redis

 


3、啟動Redis客戶端

#默認啟動命令(默認端口是6379)
/java/redis6/bin/redis-cli


#指定IP地址和指定端口啟動
#-h host 指定IP地址
#-p port 指定端口號
/java/redis6/bin/redis-cli -h 127.0.0.1 -p 6677


#指定IP地址、端口號、密碼啟動
#-h host 指定IP地址
#-p port 指定端口號
#-a auth 指定密碼,首先得在配置文件設置密碼:requirepass 123456
/java/redis6/bin/redis-cli -h 127.0.0.1 -p 6677 -a redisPassword

 

 

四、Centos7設置Redis開機啟動,Redis自啟動

1、在系統服務目錄里創建redis.service文件

sudo vi /etc/systemd/system/redis.service

 


2、redis.service文件粘貼內容(粘貼前先按字母 i , 進入編輯模式):

路徑修改成自己的安裝路徑

[Unit]
#Description:描述服務
Description=Redis
#After:描述服務類別 
After=network.target

#服務運行參數的設置 
[Service]
#Type=forking是后台運行的形式 
Type=forking
#ExecStart為服務的具體運行命令,路徑必須是絕對路徑 
ExecStart=/java/redis6/bin/redis-server /java/redis6/bin/redis.conf
#ExecReload為重啟命令 ,路徑必須是絕對路徑 
ExecReload=/java/redis6/bin/redis-server -s reload
#ExecStop為停止命令 ,路徑必須是絕對路徑 
ExecStop=/java/redis6/bin/redis-server -s stop
#PrivateTmp=True表示給服務分配獨立的臨時空間 
PrivateTmp=true

#運行級別下服務安裝的相關設置,可設置為多用戶,即系統運行級別為3
[Install]
WantedBy=multi-user.target

 


3、重載系統服務:

sudo systemctl daemon-reload

 

4、redis.service服務加入開機自啟 (注意redis.service后面不能跟空格 )

sudo systemctl enable redis.service

 

5、重啟服務器

reboot -f

 

6、系統重啟后,查看服務運行狀態:

sudo systemctl status redis.service

 

 7、其它命令

sudo systemctl start redis.service #啟動redis服務 
sudo systemctl enable redis.service #設置開機自啟動 
sudo systemctl disable redis.service #停止開機自啟動 
sudo systemctl status redis.service #查看服務當前狀態 
sudo systemctl restart redis.service  #重新啟動服務 
sudo systemctl list-units --type=service |grep redis #查看所有已啟動的服務

 


五、Redis連接的端口(6379)開放

1、開放端口:

sudo firewall-cmd --zone=public --add-port=6677/tcp --permanent

 

2、讓端口生效:

sudo firewall-cmd --reload

 

3、查看防火牆所有開放的端口

sudo firewall-cmd --zone=public --list-ports

 

 

六、Linux Redis 重啟數據丟失解決方案

在Linux系統設置一個參數(vm.overcommit_memory)即可解決。

步驟如下:

1、編輯 sysctl.conf 配置文件

sudo vi /etc/sysctl.conf 


2、在文件注釋下面另起一行增加參數 vm.overcommit_memory 配置,如下

vm.overcommit_memory = 1

 

3、使配置文件生效

sudo sysctl -p

 

4、測試

#指定IP地址和指定端口啟動
#-h host 指定IP地址
#-p port 指定端口號
/java/redis6/bin/redis-cli -h 127.0.0.1 -p 6677

 

set aa 11

get aa

 

5、重啟服務器

重啟服務器再重復第4步連接redis客戶端查看數據

reboot -f

 

 

(時間寶貴,分享不易,捐贈回饋,^_^)

 

================================

©Copyright 蕃薯耀 2022-01-20

https://www.cnblogs.com/fanshuyao/


免責聲明!

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



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