redis搭建

 1..1開始安裝redis

redis的官方網站(http://www.redis.io)下載最新的穩定版本redis,在此選的是redis-3.0.7版本。

操作命令如下:

1獲取redis安裝包,解壓

wget http://download.redis.io/releases/redis-3.0.7.tar.gz
tar xzf redis-3.0.7.tar.gz

2、進入目錄,並查看說明文件README

cd redis-3.0.7
less README

3配置redis安裝包

cd src/
make MANIFESTO=jemalloc && make PREFIX=/usr/local/redis-3.0.7/src install
make test

4創建軟連接

ln -s /usr/local/src/redis-3.0.7 /usr/local/redis

5配置環境語言

LANG=en_US.UTF-8

6查看redis命令的目錄

tree /usr/local/redis-3.0.7/src/bin/
執行結果如下:
/usr/local/redis/bin/
├── redis-benchmark
├── redis-check-aof
├── redis-check-dump
├── redis-cli
├── redis-sentinel -> redis-server
└── redis-server


 

命令執行完成後,會在/usr/local/redis/bin/目錄下生成5可執行文件,分別是:

 

redis-benchmark  redis-check-aof  redis-check-dump  redis-cli  redis-sentinel  redis-server

1.2 配置並啓動redis服務

a. 配置環境變量命令如下

echo ' PATH=/usr/local/redis/bin/:$PATH' >> /etc/profile
source /etc/profile

查看執行結果如下:

tail -1 /etc/profile

提示  PATH=/usr/local/redis-3.0.7/src/bin/:$PATH

查看是否被導入到全局路徑下

which redis-server


執行結果如下

/usr/local/redis/bin/redis-server

b. 查看命令幫助

[root@mysql01 ~]# redis-server --help
Usage: ./redis-server [/path/to/redis.conf] [options]
       ./redis-server - (read config from stdin)
       ./redis-server -v or --version
       ./redis-server -h or --help
       ./redis-server --test-memory <megabytes>
 
Examples:
       ./redis-server (run the server with default conf)
       ./redis-server /etc/redis/6379.conf
       ./redis-server --port 7777
       ./redis-server --port 7777 --slaveof 127.0.0.1 8888
       ./redis-server /etc/myredis.conf --loglevel verbose
 
Sentinel mode:
       ./redis-server /etc/sentinel.conf --sentinel

 

c. 啓動redis服務

操作命令:

mkdir /usr/local/redis/conf -p
cp /usr/local/src/redis-3.0.7/redis.conf /usr/local/redis/conf/ 
                    #安裝包內含有redis的默認配置文件
sysctl vm.overcommit_memory=1
redis-server  /usr/local/redis/conf/redis.conf &


提示:啓動後會出現夯住的命令行,但是其實只要回車就好,因爲此處回車後臺執行

提示:查看端口是否存在確定redis是否啓動成功

[root@mysql01 ~]# netstat -lntup|grep -w 6379
tcp 0  0 0.0.0.0:6379    0.0.0.0:*     LISTEN    5896/redis-server *
tcp 0  0 :::6379         :::*     LISTEN    5896/redis-server *

d. 關閉redis

方式一
redis-cli  shutdown
方式二
killall redis-server


1.3 通過客戶端測試redis服務

redis-cli客戶端幫助

[root@mysql01 ~]#  redis-cli --help
redis-cli 3.0.7
 
Usage: redis-cli [OPTIONS] [cmd [arg [arg ...]]]
  -h <hostname>      Server hostname (default: 127.0.0.1).
  -p <port>          Server port (default: 6379).
  -s <socket>        Server socket (overrides hostname and port).
  -a <password>      Password to use when connecting to the server.

執行redis-cli連接到數據庫

[root@mysql01 ~]# redis-cli 
127.0.0.1:6379>

運用help查看幫助

127.0.0.1:6379> help get
 
  GET key
  summary: Get the value of a key
  since: 1.0.0
  group: string
 
127.0.0.1:6379>


help查看set

127.0.0.1:6379> help set
 
  SET key value [EX seconds] [PX milliseconds] [NX|XX]
  summary: Set the string value of a key
  since: 1.0.0
  group: string

 


1.4 測試

1.4.1 寫入數據

127.0.0.1:6379> set 1 mobartsgame
OK
127.0.0.1:6379> get 1
"mobartsgame"


 

1.4.2 redis支持多種數據類型

127.0.0.1:6379> set mykey "hello world"
OK
127.0.0.1:6379> get mykey
"hello world"
127.0.0.1:6379>


 

1.5 多實例配置

 

1.5.1 配置多實例

 

    多實例配置其實很簡單只需要將redis.conf拷貝到一個目錄然後更改其端口和pid,啓動時指定此文件或者直接 redis-server --port 6381 即可

 

mv redis.conf redis_6379.conf 
 redis-server /usr/local/redis/conf/redis_6380.conf &
[root@puppet01 conf]# diff redis_6379.conf redis_6380.conf 
46c46
< pidfile /var/run/redis.pid
---
> pidfile /var/run/redis_6380.pid
50c50
< port 6379
---
> port 6380
[root@puppet01 conf]# netstat -lntup|grep 63
tcp 0  0 0.0.0.0:6379    0.0.0.0:*     LISTEN    5896/redis-server *
tcp 0  0 :::6379         :::*          LISTEN    5896/redis-server *


或者

[root@puppet01 conf]# redis-server --port 6381 --daemonize yes
[root@puppet01 conf]# redis-cli -p 6381
  1.5.2 測試多實例
[root@puppet01 conf]# redis-cli -p 6380
127.0.0.1:6380> set 1 mobartsgame
OK
127.0.0.1:6380> get 1
"mobartsgame"


 

1.6 redis 主從配置

1.6.1 配置主從複製

 

    配置一個從服務器非常簡單,在配置文件中增加以下一行即可:

 

slaveof 192.168.1.1 6379


 

  當然,你需要將代碼中的 192.168.1.1 6379 替換成你的主服務器的 IP 和端口號。

   另外一種方法是調用 SLAVEOF 命令,輸入主服務器的 IP 和端口,然後同步就會開始:

127.0.0.1:6379> SLAVEOF 192.168.1.1 6379
OK


 

1.6.2 測試主從

 

主庫寫入數據從庫查詢

[root@puppet01 ~]# redis-cli -p 6379
127.0.0.1:6379> set mykey mobartsgame
OK
127.0.0.1:6379> get mykey
"mobartsgame"
127.0.0.1:6379> 
    [root@puppet01 conf]# redis-cli -p 6380
    127.0.0.1:6380> get mykey
    "mobartsgame"


 

以上表示主從複製完成,由於redis是自動實現主寫從讀的,那麼我們從庫測試是否能夠寫入數據

    

127.0.0.1:6380> set mykey2 mobartsgame
    (error) READONLY You can't write against a read only slave.


表示主寫從讀正常


到此redis 搭建及主從完成,集羣請點擊 redis集羣Twemproxy




發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章