搭建Redis服務器


1 案例1:搭建Redis服務器

1.1 問題

  • 具體要求如下:
  • 在主機 192.168.4.51 上安裝並啟用 redis 服務
  • 設置變量test,值為123
  • 查看變量test的值

1.2 步驟

實現此案例需要按照如下步驟進行。

步驟一:搭建redis服務器

1)安裝redis服務器

  1. [root@redis1 ~]# cd redis
  2. [root@redis1 redis]# ls
  3. lnmp redis-4.0.8.tar.gz
  4. [root@redis1 redis]# yum -y install gcc gcc-c++ make
  5. [root@redis1 redis]# tar -zxf redis-4.0.8.tar.gz
  6. [root@redis1 redis]# cd redis-4.0.8/
  7. [root@redis1 redis-4.0.8]# ls
  8. 00-RELEASENOTES CONTRIBUTING deps Makefile README.md runtest runtest-sentinel src utils
  9. BUGS COPYING INSTALL MANIFESTO redis.conf runtest-cluster sentinel.conf tests
  10. [root@redis1 redis-4.0.8]# make
  11. [root@redis1 redis-4.0.8]# make install
  12. [root@redis1 redis-4.0.8]# cd utils/
  13. [root@redis1 utils]# ./install_server.sh
  14. Welcome to the redis service installer
  15. This script will help you easily set up a running redis server
  16. Please select the redis port for this instance: [6379]
  17. Selecting default: 6379
  18. Please select the redis config file name [/etc/redis/6379.conf]
  19. Selected default - /etc/redis/6379.conf
  20. Please select the redis log file name [/var/log/redis_6379.log]
  21. Selected default - /var/log/redis_6379.log
  22. Please select the data directory for this instance [/var/lib/redis/6379]
  23. Selected default - /var/lib/redis/6379
  24. Please select the redis executable path [/usr/local/bin/redis-server]
  25. Selected config:
  26. Port : 6379                  //端口號
  27. Config file : /etc/redis/6379.conf //配置文件目錄
  28. Log file : /var/log/redis_6379.log //日志目錄
  29. Data dir : /var/lib/redis/6379 //數據庫目錄
  30. Executable : /usr/local/bin/redis-server //啟動程序的目錄
  31. Cli Executable : /usr/local/bin/redis-cli //命令行的連接工具
  32. Is this ok? Then press ENTER to go on or Ctrl-C to abort. //回車完成配置
  33. Copied /tmp/6379.conf => /etc/init.d/redis_6379 //服務啟動腳本
  34. Installing service...
  35. Successfully added to chkconfig!
  36. Successfully added to runlevels 345!
  37. Starting Redis server...
  38. 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. [root@redis1 utils]# /etc/init.d/redis_6379 status        
  6. //再次查看,顯示 沒有那個文件或目錄
  7. cat: /var/run/redis_6379.pid: No such file or directory
  8. Redis is running ()

5)連接redis

  1. [root@redis1 utils]# /etc/init.d/redis_6379 start
  2. Starting Redis server...
  3. [root@redis1 utils]# redis-cli
  4. 127.0.0.1:6379> ping
  5. PONG            //PONG說明服務正常

6)設置變量test,值為123,查看變量test的值

常用指令操作:

set keyname keyvalue 存儲

get keyname 獲取

  1. 127.0.0.1:6379> set test 123
  2. OK
  3. 127.0.0.1:6379> get test
  4. "123"

del keyname 刪除變量

  1. 127.0.0.1:6379> set k1 v1
  2. OK
  3. 127.0.0.1:6379> get k1
  4. "v1"
  5. 127.0.0.1:6379> del k1
  6. (integer) 1

keys * 打印所有變量

  1. 127.0.0.1:6379> keys *
  2. 1) "test"

EXISTS keyname 測試是否存在

  1. 127.0.0.1:6379> exists k1
  2. (integer) 0

type keyname 查看類型

  1. 127.0.0.1:6379> set k2 v1
  2. OK
  3. 127.0.0.1:6379> type k2
  4. string

move keyname dbname 移動變量

  1. 127.0.0.1:6379> move k2 1            //移動k2到1庫
  2. (integer) 1

select 數據庫編號0-15 切換庫

  1. 127.0.0.1:6379> select 1        //切換到1庫
  2. OK
  3. 127.0.0.1:6379[1]> keys *            //查看有k2
  4. 1) "k2"

expire keyname 10 設置有效時間

  1. 127.0.0.1:6379[1]> EXPIRE k2 10
  2. (integer) 1

ttl keyname 查看生存時間

  1. 127.0.0.1:6379[1]> ttl k2

flushall 刪除所有變量

  1. 127.0.0.1:6379[1]> FLUSHALL
  2. OK

save 保存所有變量

  1. 127.0.0.1:6379[1]> save
  2. OK

