一提到Redis,相信大家都不會(huì)感到陌生吧。今天就讓我們?cè)诎⒗镌粕习惭b一下Redis,為以后使用它做個(gè)準(zhǔn)備。
一、下載
1,下載頁(yè)面:
https://redis.io/
2,下載
wget http://download.redis.io/releases/redis-5.0.7.tar.gz
二、解壓
tar -xzvf redis-5.0.7.tar.gz
三,準(zhǔn)備編譯
1, 請(qǐng)?jiān)诓僮髑按_認(rèn)gcc是否已安裝,gcc -v
如未安裝,可以執(zhí)行這個(gè)命令安裝: yum install gcc
2,請(qǐng)?jiān)诓僮髑按_認(rèn)tcl是否已安裝如未安裝,可以執(zhí)行這個(gè)命令安裝: yum install tcl
四,編譯:
[root@localhost source]# cd redis-5.0.7/
[root@localhost redis-5.0.7]# make MALLOC=libc
make 后加 MALLOC的參數(shù)的原因:
避免提示找不到 jemalloc/jemalloc.h
五,測(cè)試編譯:
[root@localhost redis-5.0.7]# make test
如果看到以下字樣:表示無(wú)錯(cuò)誤:\o/ All tests passed without errors!
六、安裝
[root@localhost redis-5.0.7]# mkdir /usr/local/soft/redis5 可分步創(chuàng)建
[root@localhost redis-5.0.7]# cd /usr/local/soft/redis5/
[root@localhost redis5]# mkdir bin
[root@localhost redis5]# mkdir conf
[root@localhost redis5]# cd bin/
find / -name redis-cli 查找文件位置
[root@localhost bin]# cp /root/redis-5.0.7/src/redis-cli ./
[root@localhost bin]# cp /root/redis-5.0.7/src/redis-server ./
[root@localhost bin]# cd ../conf/
[root@localhost conf]# cp /root/redis-5.0.7/redis.conf ./
七、配置:
[root@localhost conf]# vi redis.conf
設(shè)置以下兩個(gè)地方:
# daemonize no
daemonize yes
# maxmemory <bytes>
maxmemory 128MB
說(shuō)明:分別是以daemon方式獨(dú)立運(yùn)行 / 內(nèi)存的最大使用限制
八、運(yùn)行:
[root@localhost conf]# /usr/local/soft/redis5/bin/redis-server /usr/local/soft/redis5/conf/redis.conf
九、檢查端口是否在使用中
[root@localhost conf]# netstat -anp | grep 6379
tcp 0 0 127.0.0.1:6379 0.0.0.0:* LISTEN 16073/redis-server
十、查看redis的當(dāng)前版本:
[root@localhost conf]# /usr/local/soft/redis5/bin/redis-server -v
Redis server v=5.0.7 sha=00000000:0 malloc=libc bits=64 build=8e31d2ed9a4c9593
十一、使redis可以用systemd方式啟動(dòng)和管理
1,編輯service文件
[root@localhost liuhongdi]# vim /lib/systemd/system/redis.service
2,service文件內(nèi)容:
[Unit]Description=RedisAfter=network.target
[Service]Type=forkingPIDFile=/var/run/redis_6379.pidExecStart=/usr/local/soft/redis5/bin/redis-server /usr/local/soft/redis5/conf/redis.confExecReload=/bin/kill -s HUP $MAINPIDExecStop=/bin/kill -s QUIT $MAINPIDPrivateTmp=true
[Install]WantedBy=multi-user.target
3.重載系統(tǒng)服務(wù)
[root@localhost liuhongdi]# systemctl daemon-reload
4,用來(lái)管理redis
啟動(dòng)
systemctl start redis
查看狀態(tài)
systemctl status redis
使開(kāi)機(jī)啟動(dòng)
systemctl enable redis
十二、查看本地centos的版本:
[root@localhost lib]# cat /etc/redhat-release
CentOS Linux release 8.1.1911 (Core)
十三、客戶(hù)端連接redis
1、阿里云得設(shè)置redis.conf中的bind 后跟著的127.0.0.1修改為0.0.0.0,重啟redis
2、開(kāi)放端口:開(kāi)放服務(wù)器的端口號(hào),步驟如下:
打開(kāi)實(shí)例列表,點(diǎn)擊“ 更多”按鈕,選擇“ 網(wǎng)絡(luò)和安全組 ”中的“安全組配置”,選擇 “安全組列表”tab頁(yè)面,點(diǎn)擊 “配置規(guī)則”按鈕,點(diǎn)擊 “快速添加”按鈕,勾選“Redis(6379)”,點(diǎn)擊 “確定”之后就可以正常連接了。
3、給redis設(shè)置連接密碼:
查找到# requirepass foobared 注釋去掉并寫(xiě)入要設(shè)置的密碼,例如: requirepass 123456
redis啟動(dòng)之后測(cè)試是否可以連接命令
./redis-cli -h 127.0.0.1 -p 6379
127.0.0.1:6379> auth 123456//此處是你的密碼
注意:如果是阿里云的話一定要設(shè)置密碼,否則很可能被礦機(jī)程序注入定時(shí)任務(wù),用你的服務(wù)器挖礦,阿里云一直會(huì)有信息提示你。
想了解更多學(xué)習(xí)知識(shí),請(qǐng)關(guān)注gzh“阿Q說(shuō)”!你也可以后臺(tái)留言說(shuō)出你的疑惑,阿Q將會(huì)在后期的文章中為你解答。每天學(xué)習(xí)一點(diǎn)點(diǎn),每天進(jìn)步一點(diǎn)點(diǎn)。