一、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]# ls00-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 selectthe redis portforthisinstance: [6379]
Selecting default:6379Please selectthe redis config file name [/etc/redis/6379.conf]
Selected default- /etc/redis/6379.conf
Please selectthe redis log file name [/var/log/redis_6379.log]
Selected default- /var/log/redis_6379.log
Please selectthe data directoryforthisinstance [/var/lib/redis/6379]
Selected default- /var/lib/redis/6379Please selectthe redis executable path [/usr/local/bin/redis-server]
Selected config:
Port? ? ? ? ? : 6379Config file? ? : /etc/redis/6379.conf
Log file? ? ? : /var/log/redis_6379.log
Data dir? ? ? : /var/lib/redis/6379Executable? ? : /usr/local/bin/redis-server
Cli Executable : /usr/local/bin/redis-cli
Is thisok? 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.查看狀態(tài)
]# /etc/init.d/redis_6379? status
Redis isrunning (9485)
3.查看監(jiān)聽端口
utils]# netstat -utnlp | grep6379tcp? ? ? ? 00127.0.0.1:63790.0.0.0:*? ? ? ? ? ? ? LISTEN9485/redis-server1utils]# ps -C resis-server1? PID TTY? ? ? STAT? TIME COMMAND
? ? 1?? ? ? ? Ss0:06/usr/lib/systemd/systemd --switched-root --system --deserialize21
4.停止服務(wù)
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: 沒有那個(gè)文件或目錄
Redis isrunning ()
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說明服務(wù)正常
6.redis應(yīng)用
1】設(shè)置變量test,值為123,查看變量test值
常用指令操作:
Set keyname keyvalue存儲(chǔ)
get keyname 獲取
127.0.0.1:6379>set test 123
OK127.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】測(cè)試是否存在
EXISTS keyname 測(cè)試是否存在
127.0.0.1:6379> exists k1
(integer) 0
5】查看類型
type keyname 查看類型
127.0.0.1:6379>set k2 v1
OK127.0.0.1:6379> type k2
string
6】移動(dòng)變量
move keyname dbname 移動(dòng)變量
127.0.0.1:6379> move k2 1? //移動(dòng)k2到1庫
(integer) 1
7】 切換庫
Select 數(shù)據(jù)庫編號(hào)0-15 切換庫
127.0.0.1:6379>select 1? //切換到1庫
OK
127.0.0.1:6379[1]> keys *? //查看有k2
1)"k2"
8】設(shè)置有效時(shí)間
expire keyname 10 設(shè)置有效時(shí)間
127.0.0.1:6379[1]> expire k2 10
(integer) 1
9】查看生存時(shí)間
ttl keyname 查看生存時(shí)間
127.0.0.1:6379[1]> ttl k2
(integer) -2
10】刪除所有變量
flushall 刪除所有變量
127.0.0.1:6379[1]> flushall
OK127.0.0.1:6379[1]> keys *
(empty list or set)
11】保存所有變量
Save 保存所有變量
127.0.0.1:6379[1]> save
OK
12】關(guān)閉redis服務(wù)
Shutdown 關(guān)閉redis服務(wù)
127.0.0.1:6379[1]> shutdown
not connected>
二、修改redis運(yùn)行參數(shù)
1】修改配置文件
utils]# cp /etc/redis/6379.conf? /root/6379.conf//備份一份。避免改錯(cuò)無法還原
utils]# /etc/init.d/redis_6379 stop
utils]# vim /etc/redis/6379.conf
70bind192.168.4.50//設(shè)置服務(wù)使用的IP93port6351//更改端口號(hào)502requirepass123456//設(shè)置密碼
utils]# ss -antul | grep6351tcp? ? LISTEN? ? 0128192.168.4.50:6351*:*
2.由于修改了配置文件所以在連接的時(shí)候需要加上IP和端口
utils]# redis-cli -h192.168.4.50-p6351192.168.4.50:6351> ping
(error) NOAUTH Authentication required.
192.168.4.50:6351> auth 123456? ?//輸入密碼才能操作(因?yàn)榕渲梦募O(shè)置了密碼)
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】停止服務(wù)
由于修改redis服務(wù)運(yùn)行參數(shù),所以在停止服務(wù)的時(shí)候也不能用默認(rèn)的方法停止
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? //查看沒有端口
關(guān)于運(yùn)維學(xué)習(xí)、分享、交流,筆者開通了微信公眾號(hào)【大隆愛分享】,感興趣的朋友可以關(guān)注下,歡迎加入,建立屬于我們自己的小圈子,一起學(xué)運(yùn)維知識(shí)。