数据库类型:
关系型RDBMS, 按照预先设置的组织结构(建表),将数据存储在物理介质上;数据之间可以做关联操作(MYSQL,MARIADB,ORACLE,DB2,SQL SERVER)
非关系型NOSQL,NOT ONLY SQL,无需预先定义存储结构,每条记录可以有不同的数据类型和字段个数(Memcached,Redis,MongoDB,CouchDB,Neo4j,FlockDB)
Redis(Remote Dictionary Server)
是一款高性能(key/value)分布式内存数据库
支持数据持久化(定期把内存里的数据存储到硬盘)
支持多种数据类型string,list,hash
支持master-slave模式数据备份
中文网站www.redis.cn
搭建Redis
安装软件
yum -y install gcc
tar -xf redis-4.0.8.tar.gz
cd redis-4.0.8
make&& make install
初始化配置
[root@node50 redis-4.0.8]# ./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!
默认使用lo口,本地还回接口
[root@node50 redis-4.0.8]# /etc/init.d/redis_6379 stop //停止服务
Stopping …
Redis stopped
[root@node50 redis-4.0.8]# /etc/init.d/redis_6379 start //启动服务
Starting Redis server…
[root@node50 redis-4.0.8]# /etc/init.d/redis_6379 status //查询运行状态
Redis is running (4619)
常用操作
[root@node50 redis-4.0.8]# redis-cli //登录本机redis
127.0.0.1:6379> ping //测试
PONG
127.0.0.1:6379> keys * //查询所有变量
(empty list or set)
127.0.0.1:6379> set name bob //设置变量值, name是变量名,bob是值
OK
127.0.0.1:6379> get name //查询变量
“bob”
127.0.0.1:6379> exit //退出登录
[root@node50 redis-4.0.8]# ls /var/lib/redis/6379/dump.rdb //定期会把内存中的数据写到这个路径,关闭服务也会写入这个位置
127.0.0.1:6379> mset age 19 sex male //同时建立多个变量
OK
127.0.0.1:6379> keys * //*表示所有
配置文件/etc/redis/6379.conf
常见配置
内存清除策略
[root@node50 redis-4.0.8]# /etc/init.d/redis_6379 stop
[root@node50 redis-4.0.8]# vim +501 /etc/redis/6379.conf //修改配置文件,密码,端口,IP
requirepass 123456
port 6350
bind 192.168.4.50
:wq
/etc/init.d/redis_6379 start
redis-cli -h 192.168.4.50 -p 6350 -a 123456 //登录redis
redis-cli -h 192.168.4.50 -p 6350 -a 123456 shutdown //关闭服务
部署LNMP+Redis
安装nginx,php,mariadb
yum -y install gcc pcre-devel zlib-devel
tar -zxvf nginx-1.12.2.tar.gz
cd nginx-1.12.2
./configure
make && make install
yum -y install php-fpm
yum -y install mariadb-server mariadb
yum -y install php php-mysql
修改nginx配置文件
[root@node57 ~]# vim +65 /usr/local/nginx/conf/nginx.conf
location ~ .php$ {
root html;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
include fastcgi.conf;
}
wq
[root@node57 ~]# /usr/local/nginx/sbin/nginx -t //测试配置文件
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
[root@node57 ~]# /usr/local/nginx/sbin/nginx //启动程序
[root@node57 ~]# systemctl start php-fpm
[root@node57 ~]# systemctl start mariadb
编写网页文件
[root@node57 ~]# vim /usr/local/nginx/html/test.php
:wq
[root@node57 ~]# firefox 192.168.4.57/test.php //测试
配置支持redis
[root@node57 ~]# yum -y install php-devel autoconf automake
[root@node57 phpredis-2.2.4]# tar -xf php-redis-2.2.4.tar.gz
[root@node57 phpredis-2.2.4]# cd phpredis-2.2.4/
[root@node57 phpredis-2.2.4]# phpize //生成配置文件php-config及configure命令
[root@node57 phpredis-2.2.4]# ./configure --with-php-config=/usr/bin/php-config
[root@node57 phpredis-2.2.4]# make
[root@node57 phpredis-2.2.4]# make install
Installing shared extensions: /usr/lib64/php/modules/
[root@node57 phpredis-2.2.4]# vim +728 /etc/php.ini //修改配置文件,支持redis模块
extension_dir = “/usr/lib64/php/modules/”
; On windows:
extension = “redis.so”
:wq
[root@node57 phpredis-2.2.4]# systemctl restart php-fpm
[root@node57 phpredis-2.2.4]# php -m | grep redis //查看php启动模块
redis
编写测试页面
vim /usr/local/nginx/html/redis.php
:wq
[root@node57 phpredis-2.2.4]# curl 192.168.4.57/redis.php //测试
redhat