1. 程式人生 > >Centos7安裝Redis並設定遠端訪問

Centos7安裝Redis並設定遠端訪問

下載安裝檔案

wget http://download.redis.io/releases/redis-4.0.2.tar.gz

安裝編譯器

yum install gcc-c++

Redis安裝檔案複製到/usr/local/src目錄下,進入/usr/local/src目錄下,解壓redis安裝檔案

cd /usr/local/src
tar -xzvf redis-4.0.2.tar.gz

進入解壓後的檔案目錄,之後直接編譯即可

cd /usr/local/src/redis-4.0.2

make

建立儲存redis檔案目錄,複製redis-server redis-cli到新建立的資料夾

mkdir -p /usr/local/redis
cp
/usr/local/src/redis-4.0.2/src/redis-server /usr/local/redis/ cp /usr/local/src/redis-4.0.2/src/redis-cli /usr/local/redis/

設定Redis開機自動啟動

進入/usr/local/src/redis-4.0.2的util目錄, 執行./install_server.sh
[[email protected] utils]# ./install_server.sh 
Welcome to the redis service installer
This script will help you easily set up a running redis server

Please select
the redis port for this instance: [6379] Selecting default: 6379 Please select the redis config file name [/etc/redis/6379.conf] Selected default - /etc/redis/6379.conf Please select the redis log file name [/var/log/redis_6379.log] Selected default - /var/log/redis_6379.log Please select the data directory for
this instance [/var/lib/redis/6379] Selected default - /var/lib/redis/6379 # 這個寫你新建的那個目錄的redis-server Please select the redis executable path [] /usr/local/redis/redis-server Selected config: Port : 6379 Config file : /etc/redis/6379.conf Log file : /var/log/redis_6379.log Data dir : /var/lib/redis/6379 Executable : /usr/local/redis/redis-server Cli Executable : /usr/local/redis/redis-cli Is this ok? Then press ENTER to go on or Ctrl-C to abort. Copied /tmp/6379.conf => /etc/init.d/redis_6379 Installing service... Successfully added to chkconfig! Successfully added to runlevels 345! Starting Redis server... Installation successful!
檢視Redis程序
ps -ef|grep redis

Redis開啟遠端訪問

# 查詢Redis配置(注意不是安裝目錄下的redis.conf)
# 開啟第五步設計的Redis配置,預設為:/etc/redis/6379.conf
# 修改配置檔案如下幾項,其它保持不變
daemonize yes
#bind 127.0.0.1 (註釋,不限制IP)
protected-mode no
將 requirepass foobared前的“#”去掉,密碼改為你想要設定的密碼(我設定為123456)

# 重啟服務
[[email protected] redis-3.2.11]# service redis_6379 restart
Stopping ...
Redis stopped
Starting Redis server...

# 開放6379埠
firewall-cmd --zone=public --add-port=6379/tcp --permanent
# 重啟防火牆,否則開放埠不起作用
firewall-cmd --reload

建立redis命令軟連線

ln -s /usr/local/redis/redis-cli /usr/bin/redis
# 這樣就可以輸入進入redis進行控制檯了
[[email protected] ~]# redis
127.0.0.1:6379> auth "123456"
OK
127.0.0.1:6379> 

關閉redis服務

service redis_6379 stop
Stopping ...
OK
(error) NOAUTH Authentication required.
Waiting for Redis to shutdown ...
Waiting for Redis to shutdown ...
Waiting for Redis to shutdown ...
Waiting for Redis to shutdown ...
Waiting for Redis to shutdown ...
Waiting for Redis to shutdown ...
Waiting for Redis to shutdown ...
Waiting for Redis to shutdown ...

## 報錯
解決方法:修改redis服務指令碼,加入如下所示的資訊即可:
vim /etc/init.d/redis_6379
# 修改 新增 -a "password"
$CLIEXEC -a "123456" -p $REDISPORT shutdown
# 關閉redis服務
[[email protected] init.d]# service redis_6379 stop
Stopping ...
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
Redis stopped