Top

NSD NOSQL DAY01

  1. 案例1:搭建Redis服务器
  2. 案例2:常用命令
  3. 案例3:修改Redis服务运行参数
  4. 案例4:部署LNMP+Redis

1 案例1:搭建Redis服务器

1.1 问题

具体要求如下:

1.2 步骤

实现此案例需要按照如下步骤进行。

步骤一:搭建redis服务器

1)安装源码redis软件

  1. [root@redis1 redis]# yum -y install gcc
  2. [root@redis1 redis]# tar -zxf redis-4.0.8.tar.gz
  3. [root@redis1 redis]# cd redis-4.0.8/
  4. [root@redis1 redis-4.0.8]# ls
  5. 00-RELEASENOTES CONTRIBUTING deps Makefile README.md runtest runtest-sentinel src utils
  6. BUGS COPYING INSTALL MANIFESTO redis.conf runtest-cluster sentinel.conf tests
  7. [root@redis1 redis-4.0.8]# make
  8. [root@redis1 redis-4.0.8]# make install
  9. [root@redis1 redis-4.0.8]# cd utils/
  10. [root@redis1 utils]# install_server.sh
  11. Welcome to the redis service installer
  12. This script will help you easily set up a running redis server
  13.  
  14. Please select the redis port for this instance: [6379]
  15. Selecting default: 6379
  16. Please select the redis config file name [/etc/redis/6379.conf]
  17. Selected default - /etc/redis/6379.conf
  18. Please select the redis log file name [/var/log/redis_6379.log]
  19. Selected default - /var/log/redis_6379.log
  20. Please select the data directory for this instance [/var/lib/redis/6379]
  21. Selected default - /var/lib/redis/6379
  22. Please select the redis executable path [/usr/local/bin/redis-server]
  23. Selected config:
  24. Port : 6379                  //端口号
  25. Config file : /etc/redis/6379.conf //配置文件目录
  26. Log file : /var/log/redis_6379.log //日志目录
  27. Data dir : /var/lib/redis/6379 //数据库目录
  28. Executable : /usr/local/bin/redis-server //启动程序的目录
  29. Cli Executable : /usr/local/bin/redis-cli //命令行的连接工具
  30. Is this ok? Then press ENTER to go on or Ctrl-C to abort. //回车完成配置
  31. Copied /tmp/6379.conf => /etc/init.d/redis_6379 //服务启动脚本
  32. Installing service...
  33. Successfully added to chkconfig!
  34. Successfully added to runlevels 345!
  35. Starting Redis server... //提示服务已经启动
  36. Installation successful!        //提示安装成功

2)查看服务状态

  1. [root@redis1 utils]# /etc/init.d/redis_6379 status
  2. Redis is running (15203)

3)查看监听的端口

  1. [root@redis1 utils]# netstat -antupl |grep :6379 //查看端口
  2. tcp 0 0 127.0.0.1:6379 0.0.0.0:* LISTEN 15203/redis-server
  3. [root@redis1 utils]# ps -C redis-server //查看进程
  4. PID TTY TIME CMD
  5. 15203 ? 00:00:00 redis-server

4)停止服务

  1. [root@redis1 utils]# /etc/init.d/redis_6379 stop
  2. Stopping ...
  3. Waiting for Redis to shutdown ...
  4. Redis stopped

5)连接redis

  1. [root@redis1 utils]# /etc/init.d/redis_6379 start
  2. Starting Redis server...
  3. [root@redis1 utils]# redis-cli //默认连接127.0.0.1地址的 6379端口
  4. 127.0.0.1:6379> ping
  5. PONG            //PONG说明服务正常

6)存储变量school,值为tarena,查看变量school的值

常用指令操作:

set keyname keyvalue //存储

get keyname //获取

  1. 127.0.0.1:6379> set school tarena
  2. OK
  3. 127.0.0.1:6379> get school
  4. "tarena"
  5. 127.0.0.1:6379>

2 案例2:常用命令

2.1 问题

2.2 步骤

实现此案例需要按照如下步骤进行。

步骤一:命令set 、 mset 、 get 、 mget

具体操作如下

  1. 192.168.4.50:6350> set name bob
  2. OK
  3. 192.168.4.50:6350>
  4. 192.168.4.50:6350> mset age 19 sex boy
  5. OK
  6. 192.168.4.50:6350>
  7. 192.168.4.50:6350> get name
  8. "bob"
  9. 192.168.4.50:6350>
  10. 192.168.4.50:6350> mget age sex
  11. 1) "19"
  12. 2) "boy"
  13. 192.168.4.50:6350>
  14. 192.168.4.50:6350>

