Redis的安装、使用以及php中扩展redis并实现php操作redis的一个例子
1、下载源码包redis-2.8.21.tar.gz,并将其上传到指定目录/urs/src,然后对其进行解压:
[[email protected] src]# tar -xvf redis-2.8.21.tar.gz
进入解压后的目录,并执行下面命令,指定安装目录为/urs/local/redis:
[[email protected] src]# cd redis-2.8.21
[[email protected] redis-2.8.21]# make PREFIX=/usr/local/redis install
安装redis成功后,可以在/usr/local/redis看到一个bin的目录,里面包括了以下文件:
[[email protected] ~]# cd /usr/local/redis/bin/
[[email protected] bin]# ls
redis-benchmark redis-check-aof redis-check-dump redis-cli redis-sentinel redis-server
2、添加redis启动脚本到服务中:
在/etc/rc.d/init.d目录,创建一个文件,名为redis(说明:/etc/rc.d/init.d/目录下的脚本在系统启动的时候某些指定脚本将被执行),然后在文件中添加如下内容:
[[email protected] ~]# vim /etc/init.d/redis
#!/bin/bash
# Init file for redis
# chkconfig: - 80 12
# description: redis daemon
# processname: redis
# config: /etc/redis.conf
# pidfile: /var/run/redis.pid
source /etc/init.d/functions
BIN="/usr/local/redis/bin"
CONFIG="/etc/redis/redis.conf"
PIDFILE="/var/run/redis.pid"
### Read configuration
[ -r "$SYSCONFIG" ] && source "$SYSCONFIG"
RETVAL=0
prog="redis-server"
desc="Redis Server"
start() {
if [ -e $PIDFILE ];then
echo "$desc already running...."
exit 1
fi
echo -n $"Starting $desc: "
daemon $BIN/$prog $CONFIG
RETVAL=$?
echo
[ $RETVAL -eq 0 ] && touch /var/lock/subsys/$prog
return $RETVAL
}
stop() {
echo -n $"Stop $desc: "
killproc $prog
RETVAL=$?
echo
[ $RETVAL -eq 0 ] && rm -f /var/lock/subsys/$prog $PIDFILE
return $RETVAL
}
restart() {
stop
start
}
case "$1" in
start)
start
;;
stop)
stop
;;
restart)
restart
;;
condrestart)
[ -e /var/lock/subsys/$prog ] && restart
RETVAL=$?
;;
status)
status $prog
RETVAL=$?
;;
*)
echo $"Usage: $0 {start|stop|restart|condrestart|status}"
RETVAL=1
esac
exit $RETVAL
将解压后的redis目录下的配置文件redis.conf拷贝到/etc/redis/redis.conf (注意要与redis的启动脚本中的路径一致)
[[email protected] ~]# mkdir /etc/redis
[[email protected] ~]# cp /usr/src/redis-2.8.21/redis.conf /etc/redis/redis.conf
然后将redis添加到注册服务:
[[email protected] ~]# chkconfig --add redis
[[email protected] ~]# chkconfig redis on
[[email protected] ~]# chkconfig --list redis
redis 0:off 1:off 2:on 3:on 4:on 5:on 6:off
重启redis:
[[email protected] ~]# service redis restart
Stop Redis Server: [ OK ]
Starting Redis Server: [ OK ]
查看是否启动成功:
[[email protected] ~]# ps -ef | grep redis
root 2984 1 0 13:40 ? 00:00:00 /usr/local/bin/redis-server 0.0.0.0:6379
root 2989 2444 0 13:40 pts/1 00:00:00 grep redis
修改/etc/redis/redis.conf,设置redis进程为后台守护进程,并指定一个密码:
[[email protected] ~]# vim /etc/redis/6379.conf
daemonize yes //daemonize:是否以后台daemon方式运行
requirepass 20082009 //设置密码为20082009
设置完后,需要重启redis才能使设置生效:
[[email protected] ~]# service redis restart
修改环境变量文件,添加如下内容:
vim /etc/profile
#set redis path
export REDIS_HOME=/usr/local/redis
export PATH=${REDIS_HOME}/bin:${PATH}
通过source /etc/profile 使其立刻生效
3、调用redis-cli的命令进行简单操作(注意是否启动密码验证):
[[email protected] ~]# redis-cli
127.0.0.1:6379> ping
(error) NOAUTH Authentication required.
127.0.0.1:6379> auth 20082009 //需要输入密码
OK
127.0.0.1:6379> ping
PONG
127.0.0.1:6379> set name lebron james
(error) ERR syntax error
127.0.0.1:6379> set name "lebron james" //有空格的字符串需要加“”
OK
127.0.0.1:6379> get name
"lebron james"
127.0.0.1:6379> set name lebronjames
OK
127.0.0.1:6379> get name
"lebronjames"
127.0.0.1:6379>
4、php中扩展redis:
下载php的一个扩展phpredis的源码包phpredis-2.2.4.tar.gz,将其上传到服务器指定位置,对其进行解压,然后进入解压后的目录:
tar -xvf phpredis-2.2.4.tar.gz
cd phpredis-2.2.4
用phpize生成configure配置文件
[[email protected] phpredis-2.2.4]# /usr/local/php/bin/phpize
然后执行如下命令:
[[email protected] phpredis-2.2.4]# ./configure --with-php-config=/usr/local/php/bin/php-config
[[email protected] phpredis-2.2.4]# make && make install
配置php支持phpredis,在php.ini文件添加如下内容:
vim /usr/local/php/lib/php.ini
extension= /usr/local/php/lib/php/extensions/no-debug-non-zts-20121212/redis.so
然后重启分别重启nginx,php-fpm和redis:
service nginx restart
service php-fpm restart
service redis restart
查看phpinfo(),显示如下,表明php扩展redis成功:
一个简单的php操作redis的例子:
<?php $redis = new Redis(); $redis->connect('127.0.0.1', '6379') or die ("Could not to connect to redis"); //或者具体主机的IP地址也行 $redis->set("test", “php handle redis"); var_dump($redis->get('test')); echo "<br>"; $redis->del('test'); //删除赋值 var_dump($redis->get('test')); ?>
输出结果:
从结果中可以看出,redis并没有起到作用,后来分析分析,由于之前在redis.conf文件中配置了密码登录redis的限制,现在只需要把redis.conf里面的 requirepass 20082009 这句注释掉即可:#requirepass 20082009,然后重启redis:service redis restart
再次执行php文件,显示如下结果,表明php操作redis成功:
Redis主从Master/Slave集群配置:
Master: 172.16.2.33 6379
Slave:172.16.2.42 63791
由于前面已经配置好了master的redis了,现在配置slave只需按照前面的步骤进行即可,简单步骤如下:
1)将redis的源码包redis-2.8.21.tar.gz通过scp 的方法远程复制到slave主机上,进行解压,进入解压后的文件,执行命令:make PREFIX=/usr/local/redis install,这样就在slave上安装了redis;
2)接着修改环境变量文件:vim etc/profile,添加export REDIS_HOME=/usr/local/redis export PATH=${REDIS_HOME}/bin:${PATH},并通过source使其生效;
3)然后将master主机上的/etc/init.d/redis 复制到slave对应的目录上,另外把master主机上的/etc/redis/redis.conf文件也复制到slave主机上对应的目录上;
4)然后修改slave主机上的redis.conf,只需要将 # slaveof
5)接着将redis添加到chkconfig列表中:chkconfig --add redis;chkconfig redis on;然后重启redis:service redis restart;
6)查看master主机上的信息:
[[email protected] ~]# redis-cli info replication
# Replication
role:master
connected_slaves:1
slave0:ip=172.16.2.42,port=63791,state=online,offset=924,lag=1
master_repl_offset:924
。。。
查看slave主机上的信息:
[[email protected] ~]# redis-cli -p 63791 info replication
# Replication
role:slave
master_host:172.16.2.33
master_port:6379
master_link_status:up
。。。
7)写同步测试(master主机上的写入数据同步到slave主机上):
Master主机上:
[[email protected] ~]# redis-cli
127.0.0.1:6379> set name james
OK
127.0.0.1:6379> get name
"james"
Slave主机上:
[[email protected] ~]# redis-cli -p 63791
127.0.0.1:63791> get name
"james"
127.0.0.1:63791> set name "lebron james"
(error) READONLY You can't write against a read only slave.
(slave开启了只读模式,所以从将不能写入数据,可以保证数据只从主服务器同步至从服务器)
如果需要让slave也能写入数据,需要修改配置文件redis.conf:将slave-read-only yes 修改为slave-read-only no,然后重启redis(这样配置后可以向slave写入数据,但是不会同步到master,如下所示:)
Slave主机上:
[[email protected] ~]# redis-cli -p 63791
127.0.0.1:63791> set name "lebron james"
OK
127.0.0.1:63791> get name
"lebron james"
Master主机上(slave主机上的数据并没有同步到master上):
[[email protected] ~]# redis-cli
127.0.0.1:6379> get name
"james"
127.0.0.1:6379>
版权声明:本文为博主原创文章,未经博主允许不得转载。

