Redis一主两从
Redis简介:Redis 是一个开源(BSD许可)的,内存中的数据结构存储系统,它可以用作数据库、缓存和消息中间件。 它支持多种类型的数据结构,如 字符串(strings), 散列(hashes), 列表(lists), 集合(sets), 有序集合(sorted sets) 与范围查询, bitmaps, hyperloglogs 和 地理空间(geospatial) 索引半径查询。 Redis 内置了 复制(replication),LUA脚本(Lua scripting), LRU驱动事件(LRU eviction),事务(transactions) 和不同级别的 磁盘持久化(persistence), 并通过 Redis哨兵(Sentinel)和自动 分区(Cluster)提供高可用性(high availability)。
以上来自Redis中文官网:http://redis.cn/ , 下载地址:https://redis.io/download/
上一章中我们已经在redis1主机中安装了Redis,所以只需要把redis1已经解压好的目录直接scp到另外的redis2,redis3主机中。然后make install,再启动redis服务就ok了。
1. 再redis1,redis2中建立redis目录。(提前做好解析)
[root@Redis1 redis]# cat /etc/hosts ##做好解析redis2,redis3的解析 127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4 ::1 localhost localhost.localdomain localhost6 localhost6.localdomain6 192.168.2.20 redis2 192.168.2.30 redis3 [root@Redis1 redis]# scp -r redis-5.0.14 redis2:/root/redis ......... redis_init_script.tpl 100% 1047 1.8MB/s 00:00 01_create_tarball.sh 100% 314 535.0KB/s 00:00 02_upload_tarball.sh 100% 304 484.2KB/s 00:00 03_test_release.sh 100% 732 1.2MB/s 00:00 04_release_hash.sh 100% 404 698.3KB/s 00:00 changelog.tcl 100% 1197 1.9MB/s 00:00 speed-regression.tcl 100% 3787 4.9MB/s 00:00 whatisdoing.sh [root@Redis1 redis]# scp -r redis-5.0.14 redis3:/root/redis ........ redis_init_script.tpl 100% 1047 1.8MB/s 00:00 01_create_tarball.sh 100% 314 535.0KB/s 00:00 02_upload_tarball.sh 100% 304 484.2KB/s 00:00 03_test_release.sh 100% 732 1.2MB/s 00:00 04_release_hash.sh 100% 404 698.3KB/s 00:00 changelog.tcl 100% 1197 1.9MB/s 00:00 speed-regression.tcl 100% 3787 4.9MB/s 00:00 whatisdoing.sh
2. 在redis2,redis3中执make install,开启redis服务(前提是已经安装了gcc和make)
[root@Redis2 ~]# cd redis/redis-5.0.14/ [root@Redis2 redis-5.0.14]# make install make[1]: Entering directory '/root/redis/redis-5.0.14/src' Hint: It's a good idea to run 'make test' ;) INSTALL install INSTALL install INSTALL install INSTALL install INSTALL install make[1]: Leaving directory '/root/redis/redis-5.0.14/src' [root@Redis2 redis-5.0.14]# utils/install_server.sh Welcome to the redis service installer This script will help you easily set up a running redis server Please select the redis port for this instance: [6379] Selecting default: 6379 Please select the redis config file name [/etc/redis/6379.conf] Selected default - /etc/redis/6379.conf Please select the redis log file name [/var/log/redis_6379.log] Selected default - /var/log/redis_6379.log Please select the data directory for this instance [/var/lib/redis/6379] Selected default - /var/lib/redis/6379 Please select the redis executable path [/usr/local/bin/redis-server] Selected config: Port : 6379 Config file : /etc/redis/6379.conf Log file : /var/log/redis_6379.log Data dir : /var/lib/redis/6379 Executable : /usr/local/bin/redis-server Cli Executable : /usr/local/bin/redis-cli Is this ok? Then press ENTER to go on or Ctrl-C to abort. Copied /tmp/6379.conf => /etc/init.d/redis_6379 Installing service... Successfully added to chkconfig! Successfully added to runlevels 345! Starting Redis server... Installation successful!
3. 编辑配置文件,使其监听所有端口,重启redis服务使其生效
[root@Redis2 redis-5.0.14]# vim /etc/redis/6379.conf ...... 70 bind 0.0.0.0 ...... [root@Redis2 redis-5.0.14]# netstat -antlp | grep 6379 tcp 0 0 0.0.0.0:6379 0.0.0.0:* LISTEN 33768/redis-server [root@Redis2 redis-5.0.14]# /etc/init.d/redis_6379 restart Stopping ... Redis stopped Starting Redis server...
4. 更改配置文件指向master节点,更改后重启redis服务使其生效
[root@Redis2 redis-5.0.14]# vim /etc/redis/6379.conf ...... 288 replicaof 192.168.2.10 6379 ...... [root@Redis2 redis-5.0.14]# /etc/init.d/redis_6379 restart Stopping ... Redis stopped Starting Redis server...
注意:从第2步开始,redis2和redis3操作步骤相同,都需操作。
查看一主两从效果
redis1主master中 [root@Redis1 redis-5.0.14]# redis-cli 127.0.0.1:6379> set name redis OK 127.0.0.1:6379> get name "redis" 127.0.0.1:6379> redis2从slave中 [root@Redis2 redis-5.0.14]# redis-cli 127.0.0.1:6379> get name "redis" 127.0.0.1:6379> set name redhat (error) READONLY You can't write against a read only replica. ##只能查看,不能更改 127.0.0.1:6379> redis3中从slave root@Redis3 redis-5.0.14]# redis-cli 127.0.0.1:6379> get name "redis" 127.0.0.1:6379> set name redhat (error) READONLY You can't write against a read only replica. ##只能查看,不能更改 127.0.0.1:6379> 可以在redis1主masert查看从节点信息 ...... # Replication role:master connected_slaves:2 ##两个从节点 slave0:ip=192.168.2.20,port=6379,state=online,offset=1331,lag=0 ##redis2从节点 slave1:ip=192.168.2.30,port=6379,state=online,offset=1331,lag=0 ##redis3从节点 master_replid:153f8b06c48a1569a8559322131c2ca11b7f0ea3 master_replid2:0000000000000000000000000000000000000000 master_repl_offset:1331 second_repl_offset:-1 repl_backlog_active:1 repl_backlog_size:1048576 repl_backlog_first_byte_offset:1 repl_backlog_histlen:1331