在这里插入图片描述
CentOS 7.5
Redis 5.0.0
gcc
http://download.redis.io/releases/
[root@localhost ~]#yum -y install gcc
[root@localhost ~]# mkdir redis
[root@localhost redis]# wget http://download.redis.io/releases/redis-5.0.0.tar.gz
[root@localhost ~]# cd redis/
[root@localhost redis]# tar -xvf redis-5.0.0.tar.gz
redis-5.0.0/
redis-5.0.0/.gitignore
redis-5.0.0/00-RELEASENOTES
...
[root@localhost redis]# ls
redis-5.0.0 redis-5.0.0.tar.gz
[root@localhost redis]# cd redis-5.0.0
[root@localhost redis-5.0.0]# make && make install
...
make[1]: 离开目录“/root/redis/redis-5.0.0/src”
make[1]: 进入目录“/root/redis/redis-5.0.0/src”
Hint: It's a good idea to run 'make test' ;)
INSTALL install
INSTALL install
INSTALL install
INSTALL install
INSTALL install
make[1]: 离开目录“/root/redis/redis-5.0.0/src”
[root@localhost redis-5.0.0]# ./utils/install_server.sh //一路回车
Welcome to the redis service installer
This script will help you easily set up a running redis server
Please select the redis port for this instance: [6379]
Selecting default: 6379
Please select the redis config file name [/etc/redis/6379.conf]
Selected default - /etc/redis/6379.conf
Please select the redis log file name [/var/log/redis_6379.log]
Selected default - /var/log/redis_6379.log
Please select the data directory for this instance [/var/lib/redis/6379]
Selected default - /var/lib/redis/6379
Please select the redis executable path [/usr/local/bin/redis-server]
Selected config:
Port : 6379
Config file : /etc/redis/6379.conf
Log file : /var/log/redis_6379.log
Data dir : /var/lib/redis/6379
Executable : /usr/local/bin/redis-server
Cli Executable : /usr/local/bin/redis-cli
Is this ok? Then press ENTER to go on or Ctrl-C to abort.
Copied /tmp/6379.conf => /etc/init.d/redis_6379
Installing service...
Successfully added to chkconfig!
Successfully added to runlevels 345!
Starting Redis server...
Installation successful!
[root@localhost redis-5.0.0]# /etc/init.d/redis_6379 stop //停止服务
[root@localhost redis-5.0.0]# /etc/init.d/redis_6379 start //启动服务
[root@localhost redis-5.0.0]# ps -C redis-server //查看进程
PID TTY TIME CMD
14540 ? 00:00:00 redis-server
[root@localhost redis-5.0.0]# ss -antulp | grep :6379 //查看端口
tcp LISTEN 0 128 127.0.0.1:6379 *:* users:(("redis-server",pid=14540,fd=6))
[root@localhost redis-5.0.0]# redis-cli
127.0.0.1:6379> set name bob //存储数据,变量名为name,值为bob
OK
127.0.0.1:6379> get name //查看变量name的值
"bob"
127.0.0.1:6379> keys * //查询已有的变量
1) "name"
127.0.0.1:6379> exit
- set 变量名 变量值 //存储1个变量值
- mset 变量名 变量值 变量名 变量值... //存储多个变量值
- get 变量名 //查询1个变量的值
- mget 变量名 //查询多个变量的值
- select 数据库编号0-15(默认只有16个库) //切换库
- keys * //显示所有的变量名
- keys ? //显示只有一个字符的变量名
- keys a? //显示以a开头的2个字符的变量名
- exists 变量名 //测试某个变量是否存在
- ttl 变量名 //查看变量生存时间(-1为永久生存)
- type 变量名 //查看变量的类型
- move 变量名 数据库编号 //移动某变量到指定库中
- expire 变量名 时间 //设置变量的有效时间
- del 变量名 //删除指定的变量
- flushall //删除内存里所有的变量
- flushdb //删除所在的库中的所有变量
- save //保存所有变量到硬盘中
- shutdown //停止redis服务
[root@ku1-50 ~]# vim /etc/redis/6379.conf
93 port 6379 //端口
70 bind 127.0.0.1 //IP地址
137 daemonize yes //守护进程方式运行
187 databases 16 //数据库个数
172 logfile /var/log/redis_6379.log //日志文件
533 # maxclients 10000 //并发连接数量
264 dir /var/lib/redis/6379 //数据库目录
在这里插入图片描述 默认使用noeviction清除策略。
[root@localhost redis-5.0.0]# vim /etc/redis/6379.conf
560 # maxmemory <bytes>
//最大内存
598 # maxmemory-policy noeviction
//默认使用的内存清除策略为noeviction
609 # maxmemory-samples 5
//默认选取变量(key)模板的个数(针对lru、ttl策略)为5个
[root@localhost redis-5.0.0]# vim /etc/redis/6379.conf
70 bind 192.168.2.159 //IP地址
93 port 6379 //端口
508 requirepass 123456 //密码
[root@localhost redis-5.0.0]# /etc/init.d/redis_6379 stop
[root@localhost redis-5.0.0]# /etc/init.d/redis_6379 start
[root@localhost redis-5.0.0]# redis-cli //未携带修改的参数
Could not connect to Redis at 127.0.0.1:6379: Connection refused
not connected>
注意:如果在配置文件中修改了密码、端口、IP地址,那么在连接redis服务的时候必须带上更改的参数才可以连接上服务。
--明文密码连接
[root@localhost redis-5.0.0]# redis-cli -h 192.168.2.159 -a 123456 //密码明文连接
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
192.168.2.159:6379> keys *
1) "name"
192.168.2.159:6379> exit
--交互式密码连接
[root@localhost redis-5.0.0]# redis-cli -h 192.168.2.159
192.168.2.159:6379> keys * //不输入密码无法进行操作
(error) NOAUTH Authentication required.
192.168.2.159:6379> auth 123456
OK
192.168.2.159:6379> keys *
1) "name"
192.168.2.159:6379> exit
在修改了以上三种参数(密码、端口、IP地址)中的任意一种后,都无法再使用脚本来停止服务(启动服务不影响),需要对脚本进行修改。
[root@localhost ~]# /etc/init.d/redis_6379 stop
Stopping ...
Could not connect to Redis at 127.0.0.1:6379: Connection refused
Waiting for Redis to shutdown ...
Waiting for Redis to shutdown ...
Waiting for Redis to shutdown ... //已经使用脚本无法停止服务了
[root@localhost ~]# ss -antulp | grep :6379
tcp LISTEN 0 128 192.168.2.159:6379 *:* users:(("redis-server",pid=14558,fd=6))
[root@localhost ~]# redis-cli -h 192.168.2.159 -a 123456 shutdown
//只能这样停止redis服务
[root@localhost ~]# ss -antulp | grep :6379
修改停止服务脚本
[root@localhost ~]# vim /etc/init.d/redis_6379
43 $CLIEXEC -p $REDISPORT shutdown
↓↓↓↓根据更改的参数将以上配置修改为↓↓↓↓
43 $CLIEXEC -h 192.168.2.159 -p 6379 -a 123456 shutdown
[root@localhost ~]# /etc/init.d/redis_6379 stop //使用脚本停止服务
Stopping ...
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
Redis stopped
[root@localhost ~]# ss -antulp | grep :6379 //停止服务成功
[root@localhost ~]# /etc/init.d/redis_6379 start
Starting Redis server...
[root@localhost ~]# ss -antulp | grep :6379
tcp LISTEN 0 128 192.168.2.159:6379 *:* users:(("redis-server",pid=14649,fd=6))