---
title: "redis学习笔记-添加访问密码，设置随机启动"
author: "ssxxjjii"
date: "2013-03-21T09:38:15Z"
tags: []
categories: ["互联网"]
slug: "redis"
draft: false
lastmod: "2013-03-21T09:38:15Z"
id: 1833083
url: "https://tdcq.iteye.com/blog/1833083"
---

# 添加访问密码

你的redis在真实环境中不可以谁想访问就访问，所以，必须要设置密码。

设置密码的步骤如下：

&lt;!--\[if !supportLists]--&gt;1.       &lt;!--\[endif]--&gt;修改redis.conf文件配置

root@ubuntu:/usr/local/redis-2.4.14# vim redis.conf

\# requirepass foobared去掉注释，foobared改为自己的密码，我在这里改为123456

requirepass 123456

&lt;!--\[if !supportLists]--&gt;2.       &lt;!--\[endif]--&gt;启动服务

root@ubuntu:/usr/local/redis-2.4.14# ./src/redis-server redis.conf

&lt;!--\[if !supportLists]--&gt;3.       &lt;!--\[endif]--&gt;客户端连接

naxsu@ubuntu:/usr/local/redis-2.4.14$ ./src/redis-cli

redis 127.0.0.1:6379&gt; get a

(error) ERR operation not permitted

redis 127.0.0.1:6379&gt;

提示没有权限

naxsu@ubuntu:/usr/local/redis-2.4.14$ ./src/redis-cli -a 123456

redis 127.0.0.1:6379&gt; get a

"b"

到此说明设置密码有效了。

# 设置随机启动

在服务器上，你每次重启机器后再去启动redis的服务，这是很麻烦的，所以将Redis作为 Linux 服务随机启动是很有必要的。

修改/etc/rc.local文件

root@ubuntu:/usr/local/redis-2.4.14# vim /etc/rc.local

在最后加入下面一行代码

./usr/local/redis-2.4.14/src/redis-server /usr/local/redis-2.4.14/redis.conf

重启机器看看效果

 

根据我的测试，设置是成功的。

 

下节：redis数据类型