shutdown 關閉redis服務

  1. 127.0.0.1:6379[1]> SHUTDOWN

2 案例3:修改Redis服務運行參數

2.1 問題

  • 具體要求如下:
  • 端口號 6351
  • IP地址 192.168.4.51
  • 連接密碼 123456
  • 客戶端連接Redis服務

2.2 步驟

實現此案例需要按照如下步驟進行。

步驟一:修改redis運行參數

1)

  1. [root@redis1 utils]# cp /etc/redis/6379.conf /root/6379.conf     
  2. //可以先備份一份,防止修改錯誤沒法還原
  3. [root@redis1 utils]# /etc/init.d/redis_6379 stop
  4. [root@redis1 utils]# vim /etc/redis/6379.conf
  5. ...
  6. bind 192.168.4.51                //設置服務使用的ip
  7. port 6351                            //更改端口號
  8. requirepass 123456                //設置密碼
  9. [root@redis1 utils]# /etc/init.d/redis_6379 start
  10. Starting Redis server...
  11. [root@redis1 utils]# ss -antul | grep 6351        //查看有端口6351
  12. tcp LISTEN 0 128 192.168.4.51:6351 *:*

由於修改了配置文件所以在連接的時候需要加上ip和端口

  1. [root@redis1 utils]# redis-cli -h 192.168.4.51 -p 6351
  2. 192.168.4.51:6351> ping
  3. (error) NOAUTH Authentication required.
  4. 192.168.4.51:6351> auth 123456            //輸入密碼才能操作(因為之前設置過密碼)
  5. OK
  6. 192.168.4.51:6351> ping
  7. PONG

還可以直接在命令行輸入密碼連接

  1. [root@redis1 utils]# redis-cli -h 192.168.4.51 -p 6351 -a 123456
  2. 192.168.4.51:6351> ping
  3. PONG

2)停止服務

由於修改Redis服務運行參數,所以在停止服務的時候也不能用默認的方法停止

  1. [root@redis1 utils]# /etc/init.d/redis_6379 stop        //停止失敗
  2. Stopping ...
  3. Could not connect to Redis at 127.0.0.1:6379: Connection refused
  4. Waiting for Redis to shutdown ...
  5. Waiting for Redis to shutdown ...
  6. Waiting for Redis to shutdown ...
  7. Waiting for Redis to shutdown ...
  8. ...
  9. [root@redis1 utils]# redis-cli -h 192.168.4.51 -p 6351 -a 123456 shutdown    
  10. //停止成功
  11. [root@redis1 utils]# ss -antul | grep 6351        //查看沒有端口

3 案例2:部署LNMP+Redis

3.1 問題

  • 具體要求如下:
  • 在主機 192.168.4.52 上部署LNMP 環境
  • 把數據存儲到本機的redis服務中

3.2 步驟

實現此案例需要按照如下步驟進行。

步驟一:部署LNMP+Redis

1)安裝redis,(不會搭建的請參考案例1)

2)安裝php支持的功能模塊(52上面操作)

  1. [root@nginx utils]# which php
  2. /usr/bin/which: no php in (/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin)
  3. [root@nginx utils]# php -m
  4. bash: php: command not found...
  5. [root@nginx utils]# yum -y install php-cli
  6. [root@nginx utils]# which php
  7. /usr/bin/php
  8. [root@nginx utils]# php -m
  9. [PHP Modules]
  10. bz2
  11. calendar
  12. Core
  13. ctype
  14. curl
  15. date
  16. ereg
  17. exif
  18. fileinfo
  19. filter
  20. ftp
  21. gettext
  22. gmp
  23. hash
  24. iconv
  25. json
  26. libxml
  27. mhash
  28. openssl
  29. pcntl
  30. pcre
  31. Phar
  32. readline
  33. Reflection
  34. session
  35. shmop
  36. SimpleXML
  37. sockets
  38. SPL
  39. standard
  40. tokenizer
  41. xml
  42. zip
  43. zlib
  44. [Zend Modules]

