Linux系統(tǒng)安裝Redis(2020最新最詳細)
2020最新Linux系統(tǒng)發(fā)行版ContOS7演示安裝Redis
為防止操作權(quán)限不足蛔钙,建議切換root用戶,當然如果你對Linux命令熟悉企垦,能夠自主完成權(quán)限更新操作颜价,可以不考慮此推薦涯保。
更多命令學習推薦:
Linux安裝JDK分為兩種:手動安裝和yum安裝。
1周伦、下載Redis安裝程序包
Redis官方網(wǎng)站手動下載夕春,或命令下載:(也可以選擇Redis中文網(wǎng)下載),或直接參考Redis官網(wǎng)下載安裝說明
1[xsge@localhost downfiles]$wget http://download.redis.io/releases/redis-6.0.8.tar.gz
如果是手動下載横辆,請使用XFTP將文件上傳至Linux系統(tǒng)中撇他。
Reids官網(wǎng)版本更新早與Redis中文網(wǎng)!1吩椤困肩!
查看文件:ll
解壓文件:tar -xvf redis-6.0.8.tar.gz
1[xsge@localhost downfiles]$ ll ####查看文件2總用量1906163-rw-rw-r--.1xsge xsge1128287910月622:33apache-tomcat-9.0.39.tar.gz4-rw-r--r--.1root root16644月82020CentOS-Base.repo.backup5-rw-rw-r--.1xsge xsge413110月1511:25jdk-8u261-linux-x64.tar.gz6-rw-rw-r--.1xsge xsge260244月252019mysql80-community-release-el7-3.noarch.rpm7-rw-rw-r--.1xsge xsge1816138086月318:08mysql-community-server-5.7.31-1.el7.x86_64.rpm8-rw-rw-r--.1xsge xsge22475289月1019:11redis-6.0.8.tar.gz9[xsge@localhost downfiles]$ tar -xvf redis-6.0.8.tar.gz ####解壓Redis安裝包
解壓redis-6.0.8.tar.gz ,并把redis-6.0.8目錄移動到/usr/local目錄(或者opt目錄:通常程序文件放置在/opt目錄下)
1[root@localhost downfiles]#mvredis-6.0.8/usr/local/redis-6.x #推薦(如果redis-6.x目錄不存在脆侮,請進入local目錄新建:mkdir? redis-6.x)
或者
2[root@localhost downfiles]#mvredis-6.0.8/opt
2锌畸、安裝
1、環(huán)境安裝
Redis是C語言開發(fā)靖避,安裝Redis需要先將Redis的源碼進行編譯潭枣,編譯依賴gcc環(huán)境。因此需要安裝GCC幻捏,安裝過程中有提示時盆犁,輸入Y即可。(make命令必須在Redis程序目錄下執(zhí)行)
####安裝GCC環(huán)境
[root@localhost ~]#yuminstallgcc-c++####查看GCC版本(ContOS7默認安裝4.8.5版本)
[root@localhost ~]#gcc-v
####進入Redis程序目錄
cd? /usr/local/redis-6.x####make命令篡九,下載環(huán)境并編譯Redis程序(CentOS7執(zhí)行會報錯)make
常見錯誤如下圖:原因是因為gcc版本過低谐岁,yum安裝的gcc是4.8.5的。因此需要升級gcc榛臼。升級gcc到5.3以上版本伊佃。(如果沒有此錯誤忽略不計)
CentOS7安裝有默認GCC環(huán)境,默認4.8.5版本沛善!編譯 redis-6.x航揉,要求 C5.3以上 編譯器,否則會遇到大量的錯誤金刁。主要原因是從 redis-6.x 開始的多線程代碼依賴C標準庫中的新增類型 _Atomic 帅涂。但是注意 gcc 從 4.9 版本才開始正式和完整地支持 stdatomic(gcc-4.8.5 部分支持)。centos7默認的 gcc 版本為:4.8.5 < 5.3 無法編譯
2尤蛮、升級GCC環(huán)境
雖然此次編譯出錯漠秋,但是仍有一些文件已經(jīng)編譯生成,所以建議清除已經(jīng)編譯的文件抵屿。(如果沒有編譯出錯庆锦,忽略不計)(make命令必須在Redis程序目錄下執(zhí)行)
1# 當Redis編譯出錯時,使用命令清除已編譯生成的文件(不執(zhí)行也可以:不推薦)? ? ? 2make? distclean 3####升級GCC環(huán)境為9版本 4yum-yinstallcentos-release-scl 5yum-yinstalldevtoolset-9-gccdevtoolset-9-gcc-c++? devtoolset-9-binutils 6 7-------環(huán)境設(shè)置---------- 8#臨時有效轧葛,退出 shell 或重啟會恢復原gcc 版本 9scl enable devtoolset-9 bash1011#長期有效12echo"source /opt/rh/devtoolset-9/enable">>/etc/profile
參考:GCC源碼地址里面有GCC的各個版本搂抒,進入Redis中文網(wǎng)評論里也有網(wǎng)友說明GCC升級方式方法。
2尿扯、Redis安裝
參考官網(wǎng)安裝說明求晶!
1、進入Redis程序目錄:cd/usr/local/redis-6.x/
2衷笋、編譯Redis程序:make
3芳杏、安裝Redis:make install
1####進入Redis程序目錄。 2[root@localhost redis-6.0.8]# cd? /usr/local/redis-6.x/redis-6.0.8 3 4####執(zhí)行Redis程序編譯 5[root@localhost redis-6.0.8]#make 6 7####執(zhí)行安裝 8[root@localhost redis-6.0.8]#makeinstall##將Redis安裝在Linux默認位置:/usr/local/bin 9或者(推薦)10[root@localhost redis-6.0.8]#makeinstallPREFIX=/usr/local/redis-6.x ##將Redis安裝在指定位置: /usr/local/redis-6.x/bin? (bin是安裝自動生成的)1112####進入redis-6.x目錄13[root@localhost redis-6.0.8]# cd? /usr/local/redis-6.x14####查看所有文件15[root@localhost redis6.x]#ls16bin? redis-6.0.817####進入bin目錄:查看安裝的Redis命令18[root@localhost redis6.x]# cd bin19[root@localhost bin]# ll20總用量4608821-rwxr-xr-x.1root root639912010月2014:50redis-benchmark22-rwxr-xr-x.1root root1135196810月2014:50redis-check-aof23-rwxr-xr-x.1root root1135196810月2014:50redis-check-rdb24-rwxr-xr-x.1root root672720010月2014:50redis-cli25lrwxrwxrwx.1root root1210月2014:50redis-sentinel -> redis-server26-rwxr-xr-x.1root root1135196810月2014:50redis-server
4、配置Redis爵赵。
復制redis核心配置文件在安裝命令目錄中(默認位置在/usr/local/bin)吝秕。我在安裝時選擇了安裝在指定位置(/usr/local/redis-6.x/bin),redis配置文件需要放在名錄目錄下空幻。
說明redis配置文件:在redis程序目錄中
1####進入redis安裝目錄bin 2[root@localhost bin]# cd /usr/local/redis6.x/bin/ 3####創(chuàng)建目錄conf:用于存放配置文件 4[root@localhost bin]#mkdir conf 5####查看創(chuàng)建結(jié)果 6[root@localhost bin]# ll 7總用量46088 8drwxr-xr-x.2root root610月2015:10 conf 9-rwxr-xr-x.1root root639912010月2014:50redis-benchmark10-rwxr-xr-x.1root root1135196810月2014:50redis-check-aof11-rwxr-xr-x.1root root1135196810月2014:50redis-check-rdb12-rwxr-xr-x.1root root672720010月2014:50redis-cli13lrwxrwxrwx.1root root1210月2014:50redis-sentinel -> redis-server14-rwxr-xr-x.1root root1135196810月2014:50redis-server15####復制Redis程序目錄中的配置文件放入到redis命令所在conf目錄中16[root@localhost bin]#cp/usr/local/redis6.x/redis-6.0.8/redis.conf /usr/local/redis6.x/bin/conf/17####進入redis命令conf目錄18[root@localhost bin]# cd conf/19####查看配置文件20[root@localhost conf]# ll2122-rw-r--r--.1root root8464210月2015:10redis.conf
3烁峭、Redis啟動測試
1、進入Redis安裝目錄bin
2秕铛、使用命令啟動Redis服務(wù)(測試Redis服務(wù)會占用一個窗口)
1####進入redis命令目錄 2[root@localhost ~]# cd /usr/local/redis6.x/bin/ 3####啟動reidis服務(wù):并指定使用的配置文件 4[root@localhost bin]# ./redis-server conf/redis.conf 52414:C20Oct202015:53:10.901 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo 62414:C20Oct202015:53:10.901# Redis version=6.0.8, bits=64, commit=00000000, modified=0, pid=2414, just started 72414:C20Oct202015:53:10.901 # Configuration loaded 82414:M20Oct202015:53:10.902* Increased maximum number of open files to10032(it was originally set to1024). 9? ? ? ? ? ? ? ? _._? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? 10_.-``__''-._? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? 11_.-``? ? `.? `_.''-._? ? ? ? ? Redis6.0.8(00000000/0)64 bit12.-`` .-```.? ```\/? ? _.,_''-._? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? 13('? ? ? ,? ? ? .-`? | `,? ? )? ? Running in standalone mode14|`-._`-...-` __...-.``-._|'` _.-'|? ? Port:637915|? ? `-._? `._? ? /? ? _.-'? ? |? ? PID: 241416`-._? ? `-._? `-./? _.-'? ? _.-'17|`-._`-._? ? `-.__.-'? ? _.-'_.-'|? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? 18|? ? `-._`-._? ? ? ? _.-'_.-'|? ? ? ? ? http://redis.io? ? ? ? 19`-._? ? `-._`-.__.-'_.-'_.-'20|`-._`-._? ? `-.__.-'? ? _.-'_.-'|? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? 21|? ? `-._`-._? ? ? ? _.-'_.-'|22`-._? ? `-._`-.__.-'_.-'_.-'23`-._? ? `-.__.-'? ? _.-'24`-._? ? ? ? _.-'25`-.__.-'
3约郁、新開連接窗口,啟動客戶端連接redis服務(wù)器
1####進入reids命令目錄 2[root@localhost ~]# cd /usr/local/redis6.x/bin/ 3####啟動客戶端連接服務(wù)器:并指定端口 4[root@localhost bin]# ./redis-cli -p6379 5####測試連接 6127.0.0.1:6379>ping 7PONG 8####停止Redis服務(wù)但两,并斷開連接 9127.0.0.1:6379> shutdown10not connected> exit #### 退出redis
11 [root@localhost bin]#
4鬓梅、配置Redis為后臺啟動
以上的Redis安裝和啟動可以算是臨時服務(wù),當開啟服務(wù)后谨湘,Redis服務(wù)窗口是無法在使用的己肮,可以將Redis服務(wù)設(shè)置為后臺啟動服務(wù),避免Linux窗口連接的浪費悲关。
1谎僻、VIM編譯Redis配置文件redis.conf
2、開啟Redis后臺服務(wù)(默認是關(guān)閉的)
步驟1:vim? ?/usr/local/redis6.x/bin/conf/redis.conf #打開編輯
(鍵入命令 :set nu 顯示行號)
步驟2:輸入i寓辱、I艘绍、a等進入插入模式
步驟3:將daemonize no 改成daemonize yes(表示開啟redis后臺服務(wù):約225行)
步驟4:按ESC退出插入模式,輸入:wq 保存退出
? 3秫筏、測試
1####啟動redis服務(wù)2[root@localhost bin]# ./redis-server conf/redis.conf? 3####此時redis服務(wù)已經(jīng)作為后臺服務(wù)啟動诱鞠,不再占用窗口,直接在本窗口啟用客戶端測試連接4[root@localhost bin]# ./redis-cli -p63795127.0.0.1:6379>ping6PONG7127.0.0.1:6379>
4这敬、查看Redis服務(wù)進程
1####查看進程服務(wù)航夺,過濾獲取redis的2[root@localhost bin]#ps-ef |grep redis3root28111016:23?00:00:00./redis-server127.0.0.1:63794root28242352016:23pts/100:00:00./redis-cli -p63795root28541769016:27pts/000:00:00grep--color=auto redis6[root@localhost bin]#
5、Redis的關(guān)閉
Redis客戶端關(guān)閉或進程關(guān)閉(pkill redis)
1[root@localhost bin]# ./redis-cli -p6379 ##啟動客戶端連接Redis服務(wù)2127.0.0.1:6379>ping3PONG4127.0.0.1:6379> shutdown ##關(guān)閉服務(wù)端連接5not connected>exit ##退出
或者
6[root@localhost bin]# pkill redis ##結(jié)束Redis進程
?5崔涂、卸載
刪除Redis安裝和Redis解壓的文件即可
1#刪除安裝目錄2rm-rf? /usr/local/redis-6.x/redis-6.0.83#刪除所有redis相關(guān)命令腳本4rm-rf? /usr/local/redis-6.x/bin/redis-*