php redis:为redis设置密码

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7537905/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 02:54:38  来源:igfitidea点击:

redis: set a password for redis

phpredis

提问by Don Gorgon

I'm working with redis on my local machine so I dont really need to set up a password to connect to the server with my php client (I'm using predis as a client). However, I'm moving my app to a live server, so I want to set up a password to connect to my redis server.

我在我的本地机器上使用 redis,所以我真的不需要设置密码来使用我的 php 客户端连接到服务器(我使用 predis 作为客户端)。但是,我正在将我的应用程序移动到实时服务器,因此我想设置一个密码来连接到我的 redis 服务器。

I have few questions:

我有几个问题:

  • I checked all over the internet about how to set up the password and it looks like I need to add the password in the redis.conf. I couldnt find though what I should add exactly to the configuration file to set up the password.

  • also in predis how should I add the password. I'm using the following array of parameters to connect to the redis server

    $my_server = array('host' => '127.0.0.1','port' => 6379,'database' => 1);

  • 我在互联网上检查了如何设置密码,看起来我需要在 redis.conf 中添加密码。我找不到我应该在配置文件中添加什么来设置密码。

  • 同样在predis中我应该如何添加密码。我正在使用以下参数数组连接到 redis 服务器

    $my_server = array('host' => '127.0.0.1','port' => 6379,'database' => 1);

should I add the password this way?

我应该这样添加密码吗?

> $my_server = array('host'     => '127.0.0.1','port'     =>
> 6379,'database' => 1,'password'=>password);
  • last question, I'm trying to stop my redis-server on the live server. Every time I enter the following command , I keep getting the same error message

    redis-server stop

    [23925] 23 Sep 20:23:03 # Fatal error, can't open config file 'stop'

    usually on my local machine I enter

    /etc/init.d/redis-server stop

  • 最后一个问题,我试图在实时服务器上停止我的 redis-server。每次输入以下命令时,我都会收到相同的错误消息

    redis-服务器停止

    [23925] 9 月 23 日 20:23:03 # 致命错误,无法打开配置文件“停止”

    通常在我输入的本地机器上

    /etc/init.d/redis-server 停止

to stop redis server but its not working on my live server since there is no process called redis-server in my /etc/init.d

停止 redis 服务器,但它不能在我的实时服务器上工作,因为我的 /etc/init.d 中没有名为 redis-server 的进程

回答by profitphp

To set the password, edit your redis.conf file, find this line

要设置密码,请编辑您的 redis.conf 文件,找到这一行

# requirepass foobared

Then uncomment it and change foobared to your password. Make sure you choose something pretty long, 32 characters or so would probably be good, it's easy for an outside user to guess upwards of 150k passwords a second, as the notes in the config file mention.

然后取消注释并将 foobared 更改为您的密码。确保你选择的东西很长,32 个字符左右可能会很好,外部用户很容易每秒猜出 150k 以上的密码,正如配置文件中提到的那样。

To authenticate with your new password using predis, the syntax you have shown is correct. Just add password as one of the connection parameters.

要使用 predis 使用新密码进行身份验证,您显示的语法是正确的。只需添加密码作为连接参数之一。

To shut down redis... check in your config file for the pidfilesetting, it will probably be

要关闭 redis... 检查您的配置文件的pidfile设置,它可能是

pidfile /var/run/redis.pid

From the command line, run:

从命令行运行:

cat /var/run/redis.pid

That will give you the process id of the running server, then just kill the process using that pid:

这将为您提供正在运行的服务器的进程 ID,然后使用该 pid 终止进程:

kill 3832

Update

更新

I also wanted to add, you could also make the /etc/init.d/redis-server stopyou're used to work on your live server. All those files in /etc/init.d/ are just shell scripts, take the redis-server script off your local server, and copy it to the live server in the same location, and then just look what it does with vi or whatever you like to use, you may need to modify some paths and such, but it should be pretty simple.

我还想补充一点,你也可以让/etc/init.d/redis-server stop你习惯在你的实时服务器上工作。/etc/init.d/ 中的所有文件都只是 shell 脚本,从本地服务器上取下 redis-server 脚本,然后将其复制到同一位置的实时服务器上,然后看看它对 vi 或其他东西做了什么你喜欢使用,你可能需要修改一些路径等等,但它应该很简单。

回答by Suhas Gaikwad

you can also use following command on client

您也可以在客户端使用以下命令

cmd ::config set requirepass p@ss$12E45

指令::config set requirepass p@ss$12E45

above command will set p@ss$12E45as a redisserver password.

上面的命令将设置p@ss$12E45redis服务器密码。

回答by Flavio Troia

