ubuntu下安裝
1. 下載Redis的安裝包(x.x.x表示版本)
wget http://download.redis.io/releases/redis-x.x.x.tar.gz
2.在目錄下怎囚,解壓按照包领铐,生成新的目錄redis-x.x.x
tar xvfz redis-x.x.x.tar.gz
3. 進入解壓之后的目錄,進行編譯
cd redis-2.8.9
sudo make
//如果沒有明顯的錯誤祟绊,則表示編譯成功
4.安裝
sudo make install
5.測試是否安裝成功
sudo make test
6.啟動redis服務(wù)
進入redis的目錄逢唤,再進入src目錄彻消。
./redis-server
這時候是用了默認的配置
如果要用自己的配置可以在后面加入配置文件路徑
./redis-server ../redis.conf
查看日志
src/redis-server ../redis.conf > ../logs/log.out 2>&1 &
查看redis日志
(或者配置文件中配置)
tail logs/log.out
7.連接redis服務(wù)
./redis-cli
如果用密碼省店,則--》auth password
密碼的設(shè)置在conf文件中--》requirepass foobared 改為自己的密碼即可嚣崭。
8.java測試連接
使用jredis.jar
@Test
public void link(){
//連接服務(wù)器的 Redis 服務(wù)
Jedis jedis = new Jedis("192.168.247.133");
jedis.auth("redis");
System.out.println("Connection to server sucessfully");
//查看服務(wù)是否運行
System.out.println("Server is running: "+jedis.ping());
}
9.密碼設(shè)置
redis.conf 中requirepass foobared 改為自己的密碼
jedis.auth("redis"); 表示redis的密碼如果不設(shè)置笨触,默認會報錯
redis.clients.jedis.exceptions.JedisDataException: DENIED Redis is running in protected mode because protected mode is enabled, no bind address was specified,
no authentication password is requested to clients. In this mode connections are only accepted from the loopback interface.
If you want to connect from external computers to Redis you may adopt one of the following solutions:
1) Just disable protected mode sending the command 'CONFIG SET protected-mode no' from the loopback interface by connecting to Redis from the same host
the server is running, however MAKE SURE Redis is not publicly accessible from internet if you do so. Use CONFIG REWRITE to make this change permanent.
2) Alternatively you can just disable the protected mode by editing the Redis configuration file, and setting the protected mode option to 'no', and then restarting the server.
3) If you started the server manually just for testing, restart it with the '--protected-mode no' option.
4) Setup a bind address or an authentication password. NOTE: You only need to do one of the above things in order for the server to start accepting connections from the outside.
按照要求修改配置文件即可,但是啟動的時候記得加入配置文件路徑雹舀。
安裝方式二
在 Ubuntu 系統(tǒng)安裝 Redi 可以使用以下命令:
$sudo apt-get update
$sudo apt-get install redis-server
集群配置
1.Redis官方集群方案 Redis Cluster
3.0版本后才開始支持集群案
2.Redis Sharding(分片)集群
master-slave-sentinel方案
redis的方案采用master-slave-sentinel方案芦劣。于是分配三個主機。
角色分配為
master: 192.168.1.100:6379
slave: 192.168.1.101:6380,192.168.1.102:6381
sentinel: 192.168.1.100:26379, 192.168.1.101:26380 , 192.168.1.102:2681
1 主 2 從 3 哨兵
3.1 防火墻
三臺主機開通tcp策略
-A INPUT -m state —state NEW -m tcp -p tcp —dport16661 -j ACCEPT
-A INPUT -m state —state NEW -m tcp -p tcp —dport16662 -j ACCEPT
3.2 創(chuàng)建工作目錄
在每臺主機上創(chuàng)建目錄
mkdir –p /opt/redis-xx
3.3 配置文件
u 3臺主機各需要一份redis.conf文件
u 3臺主機也需要一份sentinel.conf配置文件
將redis-3.2.4/下的redis.conf 拷貝到各個主機工作目錄(/opt/redis- xx /)
cp /opt/redis-3.2.4/redis.conf /opt/redis-xx/ xxxxxxxxxx
將redis-3.2.4/下的sentinel.conf 拷貝到各個主機工作目錄(/opt/redis-mss/)
cp /opt/redis-3.2.4/sentinel.conf /opt/redis-xx/
master的redis.conf部分配置如下
配置master
bind 192.16.1.100 #bind 當(dāng)前主機的ip
protected-mode no # 取消掉保護模式
timeout 60
port 16661 # redis服務(wù)端口说榆,
daemonize yes
logfile /opt/redis-mss/redis.log # 日志目錄虚吟,
dbfilename dump.rdb # db文件的名字
dir /opt/redis-mss # db文件所在的目錄,最終db文件在/opt/redis-mss/dump.rdb
slave的redis.conf部分配置如下
配置slave
protected-mode no # 取消掉保護模式
timeout 60
port 16661 # redis服務(wù)端口签财,
daemonize yes
logfile /opt/redis-mss/redis.log # 日志目錄串慰,
dbfilename dump.rdb # db文件的名字
dir /opt/redis-mss # db文件所在的目錄,最終db文件在/opt/redis-mss/dump.rdb
slaveof 192.168.1.100 6379 # 填寫master的ip和port
sentinel的sentinel.conf的部分配置如下
添加下面兩行
daemonize yes
logfile /opt/redis-mss/sentinel.log
修改
protected-mode no # 取消掉保護模式
port 26379
dir /opt/redis-mss
# sentinel monitor <master-name><ip><redis-port><quorum>
# quorum 表示要多少臺sentinel 主觀判斷master斷開時唱蒸,執(zhí)行auto-failover
sentinel monitor mymaster 192.168.1.100 6379 2
# sentinel down-after-milliseconds <master-name><milliseconds>
# 多少毫秒內(nèi)會認為master掛了邦鲫,默認30s,
sentinel down-after-milliseconds mymaster 5000
需要注意的是神汹,img這個2表示3臺sentinel中有2臺主觀認為master宕機時庆捺,就執(zhí)行自動主從切換(auto-failover)。這個值應(yīng)該盡可能靠近sentinel總數(shù)的50%屁魏。
3.5 啟動
第一次啟動集群先要啟動master
/opt/redis-3.2.4/src/redis-server /opt/redis-xx/redis.conf
然后啟動slav(117, 118)
/opt/redis-3.2.4/src/redis-server /opt/redis-xx/redis.conf
最后啟動sentinel
/opt/redis-3.2.4/src/redis-server /opt/redis-mss/sentinel.conf --sentinel
各個節(jié)點的日志都會放在相應(yīng)的/opt/redis-xx目錄下
我的官網(wǎng)http://guan2ye.com
我的CSDN地址http://blog.csdn.net/chenjianandiyi
我的簡書地址http://www.reibang.com/u/9b5d1921ce34
我的githubhttps://github.com/javanan
我的碼云地址https://gitee.com/jamen/