在CentOs7上yum安装redis

在开始安装步骤之前,先把我的CentOs和redis版本号列出来:

# 系统/软件 版本号
1

CentOS7 

CentOS Linux release 7.2.1511 (Core)
2 redis redis.x86_64 0:3.2.12-2.el7

然后确保系统能上网,在我的操作系统下可以这样做:到Gnome图形桌面状态,点右上角的开关按钮,将PCI Ethernet选择为connect状态。

别看下面貌似有大段文字和许多步骤,其实真要操作的不多,蓝字才是关键。

第一步:进行yum安装redis

[root@localhost ~]# yum install redis
Loaded plugins: fastestmirror, langpacks
Loading mirror speeds from cached hostfile
 * base: mirrors.huaweicloud.com
 * extras: mirrors.huaweicloud.com
 * updates: mirrors.neusoft.edu.cn
No package redis available.
Error: Nothing to do

看到Nothing to do不要紧,只要先安装epel-release就好了。

root@localhost ~]# yum install epel-release
Loaded plugins: fastestmirror, langpacks
Loading mirror speeds from cached hostfile
 * base: mirrors.huaweicloud.com
 * extras: mirrors.huaweicloud.com
 * updates: mirrors.neusoft.edu.cn
Resolving Dependencies
--> Running transaction check
---> Package epel-release.noarch 0:7-11 will be installed
--> Finished Dependency Resolution

Dependencies Resolved

=======================================================================================
 Package                  Arch               Version          Repository          Size
=======================================================================================
Installing:
 epel-release             noarch             7-11             extras              15 k

Transaction Summary
=======================================================================================
Install  1 Package

Total download size: 15 k
Installed size: 24 k
Is this ok [y/d/N]: y
Downloading packages:
warning: /var/cache/yum/x86_64/7/extras/packages/epel-release-7-11.noarch.rpm: Header V3 RSA/SHA256 Signature, key ID f4a80eb5: NOKEY
Public key for epel-release-7-11.noarch.rpm is not installed
epel-release-7-11.noarch.rpm                                    |  15 kB  00:00:00     
Retrieving key from file:///etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-7
Importing GPG key 0xF4A80EB5:
 Userid     : "CentOS-7 Key (CentOS 7 Official Signing Key) <security@centos.org>"
 Fingerprint: 6341 ab27 53d7 8a78 a7c2 7bb1 24c6 a8a7 f4a8 0eb5
 Package    : centos-release-7-2.1511.el7.centos.2.10.x86_64 (@anaconda)
 From       : /etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-7
Is this ok [y/N]: y
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
  Installing : epel-release-7-11.noarch                                            1/1 
  Verifying  : epel-release-7-11.noarch                                            1/1 

Installed:
  epel-release.noarch 0:7-11                                                           

Complete!

看在Complete之后,再安装redis

[root@localhost ~]# yum install redis
Loaded plugins: fastestmirror, langpacks
epel/x86_64/metalink                                            | 9.0 kB  00:00:00     
epel                                                            | 5.3 kB  00:00:00     
(1/3): epel/x86_64/group_gz                                     |  90 kB  00:00:00     
(2/3): epel/x86_64/updateinfo                                   | 1.0 MB  00:00:04     
(3/3): epel/x86_64/primary_db                                   | 6.9 MB  00:00:05     
Loading mirror speeds from cached hostfile
 * base: mirrors.huaweicloud.com
 * epel: mirrors.yun-idc.com
 * extras: mirrors.huaweicloud.com
 * updates: mirrors.neusoft.edu.cn
Resolving Dependencies
--> Running transaction check
---> Package redis.x86_64 0:3.2.12-2.el7 will be installed
--> Processing Dependency: libjemalloc.so.1()(64bit) for package: redis-3.2.12-2.el7.x86_64
--> Running transaction check
---> Package jemalloc.x86_64 0:3.6.0-1.el7 will be installed
--> Finished Dependency Resolution

Dependencies Resolved

=======================================================================================
 Package             Arch              Version                   Repository       Size
=======================================================================================
Installing:
 redis               x86_64            3.2.12-2.el7              epel            544 k
Installing for dependencies:
 jemalloc            x86_64            3.6.0-1.el7               epel            105 k

Transaction Summary
=======================================================================================
Install  1 Package (+1 Dependent package)