PHP is widely used in e-commerce, content management systems and API development. 1) E-commerce: used for shopping cart function and payment processing. 2) Content management system: used for dynamic content generation and user management. 3) API development: used for RESTful API development and API security. Through performance optimization and best practices, the efficiency and maintainability of PHP applications are improved.

PHP makes it easy to create interactive web content. 1) Dynamically generate content by embedding HTML and display it in real time based on user input or database data. 2) Process form submission and generate dynamic output to ensure that htmlspecialchars is used to prevent XSS. 3) Use MySQL to create a user registration system, and use password_hash and preprocessing statements to enhance security. Mastering these techniques will improve the efficiency of web development.

PHP and Python each have their own advantages, and choose according to project requirements. 1.PHP is suitable for web development, especially for rapid development and maintenance of websites. 2. Python is suitable for data science, machine learning and artificial intelligence, with concise syntax and suitable for beginners.

PHP is still dynamic and still occupies an important position in the field of modern programming. 1) PHP's simplicity and powerful community support make it widely used in web development; 2) Its flexibility and stability make it outstanding in handling web forms, database operations and file processing; 3) PHP is constantly evolving and optimizing, suitable for beginners and experienced developers.

PHP remains important in modern web development, especially in content management and e-commerce platforms. 1) PHP has a rich ecosystem and strong framework support, such as Laravel and Symfony. 2) Performance optimization can be achieved through OPcache and Nginx. 3) PHP8.0 introduces JIT compiler to improve performance. 4) Cloud-native applications are deployed through Docker and Kubernetes to improve flexibility and scalability.

