Redis、Redis+sentinel安装(Ubuntu 14.04下Redis安装及简单测试)
Ubuntu 下 Redis 安装两种安装方式:
1、apt-get 方式
步骤:
-
以 root 权限登录,切换到 /usr 目录下.
2、手工下载安装包,再执行安装指令
摘要: Redis 是目前业界非常受到欢迎的一个内存数据库,一般用作系统的中间缓存系统,用以提升整体商业系统的吞吐量和响应速度。本文将简要介绍安装的主要过程以及给出一个简要的测试代码。
1. 系统环境和版本说明
Linux 操作系统选用 Ubuntu 14.04, Redis 的版本选取目前的最新稳定版本 redis-3.07。 客户端选用了 Redis 的 Java 版本 jedis 2.4.2。
2. Redis 的安装步骤
a. 下载 Redis 的安装包
wget http://download.redis.io/releases/redis-3.07.tar.gz
b. 在目录下,解压按照包,生成新的目录 redis-3.07
tar xvfz redis-3.07.tar.gz
c. 进入解压之后的目录, 进行编译
cd redis-3.07
sudo make
说明: 如果没有明显的错误,则表示编译成功
d. 安装
sudo make install
说明: 一般情况下,在 Ubuntu 系统中,都是需要使用 sudo 提升权限的
e. 在安装成功之后,可以运行测试,确认 Redis 的功能是否正常
sudo make test
这时候,我们可以看看 src 下的文件:
1 | [neil@neilhost src]$ ll |
可以看到此时,src 文件夹下出现了一些绿色的文件,这些文件就是我们以后需要用到的命令文件。
步骤 3:
移动文件,便于管理:(所有源代码安装的软件都安装在 /usr/local 下,如 apache 等)
创建两个文件夹,bin 用于存放命令,etc 拥有存放配置文件。
duanxz@ubuntu:~/soft/redis-3.0.7/src$ sudo mkdir -p /usr/local/redis/bin
duanxz@ubuntu:~/soft/redis-3.0.7/src$ sudo mkdir -p /usr/local/redis/etc
-p 是递归创建。
接下来,将 redis-2.8.19 文件夹下的 redis.conf 复制到 /usr/local/redis/etc/
并将 src 目录下的 7 个命令文件(绿色的),移动到 /usr/local/redis/bin/
duanxz@ubuntu:~/soft/redis-3.0.7/src$ cd ..
duanxz@ubuntu:~/soft/redis-3.0.7$ ls
00-RELEASENOTES COPYING Makefile redis.conf runtest-sentinel tests
BUGS deps MANIFESTO runtest sentinel.conf utils
CONTRIBUTING INSTALL README runtest-cluster src
duanxz@ubuntu:~/soft/redis-3.0.7$ sudo mv ./redis.conf /usr/local/redis/etc/
duanxz@ubuntu:~/soft/redis-3.0.7$ cd src
duanxz@ubuntu:~/soft/redis-3.0.7/src$ sudo mv mkreleasehdr.sh redis-benchmark redis-check-aof redis-check-dump redis-cli redis-sentinel redis-server /usr/local/redis/bin/
duanxz@ubuntu:~/soft/redis-3.0.7/src$
步骤 4:启动 Redis 服务:
首先进入刚才安装 redis 的目录:
duanxz@ubuntu:~/soft/redis-3.0.7/src$ cd /usr/local/redis/bin/ duanxz@ubuntu:/usr/local/redis/bin$ ls mkreleasehdr.sh redis-check-aof redis-cli redis-server redis-benchmark redis-check-dump redis-sentinel
之后我们启动 redis 服务。启动 redis 服务需要用到命令 redis-server
duanxz@ubuntu:/usr/local/redis/bin$ sudo ./redis-server 20022:C 22 Apr 07:40:42.008 # Warning: no config file specified, using the default config. In order to specify a config file use ./redis-server /path/to/redis.conf 20022:M 22 Apr 07:40:42.009 * Increased maximum number of open files to 10032 (it was originally set to 1024). _._ _.-``__ ''-._ _.-`` `. `_. ''-._ Redis 3.0.7 (00000000/0) 64 bit .-`` .-```. ```\/ _.,_ ''-._ ( ' , .-` | `, ) Running in standalone mode |`-._`-...-` __...-.``-._|'` _.-'| Port: 6379 | `-._ `._ / _.-' | PID: 20022 `-._ `-._ `-./ _.-' _.-' |`-._`-._ `-.__.-' _.-'_.-'| | `-._`-._ _.-'_.-' | http://redis.io `-._ `-._`-.__.-'_.-' _.-' |`-._`-._ `-.__.-' _.-'_.-'| | `-._`-._ _.-'_.-' | `-._ `-._`-.__.-'_.-' _.-' `-._ `-.__.-' _.-' `-._ _.-' `-.__.-'20022:M 22 Apr 07:40:42.017 # WARNING: The TCP backlog setting of 511 cannot be enforced because /proc/sys/net/core/somaxconn is set to the lower value of 128.
20022:M 22 Apr 07:40:42.017 # Server started, Redis version 3.0.7
20022:M 22 Apr 07:40:42.017 # 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.
20022:M 22 Apr 07:40:42.017 # 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.
20022:M 22 Apr 07:40:42.017 * The server is now ready to accept connections on port 6379
但是,这样做的话,我们并没有使用 etc 的下的配置文件进行启动。
如果希望通过指定的配置文件启动,需要在启动时指定配置文件:
这里我们先用 ctrl+C 来终止服务,然后查看 redis 服务是否终止干净了,之后通过设置配置文件来启动服务:
^C20022:signal-handler (1461336088) Received SIGINT scheduling shutdown... 20022:M 22 Apr 07:41:28.117 # User requested shutdown... 20022:M 22 Apr 07:41:28.118 * Saving the final RDB snapshot before exiting. 20022:M 22 Apr 07:41:28.123 * DB saved on disk 20022:M 22 Apr 07:41:28.123 # Redis is now ready to exit, bye bye... duanxz@ubuntu:/usr/local/redis/bin$ pstree -p | grep redis duanxz@ubuntu:/usr/local/redis/bin$ sudo ./redis-server /usr/local/redis/etc/redis.conf 20075:M 22 Apr 07:42:33.515 * Increased maximum number of open files to 10032 (it was originally set to 1024). _._ _.-``__ ''-._ _.-`` `. `_. ''-._ Redis 3.0.7 (00000000/0) 64 bit .-`` .-```. ```\/ _.,_ ''-._ ( ' , .-` | `, ) Running in standalone mode |`-._`-...-` __...-.``-._|'` _.-'| Port: 6379 | `-._ `._ / _.-' | PID: 20075 `-._ `-._ `-./ _.-' _.-' |`-._`-._ `-.__.-' _.-'_.-'| | `-._`-._ _.-'_.-' | http://redis.io `-._ `-._`-.__.-'_.-' _.-' |`-._`-._ `-.__.-' _.-'_.-'| | `-._`-._ _.-'_.-' | `-._ `-._`-.__.-'_.-' _.-' `-._ `-.__.-' _.-' `-._ _.-' `-.__.-'20075:M 22 Apr 07:42:33.522 # WARNING: The TCP backlog setting of 511 cannot be enforced because /proc/sys/net/core/somaxconn is set to the lower value of 128.
20075:M 22 Apr 07:42:33.522 # Server started, Redis version 3.0.7
20075:M 22 Apr 07:42:33.522 # 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.
20075:M 22 Apr 07:42:33.522 # 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.
20075:M 22 Apr 07:42:33.523 * DB loaded from disk: 0.000 seconds
20075:M 22 Apr 07:42:33.523 * The server is now ready to accept connections on port 6379
但是,现在 redis 仍然是在前台运行。
如果要后台启动该怎么办呢?
回想一下,后台启动 mysql 的方法是在后面加一个 & 符号。如:
1 | #mysql_safe --user=mysql & |
后台启动 mongodb,则在后面加上一个 --fork。具体方法请看我之前的 MongDB 入门系列 (2)。
但是,这两种方法都不适用于 redis。redis 的后台启动并运行需要通过配置文件中的参数设置。
Redis 的配置文件中有哪些配置呢?
daemonize 如果需要在后台运行,把该项改为 yes
pidfile 配置多个 pid 的地址 默认在 /var/run/redis.pid
bind 绑定 ip,设置后只接受来自该 ip 的请求
port 监听端口,默认是 6379
loglevel 分为 4 个等级:debug verbose notice warning
logfile 用于配置 log 文件地址
databases 设置数据库个数,默认使用的数据库为 0
save 设置 redis 进行数据库镜像的频率。
rdbcompression 在进行镜像备份时,是否进行压缩
dbfilename 镜像备份文件的文件名
Dir 数据库镜像备份的文件放置路径
Slaveof 设置数据库为其他数据库的从数据库
Masterauth 主数据库连接需要的密码验证
Requriepass 设置 登陆时需要使用密码
Maxclients 限制同时使用的客户数量
Maxmemory 设置 redis 能够使用的最大内存
Appendonly 开启 append only 模式
Appendfsync 设置对 appendonly.aof 文件同步的频率(对数据进行备份的第二种方式)
vm-enabled 是否开启虚拟内存支持 (vm 开头的参数都是配置虚拟内存的)
vm-swap-file 设置虚拟内存的交换文件路径
vm-max-memory 设置 redis 使用的最大物理内存大小
vm-page-size 设置虚拟内存的页大小
vm-pages 设置交换文件的总的 page 数量
vm-max-threads 设置 VM IO 同时使用的线程数量
Glueoutputbuf 把小的输出缓存存放在一起
hash-max-zipmap-entries 设置 hash 的临界值
Activerehashing 重新 hash
本文是 oschina 博客用户 happyBKs 的文章,转载请声明出处。http://my.oschina.net/u/1156339/blog/387335
言归正传,如果需要 redis 后台运行需要将 daemonize 由 no 改为 yes。
首先在超级权限下打开 redis.conf。
1 | [neil@neilhost bin]$ sudo gedit /usr/local/redis/etc/redis .conf |
之后将 daemonize 由 no 改为 yes。
保存退出。
之后我们再次使用配置文件启动 redis-server。
可以看到,redis 是后台启动了,并且通过 ps 命令可以查看到 redis 正在运行。
1 2 3 4 5 6 7 | [neil@neilhost bin]$ sudo . /redis-server /usr/local/redis/etc/redis .conf [neil@neilhost bin]$ ps -ef | grep redis root 13154 1 0 22:53 ? 00:00:00 . /redis-server *:6379 neil 13162 8143 0 22:54 pts /0 00:00:00 grep --color=auto redis [neil@neilhost bin]$ pstree -p | grep redis |-redis-server(13154)-+-{redis-server}(13156) | `-{redis-server}(13157) |
Redis 服务端默认连接端口是 6379.
mysql 服务端默认连接端口是 3306
Mogodb 服务端默认连接端口是 27017,还有 28017。
在平时,我们往往需要查看 6379 端口是否被占用。可以用以下命令:
1 2 3 4 5 6 7 8 9 | [neil@neilhost bin]$ netstat -tunpl | 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 0.0.0.0:6379 0.0.0.0:* LISTEN - tcp6 0 0 :::6379 :::* LISTEN - [neil@neilhost bin]$ sudo netstat -tunpl | grep 6379 [ sudo ] password for neil: tcp 0 0 0.0.0.0:6379 0.0.0.0:* LISTEN 13154/. /redis-serve tcp6 0 0 :::6379 :::* LISTEN 13154/. /redis-serve |
注意,redis 服务需要 su 权限才能查看,不然只能检查到 6379 被某个进程占用,但是看不到进程名称。
至此,redis 服务已经按照配置文件启动成功!!
客户端登陆
步骤 5:
客户端连接
duanxz@ubuntu:~/soft$ sudo /usr/local/redis/bin/redis-cli 127.0.0.1:6379>
关闭 Redis 服务
步骤 6:
停止 Redis 实例
我们可以使用 pkill redis-server
1 2 3 4 5 6 7 8 9 10 | [neil@neilhost bin]$ sudo pkill redis-server [neil@neilhost bin]$ sudo netstat -tunpl | grep 6379 [neil@neilhost bin]$ [neil@neilhost bin]$ pstree -p | grep redis [neil@neilhost bin]$ [neil@neilhost bin]$ [neil@neilhost bin]$ sudo /usr/local/redis/bin/redis-cli Could not connect to Redis at 127.0.0.1:6379: Connection refused not connected> not connected> exit |
关闭之后,发现 6379 就不再被占用了,redis 的进程也都没有了。
客户登陆也无法成功了。
也可以使用 /usr/local/redis/bin/redis-cli shutdown,这种方法使用客户端命令 redis-cli
1 2 3 4 5 6 7 8 9 10 | [neil@neilhost bin]$ sudo . /redis-server /usr/local/redis/etc/redis .conf [neil@neilhost bin]$ pstree -p | grep redis |-redis-server(13509)-+-{redis-server}(13511) | `-{redis-server}(13512) [neil@neilhost bin]$ sudo /usr/local/redis/bin/redis-cli shutdown [neil@neilhost bin]$ pstree -p | grep redis [neil@neilhost bin]$ [neil@neilhost bin]$ sudo netstat -tunpl | grep 6379 [neil@neilhost bin]$ [neil@neilhost bin]$ |
还可以使用命令 killall 和 kill -9,可以仿照我在之前博客中的关闭 Mongdb 的方法。
=====================================================================
Redis+Sentinel 安装与配置
在这里我们搭建的是一个 1 主 3 从的 redis+3 个哨兵集群的环境,由于是在一台物理机上,所有我们用端口区分。
物理机 IP:192.168.0.12
主节点 master 端口:6301
从节点 slave1 端口:6315
从节点 slave2 端口:6316
从节点 slave3 端口:6317
哨兵 sentinel1 端口:26301
哨兵 sentinel2 端口:26302
哨兵 sentinel3 端口:26303
一、下载安装
$ wget http://download.redis.io/releases/redis-3.0.0.tar.gz
$ tar xzf redis-3.0.0.tar.gz
$ cd redis-3.0.0
$ make
二、复制文件
cp redis-benchmark redis-cli redis-server redis-sentinel /usr/bin/ #这个倒是很有用,这样就不用再执行时加上./ 了,而且可以在任何地方执行
三、设置内存分配策略(可选,根据服务器的实际情况进行设置)
/proc/sys/vm/overcommit_memory
可选值:0、1、2。
0, 表示内核将检查是否有足够的可用内存供应用进程使用;如果有足够的可用内存,内存申请允许;否则,内存申请失败,并把错误返回给应用进程。
1, 表示内核允许分配所有的物理内存,而不管当前的内存状态如何。
2, 表示内核允许分配超过所有物理内存和交换空间总和的内存
四、开启 redis 端口,修改防火墙配置文件
vi /etc/sysconfig/iptables
加入端口配置
-A RH-Firewall-1-INPUT -m state --state NEW -m tcp -p tcp --dport 6379 -j ACCEPT
重新加载规则
service iptables restart
五、配置 redis.config 文件
1、主节点 master 的配置文件 redis_master_6301.config:
# Redis configuration file example
################################## INCLUDES ###################################
# include /path/to/local.conf
# include /path/to/other.conf
################################ GENERAL #####################################
daemonize yes
pidfile ./run/redis_slaver1_6315.pid
port 6301
tcp-backlog 511
# bind 192.168.1.100 10.0.0.1
# bind 127.0.0.1
# unixsocket /tmp/redis.sock
# unixsocketperm 700
timeout 0
tcp-keepalive 0
loglevel notice
logfile "./run/logs/log_master_6301.log"
databases 16
################################ SNAPSHOTTING ################################
save ""
# save 900 1
# save 300 10
# save 60 10000
stop-writes-on-bgsave-error yes
rdbcompression yes
rdbchecksum no
dbfilename dump_6301.rdb
dir ./run/data
################################# REPLICATION #################################
slave-serve-stale-data yes
slave-read-only yes
repl-diskless-sync no
repl-diskless-sync-delay 5
# repl-ping-slave-period 10
# repl-timeout 60
repl-disable-tcp-nodelay no
# repl-backlog-size 1mb
# repl-backlog-ttl 3600
slave-priority 100
# min-slaves-to-write 3
# min-slaves-max-lag 10
################################## SECURITY ###################################
# rename-command CONFIG ""
################################### LIMITS ####################################
# maxclients 10000
# maxmemory <bytes>
# maxmemory-policy noeviction
# maxmemory-samples 5
############################## APPEND ONLY MODE ###############################
appendonly no
appendfilename "appendonly_6301.aof"
appendfsync no
no-appendfsync-on-rewrite no
auto-aof-rewrite-percentage 100
auto-aof-rewrite-min-size 64mb
aof-load-truncated yes
################################ LUA SCRIPTING ###############################
lua-time-limit 5000
################################ REDIS CLUSTER ###############################
# cluster-enabled yes
# cluster-config-file nodes-6379.conf
# cluster-node-timeout 15000
# cluster-slave-validity-factor 10
# cluster-migration-barrier 1
# cluster-require-full-coverage yes
################################## SLOW LOG ###################################
slowlog-log-slower-than 10000
slowlog-max-len 128
################################ LATENCY MONITOR ##############################
latency-monitor-threshold 0
############################# EVENT NOTIFICATION ##############################
notify-keyspace-events ""
############################### ADVANCED CONFIG ###############################
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
2、从节点 slave1 的配置文件 redis_slave_6315.config:
# Redis configuration file example
################################## INCLUDES ###################################
# include /path/to/local.conf
# include /path/to/other.conf
################################ GENERAL #####################################
daemonize yes
pidfile ./run/redis_slaver1_6315.pid
port 6315
tcp-backlog 511
# bind 192.168.1.100 10.0.0.1
# bind 127.0.0.1
# unixsocket /tmp/redis.sock
# unixsocketperm 700
timeout 0
tcp-keepalive 0
loglevel notice
logfile "./run/logs/log_slaver1_6315.log"
databases 16
################################ SNAPSHOTTING ################################
save ""
# save 900 1
# save 300 10
# save 60 10000
stop-writes-on-bgsave-error yes
rdbcompression yes
rdbchecksum no
dbfilename dump_6315.rdb
dir ./run/data
################################# REPLICATION #################################
slaveof 192.168.0.12 6301
slave-serve-stale-data yes
slave-read-only yes
repl-diskless-sync no
repl-diskless-sync-delay 5
# repl-ping-slave-period 10
# repl-timeout 60
repl-disable-tcp-nodelay no
# repl-backlog-size 1mb
# repl-backlog-ttl 3600
slave-priority 80
# min-slaves-to-write 3
# min-slaves-max-lag 10
################################## SECURITY ###################################
# rename-command CONFIG ""
################################### LIMITS ####################################
# maxclients 10000
# maxmemory <bytes>
# maxmemory-policy noeviction
# maxmemory-samples 5
############################## APPEND ONLY MODE ###############################
appendonly no
appendfilename "appendonly_6315.aof"
appendfsync no
no-appendfsync-on-rewrite no
auto-aof-rewrite-percentage 100
auto-aof-rewrite-min-size 64mb
aof-load-truncated yes
################################ LUA SCRIPTING ###############################
lua-time-limit 5000
################################ REDIS CLUSTER ###############################
# cluster-enabled yes
# cluster-config-file nodes-6379.conf
# cluster-node-timeout 15000
# cluster-slave-validity-factor 10
# cluster-migration-barrier 1
# cluster-require-full-coverage yes
################################## SLOW LOG ###################################
slowlog-log-slower-than 10000
slowlog-max-len 128
################################ LATENCY MONITOR ##############################
latency-monitor-threshold 0
############################# EVENT NOTIFICATION ##############################
notify-keyspace-events ""
############################### ADVANCED CONFIG ###############################
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
3、从节点 slave2 的配置文件 redis_slave_6316.config:
与上面 slave1 配置文件需要修改的地方
pidfile ./run/redis_slaver1_6316.pid
port 6316
logfile "./run/logs/log_slaver1_6316.log"
dbfilename dump_6316.rdb
appendfilename "appendonly_6316.aof"
4、从节点 slave3 的配置文件 redis_slave_6317.config:
与上面 slave1 配置文件需要修改的地方
pidfile ./run/redis_slaver1_6317.pid
port 6317
logfile "./run/logs/log_slaver1_6317.log"
dbfilename dump_6317.rdb
appendfilename "appendonly_6317.aof"
5、哨兵 sentinel1 的配置文件 sentinel_26301.config:
# Example sentinel.conf
port 26301
# sentinel announce-ip 1.2.3.4
dir ./run/tmp
sentinel monitor master1 192.168.0.12 6301 2
# sentinel auth-pass mymaster MySUPER--secret-0123passw0rd
# sentinel down-after-milliseconds <master-name> <milliseconds>
sentinel down-after-milliseconds master1 30000
sentinel parallel-syncs master1 1
sentinel failover-timeout master1 180000
# sentinel notification-script mymaster /var/redis/notify.sh
# sentinel client-reconfig-script mymaster /var/redis/reconfig.sh
# sentinel can-failover master1 yes
logfile "/redis-3.0.0/run/logs/sentinellog_m1_26301.log"
6、哨兵 sentinel2 的配置文件 sentinel_26302.config:
# Example sentinel.conf
port 26302
# sentinel announce-ip 1.2.3.4
dir ./run/tmp
sentinel monitor master1 192.168.0.12 6301 2
# sentinel auth-pass mymaster MySUPER--secret-0123passw0rd
# sentinel down-after-milliseconds <master-name> <milliseconds>
sentinel down-after-milliseconds master1 30000
sentinel parallel-syncs master1 1
sentinel failover-timeout master1 180000
# sentinel notification-script mymaster /var/redis/notify.sh
# sentinel client-reconfig-script mymaster /var/redis/reconfig.sh
# sentinel can-failover master1 yes
logfile "/redis-3.0.0/run/logs/sentinellog_m1_26302.log"
7、哨兵 sentinel3 的配置文件 sentinel_26303.config:
# Example sentinel.conf
port 26303
# sentinel announce-ip 1.2.3.4
dir ./run/tmp
sentinel monitor master1 192.168.0.12 6301 2
# sentinel auth-pass mymaster MySUPER--secret-0123passw0rd
# sentinel down-after-milliseconds <master-name> <milliseconds>
sentinel down-after-milliseconds master1 30000
sentinel parallel-syncs master1 1
sentinel failover-timeout master1 180000
# sentinel notification-script mymaster /var/redis/notify.sh
# sentinel client-reconfig-script mymaster /var/redis/reconfig.sh
# sentinel can-failover master1 yes
logfile "/redis-3.0.0/run/logs/sentinellog_m1_26303.log"
六、启动 redis 和哨兵服务
注意:第一次要先启动 redis 主服务、从服务,然后才能启动哨兵服务
1、进入 redis 安装目录的根目录
2、启动 redis 主节点
[root@localhost redis-3.0.0]# redis-server redis_master_6301.config &
3、启动 redis 从节点
[root@localhost redis-3.0.0]# redis-server redis_slave_6315.config &
[root@localhost redis-3.0.0]# redis-server redis_slave_6316.config &
[root@localhost redis-3.0.0]# redis-server redis_slave_6317.config &
4、启动哨兵
[root@localhost redis-3.0.0]# redis-sentinel sentinel_26301.config &
[root@localhost redis-3.0.0]# redis-sentinel sentinel_26302.config &
[root@localhost redis-3.0.0]# redis-sentinel sentinel_26303.config &
到目前为止整个 redis+sentinel 的安装搭建环境就算完成
七、一些常用命令
进入 redis 的安装目录
启动 redis-server:
./redis-server redis_6305.conf &
启动 redis-sentinel
./redis-sentinel sentinel_6301.config
./redis-server sentinel_6316.conf --sentinel &
查看某个端口信息:
./redis-cli -p 6301 info
./redis-cli -p 6301 info Replication
./redis-cli -p 6301 info Sentinel
查看某个主机上的信息 redis 信息
./redis-cli -h 10.16.41.52 -p 6316 info
./redis-cli -h 10.16.41.52 -p 6316 info Replication
./redis-cli -h 10.16.41.52 -p 6316 info Sentinel
关闭本机 redis 服务
./redis-cli -p 6379 shutdown
关闭远程主机 redis 服务
./redis-cli -h 192.168.9.18 -p 6379 shutdown
客户端连接本机
./redis-cli -p 6301
客户端连接远程机
./redis-cli -h 10.16.41.53 -p 6301
切换连接
ssh app@10.16.41.52
使用命令关闭 RDB 持久化:在客户端执行 127.0.0.1:6316> config set save ""
Linux 下 redis 的安装
第一部分:安装 redis
希望将 redis 安装到此目录
1
|
/usr/local/redis |
希望将安装包下载到此目录
1
|
/usr/local/src |
那么安装过程指令如下:
1
2
3
4
5
6
7
|
$ mkdir /usr/local/redis $ cd /usr/local/src $ wget http: //redis.googlecode.com/files/redis-2.6.14.tar.gz $ tar xzf redis-2.6.14.tar.gz $ ln -s redis-2.6.14 redis #建立一个链接 $ cd redis $ make PREFIX=/usr/local/redis install #安装到指定目录中 |
注意上面的最后一行,我们通过 PREFIX 指定了安装的目录。如果 make 失败,一般是你们系统中还未安装 gcc, 那么可以通过 yum 安装:
1
|
yum install gcc |
安装完成后,继续执行 make.
在安装 redis 成功后,你将可以在 /usr/local/redis 看到一个 bin 的目录,里面包括了以下文件:
1
|
redis-benchmark redis-check-aof redis-check-dump redis-cli redis-server |
第二部分:将 redis 做成一个服务
1. 复制脚本到 /etc/rc.d/init.d 目录
ps: /etc/rc.d/init.d/ 目录下的脚本就类似与 windows 中的注册表,在系统启动的时候某些指定脚本将被执行
按以上步骤安装 Redis 时,其服务脚本位于:
1
|
/usr/local/src/redis/utils/redis_init_script |
必须将其复制到 /etc/rc.d/init.d 的目录下:
1
|
cp /usr/local/src/redis/utils/redis_init_script /etc/rc.d/init.d/redis |
将 redis_init_script 复制到 /etc/rc.d/init.d/,同时易名为 redis。
如果这时添加注册服务:
1
|
chkconfig --add redis |
将报以下错误:
1
|
redis服务不支持chkconfig |
为此,我们需要更改 redis 脚本。
2. 更改 redis 脚本
打开使用 vi 打开脚本,查看脚本信息:
1
|
vim /etc/rc.d/init.d/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
30
31
32
33
34
35
36
37
38
39
40
41
42
|
#!/bin/sh #chkconfig: 2345 80 90 # Simple Redis init.d script conceived to work on Linux systems # as it does use of the /proc filesystem. REDISPORT=6379 EXEC=/usr/local/redis/bin/redis-server CLIEXEC=/usr/local/redis/bin/redis-cli PIDFILE=/var/run/redis_${REDISPORT}.pid CONF= "/etc/redis/${REDISPORT}.conf" case "$1" in start) if [ -f $PIDFILE ] then echo "$PIDFILE exists, process is already running or crashed" else echo "Starting Redis server..." $EXEC $CONF & fi ;; stop) if [ ! -f $PIDFILE ] then echo "$PIDFILE does not exist, process is not running" else PID=$(cat $PIDFILE) echo "Stopping ..." $CLIEXEC -p $REDISPORT shutdown while [ -x /proc/${PID} ] do echo "Waiting for Redis to shutdown ..." sleep 1 done echo "Redis stopped" fi ;; *) echo "Please use start or stop as first argument" ;; esac |
和原配置文件相比:
1. 原文件是没有以下第 2 行的内容的,
1
|
#chkconfig: 2345 80 90 |
2. 原文件 EXEC、CLIEXEC 参数,也是有所更改。
1
2
|
EXEC=/usr/local/redis/bin/redis-server CLIEXEC=/usr/local/redis/bin/redis-cli |
3.redis 开启的命令,以后台运行的方式执行。
1
|
$EXEC $CONF & |
ps: 注意后面的那个“&”,即是将服务转到后面运行的意思,否则启动服务时,Redis 服务将
占据在前台,占用了主用户界面,造成其它的命令执行不了。
4. 将 redis 配置文件拷贝到 /etc/redis/${REDISPORT}.conf
1
2
|
mkdir /etc/redis cp /usr/local/src/redis/redis.conf /etc/redis/6379.conf |
这样,redis 服务脚本指定的 CONF 就存在了。默认情况下,Redis 未启用认证,可以通过开启 6379.conf 的 requirepass 指定一个验证密码。
以上操作完成后,即可注册 yedis 服务:
1
|
chkconfig --add redis |
3. 启动 redis 服务
1
|
service redis start |
第三,将 Redis 的命令所在目录添加到系统参数 PATH 中
修改 profile 文件:
1
|
vi /etc/profile |
在最后行追加:
1
|
export PATH= "$PATH:/usr/local/redis/bin" |
然后马上应用这个文件:
1
|
. /etc/profile |
这样就可以直接调用 redis-cli 的命令了,如下所示:
1
2
3
4
5
6
|
$ redis-cli redis 127.0.0.1:6379> auth superman OK redis 127.0.0.1:6379> ping PONG redis 127.0.0.1:6379> |
至此,redis 就成功安装了。
总结下:在 linux 系统中安装 redis,或多或少都能碰到一些问题。在此次安装中 3 个大部分,
1. 下载,安装,这里使用到 wget 命令,make 命令,我不太懂 make 命令的使用,而且一直担心 make 命令如何安装到指定目录下, 此次终于明白了。
2. 如何将一个程序添加到服务,当然也对 /etc/rc.d/init.d 这个文件有所了解。
3. 如何将一个程序的一些命令添加到系统参数中,直接输入命令就能达到对某个程序的操作。
其实就是指定好环境变量。
下篇简单使用 jedis 来对 redis 进行存取。