CentOS下Redis下载、安装、配置、主从集群配置

一、 下载

下载地址: http://redis.io/download

linux中下载:

进入跟目录, 创建redis目录

1
2
3
4
cd /
mkdir redis/conf/
mkdir redis/db/
mkdir redis/app/

进入redis/app/目录

1
cd /redis/app/

下载

1
wget -O redis-2.6.14.tar.gz https://storage.googleapis.com/google-code-archive-downloads/v2/code.google.com/redis/redis-2.6.14.tar.gz

二、 安装

linux下安装:
解压

1
tar -xzvf redis-2.6.14.tar.gz

解压完成进入redis目录编译:如果在make过程中没有提示错误的话,安装就成功了

进入目录

1
cd redis-2.6.14

编译

1
make

安装

1
sudo make install

三、 单服务器配置

将配置文件加入系统环境,可以在系如何目录直接启动redis

将redis系统文件拷贝到系统用户目录

1
cp src/redis-* /usr/local/bin/

将redis配置文件拷贝到/redis配置目录

1
cp redis.conf /redis/conf/redis.conf

修改密码:

1
vim /redis/conf/redis.conf

修改requirepass 项表示将密码改为1234

1
requirepass 1234

启动:

1
redis-server /etc/redis.conf &

测试:

1
2
3
4
5
6
7
[root@localhost redis]# redis-cli
redis 127.0.0.1:6379> auth 1234
OK
redis 127.0.0.1:6379> set key1 value1
OK
redis 127.0.0.1:6379> get key1
"value1"

如果设置key1值成功后,在获得key1值成功就说明安装成功了。

四、主从集群配置

将配置文件加入系统环境,可以在系如何目录直接启动redis

将redis系统文件拷贝到系统用户目录

1
cp src/redis-* /usr/local/bin/

将redis配置文件拷贝到/redis配置目录

1
2
cp redis.conf /redis/conf/redis-master.conf
cp redis.conf /redis/conf/redis-slave.conf

4.1 修改主服务器配置文件:

1
vim /redis/conf/redis-master.conf

配置redis作为守护进程运行

  • 默认情况下 redis 不是作为守护进程运行的,如果你想让它在后台运行,你就把配置文件里daemonize改成 yes。
  • 当redis作为守护进程运行的时候,它会写一个 pid 到 /var/run/redis.pid 文件里面
1
2
#daemonize no
daemonize yes

如果设置daemonize yes,那还可以修改pid文件的目录

1
pidfile /var/run/redis-master.pid

主服务器端口

1
port 6379

日志级别 (debug、verbose、notice、warning)

1
loglevel notice

日志文件路径

1
logfile /logs/redis/redis-master.log

数据库文件路径

  • 数据库文件的位置,最好添加绝对路径,若不添加时在启动用户的home目录下
    1
    dbfilename /redis/db/dump-master.rdb

主从服务器的地址和端口

  • 例如:slaveof 192.168.1.1 6379 ; 如果是主服务器不需要开启此项就注释掉
1
#slaveof <masterip> <masterport>

从服务器只读选项,默认是yes,只读模式

1
2
#slave-read-only yes
slave-read-only no

服务器密码:如果主服务设置了密码,从服务需要配置masterauth这个选项

1
requirepass 1234

主服务访问密码:从服务器设置,主服务器不需要设置

1
masterauth 1234

4.2 修改从服务器配置文件:

1
vim /redis/conf/redis-slave.conf

配置redis作为守护进程运行

  • 默认情况下 redis 不是作为守护进程运行的,如果你想让它在后台运行,你就把配置文件里daemonize改成 yes。
  • 当redis作为守护进程运行的时候,它会写一个 pid 到 /var/run/redis.pid 文件里面
1
2
#daemonize no
daemonize yes

pid文件的路径

1
2
#pidfile
pidfile /var/run/redis-slave.pid

服务器端口

1
port 6380

日志级别 (debug、verbose、notice、warning)

1
loglevel notice

日志文件路径

