Ubuntu redis 下载解压配置使用及密码管理 && 包管理工具联网安装

文章目录

    • 准备目录
    • 运行 && 配置
    • redis密码和配置文件
    • 使用包管理工具快速安装

准备目录

  • 下载 wget http://download.redis.io/releases/redis-5.0.7.tar.gz
    更多版本见redis 官网下载页
  • 解压 tar -zxvf redis-5.0.7.tar.gz
  • 进入目录 cd redis-5.0.7/
  • 查看目录结构 ll
wuyujin@ubuntu18:~/Downloads/redis-5.0.7$ ll
total 288
drwxr-xr-x  6 wuyujin wuyujin   4096 11月 20 01:05 ./
drwxr-xr-x  7 wuyujin wuyujin   4096 2月   8 17:37 ../
-rw-r--r--  1 wuyujin wuyujin 115100 11月 20 01:05 00-RELEASENOTES
-rw-r--r--  1 wuyujin wuyujin     53 11月 20 01:05 BUGS
-rw-r--r--  1 wuyujin wuyujin   2381 11月 20 01:05 CONTRIBUTING
-rw-r--r--  1 wuyujin wuyujin   1487 11月 20 01:05 COPYING
drwxr-xr-x  6 wuyujin wuyujin   4096 11月 20 01:05 deps/
-rw-r--r--  1 wuyujin wuyujin    376 11月 20 01:05 .gitignore
-rw-r--r--  1 wuyujin wuyujin     11 11月 20 01:05 INSTALL
-rw-r--r--  1 wuyujin wuyujin    151 11月 20 01:05 Makefile
-rw-r--r--  1 wuyujin wuyujin   6888 11月 20 01:05 MANIFESTO
-rw-r--r--  1 wuyujin wuyujin  20555 11月 20 01:05 README.md
-rw-r--r--  1 wuyujin wuyujin  61797 11月 20 01:05 redis.conf
-rwxr-xr-x  1 wuyujin wuyujin    275 11月 20 01:05 runtest*
-rwxr-xr-x  1 wuyujin wuyujin    280 11月 20 01:05 runtest-cluster*
-rwxr-xr-x  1 wuyujin wuyujin    373 11月 20 01:05 runtest-moduleapi*
-rwxr-xr-x  1 wuyujin wuyujin    281 11月 20 01:05 runtest-sentinel*
-rw-r--r--  1 wuyujin wuyujin   9710 11月 20 01:05 sentinel.conf
drwxr-xr-x  3 wuyujin wuyujin   4096 11月 20 01:05 src/
drwxr-xr-x 11 wuyujin wuyujin   4096 11月 20 01:05 tests/
drwxr-xr-x  8 wuyujin wuyujin   4096 11月 20 01:05 utils/
wuyujin@ubuntu18:~/Downloads/redis-5.0.7$ 

后续的运行步骤见README.md或https://github.com/antirez/redis。
可以看到,其中的Makefilesrc/源代码目录

  • 编译源代码 make
m -rf redis-server redis-sentinel redis-cli redis-benchmark redis-check-rdb redis-check-aof *.o *.gcda *.gcno *.gcov redis.info lcov-html Makefile.dep dict-benchmark
make[1]: Leaving directory '/home/wuyujin/Downloads/redis-5.0.7/src'
wuyujin@ubuntu18:~/Downloads/redis-5.0.7$ make
cd src && make all
make[1]: Entering directory '/home/wuyujin/Downloads/redis-5.0.7/src'
    CC Makefile.dep
    CC adlist.o
    CC quicklist.o

... 省略多行信息

    CC lolwut5.o
    LINK redis-server
    INSTALL redis-sentinel
    CC redis-cli.o
    LINK redis-cli
    CC redis-benchmark.o
    LINK redis-benchmark
    INSTALL redis-check-rdb
    INSTALL redis-check-aof

Hint: It's a good idea to run 'make test' ;)

make[1]: Leaving directory '/home/wuyujin/Downloads/redis-5.0.7/src'
wuyujin@ubuntu18:~/Downloads/redis-5.0.7$ 
  • 测试make test
    我第一次运行时,报错:You need tcl 8.5 or newer in order to run the Redis test,需要安装TCL。
wuyujin@ubuntu18:~/Downloads/redis-5.0.7$ sudo make test
cd src && make test
make[1]: Entering directory '/home/wuyujin/Downloads/redis-5.0.7/src'
You need tcl 8.5 or newer in order to run the Redis test
Makefile:262: recipe for target 'test' failed
make[1]: *** [test] Error 1
make[1]: Leaving directory '/home/wuyujin/Downloads/redis-5.0.7/src'
Makefile:6: recipe for target 'test' failed
make: *** [test] Error 2
wuyujin@ubuntu18:~/Downloads/redis-5.0.7$ 
  • 安装TCL
    若下载链接失效,在https://sourceforge.net/projects/tcl/获取最新链接即可。
