天天看點

Redis叢集安裝和使用

【Ubuntu上快速安裝Redis】

打開終端,然後鍵入以下指令:

$sudo apt-get update

$sudo apt-get install redis-server

這将在您的計算機上安裝Redis。

啟動 Redis

$redis-server

或者:sudo /etc/init.d/redis-server start

停止:sudo /etc/init.d/redis-server stop

進入cli指令行界面

$redis-cli

使用:

redis 127.0.0.1:6379> SET name "Zollty"

OK

redis 127.0.0.1:6379> GET name

"Zollty"

【安裝步驟一:編譯安裝redis】

去Redis官方網站下載下傳最新包,例如:

http://download.redis.io/releases/redis-4.0.7.tar.gz

然後解壓 并編譯:

tar -xvf redis-4.0.7.tar.gz

cd redis-4.0.7

make & make install

注意,有可能會報錯:

/bin/sh: cc: command not found

以及

cannot create regular file ‘/usr/local/bin/redis-server’

前者是沒有安裝 gcc,是以安裝一下:

yum install gcc

後者是賬号沒權限,換成 root 使用者執行。或者指定其他編譯輸出目錄,例如:

make PREFIX=/home/admin/redis-4.0.7 install(等價于make PREFIX=.. install)

(如果不指定,預設為:/usr/local)

如果報錯:

zmalloc.h:50:31: fatal error: jemalloc/jemalloc.h: No such file or directory

可參照:http://blog.csdn.net/libraryhu/article/details/64920124

執行:make MALLOC=libc,然後再執行 make install即可。

【安裝步驟二:配置叢集】

參考官方的文檔:https://redis.io/topics/cluster-tutorial

叢集要用到 ruby腳本,是以先安裝ruby:

yum install ruby

ruby -v

然後執行:

gem install redis

報錯,ruby版本太低,redis requires Ruby version >= 2.2.2.

