首页 > 代码库 > 生产环境部署单台redis

生产环境部署单台redis

生产环境部署redis方案
1.腾讯云有redis主从
2.自己部署redis也很简单

(1)redis使用编译安装方式,所以需要安装编译基本组件
# yum install gcc cpp glibc glibc-devel gcc-c++

(2)redis依赖jemalloc,所以先安装此组件
# wget http://www.canonware.com/download/jemalloc/jemalloc-4.2.1.tar.bz2
后续读者可以查看此链接获取更新版本jemalloc,http://www.canonware.com/download/jemalloc/
# tar -xf jemalloc-4.2.1.tar.bz2
cd jemalloc-4.2.1
# ./configure --prefix=/usr/local/
# make
# make install

(3)下载安装redis-server
# cd /usr/local/src/
# wget http://download.redis.io/redis-stable.tar.gz
后续读者可以查看此链接获取更新版本redis,http://download.redis.io/或者从官网地址开始查找下载地址
# tar -xf redis-stable.tar.gz
# cd redis-stable
# make
# make install
# mkdir /etc/redis
# cp /usr/local/src/redis-stable/redis.conf /etc/redis

先尝试前台启动redis,查看其日志输出
/usr/local/bin/redis-server /etc/redis/redis.conf

例如本文作者遇到了三个内核参数相关的警告
(1)WARNING: The TCP backlog setting of 511 cannot be enforced because /proc/sys/net/core/somaxconn is set to the lower value of 128.
(2)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.
(3)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.

解决办法:
echo never > /sys/kernel/mm/transparent_hugepage/enabled
sysctl -w net.core.somaxconn = 65536
sysctl -w vm.overcommit_memory = 1
读者可以把上述设置分别加入/etc/rc.local和/etc/sysctl.conf文件中

此时启动redis无任何警告.可以修改配置文件/etc/redis/redis.conf
daemonize no => daemonize yes
redis默认只监听本机IP:127.0.0.1.所以,如果允许redis可以由内网其他主机访问,可以在bind命令后添加本主机的内网IP即可.注意:redis由程序访问使用,通常是不会监听公网IP的.

登录访问redis-server
redis-cli:可以直接访问本主机的redis-server
redis-cli -h redis-server_内网IP -p 6379:其他内网主机访问

127.0.0.1:6379> set name Tom
OK
127.0.0.1:6379> get name
"Tom"
127.0.0.1:6379> keys *
1) "name"
127.0.0.1:6379> quit
测试可正常使用

(4)接下来我们要为redis设置一个密码
第一种情景:可以重启redis的情景.当redis重新部署
vim /etc/redis/redis.conf
# requirepass foobared =>
requirepass YOUR_PASSWORD
[随机密码生成命令: openssl rand -base64 16]
然后修改redis.conf仅root可读写: chmod 600 /etc/redis/redis.conf
最后kill掉redis进程再启动
kill -9 `pgrep redis` && /usr/local/bin/redis-server /etc/redis/redis.conf

然后再次登录redis.需要输入密码验证通过后才能访问数据
# redis-cli
127.0.0.1:6379> keys *
(error) NOAUTH Authentication required.
未经授权的登录是不能访问数据的
127.0.0.1:6379> auth 71840de8c369958
OK
127.0.0.1:6379> keys *
1) "name"

也可以在登录直接输入密码
# redis-cli -h 127.0.0.1 -p 6379 -a 71840de8c369958
127.0.0.1:6379> keys *
1) "name"

第二种情景:不能重启redis.当redis已经在生产环境中运行了,不能重启redis时可以如下操作:
# redis-cli
127.0.0.1:6379> keys *
1) "name"
127.0.0.1:6379> config get requirepass
1) "requirepass"
2) ""
此时requirepass对应的值是空的""
127.0.0.1:6379> config set requirepass 71840de8c369958
OK
现在我们为redis设置了密码
127.0.0.1:6379> keys *
(error) NOAUTH Authentication required.
127.0.0.1:6379> config get requirepass
(error) NOAUTH Authentication required.
获取keys和requirepass都遭到拒绝
127.0.0.1:6379> auth 71840de8c369958
OK
使用密码进行认证,返回OK表示通过认证,现在执行任何操作都是被允许的.
127.0.0.1:6379> config get requirepass
1) "requirepass"
2) "71840de8c369958"
127.0.0.1:6379> keys *
1) "name"

最后在redis.conf配置文件中添加requirepass 71840de8c369958.
这样重启redis后,密码同样生效.然后修改redis.conf仅root可访问
# chmod 600 /etc/redis/redis.conf


安装过程中可能会遇到的问题以及解决办法
1、不能编译没有GCC 编译工具
安装报错:
问题1:make时可能会报如下错误
cc -c -std=c99 -pedantic -O2 -Wall -W   -g -rdynamic -ggdb   adlist.c
make: cc:命令未找到
make: *** [adlist.o] 错误 127

解决方法:安装gcc
命令如下:yum install gcc

2、make时可能会报如下错误:
collect2: ld returned 1 exit status
make[1]: *** [redis-server] Error 1
make[1]: Leaving directory `/usr/local/redis/src‘
make: *** [all] Error 2

解决办法:编辑src/.make-settings里的OPT,改为OPT=-O2 -march=i686

3、提示找不到jemalloc
zmalloc.h:50:31: error: jemalloc/jemalloc.h: No such file or directory
zmalloc.h:55:2: error: #error "Newer version of jemalloc required"

解决办法: 安装jemalloc,请参考前文

4、make时可能会报如下错误:
cc: error: ../deps/hiredis/libhiredis.a: No such file or directory
cc: error: ../deps/lua/src/liblua.a: No such file or directory
cc: error: ../deps/jemalloc/lib/libjemalloc.a: No such file or directory
make: *** [redis-server] Error 1

分别进入redis下的deps下的hiredis、lua运行make
注意:jemalloc下可能要先运行./configure,然后make
回到src目录运行make.结果还是报cc: error: ../deps/lua/src/liblua.a: No such file or directory

这下子我把redis的解压包删除掉:rm -rf redis-stable
重新解压,进入redis-stable;make还真没报错了。

参考博客文章如下
http://blog.csdn.net/steve1018/article/details/26737277
http://blog.csdn.net/jy0902/article/details/19248299
http://blog.csdn.net/lxpbs8851/article/details/8136126

后续可以部署redis主从集群

本文出自 “change” 博客,请务必保留此出处http://changeflyhigh.blog.51cto.com/8317626/1932241

生产环境部署单台redis