1.關閉·防火墻
2.安装ruby环境 (如果redis版本比较低,则需要安装ruby。任选一台机器安装ruby即可) redis5.0.4,不需要安装ruby,直接创建集群即可
# 本次因安裝redis3.2.3 故要安裝 ruby
[root@MYSQL local]# cd /usr/local/src/
------------------------------------------------------------------------------------------------------------
#1. 安装以下软件:
[root@MYSQL local]# yum install -y wget openssl openssl-client
#2. 安装zlib
[root@MYSQL local]# wget https://zlib.net/zlib-1.2.11.tar.gz
#解压缩zlib包:
[root@MYSQL local]# tar -xvf zlib-1.2.11.tar.gz
#编绎及安装zlib:
[root@MYSQL local]# mkdir -p /usr/local/src/zlib-1.2.11/build
[root@MYSQL local]# cd /usr/local/src/zlib-1.2.11/build
[root@MYSQL local]# ../configure
[root@MYSQL local]# make
[root@MYSQL local]# make install
---------------------------------------------------------------------------------------------------------------
[root@MYSQL local]# cd /usr/local/src/
[root@MYSQL local]# wget https://cache.ruby-lang.org/pub/ruby/2.5/ruby-2.5.1.tar.gz
#解压缩ruby包:
[root@MYSQL local]# tar -xvf ruby-2.5.1.tar.gz
#编绎及安装ruby:
[root@MYSQL local]# mkdir /usr/local/src/ruby-2.5.1/build
[root@MYSQL local]# cd /usr/local/src/ruby-2.5.1/build
[root@MYSQL local]# ../configure
[root@MYSQL local]# make
[root@MYSQL local]# make install
#设定Ruby及gem:
[root@MYSQL local]# ln -s /usr/local/bin/gem /usr/bin/gem
[root@MYSQL local]# ln -s /usr/local/bin/ruby /usr/bin/ruby
#查看ruby及gem版本确认是否安装成功:
[root@host1 build]# ruby --version
ruby 2.5.1p57 (2018-03-29 revision 63029) [x86_64-linux]
[root@host1 build]#
[root@host1 build]# gem --version
2.7.6
#安装gem-redis
#下载gem-redis:
[root@MYSQL local]# mkdir /usr/local/src/gem/
[root@MYSQL local]# cd /usr/local/src/gem/
[root@MYSQL local]# wget https://rubygems.org/downloads/redis-4.0.1.gem
#使用gem安装ruby的redis接口:
[root@MYSQL local]# gem install -l /usr/local/src/gem/redis-4.0.1.gem
3.下載& 解壓安裝
[root@cs183 ~]# cd /usr/local/src
$ wget https://download.redis.io/releases/redis-3.2.3.tar.gz
$ tar xzf redis-3.2.3.tar.gz
$ cd redis-3.2.3
$ make
$ make install
[root@cs183 redis-3.2.3]# redis-cli -v
redis-cli 3.2.3
[root@cs183 redis-3.2.3]# ll /usr/local/bin/redis*
-rwxr-xr-x 1 root root 5580311 Dec 2 12:16 /usr/local/bin/redis-benchmark
-rwxr-xr-x 1 root root 22185 Dec 2 12:16 /usr/local/bin/redis-check-aof
-rwxr-xr-x 1 root root 7822837 Dec 2 12:16 /usr/local/bin/redis-check-rdb
-rwxr-xr-x 1 root root 5709028 Dec 2 12:16 /usr/local/bin/redis-cli
lrwxrwxrwx 1 root root 12 Dec 2 12:16 /usr/local/bin/redis-sentinel -> redis-server
-rwxr-xr-x 1 root root 7822837 Dec 2 12:16 /usr/local/bin/redis-server
#创建目录存放redis命令和配置文件
[root@cs183 redis-3.2.3]# pwd
/usr/local/src/redis-3.2.3
#mkdir -p /usr/local/redis/bin --作廢
mkdir -p /usr/local/redis/run
mkdir -p /usr/local/redis/log
mkdir -p /usr/local/redis/data/7000
mkdir -p /usr/local/redis/conf
chown -R root:root /usr/local/redis
mv redis.conf /usr/local/redis/conf/redis_7000.conf
cd /usr/local/redis/conf/
#将 127.0.0.1 替换成 本身的物理IP
sed -i 's/127.0.0.1/10.0.30.183/g' redis_7000.conf
sed -i 's/daemonize no/daemonize yes/g' redis_7000.conf
sed -i 's/port 6379/port 7000/g' redis_7000.conf
#编辑redis_7000.conf文件
[root@localhost redis-5.0.7]# vim redis_7000.conf
----------------------------------------------------------------
pidfile /usr/local/redis/run/redis_7000.pid
logfile /usr/local/redis/log/redis_7000.log
dir /usr/local/redis/data/7000
OR 如下命令
sed -i 's/\/var\/run\/redis_6379.pid/\/usr\/local\/redis\/run\/redis_7000.pid/g' redis_7000.conf
sed -i 's/logfile ""/logfile \/usr\/local\/redis\/log\/redis_7000.log/g' redis_7000.conf
sed -i 's/dir .\//dir \/usr\/local\/redis\/data\/7000/g' redis_7000.conf
5.啟動
[root@host1 conf]# which redis-server
/usr/local/bin/redis-server
[root@host1 conf]# which redis-cli
/usr/local/bin/redis-cli
redis-server /usr/local/redis/conf/redis_7000.conf
[root@cs184 conf]# ps -ef | grep redis
root 6585 1 0 13:09 ? 00:00:00 redis-server 10.0.30.184:7000
root 6589 3443 0 13:10 pts/0 00:00:00 grep redis
[root@cs184 conf]# netstat -tunpl | grep 7000
tcp 0 0 10.0.30.184:7000 0.0.0.0:* LISTEN 6585/redis-server 1
6.停止redis服务
redis-cli -h 10.0.30.184 -p 7000 shutdown
7.客戶端連接
redis-cli -h 10.0.30.184 -p 7000
10.0.30.184:7000> info
# Server
redis_version:3.2.3
redis_git_sha1:00000000
redis_git_dirty:0
redis_build_id:3eef1c94399238e5
redis_mode:standalone
os:Linux 2.6.32-431.el6.x86_64 x86_64
arch_bits:64
multiplexing_api:epoll
gcc_version:4.4.7
process_id:6599
run_id:fcbc99f4607e12405cb042456e651726a50420e8
tcp_port:7000
uptime_in_seconds:22
uptime_in_days:0
hz:10
lru_clock:13055267
executable:/usr/local/redis/conf/redis-server
config_file:/usr/local/redis/conf/redis_7000.conf
# Clients
connected_clients:1
client_longest_output_list:0
client_biggest_input_buf:0
blocked_clients:0
# Memory
used_memory:822432
used_memory_human:803.16K
used_memory_rss:7757824
used_memory_rss_human:7.40M
used_memory_peak:822432
used_memory_peak_human:803.16K
total_system_memory:8244068352
total_system_memory_human:7.68G
used_memory_lua:37888
used_memory_lua_human:37.00K
maxmemory:0
maxmemory_human:0B
maxmemory_policy:noeviction
mem_fragmentation_ratio:9.43
mem_allocator:jemalloc-4.0.3
# Persistence
loading:0
rdb_changes_since_last_save:0
rdb_bgsave_in_progress:0
rdb_last_save_time:1606890765
rdb_last_bgsave_status:ok
rdb_last_bgsave_time_sec:-1
rdb_current_bgsave_time_sec:-1
aof_enabled:0
aof_rewrite_in_progress:0
aof_rewrite_scheduled:0
aof_last_rewrite_time_sec:-1
aof_current_rewrite_time_sec:-1
aof_last_bgrewrite_status:ok
aof_last_write_status:ok
# Stats
total_connections_received:1
total_commands_processed:1
instantaneous_ops_per_sec:0
total_net_input_bytes:31
total_net_output_bytes:5856764
instantaneous_input_kbps:0.00
instantaneous_output_kbps:0.00
rejected_connections:0
sync_full:0
sync_partial_ok:0
sync_partial_err:0
expired_keys:0
evicted_keys:0
keyspace_hits:0
keyspace_misses:0
pubsub_channels:0
pubsub_patterns:0
latest_fork_usec:0
migrate_cached_sockets:0
# Replication
role:master
connected_slaves:0
master_repl_offset:0
repl_backlog_active:0
repl_backlog_size:1048576
repl_backlog_first_byte_offset:0
repl_backlog_histlen:0
# CPU
used_cpu_sys:0.01
used_cpu_user:0.00
used_cpu_sys_children:0.00
used_cpu_user_children:0.00
# Cluster
cluster_enabled:0
# Keyspace
8.配置集群
cat <<EOF >> redis_7000.conf
### REDIS CLUSTER ###
cluster-enabled yes
cluster-config-file nodes_7000.conf
cluster-node-timeout 15000
EOF
[root@host1 src]# pwd
/usr/local/src/redis-3.2.3/src
[root@host1 src]# cp redis-trib.rb /usr/local/bin/
#啟動各節點的redis
redis-server /usr/local/redis/conf/redis_7000.conf
#檢查
[root@cs183 conf]# netstat -tunpl | grep 7000
tcp 0 0 10.0.30.183:7000 0.0.0.0:* LISTEN 14054/redis-server
tcp 0 0 10.0.30.183:17000 0.0.0.0:* LISTEN 14054/redis-server
#建立集群前需先啟動各個redis服務,並在其中一个redis服务器中执行以下指令建立集群:
shell>
/usr/local/bin/redis-trib.rb create --replicas 1 \
10.0.30.14:7000 10.0.30.183:7000 10.0.30.220:7000 \
10.0.30.15:7000 10.0.30.184:7000 10.0.30.221:7000
------------------------------------------------------------------------------
>>> Creating cluster
>>> Performing hash slots allocation on 6 nodes...
Using 3 masters:
10.0.30.14:7000
10.0.30.183:7000
10.0.30.220:7000
Adding replica 10.0.30.15:7000 to 10.0.30.14:7000
Adding replica 10.0.30.184:7000 to 10.0.30.183:7000
Adding replica 10.0.30.221:7000 to 10.0.30.220:7000
M: 77d2953e160c6b44cc88d0a3c4595e6520817b3d 10.0.30.14:7000
slots:0-5460 (5461 slots) master
M: 833afe994d8d4b27306b8614d2b27ea9a8e240c1 10.0.30.183:7000
slots:5461-10922 (5462 slots) master
M: a0ac90e2d92df740da3a6e67d6650637a858e990 10.0.30.220:7000
slots:10923-16383 (5461 slots) master
S: edb5f035347d1f27f7b321694963f432242f590f 10.0.30.15:7000
replicates 77d2953e160c6b44cc88d0a3c4595e6520817b3d
S: 9136c0a49eb84b4d0937f77924e1a1d9f44bc7b6 10.0.30.184:7000
replicates 833afe994d8d4b27306b8614d2b27ea9a8e240c1
S: 1576e6d68350ee8e231126b988139fb74bb8920c 10.0.30.221:7000
replicates a0ac90e2d92df740da3a6e67d6650637a858e990
Can I set the above configuration? (type 'yes' to accept):
------------------------------------------------------------------------------------
9.登录集群&验证
[root@cs183 src]# redis-cli -h 10.0.30.183 -p 7000
10.0.30.183:7000> set key1 value1
OK
10.0.30.183:7000> get key1
"value1"
[root@cs184 conf]# redis-cli -h 10.0.30.184 -p 7000
10.0.30.184:7000> get key1
(error) MOVED 9189 10.0.30.183:7000
[root@cs184 conf]# redis-cli -c -h 10.0.30.184 -p 7000
10.0.30.184:7000> get key1
-> Redirected to slot [9189] located at 10.0.30.183:7000
"value1"
[root@dbvm1 src]# redis-cli -c -h 10.0.30.220 -p 7000
10.0.30.220:7000> get key1
-> Redirected to slot [9189] located at 10.0.30.183:7000
"value1"
#開始檢查 -c参数是必需的 如果在连接客户端的时候不加-c选项get key/ set时则会报MOVED 的错误:
[root@cs183 src]# redis-cli -c -h 10.0.30.183 -p 7000
10.0.30.183: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:2
cluster_stats_messages_sent:1653
cluster_stats_messages_received:1653
10.0.30.183:7000> CLUSTER nodes
a0ac90e2d92df740da3a6e67d6650637a858e990 10.0.30.220:7000 master - 0 1606894259747 3 connected 10923-16383
1576e6d68350ee8e231126b988139fb74bb8920c 10.0.30.221:7000 slave a0ac90e2d92df740da3a6e67d6650637a858e990 0 1606894261783 6 connected
edb5f035347d1f27f7b321694963f432242f590f 10.0.30.15:7000 slave 77d2953e160c6b44cc88d0a3c4595e6520817b3d 0 1606894259137 4 connected
833afe994d8d4b27306b8614d2b27ea9a8e240c1 10.0.30.183:7000 myself,master - 0 0 2 connected 5461-10922
9136c0a49eb84b4d0937f77924e1a1d9f44bc7b6 10.0.30.184:7000 slave 833afe994d8d4b27306b8614d2b27ea9a8e240c1 0 1606894260765 5 connected
77d2953e160c6b44cc88d0a3c4595e6520817b3d 10.0.30.14:7000 master - 0 1606894255675 1 connected 0-5460
10.0.30.183:7000> set key2 values2
-> Redirected to slot [4998] located at 10.0.30.14:7000
OK
10.0.30.14:7000> get key2
"values2"
网友评论