解決方法如下:(具體參見:https://www.cnblogs.com/carryping/p/7447823.html)

[[email protected] ~]# gem install redis

Fetching: redis-4.0.1.gem (100%)

ERROR:  Error installing redis:

redis requires Ruby version >= 2.2.2.

[[email protected] ~]# ruby -v

[[email protected] ~]# curl -L get.rvm.io | bash -s stable

% Total    % Received % Xferd  Average Speed   Time    Time     Time  Current

Dload  Upload   Total   Spent    Left  Speed

100   194  100   194    0     0    230      0 --:--:-- --:--:-- --:--:--   230

100 24090  100 24090    0     0  15190      0  0:00:01  0:00:01 --:--:--  543k

Downloading https://github.com/rvm/rvm/archive/1.29.3.tar.gz

Downloading https://github.com/rvm/rvm/releases/download/1.29.3/1.29.3.tar.gz.asc

gpg: directory `/root/.gnupg' created

gpg: new configuration file `/root/.gnupg/gpg.conf' created

gpg: WARNING: options in `/root/.gnupg/gpg.conf' are not yet active during this run

gpg: keyring `/root/.gnupg/pubring.gpg' created

gpg: Signature made Sun 10 Sep 2017 04:59:21 PM EDT using RSA key ID BF04FF17

gpg: Can't check signature: No public key

Warning, RVM 1.26.0 introduces signed releases and automated check of signatures when GPG software found. Assuming you trust Michal Papis import the mpapis public key (downloading the signatures).

GPG signature verification failed for '/usr/local/rvm/archives/rvm-1.29.3.tgz' - 'https://github.com/rvm/rvm/releases/download/1.29.3/1.29.3.tar.gz.asc'! Try to install GPG v2 and then fetch the public key:

gpg2 --recv-keys 409B6B1796C275462A1703113804BB82D39DC0E3

or if it fails:

command curl -sSL https://rvm.io/mpapis.asc | gpg2 --import -

the key can be compared with:

https://rvm.io/mpapis.asc

https://keybase.io/mpapis

NOTE: GPG version 2.1.17 have a bug which cause failures during fetching keys from remote server. Please downgrade or upgrade to newer version (if available) or use the second method described above.

[[email protected] ~]# gpg2 --recv-keys 409B6B1796C275462A1703113804BB82D39DC0E3

gpg: keyring `/root/.gnupg/secring.gpg' created

gpg: requesting key D39DC0E3 from hkp server keys.gnupg.net

gpg: /root/.gnupg/trustdb.gpg: trustdb created

gpg: key D39DC0E3: public key "Michal Papis (RVM signing) <[email protected]>" imported

gpg: no ultimately trusted keys found

gpg: Total number processed: 1

gpg:               imported: 1  (RSA: 1)

[[email protected] ~]# curl -L get.rvm.io | bash -s stable

% Total    % Received % Xferd  Average Speed   Time    Time     Time  Current

Dload  Upload   Total   Spent    Left  Speed

100   194  100   194    0     0    251      0 --:--:-- --:--:-- --:--:--   251

100 24090  100 24090    0     0  16358      0  0:00:01  0:00:01 --:--:--  183k

Downloading https://github.com/rvm/rvm/archive/1.29.3.tar.gz

Downloading https://github.com/rvm/rvm/releases/download/1.29.3/1.29.3.tar.gz.asc

gpg: Signature made Sun 10 Sep 2017 04:59:21 PM EDT using RSA key ID BF04FF17

gpg: Good signature from "Michal Papis (RVM signing) <[email protected]>"

gpg:                 aka "Michal Papis <[email protected]>"

gpg:                 aka "[jpeg image of size 5015]"

gpg: WARNING: This key is not certified with a trusted signature!

gpg:          There is no indication that the signature belongs to the owner.

Primary key fingerprint: 409B 6B17 96C2 7546 2A17  0311 3804 BB82 D39D C0E3

Subkey fingerprint: 62C9 E5F4 DA30 0D94 AC36  166B E206 C29F BF04 FF17

GPG verified '/usr/local/rvm/archives/rvm-1.29.3.tgz'

Creating group 'rvm'

Installing RVM to /usr/local/rvm/

Installation of RVM in /usr/local/rvm/ is almost complete:

* First you need to add all users that will be using rvm to 'rvm' group,

and logout - login again, anyone using rvm will be operating with `umask u=rwx,g=rwx,o=rx`.

* To start using RVM you need to run `source /etc/profile.d/rvm.sh`

in all your open shell windows, in rare cases you need to reopen all shell windows.

[[email protected] ~]# source /usr/local/rvm/scripts/rvm

[[email protected] ~]# 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

[[email protected] ~]# rvm install 2.4.1

Searching for binary rubies, this might take some time.

Found remote file https://rvm_io.global.ssl.fastly.net/binaries/centos/7/x86_64/ruby-2.4.1.tar.bz2

Checking requirements for centos.

Installing requirements for centos.

Installing required packages: patch, autoconf, automake, bison, bzip2, gcc-c++, libffi-devel, libtool, patch, readline-devel, sqlite-devel, zlib-devel, libyaml-devel, openssl-devel................/

然後再執行gem install redis就行了。

然後準備檔案:

mkdir redis-cluster

cd redis-cluster

然後把redis的編譯好的檔案(預設在/usr/local/bin/redis-server)拷貝到這個目錄。

然後建立叢集目錄:

mkdir 7000 7001 7002 7003 7004 7005

在每個目錄下,建立 redis.conf,内容如下:

port 7000

bind 192.168.11.244

daemonize yes

cluster-enabled yes

cluster-config-file nodes.conf

cluster-node-timeout 5000

appendonly yes

注意修改對應的port值和bind ip(如果ip設定為127.0.0.1的話,就隻能在本機通路)。

并且設定daemonize yes(在背景運作,否則退出就假死,連接配接報錯[ERR] Sorry, can't connect to node)。

然後就可以挨個啟動了,例如:

cd 7000

../redis-server redis.conf

啟動之後,可以測試一下:

../bin/redis-cli -h 192.168.11.243 -p 7000

如果都啟動成功了,然後就執行ruby建立叢集的腳本:

./redis-trib.rb create --replicas 1 192.168.11.244:7000 192.168.11.244:7001 192.168.11.243:7000 192.168.11.243:7001 192.168.11.245:7000 192.168.11.245:7001

成功資訊如下:

[[email protected] bin]$ ./redis-trib.rb create --replicas 1 192.168.11.244:7000 192.168.11.244:7001 192.168.11.243:7000 192.168.11.243:7001 192.168.11.245:7000 192.168.11.245:7001

>>> Creating cluster

>>> Performing hash slots allocation on 6 nodes...

Using 3 masters:

192.168.11.244:7000

192.168.11.243:7000

192.168.11.245:7000

Adding replica 192.168.11.243:7001 to 192.168.11.244:7000

Adding replica 192.168.11.245:7001 to 192.168.11.243:7000

Adding replica 192.168.11.244:7001 to 192.168.11.245:7000

M: bf24b36914715e851999e093618e991281dc29c5 192.168.11.244:7000

slots:0-5460 (5461 slots) master

S: fdd20a10e86af1be6d88d1c32c8f7f29f93a7f9e 192.168.11.244:7001

replicates 69499ebf2b09166e9df1ced51740a5783cde0386

M: 5b793cf1a1cf891185a875ce8bd936934233769a 192.168.11.243:7000

slots:5461-10922 (5462 slots) master

S: 4d9689dafb2096387aa50d376bc2c4f581a854af 192.168.11.243:7001

replicates bf24b36914715e851999e093618e991281dc29c5

M: 69499ebf2b09166e9df1ced51740a5783cde0386 192.168.11.245:7000

slots:10923-16383 (5461 slots) master

S: d7cb9d7c546c240ed2802add308d513c601bae7e 192.168.11.245:7001

replicates 5b793cf1a1cf891185a875ce8bd936934233769a

Can I set the above configuration? (type 'yes' to accept): yes

>>> Nodes configuration updated

>>> Assign a different config epoch to each node

>>> Sending CLUSTER MEET messages to join the cluster

Waiting for the cluster to join...

>>> Performing Cluster Check (using node 192.168.11.244:7000)

M: bf24b36914715e851999e093618e991281dc29c5 192.168.11.244:7000

slots:0-5460 (5461 slots) master

1 additional replica(s)

S: 4d9689dafb2096387aa50d376bc2c4f581a854af 192.168.11.243:7001

slots: (0 slots) slave

replicates bf24b36914715e851999e093618e991281dc29c5

M: 5b793cf1a1cf891185a875ce8bd936934233769a 192.168.11.243:7000

slots:5461-10922 (5462 slots) master

1 additional replica(s)

M: 69499ebf2b09166e9df1ced51740a5783cde0386 192.168.11.245:7000

slots:10923-16383 (5461 slots) master

1 additional replica(s)

S: d7cb9d7c546c240ed2802add308d513c601bae7e 192.168.11.245:7001

slots: (0 slots) slave

replicates 5b793cf1a1cf891185a875ce8bd936934233769a

S: fdd20a10e86af1be6d88d1c32c8f7f29f93a7f9e 192.168.11.244:7001

slots: (0 slots) slave

replicates 69499ebf2b09166e9df1ced51740a5783cde0386

[OK] All nodes agree about slots configuration.

>>> Check for open slots...

>>> Check slots coverage...

[OK] All 16384 slots covered.

[[email protected] bin]$

如果上面的指令報錯:

[ERR] Node is not empty. Either the node already knows other nodes (check with CLUSTER NODES) or contains some key in database 0

,則将機群所有的機器停掉,然後删除下面兩個檔案,再重新啟動後執行組成叢集:

rm appendonly.aof nodes.conf

網上的解決方法:

解決方法:

1)、将需要新增的節點下aof、rdb等本地備份檔案删除;

2)、同時将新Node的叢集配置檔案删除,即:删除你redis.conf裡面cluster-config-file所在的檔案;

3)、再次添加新節點如果還是報錯,則登入新Node,./redis-cli–h x –p對資料庫進行清除:

172.168.63.201:7001>  flushdb      #清空目前資料庫

通過以上解決方法中的1)、2)或者3)之後再執行腳本,成功執行;

起好之後,檢查機群狀态:

[email protected]:~/7001$ bin/redis-cli -h 172.16.1.169 -p 7000

172.16.1.169:7000> cluster info

cluster_state:ok

cluster_slots_assigned:16384

cluster_slots_ok:16384

cluster_slots_pfail:0

cluster_slots_fail:0

cluster_known_nodes:6

cluster_size:3

cluster_current_epoch:6

cluster_my_epoch:1

cluster_stats_messages_sent:157

cluster_stats_messages_received:157

172.16.1.169:7000> exit

文檔大全:http://doc.redisfans.com/

繼續閱讀