Redis
MongoDB
NoSQL
? NoSQL(NoSQL = Not Only SQL )
– 意思是 " 不僅僅是 SQL“
– 泛指非關(guān)系型數(shù)據(jù)庫
– 不需要預先定義數(shù)據(jù)存儲結(jié)構(gòu)
– 表的每條記錄都可以有不同的類型和結(jié)構(gòu)
NoSQL 服務軟件
? 主流軟件
– Redis
– MongoDB
– Memcached
– CouchDB
– Neo4j
– FlockDB
Redis 特點:
– 支持數(shù)據(jù)持久化,可以把內(nèi)存里數(shù)據(jù)保存到硬盤中
– 不僅僅支持 key/values 類型的數(shù)據(jù),同時還支持 list
hash set zset 類型
– 支持 master-salve 模式數(shù)據(jù)備份
裝包:
1)[root@host50 ~]# tar -xf redis-4.0.8.tar.gz
[root@host50 ~]# cd redis-4.0.8/
[root@host50 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
[root@host50 redis-4.0.8]#
[root@host50 redis-4.0.8]# rpm -q gcc gcc-c++
[root@host50 redis-4.0.8]# yum -y install gcc gcc-c++
[root@host50 redis-4.0.8]# make
[root@host50 redis-4.0.8]# make install
2)初始化配置
[root@host50 redis-4.0.8]# cd utils/
[root@host50 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
[root@host50 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? 數(shù)據(jù)存儲目錄
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@host50 utils]# ss -nuplt | grep 6379
tcp? ? LISTEN? ? 0? ? ? 128? ? 127.0.0.1:6379? ? ? ? ? ? ? ? ? *:*? ? ? ? ? ? ? ? ? users:(("redis-server",pid=5058,fd=6))
[root@host50 utils]# /etc/init.d/redis_6379 status ###服務運行腳本
Redis is running (5058)
[root@host50 utils]# /etc/init.d/redis_6379 stop 停止
Stopping ...
Waiting for Redis to shutdown ...
Redis stopped
[root@host50 utils]# /etc/init.d/redis_6379 status
cat: /var/run/redis_6379.pid: 沒有那個文件或目錄
Redis is running () 沒PId號 沒起服務
[root@host50 utils]# /etc/init.d/redis_6379 start 啟動
Starting Redis server...
[root@host50 utils]# /etc/init.d/redis_6379 status 查看狀態(tài)
Redis is running (5120)
[root@host50 utils]#
[root@host50 utils]# ps -C redis-server
? PID TTY? ? ? ? ? TIME CMD
5120 ?? ? ? ? 00:00:00 redis-server
初始化完成后服務自動運行
4訪問redis服務器存儲數(shù)據(jù)
[root@host50 utils]# redis-cli
127.0.0.1:6379>ping? ##測試能否正常存儲數(shù)據(jù)
PONG
127.0.0.1:6379> exit
[root@host50 utils]# redis-cli
127.0.0.1:6379> set name bob? 存數(shù)據(jù)
OK
127.0.0.1:6379> get name? 讀數(shù)據(jù)
"bob"
127.0.0.1:6379> quit
現(xiàn)在時寫在內(nèi)存的,但他會定期將內(nèi)存數(shù)據(jù)寫入硬盤
[root@host50 utils]# ls /var/lib/redis/6379/? ###數(shù)據(jù)庫目錄文件
dump.rdb
[root@host50 utils]#
– Set keyname keyvalue // 存儲
– get keyname // 獲取ls
– Select 數(shù)據(jù)庫編號 0-15 // 切換庫
– Keys * // 打印所以變量
– Keys a? // 打印指定變量
– Exits keyname // 測試是否存在
– ttl keyname? // 查看生存時間 (-1表示永不過期,-2表示已過期)
– type keyname // 查看類型
– move keyname dbname // 移動變量
– expire keyname 10 // 設(shè)置有效時間
– del keyname? // 刪除變量
– flushall // 刪除所有變量
– save // 保存變量
– shutdown // 關(guān)閉服務
[root@host50 6379]# redis-cli
127.0.0.1:6379> select 1
OK
127.0.0.1:6379[1]> select 2
OK
127.0.0.1:6379[2]> select 0
OKpho
127.0.0.1:6379> keys *
1) "name"
127.0.0.1:6379> keys a?? 一個問號代表一個字符
(empty list or set)
127.0.0.1:6379> keys n?
(empty list or set)
127.0.0.1:6379> keys n???
1) "name"
127.0.0.1:6379> del name 刪除 返回值為1 表示刪除成功
(integer) 1
flushall 清空所有
? ? ? ? ? ? ? ? ? ? 一 配置 php 支持 Redis
[root@host50 html]# php -m | grep mysql? 檢查php支持mysql
mysql
mysqli
pdo_mysql
[root@host50 html]# php -m | grep redis 同上
1)? 安裝 php 擴展
#yum -y install autoconf
#yum -y install automake
[root@host50 ~]# rpm -ivh php-devel-5.4.16-42.el7.x86_64.rpm
[root@host50 ~]# cd phpredis-2.2.4/
[root@host50 phpredis-2.2.4]# ls
arrays.markdown? debian? ? ? ? ? ? php_redis.h? ? ? ? redis_session.c
common.h? ? ? ? debian.control? ? README.markdown? ? redis_session.h
config.h? ? ? ? library.c? ? ? ? redis_array.c? ? ? rpm
config.m4? ? ? ? library.h? ? ? ? redis_array.h? ? ? serialize.list
config.w32? ? ? mkdeb-apache2.sh? redis_array_impl.c? tests
COPYING? ? ? ? ? mkdeb.sh? ? ? ? ? redis_array_impl.h
CREDITS? ? ? ? ? package.xml? ? ? redis.c
[root@host50 phpredis-2.2.4]# which phpize
/usr/bin/phpize
[root@host50 phpredis-2.2.4]# phpize? ###這個命令就是輸出當前源碼配置信息
Configuring for:
PHP Api Version:? ? ? ? 20100412
Zend Module Api No:? ? ? 20100525
Zend Extension Api No:? 220100525
[root@host50 phpredis-2.2.4]# ls /usr/bin/php-config 同時生成這個文件
# ./configure --with-php-config=/usr/bin/php-config
# make
[root@host50 phpredis-2.2.4]# make install
Installing shared extensions:? ? /usr/lib64/php/modules/共享文件夾
[root@host50 phpredis-2.2.4]# ls /usr/lib64/php/modules/
curl.so? ? ? json.so? ? mysql.so? ? ? pdo.so? ? ? ? phar.so? sqlite3.so
fileinfo.so? mysqli.so? pdo_mysql.so? pdo_sqlite.so? redis.so? zip.so
redis.so就是redis連接php模塊
2)修改php配置文件定義模塊名和目錄
[root@host50 phpredis-2.2.4]# vim /etc/php.ini
726 ; Directory in which the loadable extensions (modules) reside.
727 ; http://php.net/extension-dir
728? extension_dir = "/usr/lib64/php/modules" ####
729 ; On windows:
730 extension = "redis.so"? ####值修改這兩行
731
[root@host50 phpredis-2.2.4]# systemctl restart php-fpm
查看是否支持模塊
[root@host50 phpredis-2.2.4]# php -m | grep redis
redis
[root@host50 phpredis-2.2.4]#
編寫測試文件
[root@bogon bin]# cat
/usr/local/nginx/html/redis.php
<?php
$redis = new redis();
$redis->connect('127.0.0.1',6379);
$redis->set('redistest','666666');
echo $redis->get('redistest');
?>
[root@host50 phpredis-2.2.4]# redis-cli
127.0.0.1:6379> get redistest
"666666"
127.0.0.1:6379>
修改redis配置文件參數(shù)
12 # 1k => 1000 bytes
? 13 # 1kb => 1024 bytes
? 14 # 1m => 1000000 bytes
? 15 # 1mb => 1024*1024 bytes
? 16 # 1g => 1000000000 bytes
? 17 # 1gb => 1024*1024*1024 bytes
? 18 #
? 19 # units are case insensitive so 1GB 1Gb 1gB are all the sam
69 # ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
? 70 bind 192.168.4.50 跟多個ip用空格隔開
? 71
93 port 6350 修改默認端口
[root@host50 phpredis-2.2.4]# redis-cli -h 192.168.4.50 -p 6350
192.168.4.50:6350> exit
修改端口或者ip后 重啟后不能再用/etc/init.d/redis_6379腳本停止,因為ip變了
[root@host50 phpredis-2.2.4]# redis-cli -h 192.168.4.50 -p 6350 shutdown
停止只能這樣 腳本能啟動不能停止 或者把這條停止命令寫入腳本
[root@host50 phpredis-2.2.4]# /etc/init.d/redis_6379 status
cat: /var/run/redis_6379.pid: 沒有那個文件或目錄
Redis is running ()
修改腳本讓改ip和端口后還能用腳本停
[root@host50 phpredis-2.2.4]# vim /etc/init.d/redis_6379
8 REDISPORT="6350"
43? ? ? ? ? ? $CLIEXEC -h 192.168.4.50 -p $REDISPORT shutdown
常用配置選項
– port 6379 // 端口
– bind 127.0.0.1 //IP 地址
– tcp-backlog 511 //tcp 連接總數(shù)
– timeout 0 // 連接超時時間
– tcp-keepalive 300 // 長連接時間
– daemonize yes // 守護進程方式運行 /
– databases 16 // 數(shù)據(jù)庫個數(shù)
– logfile /var/log/redis_6379.log //pid 文件
– maxclients 10000 // 并發(fā)連接數(shù)量
– dir /var/lib/redis/6379 // 數(shù)據(jù)庫目錄
內(nèi)存管理
? 內(nèi)存清除策略
– volatile-lru 最近最少使用 (針對設(shè)置了過期時間的 ke
y )
– allkeys-lru 刪除最少使用的 key
– volatile-random 在設(shè)置了過期的 key 里隨機移除
– allkeys-random 隨機移除 key
– volatile-ttl (minor TTL) 移除最近過期的 key
– noeviction
不刪除 寫滿時報錯
內(nèi)存管理(續(xù) 1 )
? 選項默認設(shè)置
– maxmemory <bytes> // 最大內(nèi)存
– maxmemory-policy noeviction // 定義使用的策略
– maxmemory-samples 5 // 選取模板數(shù)據(jù)的個數(shù)
(針對 lru 和 ttl 策略)
設(shè)置連接密碼
500 #
501 requirepass 123456? 密碼
502
503 # Command renaming.
504 #
[root@host50 phpredis-2.2.4]# /etc/init.d/redis_6379 start
Starting Redis server...
[root@host50 phpredis-2.2.4]# ss -nuplt | grep 6350
tcp? ? LISTEN? ? 0? ? ? 128? ? 192.168.4.50:6350? ? ? ? ? ? ? ? ? *:*? ? ? ? ? ? ? ? ? users:(("redis-server",pid=22763,fd=6))
[root@host50 phpredis-2.2.4]# redis-cli -h 192.168.4.50 -p 6350 shutdown
(error) NOAUTH Authentication required.
[root@host50 phpredis-2.2.4]# redis-cli -h 192.168.4.50 -p 6350
192.168.4.50:6350> ping
(error) NOAUTH Authentication required.
192.168.4.50:6350> auth 123456? ####登陸后再數(shù)密碼
OK
192.168.4.50:6350> ping
PONG
192.168.4.50:6350> exit
[root@host50 phpredis-2.2.4]# redis-cli -h 192.168.4.50 -p 6350 -a 123456
192.168.4.50:6350> ping? ? ? ? ? ? ? #####登陸時輸入密碼
PONG
有密碼后停服務:
[root@host50 phpredis-2.2.4]# redis-cli -h 192.168.4.50 -p 6350 -a 123456 shutdown
把密碼寫入停止
stop)
? ? ? ? if [ ! -f $PIDFILE ]
? ? ? ? then
? ? ? ? ? ? echo "$PIDFILE does not exist, process is not running"
? ? ? ? else
? ? ? ? ? ? PID=$(cat $PIDFILE)
? ? ? ? ? ? echo "Stopping ..."
? ? ? ? ? ? $CLIEXEC -h 192.168.4.50 -p $REDISPORT -a 123456 shutdown
? ? ? ? ? ? while [ -x /proc/${PID} ]
? ? ? ? ? ? do
? ? ? ? ? ? ? ? echo "Waiting for Redis to shutdown ..."
? ? ? ? ? ? ? ? sleep 1
? ? ? ? ? ? done
? ? ? ? ? ? echo "Redis stopped"
? ? ? ? fi
? ? ? ? ;;
Port? ? ? ? ? : 6379
Config file? ? : /etc/redis/6379.conf? 主配置文件
Log file? ? ? : /var/log/redis_6379.log? 日志
Data dir? ? ? : /var/lib/redis/6379? 數(shù)據(jù)存儲目錄
Executable? ? : /usr/local/bin/redis-server
Cli Executable : /usr/local/bin/redis-cli
/etc/init.d/redis-6379 啟動腳本
[root@host57 utils]# scp 192.168.4.50:/root/1.txt /root
在57主機上拷貝50主機文件 把4.50/root/1.txt 拷貝到57/root下