wget https://nchc.dl.sourceforge.net/project/tcl/Tcl/8.6.10/tcl8.6.10-src.tar.gz
sudo tar xzvf tcl8.6.1-src.tar.gz  -C /usr/local/
cd /usr/local/tcl8.6.10/unix/
sudo ./configure
sudo make
sudo make install
  • 重新运行make test
wuyujin@ubuntu18:~/Downloads/redis-5.0.7$ sudo make test
[sudo] password for wuyujin: 
cd src && make test
make[1]: Entering directory '/home/wuyujin/Downloads/redis-5.0.7/src'
Cleanup: may take some time... OK
Starting test server at port 11111
[ready]: 13880
Testing unit/printver
[ready]: 13881

... 省略多行信息

 161 seconds - integration/replication
  107 seconds - unit/memefficiency

\o/ All tests passed without errors!

Cleanup: may take some time... OK
make[1]: Leaving directory '/home/wuyujin/Downloads/redis-5.0.7/src'
wuyujin@ubuntu18:~/Downloads/redis-5.0.7$ 

测试无误!

redis要用到的可执行文件已经生成了(在./src/下)
如:redis-server, redis-cli等。

wuyujin@ubuntu18:/opt/redis-5.0.7$ ll src/ | grep rwx
drwxr-xr-x 3 wuyujin wuyujin    4096 2月   8 20:52 ./
drwxr-xr-x 6 wuyujin wuyujin    4096 11月 20 01:05 ../
-rwxr-xr-x 1 wuyujin wuyujin     735 11月 20 01:05 mkreleasehdr.sh*
drwxr-xr-x 2 wuyujin wuyujin    4096 11月 20 01:05 modules/
-rwxr-xr-x 1 wuyujin wuyujin 4688344 2月   8 20:52 redis-benchmark*
-rwxr-xr-x 1 wuyujin wuyujin 8782824 2月   8 20:52 redis-check-aof*
-rwxr-xr-x 1 wuyujin wuyujin 8782824 2月   8 20:52 redis-check-rdb*
-rwxr-xr-x 1 wuyujin wuyujin 5216568 2月   8 20:52 redis-cli*
-rwxr-xr-x 1 wuyujin wuyujin 8782824 2月   8 20:51 redis-sentinel*
-rwxr-xr-x 1 wuyujin wuyujin 8782824 2月   8 20:51 redis-server*
-rwxr-xr-x 1 wuyujin wuyujin    3600 11月 20 01:05 redis-trib.rb*
wuyujin@ubuntu18:/opt/redis-5.0.7$ 

配置文件在redis根目录下

wuyujin@ubuntu18:~/Downloads/redis-5.0.7$ ll | grep conf
-rw-r--r--  1 wuyujin wuyujin  61797 11月 20 01:05 redis.conf
-rw-r--r--  1 wuyujin wuyujin   9710 11月 20 01:05 sentinel.conf
wuyujin@ubuntu18:~/Downloads/redis-5.0.7$ 
  • 移动文件夹 sudo mv redis-5.0.7 /opt
    其实只需要移动其中有用的部分文件。但是太麻烦(我不清楚哪些文件一点用都没有),所以移动整个文件。
  • 配置环境变量
    编辑/etc/profile,添加以下配置:
# redis
export REDIS_HOME=/opt/redis-5.0.7
export PATH=${REDIS_HOME}/src:$PATH

重启,使最新配置对所有用户生效。

  • 测试环境变量
echo $REDIS_HOME; echo $PATH
redis-server --version

运行 && 配置

  • 启动服务器 redis-server
wuyujin@ubuntu18:~$ redis-server
2356:C 08 Feb 2020 22:21:39.197 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
2356:C 08 Feb 2020 22:21:39.197 # Redis version=5.0.7, bits=64, commit=00000000, modified=0, pid=2356, just started
2356:C 08 Feb 2020 22:21:39.197 # Warning: no config file specified, using the default config. In order to specify a config file use redis-server /path/to/redis.conf
2356:M 08 Feb 2020 22:21:39.199 # You requested maxclients of 10000 requiring at least 10032 max file descriptors.
2356:M 08 Feb 2020 22:21:39.200 # Server can't set maximum open files to 10032 because of OS error: Operation not permitted.
2356:M 08 Feb 2020 22:21:39.200 # Current maximum open files is 4096. maxclients has been reduced to 4064 to compensate for low ulimit. If you need higher maxclients increase 'ulimit -n'.
                _._                                                  
           _.-``__ ''-._                                             
      _.-``    `.  `_.  ''-._           Redis 5.0.7 (00000000/0) 64 bit
  .-`` .-```.  ```\/    _.,_ ''-._                                   
 (    '      ,       .-`  | `,    )     Running in standalone mode
 |`-._`-...-` __...-.``-._|'` _.-'|     Port: 6379
 |    `-._   `._    /     _.-'    |     PID: 2356
  `-._    `-._  `-./  _.-'    _.-'                                   
 |`-._`-._    `-.__.-'    _.-'_.-'|                                  
 |    `-._`-._        _.-'_.-'    |           http://redis.io        
  `-._    `-._`-.__.-'_.-'    _.-'                                   
 |`-._`-._    `-.__.-'    _.-'_.-'|                                  
 |    `-._`-._        _.-'_.-'    |                                  
  `-._    `-._`-.__.-'_.-'    _.-'                                   
      `-._    `-.__.-'    _.-'                                       
          `-._        _.-'                                           
              `-.__.-'                                               

2356:M 08 Feb 2020 22:21:39.215 # WARNING: The TCP backlog setting of 511 cannot be enforced because /proc/sys/net/core/somaxconn is set to the lower value of 128.
2356:M 08 Feb 2020 22:21:39.215 # Server initialized
2356:M 08 Feb 2020 22:21:39.215 # WARNING overcommit_memory is set to 0! Background save may fail under low memory condition. To fix this issue add 'vm.overcommit_memory = 1' to /etc/sysctl.conf and then reboot or run the command 'sysctl vm.overcommit_memory=1' for this to take effect.
2356:M 08 Feb 2020 22:21:39.215 # WARNING you have Transparent Huge Pages (THP) support enabled in your kernel. This will create latency and memory usage issues with Redis. To fix this issue run the command 'echo never > /sys/kernel/mm/transparent_hugepage/enabled' as root, and add it to your /etc/rc.local in order to retain the setting after a reboot. Redis must be restarted after THP is disabled.
2356:M 08 Feb 2020 22:21:39.238 * DB loaded from disk: 0.022 seconds
2356:M 08 Feb 2020 22:21:39.238 * Ready to accept connections

以上,即为成功运行。
也可以在启动时指定要读取的配置文件,并且追加配置文件中有的参数对
NOTE:redis服务器启动时默认读取的配置文件是/path/to/redis.conf,而不是我们redis根目录中的redis.conf
所以如果想要修改文件,要在启动redis服务器的时候指定读取该配置文件。

wuyujin@ubuntu18:~$ redis-server --help
Usage: ./redis-server [/path/to/redis.conf] [options]
       ./redis-server - (read config from stdin)
       ./redis-server -v or --version
       ./redis-server -h or --help
       ./redis-server --test-memory <megabytes>

Examples:
       ./redis-server (run the server with default conf)
       ./redis-server /etc/redis/6379.conf
       ./redis-server --port 7777
       ./redis-server --port 7777 --replicaof 127.0.0.1 8888
       ./redis-server /etc/myredis.conf --loglevel verbose

Sentinel mode:
       ./redis-server /etc/sentinel.conf --sentinel
wuyujin@ubuntu18:~$ 
  • 客户端执行命令 redis-cli -h IP地址 -p 端口号 -a 密码
    先简单测试一下:
wuyujin@ubuntu18:~$ redis-cli 
127.0.0.1:6379> ping
PONG
127.0.0.1:6379> set name wuyujin1997
OK
127.0.0.1:6379> get name
"wuyujin1997"
127.0.0.1:6379> quit
wuyujin@ubuntu18:~$ 

在运行redis-server的窗口按Ctrl+C结束程序。打印以下:

3637:M 08 Feb 2020 22:36:52.113 * Ready to accept connections
^C3637:signal-handler (1581172675) Received SIGINT scheduling shutdown...
3637:M 08 Feb 2020 22:37:55.095 # User requested shutdown...
3637:M 08 Feb 2020 22:37:55.095 * Saving the final RDB snapshot before exiting.
3637:M 08 Feb 2020 22:37:55.099 * DB saved on disk
3637:M 08 Feb 2020 22:37:55.099 # Redis is now ready to exit, bye bye...
wuyujin@ubuntu18:~$ 
  • 后台运行redis-server及进程号查询、杀死进程
wuyujin@ubuntu18:~$ # 查询有没有redis-server运行
wuyujin@ubuntu18:~$ ps -axu | grep redis-server
wuyujin   3908  0.0  0.0  21532  1076 pts/0    S+   22:45   0:00 grep --color=auto redis-server
wuyujin@ubuntu18:~$ 
wuyujin@ubuntu18:~$ # 后台运行redis-server
wuyujin@ubuntu18:~$ redis-server --daemonize yes
3937:C 08 Feb 2020 22:45:33.717 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
3937:C 08 Feb 2020 22:45:33.717 # Redis version=5.0.7, bits=64, commit=00000000, modified=0, pid=3937, just started
3937:C 08 Feb 2020 22:45:33.717 # Configuration loaded
wuyujin@ubuntu18:~$ 
wuyujin@ubuntu18:~$ ps -axu | grep redis-server
wuyujin   3938  0.0  0.1  61320  4008 ?        Ssl  22:45   0:00 redis-server *:6379
wuyujin   3943  0.0  0.0  21532  1032 pts/0    S+   22:45   0:00 grep --color=auto redis-server
wuyujin@ubuntu18:~$ 
wuyujin@ubuntu18:~$ # 结束进程
wuyujin@ubuntu18:~$ kill -9 3938
wuyujin@ubuntu18:~$ 

redis密码和配置文件

安全起见,推荐设置redis密码。
两种,一种是单次配置:给正在运行的redis服务器设置密码(仅本次运行期间有效),需要在redis-server运行期间使用redis-cli发送命令设置密码;
另一种是固定配置,即每次redis服务器启动都有效,需要修改redis的配置文件redis.conf中的参数项requirepass

  • 固定配置:
    修改配置文件redis.conf
    其中有一段:
# 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 foobared

# requirepass foobared改为requirepass test1234(去掉注释符号,密码自定义,我的是test1234)。

启动服务器:redis-server /opt/redis-5.0.7/redis.conf
登录客户端:

wuyujin@ubuntu18:~$ redis-cli -h 127.0.0.1 -p 6379 -a test1234
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
127.0.0.1:6379> config get requirepass
1) "requirepass"
2) "test1234"
127.0.0.1:6379> quit
wuyujin@ubuntu18:~$ 
  • 单次配置
    redis-server每次运行会读取配置文件中的配置项,包括配置好的、固定的密码。
    redis-server运行之后,能不能修改密码呢?可以的。
    登录客户端,修改密码后,退出重新登录一次客户端,发现要用刚才更新的密码。
wuyujin@ubuntu18:~$ redis-cli -h 127.0.0.1 -p 6379
127.0.0.1:6379> config get requirepass
(error) NOAUTH Authentication required.
127.0.0.1:6379> auth test1234
OK
127.0.0.1:6379> config get requirepass
1) "requirepass"
2) "test1234"
127.0.0.1:6379> config set requirepass abcde
OK
127.0.0.1:6379> config get requirepass
1) "requirepass"
2) "abcde"
127.0.0.1:6379> quit
wuyujin@ubuntu18:~$ redis-cli -h 127.0.0.1 -p 6379
127.0.0.1:6379> config get requirepass
(error) NOAUTH Authentication required.
127.0.0.1:6379> auth test1234
(error) ERR invalid password
127.0.0.1:6379> auth abcde
OK
127.0.0.1:6379> quit
wuyujin@ubuntu18:~$ 

但是,新密码abcde只在本次redis-server启动时有效。
重启redis-server,密码将重置(至于重置为何值,取决于启动时指定要读取的配置文件)。

  • 配置文件结构
    配置项主要分几类:
include
module
networ
genera
snapshotting
replicatio
securit
client
memory managemen
lazy freein
append only mod
lua scripting
redis cluster
cluster docker/nat support
slow lo
latency monito
event notificatio
advanced confi
active defragmentatio
  • 常用配置项
# IP地址
bind 127.0.0.1
# 端口
port 6379
# 密码
# requirepass foobared
# 是否以守护进程运行(后台运行)
daemonize no

另有redis的两种持久化方案RDB及AOF的细节,也是在配置文件中进行参数设置。

使用包管理工具快速安装

若机器能联网,直接使用包管理工具安装redis,方便快捷,自动配置环境变量。只不过安装好的文件会比较分散,也可以通过命令查找。

  • 安装 apt-get install redis-server redis-tools
  • 卸载并清除配置文件 apt-get remove redis-server redis-tools --purge
  • 查询软件安装位置 dpkg -L redis-server redis-tools

你可能感兴趣的:(配置,Linux,数据库)