【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)
[root@middleware02 ~]# gem install redis Fetching: redis-4.0.1.gem (100%) ERROR: Error installing redis: redis requires Ruby version >= 2.2.2. [root@middleware02 ~]# ruby -v [root@middleware02 ~]# 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. [root@middleware02 ~]# 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) <mpapis@gmail.com>" imported gpg: no ultimately trusted keys found gpg: Total number processed: 1 gpg: imported: 1 (RSA: 1) [root@middleware02 ~]# 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) <mpapis@gmail.com>" gpg: aka "Michal Papis <michal.papis@toptal.com>" 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. [root@middleware02 ~]# source /usr/local/rvm/scripts/rvm [root@middleware02 ~]# 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 [root@middleware02 ~]# 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
成功信息如下:
[admin@middleware02 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. [admin@middleware02 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)之后再执行脚本,成功执行;
起好之后,检查机群状态:
redis@redis-open3:~/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/