准備環境:
1、一台centos7機器,配置沒有什么要求(能聯網)
2、下載好redis壓縮包
下載redis包:
1、登錄redis官網:
2、選擇適合自己用的版本,一定要下載穩定版,不推薦下載最新版本
我這里就下載5.05版本作為案例:
下載其它版本:
其它歷史版本需要在google code上下載
官方的安裝教程:
我這邊直接按照提供操作步驟安裝出現報錯:
解壓安裝包到/usr/local目錄下
進入解壓目錄:
執行編譯:
提示未找到gcc命令:
解決辦法安裝gcc命令:
# yum -y install gcc
gcc安裝完成后從新執行make編譯命令:
又出現報錯:
根據redis4.0的安裝命令添加MALLOC=libc:
# make MALLOC=libc
執行成功:
安裝:
# make install
二、啟動redis的三種方式:
切換到redis的src目錄下:
[root@yzn redis-5.0.5]# cd src/
1、直接啟動redis
[root@yzn src]# ./redis-server 12670:C 08 Jul 2019 15:17:07.387 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo 12670:C 08 Jul 2019 15:17:07.387 # Redis version=5.0.5, bits=64, commit=00000000, modified=0, pid=12670, just started 12670:C 08 Jul 2019 15:17:07.387 # Warning: no config file specified, using the default config. In order to specify a config file use ./redis-server /path/to/redis.conf 12670:M 08 Jul 2019 15:17:07.389 * Increased maximum number of open files to 10032 (it was originally set to 1024). _._ _.-``__ ''-._ _.-`` `. `_. ''-._ Redis 5.0.5 (00000000/0) 64 bit .-`` .-```. ```\/ _.,_ ''-._ ( ' , .-` | `, ) Running in standalone mode |`-._`-...-` __...-.``-._|'` _.-'| Port: 6379 | `-._ `._ / _.-' | PID: 12670 `-._ `-._ `-./ _.-' _.-' |`-._`-._ `-.__.-' _.-'_.-'| | `-._`-._ _.-'_.-' | http://redis.io `-._ `-._`-.__.-'_.-' _.-' |`-._`-._ `-.__.-' _.-'_.-'| | `-._`-._ _.-'_.-' | `-._ `-._`-.__.-'_.-' _.-' `-._ `-.__.-' _.-' `-._ _.-' `-.__.-' 12670:M 08 Jul 2019 15:17:07.391 # WARNING: The TCP backlog setting of 511 cannot be enforced because /proc/sys/net/core/somaxconn is set to the lower value of 128. 12670:M 08 Jul 2019 15:17:07.391 # Server initialized 12670:M 08 Jul 2019 15:17:07.391 # 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. 12670:M 08 Jul 2019 15:17:07.391 # 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. 12670:M 08 Jul 2019 15:17:07.391 * Ready to accept connections
如圖:redis啟動成功,但是這種凡是需要一直打開窗口,不能進行其他操作,不方便
ctrl+c可以關閉程序
2、以后台進程方式啟動redis
第一步:修改redis.conf文件
daemonize參數:
redis.conf配置文件中daemonize守護進程,默認是NO。
daemonize是用來指定redis是否要用守護進程的方式啟動
daemonize設置yes或者no的區別:
daemonize:yes:redis采用的是單進程多線程的模式,當redis.conf中選項daemonize參數為yes時,代表開啟守護進程模式,該模式下,redis會在后台運行,並將進程pid號寫入到redis.conf選項pidfile設置的文件中,此時redis將一直運行,除非手動kill該進程。
daemonize:no:當daemonize選項設置為no時,當前界面將進入redis的命令界面,exit強制退出或者關閉連接工具(putty、xshell等工具)都會導致redis進程退出
redis的配置文件在redis的主目錄下:
文件名:redis.conf
編輯文件:
# vim redis.conf
我們修改為yes:
綁定地址(默認僅主機可連接,修改為任意主機可以連接):
第二步:指定redis.conf文件啟動
[root@yzn redis-5.0.5]# /usr/local/redis-5.0.5/src/redis-server /usr/local/redis-5.0.5/redis.conf
12706:C 08 Jul 2019 15:33:16.232 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
12706:C 08 Jul 2019 15:33:16.233 # Redis version=5.0.5, bits=64, commit=00000000, modified=0, pid=12706, just started
12706:C 08 Jul 2019 15:33:16.233 # Configuration loaded
查看程序是否啟動
這里我們看到已經后台運行了,注意這里監聽的地址為127.0.0.1:6379也就是只能本機連接需要局域網其它設別連接我們需要再修改配置文件修改bind參數
使用kill殺死進程:
# kill 12707
# 注意:這里的12707是進程id號,在ps命令中查到的
3、設置redis開機自啟:
第一步、添加開機啟動服務:
[root@yzn ~]# vim /etc/systemd/system/redis-server.service #文件不存在要編輯創建
[Unit]
Description=The redis-server Process Manager
After=syslog.target network.target
[Service]
Type=forking
PIDFile=/var/run/redis_6379.pid
ExecStart=/usr/local/redis-5.0.5/src/redis-server /usr/local/redis-5.0.5/redis.conf
PrivateTmp=True
[Install]
WantedBy=multi-user.target
設置為開機啟動:
# systemctl daemon-reload
啟動服務
# systemctl start redis-server
# 注:暫時沒有添加暫停服務,后續會添加上