1.环境说明 192.168.60.204 redis master 192.168.60.205 redis slave
2.安装编译环境 yum -y install gcc libc
4.3.下载redis 选择版本:
Redis借鉴了Linux操作系统对于版本号的命名规则: 版本号第二位如果是奇数,则为非稳定版本(例如2.7、2.9、3.1),如果是偶数,则为稳定版本(例如2.6、2.8、3.0、3.2), 当前奇数版本就是下一个稳定版本的开发版本,例如2.9版本是3.0版本的开发版本,所以我们在生产环境通常选取偶数版的Redis。 进入目录/usr/local下载Redis: cd /usr/local wget http://download.redis.io/releases/redis-5.0.0.tar.gz
4.安装redis 192.168.60.2014和192.168.60.205都要安装
tar -xf redis-5.0.0.tar.gz make && make install
5.将redis设置成开机启动 cd /usr/local/redis-5.0.0/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] /etc/redis/redis.conf Please select the redis log file name [/var/log/redis_6379.log] /var/log/redis.log Please select the data directory for this instance [/var/lib/redis/6379] /var/lib/redis/db Please select the redis executable path [/usr/local/bin/redis-server] Selected config: Port : 6379 Config file : /etc/redis/redis.conf #redis配置文件 Log file : /var/log/redis.log #redis日志 Data dir : /var/lib/redis/db #redis DB目录 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! 启动redis redis-server /etc/redis/redis.conf [root@master ~]# ss -ntlp |grep redis LISTEN 0 128 *:6379 : users:(("redis-server",pid=17696,fd=6))
6.配置redis主从 主库 192.168.60.204
vim /etc/redis/redis.conf 修改以下几项
bind 0.0.0.0 #任何主机都可以连接 requirepass 123456 #设置redis登录密码 从库 192.168.60.205
bind 0.0.0.0 #任何主机都可以连接 slaveof 192.168.60.204 6379 #主库IP 端口 requirepass 123456 #设置redis登录密码 masterauth 123456 #主库的登录密码 注意:主从配置完两个redis都要重启生效
例如192.168.60.204
[root@master ~]# ps -ef |grep redis
root 983 1 0 16:49 ? 00:00:00 /usr/local/redis/bin/redis-server 0.0.0.0:6379
root 1728 1542 0 16:54 pts/0 00:00:00 grep --color=auto redis
[root@master ~]# kill 983
[root@master ~]# ps -ef |grep redis
root 1742 1542 0 16:55 pts/0 00:00:00 grep --color=auto redis
[root@master ~]# /etc/init.d/redis_6379 start
Starting Redis server...
[root@master ~]# ps -ef |grep redis
root 1751 1 0 16:55 ? 00:00:00 /usr/local/redis/bin/redis-server 0.0.0.0:6379
root 1759 1542 0 16:55 pts/0 00:00:00 grep --color=auto redis
[root@master ~]#
7.验证主从配置
登录从库192.168.60.205
[root@slave ~]# redis-cli
127.0.0.1:6379> AUTH 123456
OK
127.0.0.1:6379>
127.0.0.1:6379> info Replication
# Replication
role:slave 角色从库
master_host:192.168.60.204 主库IP
master_port:6379 主库端口
master_link_status:up 主库状态
master_last_io_seconds_ago:6
master_sync_in_progress:0
slave_repl_offset:7952
slave_priority:100
slave_read_only:1
connected_slaves:0
master_replid:57de84c0a6d43857a92eb1a2b9b60cc054522717
master_replid2:0000000000000000000000000000000000000000
master_repl_offset:7952
second_repl_offset:-1
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:1
repl_backlog_histlen:7952
127.0.0.1:6379>
验证主从是否同步数据
[root@master ~]# redis-cli
127.0.0.1:6379> AUTH 123456
OK
127.0.0.1:6379> set bool AAA
OK
127.0.0.1:6379> get bool
"AAA"
127.0.0.1:6379>
从库查看下是否有值
[root@slave ~]# redis-cli
127.0.0.1:6379> AUTH 123456
OK
127.0.0.1:6379> get bool
"AAA"
127.0.0.1:6379>