Redis 对 Redis Cluster 已运行的Redis集群 设置密码 Predis 连接

billy · 2021-02-23 13:43:26 · 热度: 130

已运行的 redis 集群,设置密码比较简单,centos连上redis,执行命令即可,但这种方法一旦服务器被程启,或Redis集群进程被结束,设置的密码会丢失,需连接Redis再次设置密码

按照之前写过的无密码redis集群搭建,环境已经搭建好了,现在需要远程连接,并设置连接密码

[root@localhost src]# ./redis-cli -c -h 10.10.10.171 -p 7000
10.10.10.171: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:10649
cluster_stats_messages_received:10649
10.10.10.171:7000> cluster nodes
d0d0569cc11242a31db975d4bfa2b26744b27122 10.10.10.171:7001 master - 0 1482374239246 2 connected 5461-10922
33e6a5989a0c7f9ef584d8333513122d19e23d41 10.10.10.171:7005 slave c5f7a03c0aec8b71de9ac42cec150faa43528571 0 1482374238213 6 connected
13fc5e5cceed79b3149e59530fc4ba48ea6c6260 10.10.10.171:7004 slave d0d0569cc11242a31db975d4bfa2b26744b27122 0 1482374237696 5 connected
7522d88ebe489698ba4555d8040c75640ca391e3 10.10.10.171:7000 myself,master - 0 0 1 connected 0-5460
c5f7a03c0aec8b71de9ac42cec150faa43528571 10.10.10.171:7002 master - 0 1482374238728 3 connected 10923-16383
222f71c4844adbbb42ded710cccb25e32577ac64 10.10.10.171:7003 slave 7522d88ebe489698ba4555d8040c75640ca391e3 0 1482374239245 4 connected

cluster info 命令显示 集群状态cluster_state:ok ,集群配置主从关系如上面cluster nodes命令显示的:

10.10.10.171:7000 #主

10.10.10.171:7001 #主

10.10.10.171:7002 #主

10.10.10.171:7003 #从

10.10.10.171:7004 #从

10.10.10.171:7005 #从

确认好Redis集群已运行,现在给Redis集群加密码

主:config set requirepass 123456 设置密码

从:config set masterauth 123456 设置密码

123456 可替换成你想设置的任何密码

[root@localhost src]# ./redis-cli -c -h 10.10.10.171 -p 7000
10.10.10.171:7000> config set requirepass 123456
OK
10.10.10.171:7000> exit
[root@localhost src]# ./redis-cli -c -h 10.10.10.171 -p 7001
10.10.10.171:7001> config set requirepass 123456
OK
10.10.10.171:7001> exit
[root@localhost src]# ./redis-cli -c -h 10.10.10.171 -p 7002
10.10.10.171:7002> config set requirepass 123456
OK
10.10.10.171:7002> exit
[root@localhost src]# ./redis-cli -c -h 10.10.10.171 -p 7003
10.10.10.171:7003> config set masterauth 123456
OK
10.10.10.171:7003> exit
[root@localhost src]# ./redis-cli -c -h 10.10.10.171 -p 7004
10.10.10.171:7004> config set masterauth 123456
OK
10.10.10.171:7004> exit
[root@localhost src]# ./redis-cli -c -h 10.10.10.171 -p 7005
10.10.10.171:7005> config set masterauth 123456
OK
10.10.10.171:7005> exit
#用 -a 密码 连接(这里输入一个错误的密码:testpassword测试一下)
[root@localhost src]# ./redis-cli -c -h 10.10.10.171 -p 7000 -a testpassword
# 如果密码错误,虽然也能连上,但没有执行命令权限
10.10.10.171:7000> cluster nodes
NOAUTH Authentication required.
10.10.10.171:7000> exit
#退出重新输入正确密码
[root@localhost src]# ./redis-cli -c -h 10.10.10.171 -p 7000 -a 123456
10.10.10.171: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:11566
cluster_stats_messages_received:11566

至此一切正常,已运行的redis集群加密码就这么简单

再用Predis远程连接测试 (predis:https://github.com/predis/predis)

<?php

/*
 * This file is part of the Predis package.
 *
 * (c) Daniele Alessandri <suppakilla@gmail.com>
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 */
  //远程连接 Redis 服务


require __DIR__.'/../autoload.php';
//redis实例
$servers = array(
    '10.10.10.171:7000',
    '10.10.10.171:7001',
    '10.10.10.171:7002',
    '10.10.10.171:7003',
    '10.10.10.171:7004',
    '10.10.10.171:7005',
);
$options = ['cluster' =>'redis','parameters' => [
        'password' => '123456'
    ]];
$client = new Predis\Client($servers,$options);

$client->set('foo1','11');
$client->set('foo2','22');
$client->set('foo3','33');

// 此方法用于找出所有key名以foo开头的key
$allkeys = [];
$cmdKeys = $client->createCommand('keys', ['foo*']);

foreach ($client->getConnection() as $nodeConnection) {
    $nodeKeys = $nodeConnection->executeCommand($cmdKeys);
    $allkeys = array_merge($allkeys, $nodeKeys);
}

print_r($allkeys);

猜你喜欢:
暂无回复。
需要 登录 后方可回复, 如果你还没有账号请点击这里 注册