首页 » redis » 正文

Redis添加密码并修改默认端口后的主从同步

说明:

只需配置master的地址和端口,就可以作为slave存在,这样数据就被别人盗取了,所以我们需要设置密码及修改默认的端口号。

主从复制:http://www.lmyw.net.cn/?p=757

安装文档:http://www.lmyw.net.cn/?p=753

配置:

先给master设置密码,redis提供了在线直接修改配置和修改配置文件两种方法,先使用直接修改,调用命令config set requirepass 密码,设置成功后,再执行info命令,发现返回错误,这是因为配置已经生效,访问需要使用密码,执行命令auth 密码,再次执行info命令,已经成功:

在192.168.1.2上执行如下命令:

#redis-cli

127.0.0.1:6379> config set requirepass xxtao920admin   //xxtao920admin是密码

OK

127.0.0.1:6379> info   //查看信息

NOAUTH Authentication required.   //认证失败

127.0.0.1:6379> auth xxtao920admin //输入密码 

OK

127.0.0.1:6379> info   //查看信息

# Server

redis_version:3.0.2

redis_git_sha1:00000000

redis_git_dirty:0

redis_build_id:8b1689a41dd01f6

redis_mode:standalone

os:Linux 2.6.32-71.el6.x86_64 x86_64

arch_bits:64

multiplexing_api:epoll

gcc_version:4.4.4

process_id:62033

run_id:e7e8aea57d32ea36fb93ede96f05ff364da1e702

tcp_port:6379

uptime_in_seconds:1205

uptime_in_days:0

hz:10

lru_clock:7944282

config_file:/etc/redis.conf

# Clients

connected_clients:1

client_longest_output_list:0

client_biggest_input_buf:0

blocked_clients:0

# Memory

used_memory:1884552

used_memory_human:1.80M

used_memory_rss:9834496

used_memory_peak:1920480

used_memory_peak_human:1.83M

used_memory_lua:36864

mem_fragmentation_ratio:5.22

mem_allocator:jemalloc-3.6.0

# Persistence

loading:0

rdb_changes_since_last_save:0

rdb_bgsave_in_progress:0

rdb_last_save_time:1434007338

rdb_last_bgsave_status:ok

rdb_last_bgsave_time_sec:0

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:5

total_commands_processed:1175

instantaneous_ops_per_sec:0

total_net_input_bytes:43334

total_net_output_bytes:4532

instantaneous_input_kbps:0.02

instantaneous_output_kbps:0.02

rejected_connections:0

sync_full:1

sync_partial_ok:0

sync_partial_err:0

expired_keys:0

evicted_keys:0

keyspace_hits:1

keyspace_misses:0

pubsub_channels:0

pubsub_patterns:0

latest_fork_usec:2180

migrate_cached_sockets:0

# Replication

role:master

connected_slaves:1

slave0:ip=192.168.1.3,port=6379,state=online,offset=1696,lag=17

master_repl_offset:1724

repl_backlog_active:1

repl_backlog_size:1048576

repl_backlog_first_byte_offset:2

repl_backlog_histlen:1723

# CPU

used_cpu_sys:0.05

used_cpu_user:0.01

used_cpu_sys_children:0.01

used_cpu_user_children:0.00

# Cluster

cluster_enabled:0

# Keyspace

db0:keys=1,expires=0,avg_ttl=0

127.0.0.1:6379>  set user:age 920   //插入数据

OK

127.0.0.1:6379> get user:age  //读取数据

“920”

127.0.0.1:6379>

在192.168.1.3上执行:

#redis-cli

127.0.0.1:6379> keys *   //说明已经生效了,无法获取数据,需要认证

1) “pwd”

127.0.0.1:6379> config set masterauth xxtao920admin   //设置密码,必须和主服务器一致

OK

127.0.0.1:6379> get user:age   //查询数据,已经查出来了。

“920”

127.0.0.1:6379> info   //查看信息

# Server

redis_version:3.0.2

redis_git_sha1:00000000

redis_git_dirty:0

redis_build_id:9eec45ade6d41949

redis_mode:standalone

os:Linux 3.10.0-229.el7.x86_64 x86_64

arch_bits:64

multiplexing_api:epoll

gcc_version:4.8.3

process_id:42815

run_id:f306ff9a060dc138bc0c9fa6ab1476dcb6f1e33f

tcp_port:6379

uptime_in_seconds:1814

uptime_in_days:0

hz:10

lru_clock:7944853

config_file:/etc/redis.conf

# Clients

connected_clients:2

client_longest_output_list:0

client_biggest_input_buf:0

blocked_clients:0

# Memory

used_memory:1905712

used_memory_human:1.82M

used_memory_rss:9641984

used_memory_peak:1941656

used_memory_peak_human:1.85M

used_memory_lua:36864

mem_fragmentation_ratio:5.06

mem_allocator:jemalloc-3.6.0

# Persistence

loading:0

rdb_changes_since_last_save:0

rdb_bgsave_in_progress:0

rdb_last_save_time:1434008201

rdb_last_bgsave_status:ok

rdb_last_bgsave_time_sec:0

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:551

total_commands_processed:3562

instantaneous_ops_per_sec:1

total_net_input_bytes:78930

total_net_output_bytes:114736

instantaneous_input_kbps:0.05

instantaneous_output_kbps:0.04

rejected_connections:0

sync_full:2

sync_partial_ok:0

sync_partial_err:0

expired_keys:0

evicted_keys:0

keyspace_hits:2

keyspace_misses:2

pubsub_channels:0

pubsub_patterns:0

latest_fork_usec:91883

migrate_cached_sockets:0

# Replication

role:slave

master_host:192.168.1.2

master_port:6379

master_link_status:up

master_last_io_seconds_ago:0

master_sync_in_progress:0

slave_repl_offset:2558

slave_priority:100

slave_read_only:1

connected_slaves:1

slave0:ip=192.168.1.4,port=6379,state=online,offset=2508,lag=0

master_repl_offset:2508

repl_backlog_active:1

repl_backlog_size:1048576

repl_backlog_first_byte_offset:2

repl_backlog_histlen:2507

# CPU

used_cpu_sys:2.60

used_cpu_user:0.00

used_cpu_sys_children:0.08

used_cpu_user_children:0.00

# Cluster

cluster_enabled:0

# Keyspace

db0:keys=2,expires=0,avg_ttl=0

在192.168.1.4上执行:

#redis-cli

127.0.0.1:6379> get user:age

“920”

127.0.0.1:6379>

至此,已经能访问添加了密码的master,刚才的配置在重启服务后会丢失,如果永久保存,需要修改配置文件对应的参数,如config set requirepass xxtao920admin只需要找到redis.conf中的requirepass,把注释去掉,并修改值为xxtao920admin,即可。

不过个人觉得增加密码没什么意义,因为在试验中192.168.1.4去获取数据时是不需要密码的。

修改端口的是一样的,修改192.168.1.2上的端口,同时修改192.168.1.3上redis的配置文件中的slaveof的端口参数即可。在此不再做说明。

发表评论