准备工作,安装tcl

#yum install –y tcl

解压缩文件

#tar –zxvf redis-2.6.7.tar.gz

#cd redis-2.6.7

#make

#make test

#make install

                                               

然后创建redis用户

#useradd redis

#cp /root/redis-2.6.7/redis.conf /home/redis/

修改redis.conf

daemonize yes        #后台启动

pidfile /var/run/redis.pid   #后台启动的pid

port 6379               #端口

bind 192.168.1.125         #设置IP,默认为127.0.0.1

timeout 300               #等待时间

loglevel notice

logfile stdout

databases 16

save 900 1

save 300 10

save 60 10000

stop-writes-on-bgsave-error yes

rdbcompression yes

rdbchecksum yes

dbfilename dump.rdb

dir /home/redis/            #数据库存放位置,这里应该存放在redis用户有权限修改的地方

slave-serve-stale-data yes

slave-read-only yes

slave-priority 100

appendonly yes           #打开日志功能

appendfsync always        #即时写入日志

no-appendfsync-on-rewrite no

auto-aof-rewrite-percentage 100

auto-aof-rewrite-min-size 64mb

lua-time-limit 5000

slowlog-log-slower-than 10000

slowlog-max-len 128

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

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

 

修改完后就可以启动日志了

#su – redis

#redis-server /home/redis/redis.conf                   #启动数据库

Redis-server                                            

[redis@localhost ~]$ netstat -lanp |grep 6379                 #查看监听端口

(Not all processes could be identified, non-owned process info

 will not be shown, you would have to be root to see it all.)

tcp        0      0 127.0.0.1:6379              0.0.0.0:*                   LISTEN      6373/redis-server 1

 

 

redis-benchmark                                    #测试redis的运行

[redis@localhost ~]$ redis-cli set foo bar                 #赋值value

OK

[redis@localhost ~]$ redis-cli get foo                    #取值

"bar"

[redis@localhost ~]$ redis-cli shutdown                 #关闭服务

[redis@localhost ~]$ redis-cli set hx value               #赋值value

[redis@localhost ~]$ redis-cli get hx                    #取值

"value"

 

 

 

 

在另一台机器上添加主从复制

同样安装好

创建redis用户

redis用户下修改redis.conf

daemonize yes

pidfile /var/run/redis.pid

port 6379

 bind 192.168.1.128

timeout 300

loglevel notice

logfile stdout

databases 16

save 900 1

save 300 10

save 60 10000

stop-writes-on-bgsave-error yes

rdbcompression yes

rdbchecksum yes

dbfilename salve.rdb         #修改数据库名字

dir /home/redis/            #数据库路径

slaveof 192.168.1.125 6379    #写入主服务器的位置和端口号

slave-serve-stale-data yes

slave-read-only yes

slave-priority 100

appendonly yes

appendfsync always

no-appendfsync-on-rewrite no

auto-aof-rewrite-percentage 100

auto-aof-rewrite-min-size 64mb

lua-time-limit 5000

slowlog-log-slower-than 10000

slowlog-max-len 128

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

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

 

redis用户启动,即可实现主从

测试:

[redis@localhost ~]$ redis-cli get foo

"bar"

成功