Example:

例子:

redis 127.0.0.1:6379> AUTH PASSWORD
(error) ERR Client sent AUTH, but no password is set
redis 127.0.0.1:6379> CONFIG SET requirepass "mypass"
OK
redis 127.0.0.1:6379> AUTH mypass
Ok

回答by Saurabh Chandra Patel

sudo nano /etc/redis/redis.conf 

find and uncomment line # requirepass foobared, then restart server

找到并取消注释行# requirepass foobared,然后重新启动服务器

now you password is foobared

现在你的密码是 foobared

回答by Hlod

using redis-cli:

使用 redis-cli:

root@server:~# redis-cli 
127.0.0.1:6379> CONFIG SET requirepass secret_password
OK

this will set password temporarily (until redis or server restart)

这将临时设置密码(直到 redis 或服务器重新启动)

test password:

测试密码:

root@server:~# redis-cli 
127.0.0.1:6379> AUTH secret_password
OK

回答by Andi F.

open redis configuration file

打开redis配置文件

sudo nano /etc/redis/redis.conf 

set passphrase

设置密码

replace

代替

# requirepass foobared

with

requirepass YOURPASSPHRASE

restart redis

重启redis

redis-server restart

回答by Sithara

For that, you need to update the redis configuration file.By default, there is no any password for redis.

为此,您需要更新redis 配置文件。默认情况下,redis 没有任何密码。

01) open redis configuration file

01) 打开redis配置文件

sudo vi /etc/redis/redis.conf

find requirepassfield under SECURITYsection and uncomment that field.Then set your password instead of "foobared"

SECURITY部分下找到requirepass字段并取消对该字段的注释。然后设置您的密码而不是“foobared”

# requirepass foobared

It should be like,

应该是这样的

requirepass YOUR_PASSWORD

Then restart redis and start redis-cli.

然后重启redis,启动redis-cli。

If you need to check whether you have set the password correctly, you can run below commads in redis-cli.

如果你需要检查你是否设置了正确的密码,你可以在 redis-cli 中运行下面的命令。

sithara@sithara-X555UJ ~ $ redis-cli
127.0.0.1:6379> set key1 18
(error) NOAUTH Authentication required.
127.0.0.1:6379> auth admin
OK
127.0.0.1:6379> get key1
(nil)
127.0.0.1:6379> exit


sithara@sithara-X555UJ ~ $ redis-cli
127.0.0.1:6379> set key1 18
(error) NOAUTH Authentication required.
127.0.0.1:6379> auth admin
OK
127.0.0.1:6379> set key2 check
OK
127.0.0.1:6379> get key2
"check"
127.0.0.1:6379> get key1
(nil)
127.0.0.1:6379> set key1 20
OK
127.0.0.1:6379> get key1
"20"
127.0.0.1:6379> exit

`

`

回答by Sagar Jadhav

step 1. stop redis server using below command /etc/init.d/redis-server stop step 2.enter command : sudo nano /etc/redis/redis.conf

步骤 1. 使用以下命令停止 redis 服务器 /etc/init.d/redis-server stop step 2.enter 命令:sudo nano /etc/redis/redis.conf

step 3.find # requirepass foobared word and remove # and change foobared to YOUR PASSWORD

步骤 3.find # requirepass foobared word 并删除 # 并将 foobared 更改为您的密码

ex. requirepass root

前任。要求通行证

回答by yojimbo87

i couldnt find though what i should add exactly to the configuration file to set up the password.

我找不到我应该在配置文件中添加什么来设置密码。

Configuration file should be located at /etc/redis/redis.confand password can be set up in SECURITY section which should be located between REPLICATION and LIMITS section. Password setup is done using the requirepass directive. For more information try to look at AUTHcommand description.

配置文件应该位于/etc/redis/redis.conf并且密码可以在 SECURITY 部分设置,该部分应该位于 REPLICATION 和 LIMITS 部分之间。密码设置是使用 requirepass 指令完成的。有关更多信息,请尝试查看AUTH命令描述。

回答by Sagar Jadhav

How to set redis password ?

如何设置redis密码?

step 1. stop redis server using below command /etc/init.d/redis-server stop

步骤 1. 使用以下命令停止 redis 服务器 /etc/init.d/redis-server stop

step 2.enter command : sudo nano /etc/redis/redis.conf

步骤2.输入命令:sudo nano /etc/redis/redis.conf

step 3.find # requirepass foobared word and remove # and change foobared to YOUR PASSWORD

步骤 3.find # requirepass foobared word 并删除 # 并将 foobared 更改为您的密码

ex. requirepass root

前任。要求通行证