3)安裝連接redis的功能模塊

  1. [root@nginx utils]# php -m | grep -i redis        //沒有redis模塊
  2. [root@nginx redis]# cd lnmp/
  3. [root@nginx lnmp]# ls
  4. nginx-1.12.2.tar.gz
  5. php-devel-5.4.16-42.el7.x86_64.rpm
  6. php-fpm-5.4.16-42.el7.x86_64.rpm
  7. php-redis-2.2.4.tar.gz
  8. [root@nginx lnmp]# tar -zxf php-redis-2.2.4.tar.gz
  9. [root@nginx lnmp]# cd phpredis-2.2.4/
  10. [root@nginx phpredis-2.2.4]# which phpize
  11. /usr/bin/phpize
  12. [root@nginx phpredis-2.2.4]# phpize
  13. Can't find PHP headers in /usr/include/php
  14. The php-devel package is required for use of this command.
  15. [root@nginx phpredis-2.2.4]# yum -y install autoconf automake    pcre-devel
  16. [root@nginx phpredis-2.2.4]# cd ..
  17. [root@nginx lnmp]# rpm -ivh php-devel-5.4.16-42.el7.x86_64.rpm
  18. [root@nginx lnmp]# cd phpredis-2.2.4/
  19. [root@nginx phpredis-2.2.4]# phpize     //生成一個php的文件
  20. Configuring for:
  21. PHP Api Version: 20100412
  22. Zend Module Api No: 20100525
  23. Zend Extension Api No: 220100525
  24. [root@nginx phpredis-2.2.4]# find / -name "php-config"
  25. /usr/bin/php-config
  26. [root@nginx phpredis-2.2.4]# ./configure --with-php-config=/usr/bin/php-config
  27. //指定模塊編譯的路徑
  28. [root@nginx phpredis-2.2.4]# make && make install
  29. ...
  30. Installing shared extensions: /usr/lib64/php/modules/ //模塊文件存放的路徑
  31. [root@nginx phpredis-2.2.4]# ls /usr/lib64/php/modules/
  32. curl.so fileinfo.so json.so phar.so redis.so zip.so
  33. [root@nginx phpredis-2.2.4]# vim /etc/php.ini
  34. 728 extension_dir = "/usr/lib64/php/modules/"
  35. 729 ; On windows:
  36. 730 extension = "redis.so"
  37. [root@nginx phpredis-2.2.4]# php -m | grep -i redis
  38. redis        //出現redis

4)安裝nginx(52上面操作)

  1. [root@nginx ~]# cd redis/lnmp/
  2. [root@nginx lnmp]# ls
  3. nginx-1.12.2.tar.gz
  4. [root@nginx lnmp]# tar -xf nginx-1.12.2.tar.gz
  5. [root@nginx lnmp]# cd nginx-1.12.2/
  6. [root@nginx nginx-1.12.2]# yum -y install gcc pcre-devel openssl-devel
  7. [root@nginx nginx-1.12.2]# useradd -s /sbin/nologin nginx
  8. [root@nginx nginx-1.12.2]# ./configure --user=nginx --group=nginx --with-http_ssl_module
  9. [root@nginx nginx-1.12.2]# make && make install
  10. [root@nginx nginx-1.12.2]# ln -s /usr/local/nginx/sbin/nginx /sbin/
  11. [root@nginx nginx-1.12.2]# cd /usr/local/nginx/html/
  12. [root@nginx html]# echo "aa" > text.html
  13. [root@nginx html]# yum -y install mariadb mariadb-server mariadb-devel php php-mysql
  14. [root@nginx html]# cd /root/redis/lnmp/
  15. [root@nginx lnmp]# rpm -ivh php-fpm-5.4.16-42.el7.x86_64.rpm        //安裝php
  16. [root@nginx lnmp]# cd /usr/local/nginx/html/
  17. [root@nginx html]# vim test.php
  18. <?php
  19. $i=33;
  20. $j=44;
  21. if($i<$j){
  22. echo "oK";
  23. }
  24. else{
  25. echo "error";
  26. }
  27. #echo $i;
  28. ?>
  29. [root@nginx html]# php test.php         //在命令行測試
  30. oK
  31. [root@nginx html]# systemctl restart mariadb
  32. [root@nginx html]# systemctl restart php-fpm
  33. [root@nginx html]# vim /usr/local/nginx/conf/nginx.conf
  34. ...
  35. location ~ \.php$ {
  36. root html;
  37. fastcgi_pass 127.0.0.1:9000;
  38. fastcgi_index index.php;
  39. #fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;
  40. include fastcgi.conf;
  41. }
  42. ...
  43. [root@nginx html]# nginx    //啟動nginx
  44. 客戶端用火狐瀏覽器訪問:
  45. [root@room9pc01 ~]# firefox 192.168.4.56/text.html        //成功
  46. [root@room9pc01 ~]# firefox 192.168.4.56/test.php        //成功

5)連接redis測試

  1. [root@nginx html]# vim lkredis.php
  2. <?php
  3. $redis = new redis();
  4. $redis->connect('192.168.4.51',6351);
  5. $redis ->auth("123456");
  6. $redis->set("redistest","666666");
  7. echo $redis->get("redistest");
  8. ?>
  9. [root@nginx html]# php lkredis.php         //命令行測試
  10. 666666

火狐瀏覽器測試,如圖-1所示:

圖-1

在51上面查看,有數據存入

  1. [root@redis1 lnmp]# redis-cli -h 192.168.4.51 -p 6351 -a 123456
  2. 192.168.4.51:6351> ping
  3. PONG
  4. 192.168.4.51:6351> keys *
  5. 1) "redistest"
  6. 192.168.4.51:6351> get redistest
  7. "666666"
  8. 192.168.4.51:6351>


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM