您的位置:首页 > 数据库 > Redis

Linux 下Redis安装与集群配置 Redis配置文件详解

2015-11-16 19:34 976 查看
Redis(2.8.19目前最新稳定版本)的安装:
主从Redis的安装和配置只是配置文件不同,其他的均一样,一般主从配好就可以测试主从操作了,主server上set,从server马上就可以get到了。

1.下载:
http://pan.baidu.com/s/1jGvJxBo
2.安装
解压下载文件:
[root@jhq0229 src]# tar xzvf redis-stable.tar.gz

编译安装:
[root@jhq0229 src]# cd redis-stable
[root@jhq0229 redis-stable]# make
[root@jhq0229 redis-stable]# make PREFIX=/usr/local install
配置:
[root@jhq0229 redis-stable]# mkdir /etc/redis
[root@jhq0229 redis-stable]# cp redis.conf /etc/redis/
创建Redis数据及日志存放目录:

[root@jhq0229 redis-stable]# mkdir /data/redis

Redis配置文件详解:

[plain] view
plaincopyprint?





daemonize:如果需要在后台运行,把该项心为yes

pidfile:配置多个pid的地址,默认在/var/run/redis.pid

bind:绑定ip,设置后只接受来自该ip的请求

port:监听端口,默认为6379

timeout:设置客户端连接时的超时时间,单位为秒

loglevel:分为4级,debug、verbose、notice、warning

logfile:配置log文件地址

databases:设置数据库的个数,默认使用的数据库为0

save:设置redis进行数据库镜像的频率

rdbcompression:在进行镜像备份时,是否进行压缩

Dbfilename:镜像备份文件的文件名

Dir:数据库镜像备份的文件放置路径

Slaveof:设置数据库为其他数据库的从数据库

Masterauth:主数据库连接需要的密码验证

Requirepass:设置登录登录时需要使用的密码

Maxclients:限制同时连接的客户数量

Maxmemory:设置redis能够使用的最大内存

Appendonly:开启append only模式

Appendfsync:设置对appendonly.aof文件同步的频率

vm-enabled:是否开启虚拟内存支持

vm-swap-file:设置虚拟内存的交换文件路径

vm-max-memory:设置redis使用的最大物理内存大小

vm-page-size:设置虚拟内存的页大小

vm-pages:设置交换文件的总的page数量

vm-max-threads:设置VMIO同时使用的线程数量

Glueoutputbuf:把小的输出缓存存放在一起

hash-max-zipmap-entries:设置hash的临界值

Activerehashing:重新hash

配置自己的Redis集群(若不配置集群可以只配置Master):
[root@jhq0229 redis-stable]# vim /etc/redis/redis.conf
我的Redis主从配置:
主:192.168.1.18 认证密码:01130229

从:192.168.1.16

主Redis(Master)配置:

[plain] view
plaincopyprint?





# Redis configuration file example

#是否以后台进程方式运行

daemonize yes

#以后台进行运行,需指定pid

pidfile /var/run/redis.pid

port 6379

tcp-backlog 511

bind 192.168.1.18

#客户端闲置N秒后关闭连接

timeout 30

tcp-keepalive 0

#日志级别

loglevel notice

#记录日志的文件名称

logfile "redlog"

#可用数据库数量

databases 16

#当有一条数据更新,900秒后同步数据到磁盘数据库

save 900 1

#当有10条数据更新,300秒后同步数据到磁盘数据库

save 300 10

save 60 10000

stop-writes-on-bgsave-error yes

#当dump .rdb的时候是否压缩数据对象,默认值为yes

rdbcompression yes

rdbchecksum yes

# The filename where to dump the DB

# 磁盘数据库文件名称

dbfilename myredis.rdb

#本地数据库存放路径

dir /data/redis/

################################# REPLICATION #################################

#Redis主从复制配置

#若当前服务为slave,在此处设置主服务的ip及端口

# slaveof <masterip> <masterport>

#若当前服务为slave,设置主服务的认证密码

# masterauth <master-password>

slave-serve-stale-data yes

#若当前为slave服务,设置slave是否为只读服务

#slave-read-only yes

repl-diskless-sync no

repl-diskless-sync-delay 5

repl-disable-tcp-nodelay no

slave-priority 100

#认证密码

requirepass 01130229

appendonly no

appendfsync everysec

no-appendfsync-on-rewrite no

auto-aof-rewrite-percentage 100

auto-aof-rewrite-min-size 64mb

aof-load-truncated yes

lua-time-limit 5000

slowlog-log-slower-than 10000

slowlog-max-len 128

latency-monitor-threshold 0

notify-keyspace-events ""

hash-max-ziplist-entries 512

hash-max-ziplist-value 64

list-max-ziplist-entries 512

list-max-ziplist-value 64

set-max-intset-entries 512

zset-max-ziplist-entries 128

zset-max-ziplist-value 64

hll-sparse-max-bytes 3000

activerehashing yes

client-output-buffer-limit normal 0 0 0

client-output-buffer-limit slave 256mb 64mb 60

client-output-buffer-limit pubsub 32mb 8mb 60

hz 10

aof-rewrite-incremental-fsync yes

从Redis(Slave)配置,配置与Master类似,不一致内容如下:

[plain] view
plaincopyprint?





#绑定IP地址

bind 192.168.1.16

#若当前服务为slave,在此处设置主服务的ip及端口

slaveof 192.168.1.18 6379

#若当前服务为slave,设置主服务的认证密码

masterauth 01130229

配置Redis开机启动:

[root@jhq0229 redis-stable]# vim /etc/init.d/redis

内容如下:

[plain] view
plaincopyprint?





#!/bin/bash

#

# redis - this script starts and stops the redis-server daemon

#

# chkconfig: - 80 12

# description: Redis is a persistent key-value database

# processname: redis-server

# config: /etc/redis/redis.conf

# pidfile: /var/run/redis.pid

source /etc/init.d/functions

BIN="/usr/local/bin"

CONFIG="/etc/redis/redis.conf"

PIDFILE="/var/run/redis.pid"

### Read configuration

[ -r "$SYSCONFIG" ] && source "$SYSCONFIG"

RETVAL=0

prog="redis-server"

desc="Redis Server"

start() {

if [ -e $PIDFILE ];then

echo "$desc already running...."

exit 1

fi

echo -n $"Starting $desc: "

daemon $BIN/$prog $CONFIG

RETVAL=$?

echo

[ $RETVAL -eq 0 ] && touch /var/lock/subsys/$prog

return $RETVAL

}

stop() {

echo -n $"Stop $desc: "

killproc $prog

RETVAL=$?

echo

[ $RETVAL -eq 0 ] && rm -f /var/lock/subsys/$prog $PIDFILE

return $RETVAL

}

restart() {

stop

start

}

case "$1" in

start)

start

;;

stop)

stop

;;

restart)

restart

;;

condrestart)

[ -e /var/lock/subsys/$prog ] && restart

RETVAL=$?

;;

status)

status $prog

RETVAL=$?

;;

*)

echo $"Usage: $0 {start|stop|restart|condrestart|status}"

RETVAL=1

esac

exit $RETVAL

[root@jhq0229 redis-stable]# chmod +x /etc/init.d/redis

[root@jhq0229 redis-stable]# chkconfig redis on

补充配置,配置停止服务前同步数据到磁盘:

[root@jhq0229 redis-stable]# vim /etc/sysctl.conf

进行如下修改:

vm.overcommit_memory=1

应用修改:

sysctl -p

启动Redis:

[root@jhq0229 redis-stable]# service redis start

最后,奉上公司同事辛勤总结的Redis学习资料。希望可以帮到你。

下载地址: http://pan.baidu.com/s/1qWLV87a
PPT内容版权归公司所有。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: