Redis(一)Redis基础

  一、Redis是什么

  二、Redis特性

  三、Redis可以做什么

  四、Redis安装与启动

  1.在Redis官网https://redis.io/上下载源码redis-4.0.9.tar.gz

  2.执行以下命令:

1 tar xzf redis-4.0.9.tar.gz
2 ln -s redis-4.0.9 redis
3 cd redis
4 make
5 make install

  3.安装后,查看Redis的版本:

root@myubuntu:/home/software/redis# redis-cli -v
redis-cli 4.0.9

  4.查看redis可执行文件:

root@myubuntu:/usr/local/bin# ls -l
总用量 23612
-rwxr-xr-x 1 root root 2701600 5月  31 09:20 redis-benchmark                  Redis基准测试工具
-rwxr-xr-x 1 root root 6191088 5月  31 09:20 redis-check-aof           Redis AOF持久化文件检测和修复工具
-rwxr-xr-x 1 root root 6191088 5月  31 09:20 redis-check-rdb           Redis RDB持久化文件检测和修复工具
-rwxr-xr-x 1 root root 2894704 5月  31 09:20 redis-cli               Redis命令行客户端
lrwxrwxrwx 1 root root      12 5月  31 09:20 redis-sentinel -> redis-server   启动Redis Sentinel
-rwxr-xr-x 1 root root 6191088 5月  31 09:20 redis-server             启动Redis

  5.启动Redis有三种方法:默认配置、运行配置、配置文件启动

  (1)默认配置

  从打印日志可以看出,当前版本4.0.9,默认端口6379,进程号6985.

  直接启动无法自定义配置,所以这种方式不建议使用。

root@myubuntu:/usr/local/bin# redis-server 
6985:C 31 May 09:38:46.046 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
6985:C 31 May 09:38:46.046 # Redis version=4.0.9, bits=64, commit=00000000, modified=0, pid=6985, just started
6985:C 31 May 09:38:46.046 # Warning: no config file specified, using the default config. In order to specify a config file use redis-server /path/to/redis.conf
6985:M 31 May 09:38:46.047 * Increased maximum number of open files to 10032 (it was originally set to 1024).
                _._                                                  
           _.-``__ ''-._                                             
      _.-``    `.  `_.  ''-._           Redis 4.0.9 (00000000/0) 64 bit
  .-`` .-```.  ```\/    _.,_ ''-._                                   
 (    '      ,       .-`  | `,    )     Running in standalone mode
 |`-._`-...-` __...-.``-._|'` _.-'|     Port: 6379
 |    `-._   `._    /     _.-'    |     PID: 6985
  `-._    `-._  `-./  _.-'    _.-'                                   
 |`-._`-._    `-.__.-'    _.-'_.-'|                                  
 |    `-._`-._        _.-'_.-'    |           http://redis.io        
  `-._    `-._`-.__.-'_.-'    _.-'                                   
 |`-._`-._    `-.__.-'    _.-'_.-'|                                  
 |    `-._`-._        _.-'_.-'    |                                  
  `-._    `-._`-.__.-'_.-'    _.-'                                   
      `-._    `-.__.-'    _.-'                                       
          `-._        _.-'                                           
              `-.__.-'                                               

6985:M 31 May 09:38:46.047 # WARNING: The TCP backlog setting of 511 cannot be enforced because /proc/sys/net/core/somaxconn is set to the lower value of 128.
6985:M 31 May 09:38:46.047 # Server initialized
6985:M 31 May 09:38:46.047 # 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.
6985:M 31 May 09:38:46.047 # 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.
6985:M 31 May 09:38:46.047 * DB loaded from disk: 0.000 seconds
6985:M 31 May 09:38:46.047 * Ready to accept connections

  (2)运行启动

  可以通过这种方式修改配置名和值,但是如果需要修改的配置较多或者希望将配置保存到文件中,不建议使用这种方式。

root@myubuntu:/usr/local/bin# redis-server --port 6380

  (3)配置文件启动

  通常是使用读取配置文件来启动Redis的方式,因为这种方式可以提供更大的灵活性,或者应用在一台机器上启动多个Redis的场景下。

root@myubuntu:/home/software/redis# redis-server /home/software/redis/redis.conf 

  6.Redis命令行客户端

  在启动Redis服务后,使用redis-cli连接、操作Redis服务。redis-cli可以使用两种方式连接Redis服务器:

  (1)交互式方式

bigjun@myubuntu:~/下载$ redis-cli -h 127.0.0.1 -p 6379
127.0.0.1:6379> set hello world
OK
127.0.0.1:6379> get hello
"world"
127.0.0.1:6379> get hello
"world"
127.0.0.1:6379> set myname bigjun
OK
127.0.0.1:6379> get myname
"bigjun"
127.0.0.1:6379> shutdown
not connected> redis-cli shutdown
Could not connect to Redis at 127.0.0.1:6379: Connection refused
not connected> redis-cli 
Could not connect to Redis at 127.0.0.1:6379: Connection refused

  (2)命令方式

bigjun@myubuntu:~/下载$ redis-cli -h 127.0.0.1 -p 6379 get hello
“world”

   7.停止Redis服务

  除了通过shutdown命令关闭Redis服务以外,还可以通过kill进程号的方式关闭掉Redis。一般来说,断开与客户端的连接、持久化文件生成,是一种相对优雅的关闭方式。

redis-cli shutdown

猜你喜欢

转载自www.cnblogs.com/BigJunOba/p/9114861.html