日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 运维知识 > 数据库 >内容正文

数据库

Redis单机配置多实例,实现主从同步

發(fā)布時間:2024/6/21 数据库 23 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Redis单机配置多实例,实现主从同步 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

版權(quán)聲明:本文為博主原創(chuàng)文章,歡迎轉(zhuǎn)載,轉(zhuǎn)載請保留或注明出處
本文轉(zhuǎn)自:http://www.cnblogs.com/lgeng/p/6623336.html?

?

一,單機多實例:
Redis官網(wǎng): https://redis.io/
1,安裝:

yum -y install gcc gcc-c++ #安裝編譯工具[root@localhost data]# wget http://download.redis.io/releases/redis-3.2.8.tar.gz [root@localhost data]# tar xzf redis-3.2.8.tar.gz [root@localhost data]# cd redis-3.2.8 [root@localhost data]# make [root@localhost data]# mv redis-3.2.8 /usr/local/redis注意:可直接 yum install redis -y 安裝
啟動:(默認(rèn)啟動6379端口)
[root@localhost redis]# /usr/local/redis/src/redis-server?

?

?

?

?

?


2,驗證:使用 redis-cli命令驗證 (注意路徑)
[root@localhost src]# /usr/local/redis/src/redis-cli -p 6379 127.0.0.1:6379> keys * (empty list or set) 127.0.0.1:6379> set k1 v1 OK 127.0.0.1:6379> keys * 1) "k1" 127.0.0.1:6379> get k1 "v1" 127.0.0.1:6379> 127.0.0.1:6379>

?

3,配置環(huán)境變量?
輸入redis-server和redis-cli命令,每次輸入完整的路徑
將路徑添加到PATH變量中

echo 'PATH=${PATH}:/usr/local/redis/src/' >> /etc/profile source /etc/profile #<== 重新加載配置文件

?

?

?

二,單機多實例

創(chuàng)建不同實例的數(shù)據(jù)存放目錄 分別創(chuàng)建6380,6381,6382 三個實例
每個實例目錄中分別創(chuàng)建 conf,db,log目錄,并拷貝配置文件到conf中

[root@localhost /]# mkdir -p /data/redis/{6380,6381,6382}/{conf,db,log} [root@localhost /]# cp /usr/local/redis/redis.conf /data/redis/6380/conf/ [root@localhost /]# cp /usr/local/redis/redis.conf /data/redis/6381/conf/ [root@localhost /]# cp /usr/local/redis/redis.conf /data/redis/6382/conf/ [root@localhost /]# [root@localhost /]# cd /data/redis [root@localhost redis]# ls 6380 6381 6382 [root@localhost redis]# tree . ├── 6380 │ ├── conf │ │ └── redis.conf │ ├── db │ └── log ├── 6381 │ ├── conf │ │ └── redis.conf │ ├── db │ └── log └── 6382├── conf│ └── redis.conf├── db└── log12 directories, 3 files [root@localhost redis]# [root@localhost redis]#

?

?

修改配置文件:


將redis.conf修改為對應(yīng)的實例參數(shù),修改部分如下

[root@localhost redis]# grep "6380\|daemonize" 6380/conf/redis.conf daemonize yes                     <== daemon進(jìn)程運行 pidfile /data/redis/6380/redis.pid     <== 進(jìn)程id存放文件 port 6380     <== 端口 logfile /data/redis/6380/log/redis.log     <== 日志目錄 dir /data/redis/6380/db/     <== db目錄 [root@localhost redis]# [root@localhost redis]# grep "6381" 6381/conf/redis.conf daemonize yes pidfile /data/redis/6381/redis.pid port 6381 logfile /data/redis/6381/log/redis.log dir /data/redis/6381/db/ [root@localhost redis]# [root@localhost redis]# grep "6382" 6382/conf/redis.conf daemonize yes pidfile /data/redis/6382/redis.pid port 6382 logfile /data/redis/6382/log/redis.log dir /data/redis/6382/db/ [root@localhost redis]# [root@localhost redis]#

?

啟動實例:

[root@localhost redis]# redis-server /data/redis/6380/conf/redis.conf [root@localhost redis]# redis-server /data/redis/6381/conf/redis.conf [root@localhost redis]# redis-server /data/redis/6382/conf/redis.conf [root@localhost redis]# [root@localhost redis]# netstat -ntlp | grep -E ":6380|:6381|:6382" tcp 0 0 127.0.0.1:6380 0.0.0.0:* LISTEN 14301/redis-server tcp 0 0 127.0.0.1:6381 0.0.0.0:* LISTEN 14305/redis-server tcp 0 0 127.0.0.1:6382 0.0.0.0:* LISTEN 14309/redis-server [root@localhost redis]# [root@localhost redis]#

?

驗證(略) redis-cli -p 6380 ; redi-cli -p 6381 ; redis-cli -p 6382

?

?

三,配置主從同步
修改從庫配置,6380實例為主庫, ?從庫為 6381,6382

