Centos7 安装 Redis

Centos7 安装 Redis

下载Redis

资料1

centos7安装redis-知乎

image

安装前的准备工作 针对make安装错误的问题

  1. 检查服务器是否安装gcc,以及gcc版本是否过低
1
[root@jdu4e00u53f7 redis-4.0.1]#  gcc -v
  1. 安装gcc
    1
    2
    3
    4
    5
    6
    yum install gcc
    yum install gcc-c++

    有可能还要安装tcl(如果make test出现You need tcl 8.5 or newer in order to run the Redis test的话)

    yum install tcl

3.如果之前操作过make并且报错,清理make

1
make clean
  1. 运行make语句还是报错的话
    image

解压缩Redis以及初步安装

备注:此时redis压缩包在root目录下

  1. tar -zxvf redis-3.2.9.tar.gz
  2. cd redis-3.2.9
  3. make
  4. make install

安装Redis服务 并且设置外网能够访问 参考资料

image

1
注意:上面看到6379端口绑定了127.0.0.1地址,可以在 /etc/redis/6379.conf 文件中修改为 0.0.0.0 来允许外部访问!

启动Redis

  • 直接在redis安装目录下启动
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
[root@jdu4e00u53f7 redis-4.0.1]# redis-server 

13979:C 30 Aug 13:54:35.211 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
13979:C 30 Aug 13:54:35.211 # Redis version=4.0.1, bits=64, commit=00000000, modified=0, pid=13979, just started
13979:C 30 Aug 13:54:35.211 # Warning: no config file specified, using the default config. In order to specify a config file use redis-server /path/to/redis.conf
13979:M 30 Aug 13:54:35.212 * Increased maximum number of open files to 10032 (it was originally set to 1024).
_._
_.-``__ ''-._
_.-`` `. `_. ''-._ Redis 4.0.1 (00000000/0) 64 bit
.-`` .-```. ```\/ _.,_ ''-._
( ' , .-` | `, ) Running in standalone mode
|`-._`-...-` __...-.``-._|'` _.-'| Port: 6379
| `-._ `._ / _.-' | PID: 13979
`-._ `-._ `-./ _.-' _.-'
|`-._`-._ `-.__.-' _.-'_.-'|
| `-._`-._ _.-'_.-' | http://redis.io
`-._ `-._`-.__.-'_.-' _.-'
|`-._`-._ `-.__.-' _.-'_.-'|
| `-._`-._ _.-'_.-' |
`-._ `-._`-.__.-'_.-' _.-'
`-._ `-.__.-' _.-'
`-._ _.-'
`-.__.-'

13979:M 30 Aug 13:54:35.214 # WARNING: The TCP backlog setting of 511 cannot be enforced because /proc/sys/net/core/somaxconn is set to the lower value of 128.
13979:M 30 Aug 13:54:35.214 # Server initialized
13979:M 30 Aug 13:54:35.214 # 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.
13979:M 30 Aug 13:54:35.214 # 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.
```

官网:您可以使用内置客户端与Redis交互 (其实就是打开另一个窗口,然后访问)

$ src/redis-cli
redis> set foo bar
OK
redis> get foo
“bar”

1
2
3
4
5
6
7
8

> #### Redis服务 启动/停止 以及开机服务配置
刚才安装的时候,可看到在 /etc/init.d 下放了 redis_6379 这个文件

![image](http://ww4.sinaimg.cn/large/0060lm7Tgy1fj1puldl0rj30f103pjre.jpg)
![image](http://ww4.sinaimg.cn/large/0060lm7Tgy1fj1pxckkb6j30er01a0sl.jpg)

那么,服务名是 redis_6379,所以相关的启动/停止等命令如下:

  1. #开机不启动
    chkconfig redis_6379 off
  2. #开机启动
    chkconfig redis_6379 on
  3. #停止
    service redis_6379 stop
  4. #启动
    service redis_6379 start
    1
    2
    3

    > 测试连接
    直接启动服务 。然后直接运行

[root@localhost bin]# redis-cli
或者
[root@localhost bin]# redis-cli -a 密码

1
2
3
4


> 其他一些问题
- 如果在启动服务时更改配置文件密码,需停止服务后重载但是会报错 [参考资料](http://blog.csdn.net/mingjie1212)

[root@localhost bin]# service redis stop
Stopping …
(error) NOAUTH Authentication required.

Waiting for Redis to shutdown …
Waiting for Redis to shutdown …
Waiting for Redis to shutdown …
Waiting for Redis to shutdown …
Waiting for Redis to shutdown …

1
2
3
4
5
6

这样的错误,而且redis 这时是没有停止服务的。
当然可以使用ps -ef 查进程号 然后kill 掉,如果在deamon下 这样还要去删除pid文件,有点繁琐。

解决办法:
用redis-cli 密码登陆 然后shutdown 然后exit 就OK了。

[root@localhost bin]# redis-cli -a 123456
127.0.0.1:6379> SHUTDOWN
not connected> exit
`
再用ps -ef 可以看到redis进程已经退出。