1. 程式人生 > 資料庫 >CentOS7安裝redis穩定版

CentOS7安裝redis穩定版

偶數為穩定版:例如1.0,1.2,1.4

下載並安裝:
wget http://download.redis.io/releases/redis-5.0.10.tar.gz
tar -xf redis-5.0.10.tar.gz
mv redis-5.0.10 /usr/local/redis
cd /usr/local/redis/
yum -y install gcc
make MALLOC=libc
make PREFIX=/usr/local/redis install
#沒有gcc無法安裝上面兩步

臨時啟動:
[root@localhost redis]# cd bin/
[root@localhost bin]# ./redis-server 

3653:C 27 Dec 2020 22:59:12.858 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
3653:C 27 Dec 2020 22:59:12.858 # Redis version=5.0.10, bits=64, commit=00000000, modified=0, pid=3653, just started
3653:C 27 Dec 2020 22:59:12.858 # Warning: no config file specified, using the default config. In order to specify a config file use ./redis-server /path/to/redis.conf
3653:M 27 Dec 2020 22:59:12.859 * Increased maximum number of open files to 10032 (it was originally set to 1024).
                _._                                                  
           _.-``__ ''-._                                             
      _.-``    `.  `_.  ''-._           Redis 5.0.10 (00000000/0) 64 bit
  .-`` .-```.  ```\/    _.,_ ''-._                                   
 (    '      ,       .-`  | `,    )     Running in standalone mode
 |`-._`-...-` __...-.``-._|'` _.-'|     Port: 6379
 |    `-._   `._    /     _.-'    |     PID: 3653
  `-._    `-._  `-./  _.-'    _.-'                                   
 |`-._`-._    `-.__.-'    _.-'_.-'|                                  
 |    `-._`-._        _.-'_.-'    |           http://redis.io        
  `-._    `-._`-.__.-'_.-'    _.-'                                   
 |`-._`-._    `-.__.-'    _.-'_.-'|                                  
 |    `-._`-._        _.-'_.-'    |                                  
  `-._    `-._`-.__.-'_.-'    _.-'                                   
      `-._    `-.__.-'    _.-'                                       
          `-._        _.-'                                           
              `-.__.-'                                               

3653:M 27 Dec 2020 22:59:12.861 # WARNING: The TCP backlog setting of 511 cannot be enforced because /proc/sys/net/core/somaxconn is set to the lower value of 128.
3653:M 27 Dec 2020 22:59:12.861 # Server initialized
3653:M 27 Dec 2020 22:59:12.861 # WARNING overcommit_memory is set to 0! Background save may fail under low memory condition. To fix this issue add 'vm.overcommit_memory = 1' to /etc/sysctl.conf and then reboot or run the command 'sysctl vm.overcommit_memory=1' for this to take effect.
3653:M 27 Dec 2020 22:59:12.861 # WARNING you have Transparent Huge Pages (THP) support enabled in your kernel. This will create latency and memory usage issues with Redis. To fix this issue run the command 'echo never > /sys/kernel/mm/transparent_hugepage/enabled' as root, and add it to your /etc/rc.local in order to retain the setting after a reboot. Redis must be restarted after THP is disabled.
3653:M 27 Dec 2020 22:59:12.861 * Ready to accept connections


編輯systemctl服務配置檔案並設定開機自啟:
vim /usr/lib/systemd/system/redis.service

[Unit]
Description=Redis
After=network.target
 
[Service]
Type=forking
ExecStart=/usr/local/redis/bin/redis-server /usr/local/redis/redis.conf
ExecReload=/bin/kill -s HUP $MAINPID
ExecStop=/bin/kill -s QUIT $MAINPID
PrivateTmp=true
 
[Install]
WantedBy=multi-user.target


[root@localhost redis]# vim redis.conf
#136行修改為:daemonize yes            


載入系統服務:
systemctl daemon-reload
systemctl enable redis
systemctl start redis

[root@localhost redis]# systemctl status redis
● redis.service - Redis
   Loaded: loaded (/usr/lib/systemd/system/redis.service; disabled; vendor preset: disabled)
   Active: active (running) since Sun 2020-12-27 23:05:46 EST; 1min 32s ago
  Process: 3877 ExecStart=/usr/local/redis/bin/redis-server /usr/local/redis/bin/redis.conf (code=exited, status=0/SUCCESS)
 Main PID: 3878 (redis-server)
   CGroup: /system.slice/redis.service
           └─3878 /usr/local/redis/bin/redis-server 127.0.0.1:6379

Dec 27 23:05:46 localhost.localdomain systemd[1]: Starting Redis...
Dec 27 23:05:46 localhost.localdomain redis-server[3877]: 3877:C 27 Dec 2020 23:05:46.471 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
Dec 27 23:05:46 localhost.localdomain redis-server[3877]: 3877:C 27 Dec 2020 23:05:46.471 # Redis version=5.0.10, bits=64, commit=00000000, modified=0, pid=387...t started
Dec 27 23:05:46 localhost.localdomain redis-server[3877]: 3877:C 27 Dec 2020 23:05:46.471 # Configuration loaded
Dec 27 23:05:46 localhost.localdomain systemd[1]: Started Redis.
Hint: Some lines were ellipsized, use -l to show in full.


最後給經常用到的redis命令列工具redis-cli做一個軟連線:
ln -s /usr/local/redis/bin/redis-cli /usr/local/bin/redis-cli
#如果不管用就寫到系統環境變數裡面vim /etc/profile
export PATH=$PATH:/usr/local/redis/bin
記得重新載入:source /etc/profile


連線redis:
[root@localhost redis]# redis-cli 
127.0.0.1:6379> exit


客戶端管理工具使用ip連線redis服務:

vim /usr/local/redis/bin/redis.conf

69行註釋掉:bind 127.0.0.1
88行修改引數: protected-mode no

重啟redis即可:systemctl restart redis.service


以下為redis.conf檔案中內容:
daemonize yes                      # 修改原值no為yes, 用來配置redis為後臺駐留
#bind 127.0.0.1                  # 註釋掉該行, 使得redis可遠端連結
protected-mode no              # 修改為no, 禁用保護模式


設定登入密碼:
[root@localhost ~]# redis-cli 
127.0.0.1:6379> config set requirepass 123456
OK
127.0.0.1:6379> exit
[root@localhost ~]# redis-cli -h 127.0.0.1 -p 6379 -a 123456
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
127.0.0.1:6379> ping
PONG
127.0.0.1:6379> exit

修改redis.conf配置檔案:
找到#requirepass foobared去掉註釋的#,把foobared替換成自己的密碼即可。 

最後重啟redis服務。