CentOS7 安裝 redis 並設置開機啟動

  • 2019 年 12 月 13 日
  • 筆記

1.下載

https://redis.io/download  cd /usr/local/src  wget -c http://download.redis.io/releases/redis-3.2.6.tar.gz

2.解壓

cd /usr/local/src  tar xzf redis-3.2.6.tar.gz

3.編譯

cd /usr/local/src/redis-3.2.6make

#若出現錯誤則說明未安裝gcc

yum install gcc && make MALLOC=libccd /usr/local/src/redis-3.2.6 &&  make

4.創建redis目錄

mkdir -p /usr/local/redis

5.複製主要配置文件到新文件夾

cp /usr/local/src/redis-3.2.6/src/redis-server /usr/local/redis/  cp /usr/local/src/redis-3.2.6/src/redis-cli /usr/local/redis/  cp /usr/local/src/redis-3.2.6/redis.conf /usr/local/redis/

6.開啟後台運行

vim /usr/local/redis/redis.conf  修改redis.conf文件中的daemonize no為daemonize yes並保存

7.創建redis命令軟連接

ln -s /usr/local/redis/redis-cli /usr/bin/redis  在任意命令行執行redis

8.添加開機啟動服務

vim /etc/systemd/system/redis-server.service

粘貼以下內容

[Unit]  Description=The redis-server Process Manager  After=syslog.target network.target      [Service]  Type=simple  PIDFile=/var/run/redis_6379.pid  ExecStart=/usr/local/redis/redis-server /usr/local/redis/redis.conf  ExecReload=/bin/kill -USR2 $MAINPID  ExecStop=/bin/kill -SIGINT $MAINPID      [Install]  WantedBy=multi-user.target

9.設置開機啟動

systemctl daemon-reload  systemctl start redis-server.service  systemctl enable redis-server.servic

10.檢查是否安裝成功

ps -A|grep redis

11.連接登錄

#互動式

  #redis-cli -h {host} -p {port}  $redis-cli -h 127.0.0.1 -p 6379  127.0.0.1:6379>set hello world  OK  127.0.0.1:6379>get hello  "world"

#命令式

  #redis-cli -h {host} -p {port} {command}  $redis-cli -h 127.0.0.1-p 6379 get hello  "world"

12.永久設置Redis密碼

vim /etc/redis.conf  #在配置文件中有個參數:requirepass 這個就是配置redis訪問密碼的參數;  #比如 requirepass test123;

需重啟Redis才能生效

13.臨時設置Redis密碼

redis 127.0.0.1:6379> config set requirepass test123

#查詢密碼:

redis 127.0.0.1:6379> config get requirepass  (error) ERR operation not permitted

#密碼驗證:

redis 127.0.0.1:6379> auth test123  OK

#再次查詢:

redis 127.0.0.1:6379> config get requirepass  1) "requirepass"  2) "test123"

14.登陸有密碼的Redis

redis-cli -p 6379 -a test123

15.先登陸後驗證

redis-cli -p 6379  redis 127.0.0.1:6379> auth test123  OK

16.開啟後台運行

將redis.conf中的"daemonize no"為"daemonize yes"並保存,重啟redis生效。