PHP is suitable for web development, especially in rapid development and processing dynamic content, but is not good at data science and enterprise-level applications. Compared with Python, PHP has more advantages in web development, but is not as good as Python in the field of data science; compared with Java, PHP performs worse in enterprise-level applications, but is more flexible in web development; compared with JavaScript, PHP is more concise in back-end development, but is not as good as JavaScript in front-end development.

PHP and Python each have their own advantages and are suitable for different scenarios. 1.PHP is suitable for web development and provides built-in web servers and rich function libraries. 2. Python is suitable for data science and machine learning, with concise syntax and a powerful standard library. When choosing, it should be decided based on project requirements.

PHP is a scripting language widely used on the server side, especially suitable for web development. 1.PHP can embed HTML, process HTTP requests and responses, and supports a variety of databases. 2.PHP is used to generate dynamic web content, process form data, access databases, etc., with strong community support and open source resources. 3. PHP is an interpreted language, and the execution process includes lexical analysis, grammatical analysis, compilation and execution. 4.PHP can be combined with MySQL for advanced applications such as user registration systems. 5. When debugging PHP, you can use functions such as error_reporting() and var_dump(). 6. Optimize PHP code to use caching mechanisms, optimize database queries and use built-in functions. 7


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

ZendStudio 13.5.1 Mac
Powerful PHP integrated development environment

SublimeText3 Linux new version
SublimeText3 Linux latest version

VSCode Windows 64-bit Download
A free and powerful IDE editor launched by Microsoft

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Dreamweaver CS6
Visual web development tools