步骤二:命令keys 、 type 、 exists 、 del

具体操作如下

  1. 192.168.4.50:6350> keys *
  2. 1) "sex"
  3. 2) "age"
  4. 3) "name"
  5. 192.168.4.50:6350>
  6. 192.168.4.50:6350> keys ???
  7. 1) "sex"
  8. 2) "age"
  9. 192.168.4.50:6350> keys a*
  10. 1) "age"
  11. 192.168.4.50:6350>
  12. 192.168.4.50:6350> type age //使用set命令存储的变量都是字符类型
  13. string
  14. 192.168.4.50:6350>
  15. 192.168.4.50:6350> del age
  16. (integer) 1
  17. 192.168.4.50:6350>
  18. 192.168.4.50:6350> exists age //变量不存储返回值0
  19. (integer) 0
  20. 192.168.4.50:6350>
  21. 192.168.4.50:6350> exists sex //变量存在 返回值1
  22. (integer) 1
  23. 192.168.4.50:6350>

步骤三:命令ttl 、 expire 、 move 、 flushdb 、flushall 、save、shutdown

、select

具体操作如下

  1. 192.168.4.50:6350> keys *
  2. 1) "sex"
  3. 2) "name"
  4. 192.168.4.50:6350> ttl sex //返回值-1 表示变量永不过期
  5. (integer) -1
  6. 192.168.4.50:6350>
  7. 192.168.4.50:6350> expire sex 20 //设置变量过期时间为 20 秒
  8. (integer) 1
  9. 192.168.4.50:6350>
  10. 192.168.4.50:6350> ttl sex //还剩14秒过期
  11. (integer) 14
  12. 192.168.4.50:6350>
  13. 192.168.4.50:6350> ttl sex //返回值-2 表示已经过期
  14. (integer) -2
  15. 192.168.4.50:6350> exists sex //变量已经不存在
  16. (integer) 0
  17. 192.168.4.50:6350>
  18. 192.168.4.50:6350> move name 1 //把变量name移动到1号库里
  19. (integer) 1
  20. 192.168.4.50:6350>
  21. 192.168.4.50:6350> select 1 //切换到1号库
  22. OK
  23. 192.168.4.50:6350[1]> keys * //查看
  24. 1) "name"
  25. 192.168.4.50:6350[1]> select 0 //切换到0号库
  26. OK
  27. 192.168.4.50:6350> keys * //查看
  28. (empty list or set)
  29. 192.168.4.50:6350>
  30. 192.168.4.50:6350> select 1 //切换到1号库
  31. OK
  32. 192.168.4.50:6350[1]>
  33. 192.168.4.50:6350[1]> keys *
  34. 1) "name"
  35. 192.168.4.50:6350[1]>
  36. 192.168.4.50:6350[1]> flushdb
  37. OK
  38. 192.168.4.50:6350[1]>
  39. 192.168.4.50:6350[1]> keys *
  40. (empty list or set)
  41. 192.168.4.50:6350[1]>
  42. 192.168.4.50:6350[1]> flushall
  43. OK
  44. 192.168.4.50:6350[1]>
  45. 192.168.4.50:6350[1]> save
  46. OK
  47. 192.168.4.50:6350[1]>
  48. 192.168.4.50:6350[1]> shutdown
  49. not connected> //提示连接断开
  50. not connected> exit //退出登录
  51. [root@host50 ~]#
  52. [root@host50 ~]# netstat -utnlp | grep redis-server //没有进程信息
  53. [root@host50 ~]#
  54. [root@host50 ~]# /etc/init.d/redis_6379 start //启动服务
  55. Starting Redis server...
  56. [root@host50 ~]#
  57. [root@host50 ~]# netstat -utnlp | grep redis-server //查看进程信息
  58. tcp 0 0 192.168.4.50:6350 0.0.0.0:* LISTEN 11475/redis-server
  59. [root@host50 ~]#

3 案例3:修改Redis服务运行参数

3.1 问题

3.2 步骤

实现此案例需要按照如下步骤进行。

步骤一:修改主配置文件

1)修改配置文件

  1. [root@host50 utils]# cp /etc/redis/6379.conf /root/6379.conf     
  2. //可以先备份一份,防止修改错误没法还原
  3. [root@host50 utils]# /etc/init.d/redis_6379 stop
  4.  
  5. [root@host50 utils]# vim /etc/redis/6379.conf
  6. ...
  7. bind 192.168.4.50                //设置服务使用的ip
  8. port 6350                            //更改端口号
  9. requirepass 123456                //设置密码
  10. :wq