Total download size: 648 k
Installed size: 1.7 M
Is this ok [y/d/N]: y
Downloading packages:
warning: /var/cache/yum/x86_64/7/epel/packages/redis-3.2.12-2.el7.x86_64.rpm: Header V3 RSA/SHA256 Signature, key ID 352c64e5: NOKEY
Public key for redis-3.2.12-2.el7.x86_64.rpm is not installed
(1/2): redis-3.2.12-2.el7.x86_64.rpm                            | 544 kB  00:00:01     
(2/2): jemalloc-3.6.0-1.el7.x86_64.rpm                          | 105 kB  00:00:03     
---------------------------------------------------------------------------------------
Total                                                     176 kB/s | 648 kB  00:03     
Retrieving key from file:///etc/pki/rpm-gpg/RPM-GPG-KEY-EPEL-7
Importing GPG key 0x352C64E5:
 Userid     : "Fedora EPEL (7) <epel@fedoraproject.org>"
 Fingerprint: 91e9 7d7c 4a5e 96f1 7f3e 888f 6a2f aea2 352c 64e5
 Package    : epel-release-7-11.noarch (@extras)
 From       : /etc/pki/rpm-gpg/RPM-GPG-KEY-EPEL-7
Is this ok [y/N]: y
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
  Installing : jemalloc-3.6.0-1.el7.x86_64                                         1/2 
  Installing : redis-3.2.12-2.el7.x86_64                                           2/2 
  Verifying  : redis-3.2.12-2.el7.x86_64                                           1/2 
  Verifying  : jemalloc-3.6.0-1.el7.x86_64                                         2/2 

Installed:
  redis.x86_64 0:3.2.12-2.el7                                                          

Dependency Installed:
  jemalloc.x86_64 0:3.6.0-1.el7                                                        

Complete!
[root@localhost ~]#

到了这,redis就装好了。

第二步:启动和关闭redis

 装好后,可以用以下方式启动redis

[root@localhost ~]# systemctl start redis.service

按以下方式关闭redis

[root@localhost ~]# systemctl stop redis.service

按以下方式查看redis状态:

[root@localhost ~]# systemctl status redis.service
● redis.service - Redis persistent key-value database
   Loaded: loaded (/usr/lib/systemd/system/redis.service; disabled; vendor preset: disabled)
  Drop-In: /etc/systemd/system/redis.service.d
           └─limit.conf
   Active: inactive (dead)

Jan 17 23:20:10 localhost.localdomain systemd[1]: Starting Redis persistent key-val....
Jan 17 23:20:10 localhost.localdomain systemd[1]: Started Redis persistent key-valu....
Jan 17 23:20:57 localhost.localdomain systemd[1]: Started Redis persistent key-valu....
Jan 17 23:21:12 localhost.localdomain systemd[1]: Stopping Redis persistent key-val....
Jan 17 23:21:13 localhost.localdomain systemd[1]: Stopped Redis persistent key-valu....
Hint: Some lines were ellipsized, use -l to show in full.

执行以下命令让redis开机启动:

[root@localhost ~]# systemctl enable redis
Created symlink from /etc/systemd/system/multi-user.target.wants/redis.service to /usr/lib/systemd/system/redis.service.

然后开放端口和重启防火墙:

[root@localhost ~]# firewall-cmd --zone=public --add-port=80/tcp --permanent
success
[root@localhost ~]# firewall-cmd --zone=public --add-port=6379/tcp --permanent
success
[root@localhost ~]# systemctl restart firewalld

第三步:配置redis.conf

redis的配置文件放在了/etc下,使用vi来编辑它:

[root@localhost etc]# pwd
/etc
[root@localhost etc]# ls r*.conf
radvd.conf  redis-sentinel.conf  resolv.conf  rsyslog.conf
redis.conf  request-key.conf     rsyncd.conf

用vi打开redis.conf,进行以下三项更改:

#bind 127.0.0.1        # 这里要注释掉,否则只有本机能访问

protected-mode no   # 这里由yes改为no

requirepass 123456  # 这里设置上密码

改完后再重启下服务:

[root@localhost etc]# systemctl restart redis.service

然后用Redis-cli去探探:

[root@localhost etc]# redis-cli -h 127.0.0.1 -p 6379
127.0.0.1:6379> info
NOAUTH Authentication required.
127.0.0.1:6379> auth 123456
OK
127.0.0.1:6379> info
# Server
redis_version:3.2.12
redis_git_sha1:00000000
redis_git_dirty:0
redis_build_id:7897e7d0e13773f
redis_mode:standalone
os:Linux 3.10.0-327.el7.x86_64 x86_64
arch_bits:64
multiplexing_api:epoll
gcc_version:4.8.5
process_id:18025
run_id:3e1f70a2a356d5d1f71c7de12ea4122cd29f7755
tcp_port:6379
uptime_in_seconds:78
uptime_in_days:0
hz:10
lru_clock:2263691
executable:/usr/bin/redis-server
config_file:/etc/redis.conf

# Clients
connected_clients:1
client_longest_output_list:0
client_biggest_input_buf:0
blocked_clients:0

