1. 程式人生 > >Redis服務-linux下的配置

Redis服務-linux下的配置

關係資料庫管理系統
-RDMS
-資料按照預先設定的組織結構,儲存在


NoSQL(NoSQL=Not Only SQL)
-非關係型資料庫
-不需要預先定義資料儲存結構
-表的每條記錄都可以有不同的型別和結構


主流軟體
-Redis
-MongoDB
-Memcached
-CouchDB
-Neo4j
-FlockDB


部署Redis服務
--Remote Dictionary Server(遠端字典伺服器)
--是一款高效能的(Key/Values)分散式記憶體資料庫
--支援資料持久化,可以把記憶體資料儲存到硬碟中
也支援list、hash、set、zset資料型別
-支援master-salve模式資料備份
-中文網站www.redis.cn
--支援主從模式,實現資料的自動備份






裝包
初始化配置
啟動服務
檢視服務執行資訊
連線服務儲存資料
[
[email protected]
~]# tar -xzf redis-4.0.8.tar.gz 


[[email protected] ~]# cd redis-4.0.8/
[[email protected] 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
[
[email protected]
~]# rpm -q gcc gcc-c++
gcc-4.8.5-16.el7.x86_64
gcc-c++-4.8.5-16.el7.x86_64


[[email protected] redis-4.0.8]# make
[[email protected] redis-4.0.8]# make install
[[email protected] redis-4.0.8]# cd utils/
[[email protected] utils]# ls
build-static-symbols.tcl  hashtable          redis_init_script.tpl
cluster_fail_time.tcl     hyperloglog        redis-sha1.rb
corrupt_rdb.c             install_server.sh  releasetools
create-cluster            lru                speed-regression.tcl
generate-command-help.rb  redis-copy.rb      whatisdoing.sh
graphs                    redis_init_script
[
[email protected]
utils]# vim install_server.sh 
[[email protected] 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!
[[email protected] utils]# netstat -utnlp | grep :6379
tcp        0      0 127.0.0.1:6379          0.0.0.0:*               LISTEN      5118/redis-server 1 
[[email protected] utils]# /etc/init.d/redis_6379 status
Redis is running (5118)
[[email protected] utils]# /etc/init.d/redis_6379 stop
Stopping ...
Waiting for Redis to shutdown ...
Redis stopped
[[email protected] utils]# /etc/init.d/redis_6379 status
cat: /var/run/redis_6379.pid: 沒有那個檔案或目錄
Redis is running ()
[[email protected] utils]# /etc/init.d/redis_6379 start
Starting Redis server...
[[email protected] utils]# /etc/init.d/redis_6379 status
Red is running (5151)
[[email protected] utils]# ps -C redis-server
  PID TTY          TIME CMD
 5151 ?        00:00:00 redis-server
is is running (5151)


4.訪問資料行命令格式


[[email protected] utils]# redis-cli
127.0.0.1:6379> ping
PONG
127.0.0.1:6379> quit
[[email protected] utils]# redis-cli
127.0.0.1:6379> ping
PONG
127.0.0.1:6379> set name bob
OK
127.0.0.1:6379> get name
"bob"
127.0.0.1:6379> quit
[[email protected] utils]# ls /var/lib/redis/6379/
dump.rdb










[[email protected] ~]# /etc/init.d/redis_6379  start
Starting Redis server...
[[email protected] ~]# mysql -uroot -p123456
mysql: [Warning] Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 3
Server version: 5.7.17 MySQL Community Server (GPL)


Copyright (c) 2000, 2016, Oracle and/or its affiliates. All rights reserved.


Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.


Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.


mysql> cd
    -> ^DBye


[[email protected] ~]# redis-cli
127.0.0.1:6379> select 1
OK
127.0.0.1:6379[1]> select 0
OK
127.0.0.1:6379> select 16
(error) ERR DB index is out of range
127.0.0.1:6379> select 2
OK
127.0.0.1:6379[2]> select 0
OK
127.0.0.1:6379> 
127.0.0.1:6379> Keys *
1) "name"
127.0.0.1:6379> get  name
"bob"
127.0.0.1:6379> set name lucy
OK
127.0.0.1:6379> get name
"lucy"
127.0.0.1:6379> set aa 1
OK
127.0.0.1:6379> set aaa 21
OK
127.0.0.1:6379> Keys *
1) "aaa"
2) "name"
3) "aa"
127.0.0.1:6379> del aa
(integer) 1
127.0.0.1:6379> Keys *
1) "aaa"
2) "name"
127.0.0.1:6379> flushall
OK
127.0.0.1:6379> Keys *
(empty list or set)
127.0.0.1:6379> save
OK
127.0.0.1:6379> shutdown
not connected> ping
Could not connect to Redis at 127.0.0.1:6379: Connection refused
not connected> exit
[[email protected] ~]# netstat -utnlp | grep 6379






[[email protected] ~]# redis-cli
127.0.0.1:6379> Exits age
(error) ERR unknown command 'Exits'
127.0.0.1:6379> Exsits age
\(error) ERR unknown command 'Exsits'
127.0.0.1:6379> EXISTS age
(integer) 0
127.0.0.1:6379> set age
(error) ERR wrong number of arguments for 'set' command
127.0.0.1:6379> set age 12
OK
127.0.0.1:6379> EXISTS age
(integer) 1
127.0.0.1:6379> type age
string
127.0.0.1:6379> select 1
OK
127.0.0.1:6379[1]> key *
(error) ERR unknown command 'key'
127.0.0.1:6379[1]> keys *
(empty list or set)
127.0.0.1:6379[1]> select 0
OK
127.0.0.1:6379> move age 1
(integer) 1
127.0.0.1:6379> select 1
OK
127.0.0.1:6379[1]> Keys *
1) "age"
127.0.0.1:6379[1]> select 0
OK
127.0.0.1:6379> Keys *
(empty list or set)
127.0.0.1:6379> 
















更改IP和埠號
[[email protected] ~]# vim /etc/redis/6379.conf


  70 bind 192.168.4.52
  93 port 6379
[[email protected] ~]# /etc/init.d/redis_6379 start
Starting Redis server...
[[email protected] ~]# /etc/init.d/redis_6379 status




讓其手動關閉變成自動關閉


[[email protected] ~]# vim /etc/init.d/redis_6379 
 8 REDISPORT="6350"
 43  $CLIEXEC  -h 192.168.4.50 -p $REDISPORT shutdown -a 123456


[[email protected] ~]# /etc/init.d/redis_6379 start
Starting Redis server...
[[email protected] ~]# /etc/init.d/redis_6379 status


設定密碼
[[email protected] ~]# vim /etc/init.d/redis_6379 
501 requirepass 123456


[[email protected] ~]# redis-cli -h 192.168.4.51 -p6350 -a 123456


[[email protected] ~]# /etc/init.d/redis_6379 start
Starting Redis server...
[[email protected] ~]# /etc/init.d/redis_6379 status




預設儲存資料的檔案
[[email protected] ~]# ls /var/lib/redis/6379/
dump.rdb














搭建Redis伺服器
服務的常用配置 IP地址 埠 資料庫位置 資料庫個數 記憶體清除 設定連線密碼


管理資料的常用命令:set get del move flusha;; type ttl select


網站網路的環境 LNMP+Redis






訪問熱度高的在redis上,因為redis資料儲存在記憶體中