搭建Redis服务器
Posted IT资料群1085043784
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了搭建Redis服务器相关的知识,希望对你有一定的参考价值。
1 案例1:搭建Redis服务器
1.1 问题
- 具体要求如下:
- 在主机 192.168.4.51 上安装并启用 redis 服务
- 设置变量test,值为123
- 查看变量test的值
1.2 步骤
实现此案例需要按照如下步骤进行。
步骤一:搭建redis服务器
1)安装redis服务器
- [[email protected] ~]# cd redis
- [[email protected] redis]# ls
- lnmp redis-4.0.8.tar.gz
- [[email protected] redis]# yum -y install gcc gcc-c++ make
- [[email protected] redis]# tar -zxf redis-4.0.8.tar.gz
- [[email protected] redis]# cd redis-4.0.8/
- [[email protected] 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
- [[email protected] redis-4.0.8]# make
- [[email protected] redis-4.0.8]# make install
- [[email protected] redis-4.0.8]# cd utils/
- [[email protected] 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)查看状态
- [[email protected] utils]# /etc/init.d/redis_6379 status
- Redis is running (15203)
3)查看监听的端口
- [[email protected] utils]# netstat -antupl |grep :6379
- tcp 0 0 127.0.0.1:6379 0.0.0.0:* LISTEN 15203/redis-server
- [[email protected] utils]# ps -C redis-server
- PID TTY TIME CMD
- 15203 ? 00:00:00 redis-server
4)停止服务
- [[email protected] utils]# /etc/init.d/redis_6379 stop
- Stopping ...
- Waiting for Redis to shutdown ...
- Redis stopped
- [[email protected] utils]# /etc/init.d/redis_6379 status
- //再次查看,显示 没有那个文件或目录
- cat: /var/run/redis_6379.pid: No such file or directory
- Redis is running ()
5)连接redis
- [[email protected] utils]# /etc/init.d/redis_6379 start
- Starting Redis server...
- [[email protected] 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)
- [[email protected] utils]# cp /etc/redis/6379.conf /root/6379.conf
- //可以先备份一份,防止修改错误没法还原
- [[email protected] utils]# /etc/init.d/redis_6379 stop
- [[email protected] utils]# vim /etc/redis/6379.conf
- ...
- bind 192.168.4.51 //设置服务使用的ip
- port 6351 //更改端口号
- requirepass 123456 //设置密码
- [[email protected] utils]# /etc/init.d/redis_6379 start
- Starting Redis server...
- [[email protected] utils]# ss -antul | grep 6351 //查看有端口6351
- tcp LISTEN 0 128 192.168.4.51:6351 *:*
由于修改了配置文件所以在连接的时候需要加上ip和端口
- [[email protected] 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
还可以直接在命令行输入密码连接
- [[email protected] utils]# redis-cli -h 192.168.4.51 -p 6351 -a 123456
- 192.168.4.51:6351> ping
- PONG
2)停止服务
由于修改Redis服务运行参数,所以在停止服务的时候也不能用默认的方法停止
- [[email protected] 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 ...
- ...
- [[email protected] utils]# redis-cli -h 192.168.4.51 -p 6351 -a 123456 shutdown
- //停止成功
- [[email protected] 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上面操作)
- [[email protected] utils]# which php
- /usr/bin/which: no php in (/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin)
- [[email protected] utils]# php -m
- bash: php: command not found...
- [[email protected] utils]# yum -y install php-cli
- [[email protected] utils]# which php
- /usr/bin/php
- [[email protected] 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的功能模块
- [[email protected] utils]# php -m | grep -i redis //没有redis模块
- [[email protected] redis]# cd lnmp/
- [[email protected] 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
- [[email protected] lnmp]# tar -zxf php-redis-2.2.4.tar.gz
- [[email protected] lnmp]# cd phpredis-2.2.4/
- [[email protected] phpredis-2.2.4]# which phpize
- /usr/bin/phpize
- [[email protected] 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.
- [[email protected] phpredis-2.2.4]# yum -y install autoconf automake pcre-devel
- [[email protected] phpredis-2.2.4]# cd ..
- [[email protected] lnmp]# rpm -ivh php-devel-5.4.16-42.el7.x86_64.rpm
- [[email protected] lnmp]# cd phpredis-2.2.4/
- [[email protected] phpredis-2.2.4]# phpize //生成一个php的文件
- Configuring for:
- PHP Api Version: 20100412
- Zend Module Api No: 20100525
- Zend Extension Api No: 220100525
- [[email protected] phpredis-2.2.4]# find / -name "php-config"
- /usr/bin/php-config
- [[email protected] phpredis-2.2.4]# ./configure --with-php-config=/usr/bin/php-config
- //指定模块编译的路径
- [[email protected] phpredis-2.2.4]# make && make install
- ...
- Installing shared extensions: /usr/lib64/php/modules/ //模块文件存放的路径
- [[email protected] phpredis-2.2.4]# ls /usr/lib64/php/modules/
- curl.so fileinfo.so json.so phar.so redis.so zip.so
- [[email protected] phpredis-2.2.4]# vim /etc/php.ini
- 728 extension_dir = "/usr/lib64/php/modules/"
- 729 ; On windows:
- 730 extension = "redis.so"
- [[email protected] phpredis-2.2.4]# php -m | grep -i redis
- redis //出现redis
4)安装nginx(52上面操作)
- [[email protected] ~]# cd redis/lnmp/
- [[email protected] lnmp]# ls
- nginx-1.12.2.tar.gz
- [[email protected] lnmp]# tar -xf nginx-1.12.2.tar.gz
- [[email protected] lnmp]# cd nginx-1.12.2/
- [[email protected] nginx-1.12.2]# yum -y install gcc pcre-devel openssl-devel
- [[email protected] nginx-1.12.2]# useradd -s /sbin/nologin nginx
- [[email protected] nginx-1.12.2]# ./configure --user=nginx --group=nginx --with-http_ssl_module
- [[email protected] nginx-1.12.2]# make && make install
- [[email protected] nginx-1.12.2]# ln -s /usr/local/nginx/sbin/nginx /sbin/
- [[email protected] nginx-1.12.2]# cd /usr/local/nginx/html/
- [[email protected] html]# echo "aa" > text.html
- [[email protected] html]# yum -y install mariadb mariadb-server mariadb-devel php php-mysql
- [[email protected] html]# cd /root/redis/lnmp/
- [roo[email protected] lnmp]# rpm -ivh php-fpm-5.4.16-42.el7.x86_64.rpm //安装php
- [[email protected] lnmp]# cd /usr/local/nginx/html/
- [[email protected] html]# vim test.php
- <?php
- $i=33;
- $j=44;
- if($i<$j){
- echo "oK";
- }
- else{
- echo "error";
- }
- #echo $i;
- ?>
- [[email protected] html]# php test.php //在命令行测试
- oK
- [[email protected] html]# systemctl restart mariadb
- [[email protected] html]# systemctl restart php-fpm
- [[email protected] 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;
- }
- ...
- [[email protected] html]# nginx //启动nginx
- 客户端用火狐浏览器访问:
- [[email protected] ~]# firefox 192.168.4.56/text.html //成功
- [[email protected] ~]# firefox 192.168.4.56/test.php //成功
5)连接redis测试
- [[email protected] 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");
- ?>
- [[email protected] html]# php lkredis.php //命令行测试
- 666666
火狐浏览器测试,如图-1所示:
图-1
在51上面查看,有数据存入
- [[email protected] 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>
以上是关于搭建Redis服务器的主要内容,如果未能解决你的问题,请参考以下文章