# Memory
used_memory:813440
used_memory_human:794.38K
used_memory_rss:2527232
used_memory_rss_human:2.41M
used_memory_peak:813440
used_memory_peak_human:794.38K
total_system_memory:1025363968
total_system_memory_human:977.86M
used_memory_lua:37888
used_memory_lua_human:37.00K
maxmemory:0
maxmemory_human:0B
maxmemory_policy:noeviction
mem_fragmentation_ratio:3.11
mem_allocator:jemalloc-3.6.0

# Persistence
loading:0
rdb_changes_since_last_save:0
rdb_bgsave_in_progress:0
rdb_last_save_time:1579321917
rdb_last_bgsave_status:ok
rdb_last_bgsave_time_sec:-1
rdb_current_bgsave_time_sec:-1
aof_enabled:0
aof_rewrite_in_progress:0
aof_rewrite_scheduled:0
aof_last_rewrite_time_sec:-1
aof_current_rewrite_time_sec:-1
aof_last_bgrewrite_status:ok
aof_last_write_status:ok

# Stats
total_connections_received:1
total_commands_processed:1
instantaneous_ops_per_sec:0
total_net_input_bytes:71
total_net_output_bytes:73
instantaneous_input_kbps:0.00
instantaneous_output_kbps:0.00
rejected_connections:0
sync_full:0
sync_partial_ok:0
sync_partial_err:0
expired_keys:0
evicted_keys:0
keyspace_hits:0
keyspace_misses:0
pubsub_channels:0
pubsub_patterns:0
latest_fork_usec:0
migrate_cached_sockets:0

# Replication
role:master
connected_slaves:0
master_repl_offset:0
repl_backlog_active:0
repl_backlog_size:1048576
repl_backlog_first_byte_offset:0
repl_backlog_histlen:0

# CPU
used_cpu_sys:0.11
used_cpu_user:0.03
used_cpu_sys_children:0.00
used_cpu_user_children:0.00

# Cluster
cluster_enabled:0

# Keyspace
127.0.0.1:6379> set name ufo
OK
127.0.0.1:6379> get name
"ufo"
127.0.0.1:6379> 

到这里,redis就安装配置完毕了。

将再次完整操作记录粘贴如下,上面全操作成功的同学可以忽略:

[hy@localhost ~]$ su - root
Password: 
[root@localhost ~]# pwd
/root
[root@localhost ~]# yum install epel-release
Loaded plugins: fastestmirror, langpacks
base                                                     | 3.6 kB     00:00     
extras                                                   | 2.9 kB     00:00     
updates                                                  | 2.9 kB     00:00     
(1/4): base/7/x86_64/group_gz                              | 165 kB   00:00     
(2/4): extras/7/x86_64/primary_db                          | 153 kB   00:01     
(3/4): updates/7/x86_64/primary_db                         | 5.9 MB   00:04     
(4/4): base/7/x86_64/primary_db                            | 6.0 MB   00:30     
Determining fastest mirrors
 * base: ap.stykers.moe
 * extras: ap.stykers.moe
 * updates: mirror.lzu.edu.cn
Resolving Dependencies
--> Running transaction check
---> Package epel-release.noarch 0:7-11 will be installed
--> Finished Dependency Resolution

Dependencies Resolved

================================================================================
 Package                Arch             Version         Repository        Size
================================================================================
Installing:
 epel-release           noarch           7-11            extras            15 k

Transaction Summary
================================================================================
Install  1 Package

Total download size: 15 k
Installed size: 24 k
Is this ok [y/d/N]: y
Downloading packages:
warning: /var/cache/yum/x86_64/7/extras/packages/epel-release-7-11.noarch.rpm: Header V3 RSA/SHA256 Signature, key ID f4a80eb5: NOKEY
Public key for epel-release-7-11.noarch.rpm is not installed
epel-release-7-11.noarch.rpm                               |  15 kB   00:00     
Retrieving key from file:///etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-7
Importing GPG key 0xF4A80EB5:
 Userid     : "CentOS-7 Key (CentOS 7 Official Signing Key) <security@centos.org>"
 Fingerprint: 6341 ab27 53d7 8a78 a7c2 7bb1 24c6 a8a7 f4a8 0eb5
 Package    : centos-release-7-2.1511.el7.centos.2.10.x86_64 (@anaconda)
 From       : /etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-7
Is this ok [y/N]: y
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
  Installing : epel-release-7-11.noarch                                     1/1 
  Verifying  : epel-release-7-11.noarch                                     1/1 

Installed:
  epel-release.noarch 0:7-11                                                    

Complete!
[root@localhost ~]# yum install redis
Loaded plugins: fastestmirror, langpacks
epel/x86_64/metalink                                     | 9.4 kB     00:00     
epel                                                     | 5.3 kB     00:00     
(1/3): epel/x86_64/group_gz                                |  90 kB   00:00     
(2/3): epel/x86_64/primary_db                              | 6.9 MB   00:02     
(3/3): epel/x86_64/updateinfo                              | 1.0 MB   00:03     
Loading mirror speeds from cached hostfile
 * base: ap.stykers.moe
 * epel: mirrors.yun-idc.com
 * extras: ap.stykers.moe
 * updates: mirror.lzu.edu.cn
