How to set password for Redis?

后端 未结 10 1282
长发绾君心
长发绾君心 2021-01-29 19:17

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\'

相关标签:
10条回答
  • 2021-01-29 20:11

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

    # 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.

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

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

    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:

    kill 3832
    

    Update

    I also wanted to add, you could also make the /etc/init.d/redis-server stop you'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.

    0 讨论(0)
  • 2021-01-29 20:12

    you can also use following command on client

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

    above command will set p@ss$12E45 as a redis server password.

    0 讨论(0)
  • 2021-01-29 20:14

    open redis configuration file

    sudo nano /etc/redis/redis.conf 
    

    set passphrase

    replace

    # requirepass foobared
    

    with

    requirepass YOURPASSPHRASE
    

    restart redis

    redis-server restart
    
    0 讨论(0)
  • 2021-01-29 20:15

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

    01) open redis configuration file

    sudo vi /etc/redis/redis.conf
    

    find requirepass field under SECURITY section and uncomment that field.Then set your password instead of "foobared"

    # requirepass foobared
    

    It should be like,

    requirepass YOUR_PASSWORD
    

    Then restart redis and start redis-cli.

    If you need to check whether you have set the password correctly, you can run below commads in 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
    

    `

    0 讨论(0)
提交回复
热议问题