2)修改启动脚本

  1. [root@host50 ~]# vim +43 /etc/init.d/redis_6379
  2. $CLIEXEC -h 192.168.4.50 -p 6350 -a 123456 shutdown
  3. :wq

3)启动服务

  1. [root@host50 ~]# /etc/init.d/redis_6379 start
  2. Starting Redis server...
  3. [root@host50 ~]#
  4. [root@host50 ~]# netstat -utnlp | grep redis-server
  5. tcp 0 0 192.168.4.50:6350 0.0.0.0:* LISTEN 11523/redis-server
  6. [root@host50 ~]#

4)测试配置

访问服务存取数据

  1. [root@host50 ~]# redis-cli -h 192.168.4.50 -p 6350 -a 123456 //访问服务
  2. 192.168.4.50:6350> ping
  3. PONG
  4. 192.168.4.50:6350> keys *
  5. (empty list or set)
  6. 192.168.4.50:6350>
  7. 192.168.4.50:6350> set x 99
  8. OK
  9. 192.168.4.50:6350>
  10. 192.168.4.50:6350> exit
  11. [root@host50 ~]#

4 案例4:部署LNMP+Redis

4.1 问题

4.2 步骤

实现此案例需要按照如下步骤进行。

步骤一:在主机192.168.4.57部署LNMP 环境

1)安装源码nginx软件及php-fpm

  1. ]#yum -y install gcc pcre-devel zlib-devel //安装依赖
  2. ]#tar -zxvf nginx-1.12.2.tar.gz //解压
  3. ]#cd nginx-1.12.2 //进源码目录
  4. ]#./configure //配置
  5. ……
  6. ……
  7. Configuration summary
  8. + using system PCRE library
  9. + OpenSSL library is not used
  10. + using system zlib library
  11.  
  12. nginx path prefix: "/usr/local/nginx"
  13. nginx binary file: "/usr/local/nginx/sbin/nginx"
  14. nginx modules path: "/usr/local/nginx/modules"
  15. nginx configuration prefix: "/usr/local/nginx/conf"
  16. nginx configuration file: "/usr/local/nginx/conf/nginx.conf"
  17. nginx pid file: "/usr/local/nginx/logs/nginx.pid"
  18. nginx error log file: "/usr/local/nginx/logs/error.log"
  19. nginx http access log file: "/usr/local/nginx/logs/access.log"
  20. nginx http client request body temporary files: "client_body_temp"
  21. nginx http proxy temporary files: "proxy_temp"
  22. nginx http fastcgi temporary files: "fastcgi_temp"
  23. nginx http uwsgi temporary files: "uwsgi_temp"
  24. nginx http scgi temporary files: "scgi_temp"
  25.  
  26. [root@localhost nginx-1.12.2]# make //编译
  27. ……
  28. ……
  29. objs/src/http/modules/ngx_http_upstream_zone_module.o \
  30. objs/ngx_modules.o \
  31. -ldl -lpthread -lcrypt -lpcre -lz \
  32. -Wl,-E
  33. sed -e "s|%%PREFIX%%|/usr/local/nginx|" \
  34. -e "s|%%PID_PATH%%|/usr/local/nginx/logs/nginx.pid|" \
  35. -e "s|%%CONF_PATH%%|/usr/local/nginx/conf/nginx.conf|" \
  36. -e "s|%%ERROR_LOG_PATH%%|/usr/local/nginx/logs/error.log|" \
  37. < man/nginx.8 > objs/nginx.8
  38. make[1]: 离开目录“/root/lnmp/nginx-1.12.2
  39.  
  40. [root@localhost nginx-1.12.2]#make install //安装
  41. ……
  42. ……
  43. test -d '/usr/local/nginx/logs' \
  44. || mkdir -p '/usr/local/nginx/logs'
  45. test -d '/usr/local/nginx/html' \
  46. || cp -R html '/usr/local/nginx'
  47. test -d '/usr/local/nginx/logs' \
  48. || mkdir -p '/usr/local/nginx/logs'
  49. make[1]: 离开目录“/root/lnmp/nginx-1.12.2
  50. [root@localhost nginx-1.12.2]# ls /usr/local //查看安装目录
  51. bin etc games include lib lib64 libexec nginx sbin share src
  52. [root@localhost nginx-1.12.2]#
  53. [root@localhost nginx-1.12.2]# ls /usr/local/nginx //查看目录列表
  54. conf html logs sbin
  55. [root@localhost nginx-1.12.2]#
  56.  
  57. ]#yum -y install php-fpm //安装php-fpm
  58. ……
  59. ……
  60. 已安装:
  61. php-fpm.x86_64 0:5.4.16-45.el7
  62.  
  63. 作为依赖被安装:
  64. libzip.x86_64 0:0.10.1-8.el7 php-common.x86_64 0:5.4.16-45.el7
  65.  
  66. 完毕!