[root@localhost ~]# vim /data/redis/6381/conf/redis.conf [root@localhost ~]# vim /data/redis/6382/conf/redis.conf################################# REPLICATION ################################## Master-Slave replication. Use slaveof to make a Redis instance a copy of # another Redis server. A few things to understand ASAP about Redis replication. # # 1) Redis replication is asynchronous, but you can configure a master to # stop accepting writes if it appears to be not connected with at least # a given number of slaves. # 2) Redis slaves are able to perform a partial resynchronization with the # master if the replication link is lost for a relatively small amount of # time. You may want to configure the replication backlog size (see the next # sections of this file) with a sensible value depending on your needs. # 3) Replication is automatic and does not need user intervention. After a # network partition slaves automatically try to reconnect to masters # and resynchronize with them. # # slaveof <masterip> <masterport> slaveof 127.0.0.1 6380# If the master is password protected (using the "requirepass" configuration # directive below) it is possible to tell the slave to authenticate before # starting the replication synchronization process, otherwise the master will # refuse the slave request. # # masterauth <master-password># When a slave loses its connection with the master, or when the replication # is still in progress, the slave can act in two different ways: # # 1) if slave-serve-stale-data is set to 'yes' (the default) the slave will # still reply to client requests, possibly with out of date data, or the # data set may just be empty if this is the first synchronization. # # 2) if slave-serve-stale-data is set to 'no' the slave will reply with # an error "SYNC with master in progress" to all the kind of commands

?


?驗證:

先在主庫上info一下

[root@localhost 6380]# redis-cli -p 6380 "info" . .此處略去n行 . # Replication role:master            <== 角色:master connected_slaves:2                      <== slave鏈接數(shù) 2 slave0:ip=127.0.0.1,port=6381,state=online,offset=141,lag=1 <== slave 的信息 slave1:ip=127.0.0.1,port=6382,state=online,offset=141,lag=1 master_repl_offset:141 repl_backlog_active:1 repl_backlog_size:1048576 repl_backlog_first_byte_offset:2 repl_backlog_histlen:140

?

看看從庫

[root@localhost 6381]# redis-cli -p 6381 "info" ... .此處略去n行 .... # Replication role:slave         <==角色 slave master_host:127.0.0.1   <==master主機 master_port:6380      <== master端口 master_link_status:up <== 鏈接狀態(tài) up master_last_io_seconds_ago:5 master_sync_in_progress:0 slave_repl_offset:673 slave_priority:100 slave_read_only:1 connected_slaves:0 master_repl_offset:0 repl_backlog_active:0 repl_backlog_size:1048576 repl_backlog_first_byte_offset:0 repl_backlog_histlen:0

?

[root@localhost 6382]# redis-cli -p 6382 "info" ... .此處略去n行 .... # Replication role:slave master_host:127.0.0.1 master_port:6380 master_link_status:up master_last_io_seconds_ago:4 master_sync_in_progress:0 slave_repl_offset:911 slave_priority:100 slave_read_only:1 connected_slaves:0 master_repl_offset:0 repl_backlog_active:0 repl_backlog_size:1048576 repl_backlog_first_byte_offset:0 repl_backlog_histlen:0

?



在主庫上寫入數(shù)據(jù): [root@localhost 6380]# redis-cli -p 6380 127.0.0.1:6380> set k1 v1 OK 127.0.0.1:6380> keys * 1) "k1" 127.0.0.1:6380> get k1 "v1" 127.0.0.1:6380>
從庫上查看是否已同步 [root@localhost 6381]# redis-cli -p 6381 127.0.0.1:6381> keys * 1) "k1" 127.0.0.1:6381> get k1 "v1" 127.0.0.1:6381> 127.0.0.1:6381> exit [root@localhost 6381]# redis-cli -p 6382 127.0.0.1:6382> keys * 1) "k1" 127.0.0.1:6382> get k1 "v1" 127.0.0.1:6382>

?

?

主庫刪除數(shù)據(jù):

[root@localhost 6380]# redis-cli -p 6380 127.0.0.1:6380> keys * 1) "k1" 127.0.0.1:6380> del k1 (integer) 1 127.0.0.1:6380> keys * (empty list or set) 127.0.0.1:6380>

?

從庫查看?

[root@localhost 6381]# redis-cli -p 6381 127.0.0.1:6381> keys * (empty list or set) 127.0.0.1:6381> exit [root@localhost 6381]# redis-cli -p 6382 127.0.0.1:6382> keys * (empty list or set) 127.0.0.1:6382> 127.0.0.1:6382> exit [root@localhost 6381]#

?

END

本文轉(zhuǎn)自:http://www.cnblogs.com/lgeng/p/6623336.html?

轉(zhuǎn)載于:https://www.cnblogs.com/lgeng/p/6623336.html

總結(jié)

以上是生活随笔為你收集整理的Redis单机配置多实例,实现主从同步的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。