在Linux下部署Redis以及使用說明

Linux 安裝redis(需要gcc)

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

(也可以在瀏覽器打開http://download.redis.io/releases選擇自己想要的版本)
之後進入解壓後的文件夾,使用make指令
編譯結束後進入解壓後目錄的src文件夾,將其中三個文件使用cp拷貝到你自定義的目錄,使用redis-server即可啓動redis,注意redis.conf不在src文件夾,而是在解壓後的目錄下

-rwxr-xr-x 1 root root 1952104 322 18:38 redis-benchmark
-rwxr-xr-x 1 root root 2031272 322 18:38 redis-cli
-rwxr-xr-x 1 root root 3737528 322 18:38 redis-server

-rw-r--r-- 1 root root   29590 322 20:33 redis.conf

啓動服務


[root@VM_16_11_centos redis]# ./redis-server redis.conf 

常見問題


無法遠程登陸

redis現在的版本開啓redis-server後,redis-cli只能訪問到127.0.0.1,因爲在配置文件中固定了ip,因此需要修改redis.conf(有的版本不是這個文件名,只要找到相對應的conf後綴的文件即可)文件以下幾個地方。
1.bind 127.0.0.1改爲 #bind 127.0.0.1

2.protected-mode yes 改爲 protected-mode no,這樣就可以使用公網ip遠程登陸

3.加入 daemonize no(這個是是否在後臺啓動不佔用一個主程窗口,在配置文件設置後啓動將默認掛在後臺)


創建集羣


安裝 ruby

由於redis3.x版本的集羣創建是使用redis-trib.rb,該文件運行需要Ruby環境
1.使用RVM修改Ruby到最新版本
RVM是一個命令行工具,可以提供一個便捷的多版本Ruby環境的管理和切換。
《 ruby 中國官網 》也介紹了RVM的安裝,安裝方法如下:
這裏所有的命令都是在用戶權限下操作的,任何命令最好都不要用 sudo,

[root@localhost ~]# gpg --keyserver hkp://keys.gnupg.net --recv-keys 409B6B1796C275462A1703113804BB82D39DC0E3
[root@localhost ~]# \curl -sSL https://get.rvm.io | bash -s stable
[root@localhost ~]# source ~/.bashrc
[root@localhost ~]# source ~/.bash_profile

修改 RVM 的 Ruby 安裝源到 Ruby China 的 Ruby 鏡像服務器,這樣能提高安裝速度

[root@localhost ~]# echo "ruby_url=https://cache.ruby-china.org/pub/ruby" > ~/.rvm/user/db

2..輸入命令 “source /usr/local/rvm/scripts/rvm ,查看rvm中管理的所有ruby版本,
3..輸入命令 rvm list known 進行查詢,會出現下面的內容

[root@VM_16_11_centos ~]# rvm list known
# MRI Rubies
[ruby-]1.8.6[-p420]
[ruby-]1.8.7[-head] # security released on head
[ruby-]1.9.1[-p431]
[ruby-]1.9.2[-p330]
[ruby-]1.9.3[-p551]
[ruby-]2.0.0[-p648]
[ruby-]2.1[.10]
[ruby-]2.2[.7]
[ruby-]2.3[.4]
[ruby-]2.4[.1]
ruby-head

.....省略

4.選擇一個適合的版本進行安裝,注意redfis要求的版本是2.0.0以上,輸入rvm install 2.3.4
5.使用剛纔安裝完成的Ruby版本,輸入rvm use 2.3.4


啓動集羣

1.這裏以創建六個節點爲樣例,首先創建6個配置文件700x.conf
文件內容如下,根據端口號進行修改:

[root@VM_16_11_centos cluster]# cat 7000.conf 
daemonize yes
port 7000 
cluster-enabled yes 
#集羣啓動後的節點的配置文件設置,下面默認在同一目錄下
cluster-config-file nodes_7000.conf
cluster-node-timeout 15000
appendonly yes
protected-mode no         

2.啓動6個節點,這裏寫了一個腳本簡化運行

#打開節點所在目錄
cd /usr/local/redis/cluster

rm -f dump.rdb

#使用redis-server啓動,注意要用自己的路徑
../redis-server 7000.conf
../redis-server 7001.conf
../redis-server 7002.conf
../redis-server 7003.conf
../redis-server 7004.conf
../redis-server 7005.conf

。。啓動集羣
../redis-trib.rb create --replicas 1 xxx.xxx.xxx.xxx:7000 xxx.xxx.xxx.xxx:7001 xxx.xxx.xxx.xxx:7002 xxx.xxx.xxx.xxx:7003 xxx.xxx.xxx.xxx:7004 xxx.xxx.xxx.xxx:7005

xxx.xxx.xxx.xxx爲節點的ip地址,其中的redis-trib.rb在編譯好的redis文件夾下的src裏面


客戶端cli常用指令

shutdown#關閉客戶端連接
ttl [key]#查看key的存活時間:
keys * #查看所有key 
info #查看server版本內存使用連接等信息 
client list #獲取客戶連接列表 
client kill 127.0.0.1:33441 #終止某個客戶端連接 
dbsize #當前保存key的數量 
save #立即保存數據到硬盤 
bgsave #異步保存數據到硬盤 
flushdb #當前庫中移除所有key 
flushall #移除所有key從所有庫中 
lastsave #獲取上次成功保存到硬盤的時間戳 
monitor #實時監測服務器接收到的請求 
slowlog len #查詢慢查詢日誌條數 
slowlog get #返回所有的慢查詢日誌,最大值取決於slowlog-max-len配置 
slowlog get 2 #打印兩條慢查詢日誌 
slowlog reset #清空慢查詢日誌信息
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章