一. NoSQL概述
1. NoSQL=Not Only SQL
- 泛指非关系型数据库
- 不需要预先定义数据存储结构
- 每条记录可以有不同的类型和结构
2. 主流软件
- Redis
- MongoDB
- Memcached
二. Redis部署
1. Redis介绍
- Remote Dictionary Server(远程字典服务器)
- 高性能key/value分布式内存数据库
- 支持数据持久化,可以把内存数据保存在硬盘中
- 支持list、hash、set、zset数据类型
- 支持master-slave数据备份
2. 安装
(1) 装包
yum -y install gcc gcc-c++ #安装依赖tar -zxvf redis-4.0.8.tar.gz #安装redis源码包cd redis-4.0.8/make && make install
(2) 初始化配置
- 端口:6379
- 主配置文件:/etc/redis/6379.conf
- 数据库目录:/var/lib/redis/6379
- log文件:/var/log/redis_6379.log
cd redis-4.0.8/utils ./install_server.sh Welcome to the redis service installerThis script will help you easily set up a running redis serverPlease 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 : 6379Config file : /etc/redis/6379.confLog file : /var/log/redis_6379.logData dir : /var/lib/redis/6379Executable : /usr/local/bin/redis-serverCli Executable : /usr/local/bin/redis-cliIs this ok? Then press ENTER to go on or Ctrl-C to abort. #回车Copied /tmp/6379.conf => /etc/init.d/redis_6379Installing service...Successfully added to chkconfig!Successfully added to runlevels 345!Starting Redis server...Installation successful!
(3) 启动、停止服务
/etc/init.d/redis_6379 start #启动/etc/init.d/redis_6379 stop #停止
(4) 连接Redis数据库
Redis-cli
3. 常用操作指令
127.0.0.1:6379> keys * //输出所有变量(empty list or set)127.0.0.1:6379> set name bob //存储OK127.0.0.1:6379> keys *1) "name"127.0.0.1:6379> get name //获取"bob"127.0.0.1:6379> set aa 10OK127.0.0.1:6379> set ab 10OK127.0.0.1:6379> set ac 20OK127.0.0.1:6379> keys a? //打印指定变量1) "ac"2) "ab"3) "aa"127.0.0.1:6379> keys a??(empty list or set)127.0.0.1:6379> EXISTS name //测试是否存在,存在(integer) 1127.0.0.1:6379> EXISTS age //不存在(integer) 0127.0.0.1:6379> set sex girl OK127.0.0.1:6379> ttl sex //查看生存时间(integer) -1127.0.0.1:6379> EXPIRE sex 20 //设置有效时间,秒127.0.0.1:6379> type ac //查看类型string127.0.0.1:6379> get ac"20"127.0.0.1:6379> select 1 //切换库,0-15OK127.0.0.1:6379[1]> keys *(empty list or set)127.0.0.1:6379[1]> select 0OK127.0.0.1:6379> keys *1) "ac"2) "aa"3) "ab"127.0.0.1:6379> MOVE ac 1 //移动变量(integer) 1127.0.0.1:6379> SELECT 1OK127.0.0.1:6379[1]> keys *1) "ac"del ac //删除变量flushdb //删除当前数据的变量flushall //删除所有变量save //保存所有变量(放入内存)shutdown //关闭服务
4. 内存管理
(1) 内存清除策略
- volatile-lru //删除最近最少使用的设置了TTL的key
- allkeys-lru //删除最近最少使用的key
- volatile-random //在设置了TTL的key中随机删除
- allkeys-random //随机删除key
- volatile-ttl //移除最近过期的key
- noeviction //不删除,写满时报错。默认。
(2) 默认设置(位于配置文件中)
560 # maxmemory#设置最大内存591 # maxmemory-policy noeviction #定义使用策略,默认为不删除602 # maxmemory-samples 5 #针对lru和ttl选取模板数据的个数
5. 设置端口,主机,连接密码
vim /etc/redis/6379.conf 70 bind 192.168.4.50 #修改登陆主机 93 port 6350 #修改端口 501 requirepass 123456 #修改登陆密码redis-cli -h 192.168.4.50 -p 6350 -a 123456 #登陆
三. 部署LNMP+Redis
1. 部署LNMP
装包:
yum -y install gcc gcc-c++ pcre-devel zlib-devel #安装依赖tar -zxvf nginx-1.12.2.tar.gz cd nginx-1.12.2/./configure --prefix=/usr/local/nginxmake && make install
yum -y install php-fpm-5.4.16-42.el7.x86_64.rpm #安装php支持
修改nginx配置文件:
vim /usr/local/nginx/conf/nginx.conf location ~ \.php$ { root html; fastcgi_pass 127.0.0.1:9000; fastcgi_index index.php; # fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name; include fastcgi.conf; }
启动服务:
systemctl stop httpdln -s /usr/local/nginx/sbin/nginx /usr/local/binnginx #启动nginxsystemctl start php-fpm #启动php-fpmss -antup | grep :9000
测试:
vim /usr/local/nginx/html/test.php curl 127.0.0.1/test.php
2. 配置php支持Redis
yum -y install php-devel-5.4.16-42.el7.x86_64.rpm #安装依赖,此包需要单独下载tar -zxf php-redis-2.2.4.tar.gz cd phpredis-2.2.4//usr/bin/phpizefind / -name php-config./configure --with-php-config=/usr/bin/php-configmake && make install#需要记录下输出#Installing shared extensions: /usr/lib64/php/modules/vim /etc/php.ini #修改模块的路径和模块名 728 extension_dir = "/usr/lib64/php/modules/" 730 extension = "redis.so"php -m | grep -i redis #检查是否支持redis
3. 编写配置文件测试
vim /usr/local/nginx/html/redis.php connect('192.168.4.54',6354);$redis->set('abc','6666');echo $redis->get('abc');?>
firefox 127.0.0.1/redis.php