日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 运维知识 > 数据库 >内容正文

数据库

搭建Redis服务器

發布時間:2023/12/18 数据库 23 豆豆
生活随笔 收集整理的這篇文章主要介紹了 搭建Redis服务器 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

1 案例1:搭建Redis服務器

1.1 問題

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

1.2 步驟

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

步驟一:搭建redis服務器

1)安裝redis服務器

  • [root@redis1 ~]# cd redis
  • [root@redis1 redis]# ls
  • lnmp redis-4.0.8.tar.gz
  • [root@redis1 redis]# yum -y install gcc gcc-c++ make
  • [root@redis1 redis]# tar -zxf redis-4.0.8.tar.gz
  • [root@redis1 redis]# cd redis-4.0.8/
  • [root@redis1 redis-4.0.8]# ls
  • 00-RELEASENOTES CONTRIBUTING deps Makefile README.md runtest runtest-sentinel src utils
  • BUGS COPYING INSTALL MANIFESTO redis.conf runtest-cluster sentinel.conf tests
  • [root@redis1 redis-4.0.8]# make
  • [root@redis1 redis-4.0.8]# make install
  • [root@redis1 redis-4.0.8]# cd utils/
  • [root@redis1 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!????????//安裝成功
  • 2)查看狀態

  • [root@redis1 utils]# /etc/init.d/redis_6379 status
  • Redis is running (15203)
  • 3)查看監聽的端口

  • [root@redis1 utils]# netstat -antupl |grep :6379
  • tcp 0 0 127.0.0.1:6379 0.0.0.0:* LISTEN 15203/redis-server
  • [root@redis1 utils]# ps -C redis-server
  • PID TTY TIME CMD
  • 15203 ? 00:00:00 redis-server
  • 4)停止服務

  • [root@redis1 utils]# /etc/init.d/redis_6379 stop
  • Stopping ...
  • Waiting for Redis to shutdown ...
  • Redis stopped
  • [root@redis1 utils]# /etc/init.d/redis_6379 status????????
  • //再次查看,顯示 沒有那個文件或目錄
  • cat: /var/run/redis_6379.pid: No such file or directory
  • Redis is running ()
  • 5)連接redis

  • [root@redis1 utils]# /etc/init.d/redis_6379 start
  • Starting Redis server...
  • [root@redis1 utils]# redis-cli
  • 127.0.0.1:6379> ping
  • PONG????????????//PONG說明服務正常
  • 6)?設置變量test,值為123,查看變量test的值

    常用指令操作:

    set keyname keyvalue 存儲

    get keyname 獲取

  • 127.0.0.1:6379> set test 123
  • OK
  • 127.0.0.1:6379> get test
  • "123"
  • del keyname 刪除變量

  • 127.0.0.1:6379> set k1 v1
  • OK
  • 127.0.0.1:6379> get k1
  • "v1"
  • 127.0.0.1:6379> del k1
  • (integer) 1
  • keys * 打印所有變量

  • 127.0.0.1:6379> keys *
  • 1) "test"
  • EXISTS keyname 測試是否存在

  • 127.0.0.1:6379> exists k1
  • (integer) 0
  • type keyname 查看類型

  • 127.0.0.1:6379> set k2 v1
  • OK
  • 127.0.0.1:6379> type k2
  • string
  • move keyname dbname 移動變量

  • 127.0.0.1:6379> move k2 1????????????//移動k2到1庫
  • (integer) 1
  • select 數據庫編號0-15 切換庫

  • 127.0.0.1:6379> select 1????????//切換到1庫
  • OK
  • 127.0.0.1:6379[1]> keys *????????????//查看有k2
  • 1) "k2"
  • expire keyname 10 設置有效時間

  • 127.0.0.1:6379[1]> EXPIRE k2 10
  • (integer) 1
  • ttl keyname 查看生存時間

  • 127.0.0.1:6379[1]> ttl k2
  • flushall 刪除所有變量

  • 127.0.0.1:6379[1]> FLUSHALL
  • OK
  • save 保存所有變量

  • 127.0.0.1:6379[1]> save
  • OK
  • shutdown 關閉redis服務

  • 127.0.0.1:6379[1]> SHUTDOWN
  • 2 案例3:修改Redis服務運行參數

    2.1 問題

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

    2.2 步驟

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

    步驟一:修改redis運行參數

    1)

  • [root@redis1 utils]# cp /etc/redis/6379.conf /root/6379.conf ????
  • //可以先備份一份,防止修改錯誤沒法還原
  • [root@redis1 utils]# /etc/init.d/redis_6379 stop
  • [root@redis1 utils]# vim /etc/redis/6379.conf
  • ...
  • bind 192.168.4.51????????????????//設置服務使用的ip
  • port 6351????????????????????????????//更改端口號
  • requirepass 123456????????????????//設置密碼
  • [root@redis1 utils]# /etc/init.d/redis_6379 start
  • Starting Redis server...
  • [root@redis1 utils]# ss -antul | grep 6351????????//查看有端口6351
  • tcp LISTEN 0 128 192.168.4.51:6351 *:*
  • 由于修改了配置文件所以在連接的時候需要加上ip和端口

  • [root@redis1 utils]# redis-cli -h 192.168.4.51 -p 6351
  • 192.168.4.51:6351> ping
  • (error) NOAUTH Authentication required.
  • 192.168.4.51:6351> auth 123456????????????//輸入密碼才能操作(因為之前設置過密碼)
  • OK
  • 192.168.4.51:6351> ping
  • PONG
  • 還可以直接在命令行輸入密碼連接

  • [root@redis1 utils]# redis-cli -h 192.168.4.51 -p 6351 -a 123456
  • 192.168.4.51:6351> ping
  • PONG
  • 2)停止服務

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

  • [root@redis1 utils]# /etc/init.d/redis_6379 stop????????//停止失敗
  • Stopping ...
  • Could not connect to Redis at 127.0.0.1:6379: Connection refused
  • Waiting for Redis to shutdown ...
  • Waiting for Redis to shutdown ...
  • Waiting for Redis to shutdown ...
  • Waiting for Redis to shutdown ...
  • ...
  • [root@redis1 utils]# redis-cli -h 192.168.4.51 -p 6351 -a 123456 shutdown????
  • //停止成功
  • [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上面操作)

  • [root@nginx utils]# which php
  • /usr/bin/which: no php in (/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin)
  • [root@nginx utils]# php -m
  • bash: php: command not found...
  • [root@nginx utils]# yum -y install php-cli
  • [root@nginx utils]# which php
  • /usr/bin/php
  • [root@nginx utils]# php -m
  • [PHP Modules]
  • bz2
  • calendar
  • Core
  • ctype
  • curl
  • date
  • ereg
  • exif
  • fileinfo
  • filter
  • ftp
  • gettext
  • gmp
  • hash
  • iconv
  • json
  • libxml
  • mhash
  • openssl
  • pcntl
  • pcre
  • Phar
  • readline
  • Reflection
  • session
  • shmop
  • SimpleXML
  • sockets
  • SPL
  • standard
  • tokenizer
  • xml
  • zip
  • zlib
  • [Zend Modules]
  • 3)安裝連接redis的功能模塊

  • [root@nginx utils]# php -m | grep -i redis????????//沒有redis模塊
  • [root@nginx redis]# cd lnmp/
  • [root@nginx lnmp]# ls
  • nginx-1.12.2.tar.gz
  • php-devel-5.4.16-42.el7.x86_64.rpm
  • php-fpm-5.4.16-42.el7.x86_64.rpm
  • php-redis-2.2.4.tar.gz
  • [root@nginx lnmp]# tar -zxf php-redis-2.2.4.tar.gz
  • [root@nginx lnmp]# cd phpredis-2.2.4/
  • [root@nginx phpredis-2.2.4]# which phpize
  • /usr/bin/phpize
  • [root@nginx phpredis-2.2.4]# phpize
  • Can't find PHP headers in /usr/include/php
  • The php-devel package is required for use of this command.
  • [root@nginx phpredis-2.2.4]# yum -y install autoconf automake????pcre-devel
  • [root@nginx phpredis-2.2.4]# cd ..
  • [root@nginx lnmp]# rpm -ivh php-devel-5.4.16-42.el7.x86_64.rpm
  • [root@nginx lnmp]# cd phpredis-2.2.4/
  • [root@nginx phpredis-2.2.4]# phpize ????//生成一個php的文件
  • Configuring for:
  • PHP Api Version: 20100412
  • Zend Module Api No: 20100525
  • Zend Extension Api No: 220100525
  • [root@nginx phpredis-2.2.4]# find / -name "php-config"
  • /usr/bin/php-config
  • [root@nginx phpredis-2.2.4]# ./configure --with-php-config=/usr/bin/php-config
  • //指定模塊編譯的路徑
  • [root@nginx phpredis-2.2.4]# make && make install
  • ...
  • Installing shared extensions: /usr/lib64/php/modules/ //模塊文件存放的路徑
  • [root@nginx phpredis-2.2.4]# ls /usr/lib64/php/modules/
  • curl.so fileinfo.so json.so phar.so redis.so zip.so
  • [root@nginx phpredis-2.2.4]# vim /etc/php.ini
  • 728 extension_dir = "/usr/lib64/php/modules/"
  • 729 ; On windows:
  • 730 extension = "redis.so"
  • [root@nginx phpredis-2.2.4]# php -m | grep -i redis
  • redis????????//出現redis
  • 4)安裝nginx(52上面操作)

  • [root@nginx ~]# cd redis/lnmp/
  • [root@nginx lnmp]# ls
  • nginx-1.12.2.tar.gz
  • [root@nginx lnmp]# tar -xf nginx-1.12.2.tar.gz
  • [root@nginx lnmp]# cd nginx-1.12.2/
  • [root@nginx nginx-1.12.2]# yum -y install gcc pcre-devel openssl-devel
  • [root@nginx nginx-1.12.2]# useradd -s /sbin/nologin nginx
  • [root@nginx nginx-1.12.2]# ./configure --user=nginx --group=nginx --with-http_ssl_module
  • [root@nginx nginx-1.12.2]# make && make install
  • [root@nginx nginx-1.12.2]# ln -s /usr/local/nginx/sbin/nginx /sbin/
  • [root@nginx nginx-1.12.2]# cd /usr/local/nginx/html/
  • [root@nginx html]# echo "aa" > text.html
  • [root@nginx html]# yum -y install mariadb mariadb-server mariadb-devel php php-mysql
  • [root@nginx html]# cd /root/redis/lnmp/
  • [root@nginx lnmp]# rpm -ivh php-fpm-5.4.16-42.el7.x86_64.rpm????????//安裝php
  • [root@nginx lnmp]# cd /usr/local/nginx/html/
  • [root@nginx html]# vim test.php
  • <?php
  • $i=33;
  • $j=44;
  • if($i<$j){
  • echo "oK";
  • }
  • else{
  • echo "error";
  • }
  • #echo $i;
  • ?>
  • [root@nginx html]# php test.php ????????//在命令行測試
  • oK
  • [root@nginx html]# systemctl restart mariadb
  • [root@nginx html]# systemctl restart php-fpm
  • [root@nginx html]# 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;
  • }
  • ...
  • [root@nginx html]# nginx????//啟動nginx
  • 客戶端用火狐瀏覽器訪問:
  • [root@room9pc01 ~]# firefox 192.168.4.56/text.html????????//成功
  • [root@room9pc01 ~]# firefox 192.168.4.56/test.php????????//成功
  • 5)連接redis測試

  • [root@nginx html]# vim lkredis.php
  • <?php
  • $redis = new redis();
  • $redis->connect('192.168.4.51',6351);
  • $redis ->auth("123456");
  • $redis->set("redistest","666666");
  • echo $redis->get("redistest");
  • ?>
  • [root@nginx html]# php lkredis.php ????????//命令行測試
  • 666666
  • 火狐瀏覽器測試,如圖-1所示:

    圖-1

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

  • [root@redis1 lnmp]# redis-cli -h 192.168.4.51 -p 6351 -a 123456
  • 192.168.4.51:6351> ping
  • PONG
  • 192.168.4.51:6351> keys *
  • 1) "redistest"
  • 192.168.4.51:6351> get redistest
  • "666666"
  • 192.168.4.51:6351>
  • 轉載于:https://www.cnblogs.com/tiki/p/10714163.html

    總結

    以上是生活随笔為你收集整理的搭建Redis服务器的全部內容,希望文章能夠幫你解決所遇到的問題。

    如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。