Resolving Dependencies
--> Running transaction check
---> Package redis.x86_64 0:3.2.12-2.el7 will be installed
--> Processing Dependency: libjemalloc.so.1()(64bit) for package: redis-3.2.12-2.el7.x86_64
--> Running transaction check
---> Package jemalloc.x86_64 0:3.6.0-1.el7 will be installed
--> Finished Dependency Resolution

Dependencies Resolved

================================================================================
 Package           Arch            Version                  Repository     Size
================================================================================
Installing:
 redis             x86_64          3.2.12-2.el7             epel          544 k
Installing for dependencies:
 jemalloc          x86_64          3.6.0-1.el7              epel          105 k

Transaction Summary
================================================================================
Install  1 Package (+1 Dependent package)

Total download size: 648 k
Installed size: 1.7 M
Is this ok [y/d/N]: y
Downloading packages:
warning: /var/cache/yum/x86_64/7/epel/packages/redis-3.2.12-2.el7.x86_64.rpm: Header V3 RSA/SHA256 Signature, key ID 352c64e5: NOKEY
Public key for redis-3.2.12-2.el7.x86_64.rpm is not installed
(1/2): redis-3.2.12-2.el7.x86_64.rpm                       | 544 kB   00:01     
(2/2): jemalloc-3.6.0-1.el7.x86_64.rpm                     | 105 kB   00:02     
--------------------------------------------------------------------------------
Total                                              211 kB/s | 648 kB  00:03     
Retrieving key from file:///etc/pki/rpm-gpg/RPM-GPG-KEY-EPEL-7
Importing GPG key 0x352C64E5:
 Userid     : "Fedora EPEL (7) <epel@fedoraproject.org>"
 Fingerprint: 91e9 7d7c 4a5e 96f1 7f3e 888f 6a2f aea2 352c 64e5
 Package    : epel-release-7-11.noarch (@extras)
 From       : /etc/pki/rpm-gpg/RPM-GPG-KEY-EPEL-7
Is this ok [y/N]: y
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
  Installing : jemalloc-3.6.0-1.el7.x86_64                                  1/2 
  Installing : redis-3.2.12-2.el7.x86_64                                    2/2 
  Verifying  : redis-3.2.12-2.el7.x86_64                                    1/2 
  Verifying  : jemalloc-3.6.0-1.el7.x86_64                                  2/2 

Installed:
  redis.x86_64 0:3.2.12-2.el7                                                   

Dependency Installed:
  jemalloc.x86_64 0:3.6.0-1.el7                                                 

Complete!
[root@localhost ~]# systemctl start redis.service
[root@localhost ~]# systemctl stop redis.service
[root@localhost ~]# systemctl status redis.service
● redis.service - Redis persistent key-value database
   Loaded: loaded (/usr/lib/systemd/system/redis.service; disabled; vendor preset: disabled)
  Drop-In: /etc/systemd/system/redis.service.d
           └─limit.conf
   Active: inactive (dead)

Jan 18 01:12:30 localhost.localdomain systemd[1]: Starting Redis persistent k...
Jan 18 01:12:30 localhost.localdomain systemd[1]: Started Redis persistent ke...
Jan 18 01:12:39 localhost.localdomain systemd[1]: Stopping Redis persistent k...
Jan 18 01:12:40 localhost.localdomain systemd[1]: Stopped Redis persistent ke...
Hint: Some lines were ellipsized, use -l to show in full.
[root@localhost ~]# systemctl enable redis
Created symlink from /etc/systemd/system/multi-user.target.wants/redis.service to /usr/lib/systemd/system/redis.service.
[root@localhost ~]# firewall-cmd --zone=public --add-port=80/tcp --permanent
success
[root@localhost ~]# firewall-cmd --zone=public --add-port=6379/tcp --permanent
success
[root@localhost ~]# systemctl restart firewalld
[root@localhost ~]# cd etc
-bash: cd: etc: No such file or directory
[root@localhost ~]# 
[root@localhost ~]# cd /etc
[root@localhost etc]# vi redis.conf
[root@localhost etc]# systemctl restart redis.service
[root@localhost etc]# redis-cli -h 127.0.0.1 -p 6379
127.0.0.1:6379> auth 123456
OK
127.0.0.1:6379> set username ufo
OK
127.0.0.1:6379> get username
"ufo"
127.0.0.1:6379> exit

参考资料:https://www.cnblogs.com/hjw-zq/p/9778514.html

--END-- 2020年1月18日13点23分

原文地址:https://www.cnblogs.com/heyang78/p/12208775.html