一、redis安装教程
1.安装redis
~]# yum -y install gcc gcc-c++ make ~]# tar -xf redis-4.0.8.tar.gz ~]# cd redis-4.0.8/ redis-4.0.8]# ls 00-RELEASENOTES COPYING Makefile redis.conf runtest-sentinel tests BUGS deps MANIFESTO runtest sentinel.conf utils CONTRIBUTING INSTALL README.md runtest-cluster src redis-4.0.8]# make && make install redis-4.0.8]# cd utils/ 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!
2.查看状态
]# /etc/init.d/redis_6379 status Redis is running (9485)
3.查看监听端口
utils]# netstat -utnlp | grep 6379 tcp 0 0 127.0.0.1:6379 0.0.0.0:* LISTEN 9485/redis-server 1 utils]# ps -C resis-server 1 PID TTY STAT TIME COMMAND 1 ? Ss 0:06 /usr/lib/systemd/systemd --switched-root --system --deserialize 21
4.停止服务
utils]# /etc/init.d/redis_6379 stop Stopping ... Redis stopped [root@haproxy utils]# /etc/init.d/redis_6379 status cat: /var/run/redis_6379.pid: 没有那个文件或目录 Redis is running ()
5.连接redis
utils]# /etc/init.d/redis_6379 start Starting Redis server... [root@haproxy utils]# redis-cli 127.0.0.1:6379> ping PONG //PONG说明服务正常
6.redis应用
1】设置变量test,值为123,查看变量test值
常用指令操作:
Set keyname keyvalue存储
get keyname 获取
127.0.0.1:6379> set test 123 OK 127.0.0.1:6379> get test "123"
2】删除变量
del keyname 删除变量
127.0.0.1:6379> set k1 v1 OK 127.0.0.1:6379> get k1 "v1" 127.0.0.1:6379> del k1 (integer) 1
3】打印所有变量
Keys * 打印所有变量
127.0.0.1:6379> keys * 1) "k1" 2) "test"
4】测试是否存在
EXISTS keyname 测试是否存在
127.0.0.1:6379> exists k1 (integer) 0
5】查看类型
type keyname 查看类型
127.0.0.1:6379> set k2 v1 OK 127.0.0.1:6379> type k2 string
6】移动变量
move keyname dbname 移动变量
127.0.0.1:6379> move k2 1 //移动k2到1库 (integer) 1
7】 切换库
Select 数据库编号0-15 切换库
127.0.0.1:6379> select 1 //切换到1库 OK 127.0.0.1:6379[1]> keys * //查看有k2 1) "k2"
8】设置有效时间
expire keyname 10 设置有效时间
127.0.0.1:6379[1]> expire k2 10 (integer) 1
9】查看生存时间
ttl keyname 查看生存时间
127.0.0.1:6379[1]> ttl k2 (integer) -2
10】删除所有变量
flushall 删除所有变量
127.0.0.1:6379[1]> flushall OK 127.0.0.1:6379[1]> keys * (empty list or set)
11】保存所有变量
Save 保存所有变量
127.0.0.1:6379[1]> save OK
12】关闭redis服务
Shutdown 关闭redis服务
127.0.0.1:6379[1]> shutdown not connected>
二、修改redis运行参数
1】修改配置文件
utils]# cp /etc/redis/6379.conf /root/6379.conf //备份一份。避免改错无法还原 utils]# /etc/init.d/redis_6379 stop utils]# vim /etc/redis/6379.conf 70 bind 192.168.4.50 //设置服务使用的IP 93 port 6351 //更改端口号 502 requirepass 123456 //设置密码 utils]# ss -antul | grep 6351 tcp LISTEN 0 128 192.168.4.50:6351 *:*
2.由于修改了配置文件所以在连接的时候需要加上IP和端口
utils]# redis-cli -h 192.168.4.50 -p 6351 192.168.4.50:6351> ping (error) NOAUTH Authentication required. 192.168.4.50:6351> auth 123456 //输入密码才能操作(因为配置文件设置了密码) OK 192.168.4.50:6351> ping PONG
3.也可以直接在命令行输入密码连接
utils]# redis-cli -h 192.168.4.50 -p 6351 -a 123456 192.168.4.50:6351> ping PONG
2】停止服务
由于修改redis服务运行参数,所以在停止服务的时候也不能用默认的方法停止
utils]# /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 ... .......
utils]# redis-cli -h 192.168.4.50 -p 6351 -a 123456 shutdown //停止成功 [root@haproxy utils]# ss -antul | grep 6351 //查看没有端口
关于运维学习、分享、交流,笔者开通了微信公众号【大隆爱分享】,感兴趣的朋友可以关注下,欢迎加入,建立属于我们自己的小圈子,一起学运维知识。