1
logfile /logs/redis/redis-slave.log

数据库文件路径

1
dbfilename /redis/db/dump-slave.rdb

主从服务器的地址和端口。例如:slaveof 192.168.1.1 6379 如果是主服务器不需要开启此项

1
slaveof 192.168.1.1 6379

从服务器只读选项,默认是yes,只读模式

1
slave-read-only no

服务器密码:从服务器也可以设置自己的密码

1
requirepass 5678

主服务访问密码:如果主服务设置了密码,从服务需要配置masterauth这个选项从服务器设置,主服务器不需要设置

1
masterauth 1234

启动主从服务器顺序:先启动主服务器,在启动从服务器:

1
2
3
4
5
6
7
8
9
10
11
[root@localhost redis]# redis-server /etc/redis-master.conf &
[1] 19928
[root@localhost redis]# redis-server /etc/redis-slave.conf &
[2] 19933
[1] Done redis-server /etc/redis-master.conf
[root@localhost redis]# ps -ef | grep redis
root 19929 1 0 12:51 ? 00:00:00 redis-server /etc/redis-master.conf
root 19934 1 0 12:51 ? 00:00:00 redis-server /etc/redis-slave.conf
root 19939 2697 0 12:51 pts/0 00:00:00 grep redis
[2]+ Done redis-server /etc/redis-slave.conf
[root@localhost redis]#

测试:

1
2
3
4
5
6
7
8
9
10
11
12
[root@localhost opt]# redis-cli -p 6379
redis 127.0.0.1:6379> auth 1234
OK
redis 127.0.0.1:6379> set k1 aaa
OK
redis 127.0.0.1:6379> exit
[root@localhost opt]# redis-cli -p 6380
redis 127.0.0.1:6380> auth 5678
OK
redis 127.0.0.1:6380> get k1
"aaa"
redis 127.0.0.1:6380>

4.3 关闭服务器:

4.3.1 杀进程关闭

1
2
3
4
5
6
7
8
9
10
11
# 先查看redis服务器启动状态:
[root@localhost redis]# ps -ef | grep redis
root 2672 1 0 11:37 ? 00:00:00 redis-server /etc/redis-master.conf
root 2676 1 0 11:37 ? 00:00:00 redis-server /etc/redis-slave.conf
root 19924 2697 0 12:50 pts/0 00:00:00 grep redis
# 杀掉进程
[root@localhost redis]# kill -9 2672 2676
[root@localhost redis]#
[root@localhost redis]# ps -ef | grep redis
root 19927 2697 0 12:51 pts/0 00:00:00 grep redis
[root@localhost redis]#

4.3.1.2 正常关闭

查看redis进程

1
2
3
4
[root@localhost redis]# ps -ef |grep redis
root 19929 1 0 12:51 ? 00:00:00 redis-server /etc/redis-master.conf
root 19934 1 0 12:51 ? 00:00:00 redis-server /etc/redis-slave.conf
root 20446 2697 0 15:25 pts/0 00:00:00 grep redis

没有密码的情况下关闭命令 如果设置密码会提示错误 如果是本机(-h 127.0.0.1 )是可选项

1
2
3
4
[root@localhost redis]# redis-cli -h 127.0.0.1 -p 6380 shutdown
(error) ERR operation not permitted
[root@localhost redis]# redis-cli -h 127.0.0.1 -p 6379 shutdown
(error) ERR operation not permitted

设置密码的时候需要加上密码才能关闭

1
2
3
4
5
6
7
8
[root@localhost redis]# redis-cli -p 6379 -a 5678 shutdown
[root@localhost redis]# ps -ef |grep redis
root 19934 1 0 12:51 ? 00:00:00 redis-server /etc/redis-slave.conf
root 20463 2697 0 15:31 pts/0 00:00:00 grep redis
[root@localhost redis]# redis-cli -p 6380 -a 1234 shutdown
[root@localhost redis]# ps -ef |grep redis
root 20466 2697 0 15:31 pts/0 00:00:00 grep redis
[root@localhost redis]#