雲服務器配置CentOS 7.6(三)--安裝Redis

1.準備工作

1.1 安裝gcc依賴

yum -y install gcc

[root@instance-6uz2q7zs home]# yum -y install gcc
Loaded plugins: langpacks, versionlock
Resolving Dependencies
--> Running transaction check
---> Package gcc.x86_64 0:4.8.5-39.el7 will be installed
--> Processing Dependency: libgomp = 4.8.5-39.el7 for package: gcc-4.8.5-39.el7.x86_64
--> Processing Dependency: cpp = 4.8.5-39.el7 for package: gcc-4.8.5-39.el7.x86_64
--> Processing Dependency: libgcc >= 4.8.5-39.el7 for package: gcc-4.8.5-39.el7.x86_64
--> Processing Dependency: libmpfr.so.4()(64bit) for package: gcc-4.8.5-39.el7.x86_64
--> Processing Dependency: libmpc.so.3()(64bit) for package: gcc-4.8.5-39.el7.x86_64
--> Running transaction check
---> Package cpp.x86_64 0:4.8.5-39.el7 will be installed
---> Package libgcc.x86_64 0:4.8.5-36.el7 will be updated
---> Package libgcc.x86_64 0:4.8.5-39.el7 will be an update
---> Package libgomp.x86_64 0:4.8.5-36.el7 will be updated
---> Package libgomp.x86_64 0:4.8.5-39.el7 will be an update
---> Package libmpc.x86_64 0:1.0.1-3.el7 will be installed
---> Package mpfr.x86_64 0:3.1.1-4.el7 will be installed
--> Finished Dependency Resolution

Dependencies Resolved

==================================================================================================================
 Package                   Arch                     Version                          Repository              Size
==================================================================================================================
Installing:
 gcc                       x86_64                   4.8.5-39.el7                     base                    16 M
Installing for dependencies:
 cpp                       x86_64                   4.8.5-39.el7                     base                   5.9 M
 libmpc                    x86_64                   1.0.1-3.el7                      base                    51 k
 mpfr                      x86_64                   3.1.1-4.el7                      base                   203 k
Updating for dependencies:
 libgcc                    x86_64                   4.8.5-39.el7                     base                   102 k
 libgomp                   x86_64                   4.8.5-39.el7                     base                   158 k

Transaction Summary
==================================================================================================================
Install  1 Package  (+3 Dependent packages)
Upgrade             ( 2 Dependent packages)

Total download size: 23 M
Downloading packages:
No Presto metadata available for base
(1/6): cpp-4.8.5-39.el7.x86_64.rpm                                                         | 5.9 MB  00:00:00     
(2/6): libgcc-4.8.5-39.el7.x86_64.rpm                                                      | 102 kB  00:00:00     
(3/6): libgomp-4.8.5-39.el7.x86_64.rpm                                                     | 158 kB  00:00:00     
(4/6): libmpc-1.0.1-3.el7.x86_64.rpm                                                       |  51 kB  00:00:00     
(5/6): gcc-4.8.5-39.el7.x86_64.rpm                                                         |  16 MB  00:00:00     
(6/6): mpfr-3.1.1-4.el7.x86_64.rpm                                                         | 203 kB  00:00:00     
------------------------------------------------------------------------------------------------------------------
Total                                                                              60 MB/s |  23 MB  00:00:00     
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
Warning: RPMDB altered outside of yum.
  Installing : mpfr-3.1.1-4.el7.x86_64                                                                        1/8 
  Installing : libmpc-1.0.1-3.el7.x86_64                                                                      2/8 
  Installing : cpp-4.8.5-39.el7.x86_64                                                                        3/8 
  Updating   : libgomp-4.8.5-39.el7.x86_64                                                                    4/8 
  Updating   : libgcc-4.8.5-39.el7.x86_64                                                                     5/8 
  Installing : gcc-4.8.5-39.el7.x86_64                                                                        6/8 
  Cleanup    : libgomp-4.8.5-36.el7.x86_64                                                                    7/8 
  Cleanup    : libgcc-4.8.5-36.el7.x86_64                                                                     8/8 
  Verifying  : mpfr-3.1.1-4.el7.x86_64                                                                        1/8 
  Verifying  : libgcc-4.8.5-39.el7.x86_64                                                                     2/8 
  Verifying  : libgomp-4.8.5-39.el7.x86_64                                                                    3/8 
  Verifying  : libmpc-1.0.1-3.el7.x86_64                                                                      4/8 
  Verifying  : cpp-4.8.5-39.el7.x86_64                                                                        5/8 
  Verifying  : gcc-4.8.5-39.el7.x86_64                                                                        6/8 
  Verifying  : libgomp-4.8.5-36.el7.x86_64                                                                    7/8 
  Verifying  : libgcc-4.8.5-36.el7.x86_64                                                                     8/8 

Installed:
  gcc.x86_64 0:4.8.5-39.el7                                                                                       

Dependency Installed:
  cpp.x86_64 0:4.8.5-39.el7            libmpc.x86_64 0:1.0.1-3.el7            mpfr.x86_64 0:3.1.1-4.el7           

Dependency Updated:
  libgcc.x86_64 0:4.8.5-39.el7                            libgomp.x86_64 0:4.8.5-39.el7                           

Complete!

2.下載Redis

Redis官方下載鏈接
下載安裝包:

wget http://download.redis.io/releases/redis-5.0.7.tar.gz

在這裏插入圖片描述

3.解壓/編譯/安裝

  • 先準備個文件夾 /usr/local/redis 將解壓後的文件存放在該目錄

tar -zxvf redis-5.0.7.tar.gz -C /usr/local/redis

在這裏插入圖片描述

  • 等待解壓完成 cd 到/usr/local/redis目錄下 查看解壓後的文件

在這裏插入圖片描述

  • cd到redis-4.0.9目錄下 運行編譯命令
make MALLOC=libc

在這裏插入圖片描述

  • 等待編譯完成 cd 到redis-4.0.9下的src目錄 運行安裝命令
 make install

在這裏插入圖片描述

  • 等待安裝完成 會自動在/usr/local/bin目錄下生成redis相關的可執行文件 可以cd到該目錄下查看一下
    在這裏插入圖片描述

4. 啓動Redis(臨時方式)

我們以後也可以在該目錄下用./redis-server來啓動redis 也可以在剛剛的src目錄下運行./redis-server
無論用哪個目錄下的./redis-server都可以啓動redis 我們來試一下

/usr/local/bin目錄 啓動redis【./redis-server】
/usr/local/redis/redis-5.0.7/src 目錄啓動redis 【./redis-server】

[root@instance-6uz2q7zs bin]# ./redis-server
15886:C 02 Feb 2020 14:53:32.730 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
15886:C 02 Feb 2020 14:53:32.730 # Redis version=5.0.7, bits=64, commit=00000000, modified=0, pid=15886, just started
15886:C 02 Feb 2020 14:53:32.730 # Warning: no config file specified, using the default config. In order to specify a config file use ./redis-server /path/to/redis.conf
                _._                                                  
           _.-``__ ''-._                                             
      _.-``    `.  `_.  ''-._           Redis 5.0.7 (00000000/0) 64 bit
  .-`` .-```.  ```\/    _.,_ ''-._                                   
 (    '      ,       .-`  | `,    )     Running in standalone mode
 |`-._`-...-` __...-.``-._|'` _.-'|     Port: 6379
 |    `-._   `._    /     _.-'    |     PID: 15886
  `-._    `-._  `-./  _.-'    _.-'                                   
 |`-._`-._    `-.__.-'    _.-'_.-'|                                  
 |    `-._`-._        _.-'_.-'    |           http://redis.io        
  `-._    `-._`-.__.-'_.-'    _.-'                                   
 |`-._`-._    `-.__.-'    _.-'_.-'|                                  
 |    `-._`-._        _.-'_.-'    |                                  
  `-._    `-._`-.__.-'_.-'    _.-'                                   
      `-._    `-.__.-'    _.-'                                       
          `-._        _.-'                                           
              `-.__.-'                                               

15886:M 02 Feb 2020 14:53:32.731 # WARNING: The TCP backlog setting of 511 cannot be enforced because /proc/sys/net/core/somaxconn is set to the lower value of 128.
15886:M 02 Feb 2020 14:53:32.731 # Server initialized
15886:M 02 Feb 2020 14:53:32.731 # 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.
15886:M 02 Feb 2020 14:53:32.732 # 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.
15886:M 02 Feb 2020 14:53:32.732 * Ready to accept connections

如上圖 redis就成功啓動了 當然這種並不是後臺啓動(守護線程)的方式 因爲退出該窗口redis就關閉了 接下來我來教大家如何以守護線程啓動redis

5.守護線程(後臺啓動)方式啓動redis

首先按ctrl+c退出該窗口 我們cd 到redis的解壓目錄下 我們可以看到有個redis.conf文件 這個文件是redis的配置文件
在這裏插入圖片描述
vim 打開redis.conf文件
在這裏插入圖片描述
我們可以看到有個daemonize配置項 這個就是後臺啓動的意思 將no改成yes即可 修改完之後 保存退出(修改配置文件並保存退出這裏我就不介紹了哈)

5.1 啓動Redis(方式1)

這時候我們再去啓動 要知道啓動文件在src目錄下 cd 到src目錄 運行如下命令

./redis-server ../redis.conf

運行該目錄下的啓動文件redis-server 再將上級目錄下的redis.conf配置文件帶進去 就可成功後臺啓動redis了
在這裏插入圖片描述

5.1 啓動Redis(方式2)

cd redis-5.0.7目錄下,將redis.conf文件複製到/usr/local/bin目錄下啓動redis

 cp redis.conf  /usr/local/bin

啓動Redis

[root@zzy bin]# ./redis-server redis.conf
32483:C 25 Feb 2020 12:41:01.729 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
32483:C 25 Feb 2020 12:41:01.729 # Redis version=5.0.7, bits=64, commit=00000000, modified=0, pid=32483, just started
32483:C 25 Feb 2020 12:41:01.729 # Configuration loaded
[root@zzy bin]# pwd
/usr/local/bin

修改 /usr/local/bin下的redis.conf配置文件可以設置

  • 守護線程啓動
  • 遠程鏈接redis
  • 設置redis密碼

在這裏插入圖片描述
這時候 我們可以去看一下redis是否啓動成功
在這裏插入圖片描述
如上圖 redis我們成功後臺啓動了


到此完成

若關閉redis 可以直接 kill -9 16582

6 遠程連接redis

6.1 修改redis配置文件

找到redis的安裝目錄
在這裏插入圖片描述

vim  redis.conf 
bind  0.0.0.0 
protected-mode   no

在這裏插入圖片描述
關閉redis後重啓redis
在這裏插入圖片描述

遠程連接Redis 成功
在這裏插入圖片描述

7. 設置Redis密碼

在服務器上,這裏以linux服務器爲例,爲redis配置密碼。

7.1 第一種方式 (臨時方式)

(當前這種linux配置redis密碼的方法是一種臨時的,如果redis重啓之後密碼就會失效)

(1)首先進入redis,如果沒有開啓redis則需要先開啓:
[root@iZ94jzcra1hZ bin]# redis-cli -p 6379
127.0.0.1:6379> 
(2)查看當前redis有沒有設置密碼:
127.0.0.1:6379> config get requirepass
1) "requirepass"
2) ""
(3)爲以上顯示說明沒有密碼,那麼現在來設置密碼:
127.0.0.1:6379> config set requirepass abcdefg
OK
127.0.0.1:6379> 
(4)再次查看當前redis就提示需要密碼:
127.0.0.1:6379> config get requirepass
(error) NOAUTH Authentication required.
127.0.0.1:6379>

7.2 第二種方式 (永久方式)

需要永久配置密碼的話就去redis.conf的配置文件中找到requirepass這個參數,如下配置:

修改redis.conf配置文件

vim redis.conf

查找requirepass foobared(Esc :/requirepass foobared),把前面的註釋去掉,
將foobared改成自己的密碼

#requirepass foobared
requirepass 123   指定密碼123
保存後重啓redis就可以了

7.3 redis-cli連接redis

[root@iZ2ze3zda3caeyx6pn7c5zZ bin]# redis-cli
127.0.0.1:6379> keys *
(error) NOAUTH Authentication required.
#使用auth password驗證密碼
127.0.0.1:6379> auth 123        //指定密碼
OK
127.0.0.1:6379> keys *
1) "a"
2) "cit"
3) "clist"
4) "1"
#客戶端使用config get requirepass命令查看密碼
127.0.0.1:6379> config get requirepass
1) "requirepass"
2) "123"

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章