1. 程式人生 > >CentOS 安裝 Redis

CentOS 安裝 Redis

CentOS 安裝 Redis

Redis是一個使用ANSI C編寫的開源、支援網路、基於記憶體、可選永續性的鍵值對儲存資料庫

安裝

1、安裝依賴源

yum install gcc-c++ -y

2、獲取安裝檔案

  • 可以去redis官網下載適合你自己的版本 http://download.redis.io/releases
  • 搭建 redis 叢集要求最低版本為 3.0.0
  • 本文使用 redis-2.8.19 版本作為演示
wget http://download.redis.io/releases/redis-2.8.19.tar.gz

3、解壓檔案

tar -xzvf redis-2.8.19.tar.gz
mv redis-2.8.19 /usr/local/redis

4、切換至程式目錄,並執行編譯

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

5、設定配置檔案路徑

mkdir -p /etc/redis
cp -f redis.conf /etc/redis

6、修改配置檔案

vi /etc/redis/redis.conf
  • 僅修改: 將daemonize no 改為 daemonize yes

7、啟動

/usr/local/bin/redis-server
/etc/redis/redis.conf

8、檢視啟動

ps -ef | grep redis

9、使用客戶端

redis-cli #進入客戶端

>set name aaa #設定鍵值對
OK
>get name #通過 鍵 獲取 值
"aaa"

10、關閉客戶端

redis-cli shutdown

11、開機啟動配置

echo "/usr/local/bin/redis-server /etc/redis/redis.conf &" >> /etc/rc.local

12、遠端連線

redis-cli -h 127.0.0.1 -p 6379
# 遠端服務連線 redis-cli -h 127.0.0.1 -p 6379 shutdown # 遠端服務停止 redis-cli -h 127.0.0.1 -p 6379 -a 123456 # 有許可權控制時(加上-a 密碼)

Ps:

修改redis.conf
1、預設情況Redis不是在後臺執行,我們需要修改把 redis 放在後臺執行:daemonize yes
2、redis 安全策略預設本機訪問,所以遠端訪問的話需要將 bind 127.0.0.1加 # 註釋掉
僅僅註釋掉這些還不行,因為新版本增加了保護模式,具體提示資訊

DENIED Redis is running in protected mode because protected mode is enabled, no bind address was specified, no authentication password is requested to clients. In this mode connections are only accepted from the loopback interface. If you want to connect from external computers to Redis you may adopt one of the following solutions: 1) Just disable protected mode sending the command 'CONFIG SET protected-mode no' from the loopback interface by connecting to Redis from the same host the server is running, however MAKE SURE Redis is not publicly accessible from internet if you do so. Use CONFIG REWRITE to make this change permanent. 2) Alternatively you can just disable the protected mode by editing the Redis configuration file, and setting the protected mode option to 'no', and then restarting the server. 3) If you started the server manually just for testing, restart it with the '--protected-mode no' option. 4) Setup a bind address or an authentication password. NOTE: You only need to do one of the above things in order for the server to start accepting connections from the outside.

  • 可以將保護模式設為 no 來關閉 修改 protected-mode no
  • 雖然可以將保護模式設為 no 來關閉,但為了安全還是推薦設許可權密碼
  • 找到這一行 # requirepass foobared,將 # 去掉並把 foobared 改成你的密碼