2)修改配置nginx.conf

  1. ] # vim +65 /usr/local/nginx/conf/nginx.conf
  2. location ~ \.php$ {
  3. root html;
  4. fastcgi_pass 127.0.0.1:9000;
  5. fastcgi_index index.php;
  6. include fastcgi.conf;
  7. }
  8. :wq
  9.  
  10. ]# /usr/local/nginx/sbin/nginx -t     //测试修改
  11. nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
  12. nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful

3)启动服务

启动php-fpm服务

  1. ]# systemctl start php-fpm //启动服务
  2. ]# netstat -utnlp | grep :9000 //查看端口

启动nginx服务

  1. ]# /usr/local/nginx/sbin/nginx
  2. ]# netstat -utnlp | grep :80
  3. tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 23505/nginx: master

4)测试配置

  1. ]# vim /usr/local/nginx/html/test.php //编写php文件
  2. <?php
  3. echo "hello world!!!";
  4. ?>
  5. :wq
  6.  
  7. ]# curl http://localhost/test.php //访问nginx服务
  8. hello world!!!

步骤二:配置PHP支持redis

1)安装php扩展

  1. [root@host71 ~]# rpm -q php php-devel
  2. 未安装软件包 php
  3. 未安装软件包 php-devel
  4. [root@host71 ~]#
  5. [root@host71 ~]# rpm -q automake autoconf
  6. 未安装软件包 automack
  7. 未安装软件包 autoconf
  8. [root@host71 ~]#
  9. [root@host71 ~]# yum -y install php php-devel automake autoconf //安装依赖
  10.  
  11. ]# tar -zxf php-redis-2.2.4.tar.gz //安装扩展包
  12. ]# cd phpredis-2.2.4/
  13. ]# phpize //生成配置文件php-config及 configure命令
  14. Configuring for:
  15. PHP Api Version: 20100412
  16. Zend Module Api No: 20100525
  17. Zend Extension Api No: 220100525
  18.  
  19. ]# ./configure --with-php-config=/usr/bin/php-config //配置
  20. ]# make //编译
  21. ]# make install //安装

2)修改php.ini文件

  1. ]#vim /etc/php.ini
  2. 728 extension_dir = "/usr/lib64/php/modules/" //模块文件目录
  3. 730 extension = "redis.so" //模块文件名
  4. :wq
  5.  
  6. ]# systemctl restart php-fpm //重启php-fpm服务
  7. ]# php -m | grep -i redis //查看已加载的模块
  8. redis

步骤三:测试配置:编写网站脚本,把数据存储到redis服务器192.168.4.50

1)查看192.168.4.50主机的redis服务是否运行

  1. [root@host50 ~]# netstat -utnlp | grep redis-server
  2. tcp 0 0 192.168.4.50:6350 0.0.0.0:* LISTEN 11523/redis-server
  3. [root@host50 ~]#
  4.  
  5. [root@host50 ~]# redis-cli -h 192.168.4.50 -p 6350 -a 123456 //访问服务
  6. 192.168.4.50:6350> ping
  7. PONG
  8. 192.168.4.50:6350> exit

2)编写网站脚本

  1. ]# vim /usr/local/nginx/html/linkredis.php
  2. <?php
  3. $redis = new redis();
  4. $redis->connect("192.168.4.50","6350");
  5. $redis->auth("123456");
  6. $redis->set("linux","redhat");
  7. echo $redis->get("linux");
  8. ?>
  9. :wq

3)访问网站脚本

  1. ]#curl http://localhost/linkredis.php     //访问nginx服务
  2. redhat

4)在192.168.4.50 服务器,查看数据

  1. [root@host50 ~]# redis-cli -h 192.168.4.50 -p 6350 -a 123456 //连接redis服务
  2. 192.168.4.50:6350> keys * //查看变量
  3. 1) "linux"
  4. 192.168.4.50:6350>
  5. 192.168.4.50:6350> get linux //获取值
  6. "redhat"
  7. 192.168.4.50:6350>
  8.  
  9.