在linux下安裝redis很簡單,網(wǎng)上也有很多教程,但是在設(shè)置遠(yuǎn)程連接還有一些坑,本篇文章主要內(nèi)容是講解如果安裝redis數(shù)據(jù)庫,并且配置遠(yuǎn)程連接
1. 安裝redis數(shù)據(jù)庫
wget http://download.redis.io/releases/redis-4.0.1.tar.gztar xzf redis-4.0.1.tar.gzcd redis-4.0.1make
2.初始化配置
./utils/redis_init_script
設(shè)置redis.conf位置等信息。
通過上面的操作后, redis數(shù)據(jù)庫就安裝完成了, 可以通過
service redis_6379 start來啟動(dòng)數(shù)據(jù)庫
3. 修改redis.conf(/etc/redis.conf)文件來設(shè)置遠(yuǎn)程連接
-
bind 127.0.0.1=>bind 0.0.0.0修改綁定的ip地址 -
protected-mode yes=>protect-mode no關(guān)閉保護(hù)模式 -
# requirepass foobared=>requirepass yourpassword設(shè)置密碼,因?yàn)閞edis速度很快,密碼盡量設(shè)置復(fù)雜點(diǎn) -
:wq保存設(shè)置的信息,重啟數(shù)據(jù)庫service redis_6379 restart
4. 設(shè)置防火墻
這也是最重要的一步,通過百度、谷歌都能在找到redis安裝、遠(yuǎn)程連接的文章,但是就是不能遠(yuǎn)程連接redis數(shù)據(jù)庫,這有可能是沒有配置防火墻,出現(xiàn)Could not connect to Redis at xx.xx.xx.xx:6379: Operation timed out的提示
添加防火墻規(guī)則
iptables -A INPUT -p tcp -m tcp --dport 6379 -j ACCEPT
5.總結(jié)
好了這個(gè)時(shí)候應(yīng)該能遠(yuǎn)程連接上redis數(shù)據(jù)庫了
連接數(shù)據(jù)庫的兩種方法:
-
redis-cli -h 服務(wù)器ip地址 -a 'your_password'(默認(rèn)6379端口) - 先進(jìn)入數(shù)據(jù)庫
redis-cli -h 服務(wù)器ip地址, 在填寫密碼auth your_password