redis 遠程連接方法
解決方法
1、修改redis服務器的配置文件
vi redis.conf
註釋以下綁定的主機地址
# bind 127.0.0.1
或
vim redis.conf
bind 0.0.0.0
protected-mode no
2、修改redis服務器的參數配置
修改redis的守護進程爲no,不啓用
127.0.0.1:6379> config set daemonize "no"
OK
修改redis的保護模式爲no,不啓用
127.0.0.1:6379> config set protected-mode"no"
OK
或者
config set requirepass 123 ->123是密碼
注意:開啓 6379端口
3、遠程連接
$ redis-cli -h 138.138.138.138 -p 6379
redis>ping
PONG
-------------------------------------------------------------------------------------------------------------------
如何讓Redis允許遠程連接,並設置密碼
最近網站數據量過大,需要在數據庫上層增加一個緩存層,所以採用了Redis作爲數據庫。但是Redis默認是不允許遠程連接的,主要還是考慮到了安全問題。
但是如果需要遠程連接,那該如何操作呢:
在/etc/redis/redis.conf
文件裏面修改如下配置文件,
################################## NETWORK #####################################
# By default, if no "bind" configuration directive is specified, Redis listens
# for connections from all the network interfaces available on the server.
# It is possible to listen to just one or multiple selected interfaces using
# the "bind" configuration directive, followed by one or more IP addresses.
#
# Examples:
#
# bind 192.168.1.100 10.0.0.1
# bind 127.0.0.1 ::1
#
# ~~~ WARNING ~~~ If the computer running Redis is directly exposed to the
# internet, binding to all the interfaces is dangerous and will expose the
# instance to everybody on the internet. So by default we uncomment the
# following bind directive, that will force Redis to listen only into
# the IPv4 lookback interface address (this means Redis will be able to
# accept connections only from clients running into the same computer it
# is running).
#
# IF YOU ARE SURE YOU WANT YOUR INSTANCE TO LISTEN TO ALL THE INTERFACES
# JUST COMMENT THE FOLLOWING LINE.
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
bind 127.0.0.1 ::1
將bind 127.0.0.1 ::1
註釋掉:
# bind 127.0.0.1 ::1
這樣就可以遠程連接了。
允許遠程連接就帶來了一個很大的問題,可以用過互聯網破解Redis密碼,所以需要設置Redis的密碼。
redis的查詢速度是非常快的,外部用戶一秒內可以嘗試多大150K個密碼;所以密碼要儘量長(對於DBA 沒有必要必須記住密碼);
所以需要設置一個很長的密碼,防止破解。
找到剛纔的配置文件,有如下內容:
################################## SECURITY ###################################
# Require clients to issue AUTH before processing any other # commands. This might be useful in environments in which you do not trust # others with access to the host running redis-server. # # This should stay commented out for backward compatibility and because most # people do not need auth (e.g. they run their own servers). # # Warning: since Redis is pretty fast an outside user can try up to # 150k passwords per second against a good box. This means that you should # use a very strong password otherwise it will be very easy to break. # requirepass xxxxxxxxxxxx
修改requirepass
後面就是密碼,密碼設置的儘量長。
之後重啓Redis,在真正的項目上,需要注意數據的持久化,否則重啓數據會丟失。
如果是用apt-get或者yum install安裝的redis,可以直接通過下面的命令停止/啓動/重啓redis
/etc/init.d/redis-server stop
/etc/init.d/redis-server start
/etc/init.d/redis-server restart
如果是通過源碼安裝的redis,則可以通過redis的客戶端程序redis-cli的shutdown命令來重啓redis
1、Redis關閉:
redis-cli -h 127.0.0.1 -p 6379 shutdown
2、Redis啓動:
redis-server