Redis
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Redis相关的知识,希望对你有一定的参考价值。
Redis
redis 是一个高性能的 key-value 数据库。 redis 的出现,很大程度补偿了memcached 这类 keyvalue 存储的不足,在部分场合可以对关系数据库起到很好的补充作用。它提供了 Python,Ruby,Erlang,php 客户端,使用方便。Redis 的所有数据都是保存在内存中,然后不定期的通过异步方式保存到磁盘上(这称为“半持久化模式”);也可以把每一次数据变化都写入到一个 append only file(aof)里面(这称为“全持久化模式”)。
Redis 由四个可执行文件:redis-benchmark、redis-cli、redis-server、redis-stat 这四个文件,加上一个redis.conf就构成了整个redis的最终可用包。它们的作用如下:
redis-server:Redis服务器的daemon启动程序
redis-cli:Redis命令行操作工具。当然,你也可以用telnet根据其纯文本协议来操作
redis-benchmark:Redis性能测试工具,测试Redis在你的系统及你的配置下的读写性能
redis-stat:Redis状态检测工具,可以检测Redis当前状态参数及延迟状况
1.安装并配置Redis
tar zxf redis-3.0.2.tar.gz
cd redis-3.0.2
yum install -y gcc
make
make install
cd utils/
./install_server.sh ##启动脚本
/etc/init.d/redis_6379 restart ##启动Redis
2.安装Redis的Php扩展
yum install nginx-1.8.0-1.el6.ngx.x86_64.rpm php-* -y
unzip phpredis-master.zip
vim /etc/php.ini
date.timezone = Asia/Shanghai ##将时区改成亚洲/上海
/etc/init.d/php-fpm restart ##重启Php
vim /etc/php-fpm.d/www.conf
; RPM: apache Choosed to be able to access some dir as httpd
user = nginx ##将php-fpm的用户改为nginx
; RPM: Keep a group allowed to write in log dir.
group = nginx ##将php-fpm的用户组改为nginx
cd phpredis-master/
phpize
./configure ##编译
make && make install ##安装
cd /etc/php.d/
cp mysql.ini redis.ini
vim redis.ini
extension=redis.so
/etc/init.d/php-fpm restart ##重启php
3.配置Nginx
# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
location ~ \.php$ {
root html;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME /usr/share/nginx/html$fastcgi_script_name;
include fastcgi_params;
}
/etc/init.d/nginx restart ##重启Nginx
4.配置Mysql并测试
/etc/init.d/mysqld start ##启动数据库
mysql
mysql> grant all on *.* to [email protected] identified by ‘westos‘;
Query OK, 0 rows affected (0.00 sec)
mysql> quit
Bye
cd redis/
mysql < test.sql ##将测试数据库导入Mysql
mysql
mysql> use test;
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A
Database changed
mysql> select * from test;
+----+-------+
| id | name |
+----+-------+
| 1 | test1 |
| 2 | test2 |
| 3 | test3 |
| 4 | test4 |
| 5 | test5 |
| 6 | test6 |
| 7 | test7 |
| 8 | test8 |
| 9 | test9 |
+----+-------+
9 rows in set (0.00 sec)
登陆172.25.35.6测试,如图:
我们实现了redis作为mysql的缓存服务器,但是如果更新了mysql,redis中仍然会有对应的 KEY,数据就不会更新,此时就会出现mysql和redis数据不一致的情况。所以接下来就要通过 mysql 触发器将改变的数据同步到redis中。
我们可以通过Gearman来实现数据同步:
Gearman 是一个支持分布式的任务分发框架:
Gearman Job Server:Gearman 核心程序,需要编译安装并以守护进程形式运行在后台。
Gearman Client:可以理解为任务的请求者。
Gearman Worker:任务的真正执行者,一般需要自己编写具体逻辑并通过守护进程方式
运行,Gearman Worker 接收到 Gearman Client 传递的任务内容后,会按顺序处理。
5.安装并且配置Gearman
cd redis/
yum install gcc libgearman-devel-1.1.8-2.el6.x86_64.rpm
libevent-devel-1.4.13-4.el6.x86_64.rpm
libevent-doc-1.4.13-4.el6.noarch.rpm
libevent-headers-1.4.13-4.el6.noarch.rpm
libgearman-1.1.8-2.el6.x86_64.rpm
tokyocabinet-1.4.33-6.el6.x86_64.rpm
cd gearman-1.1.2/
./configure
make && make install
6.安装lib_mysqludf_json
lib_mysqludf_json UDF 库函数将关系数据映射为 JSON 格式。通常,数据库中的数据映射为 JSON 格式,是通过程序来转换的。
unzip lib_mysqludf_json-master.zip
cd lib_mysqludf_json-master/
gcc $(mysql_config --cflags) -shared -fPIC -o lib_mysqludf_json.so lib_mysqludf_json.c
mysql
mysql> show global variables like ‘plugin_dir‘;
+---------------+-------------------------+
| Variable_name | Value |
+---------------+-------------------------+
| plugin_dir | /usr/lib64/mysql/plugin |
+---------------+-------------------------+
1 row in set (0.00 sec)
cp lib_mysqludf_json.so /usr/lib64/mysql/plugin/ ##拷贝lib_mysqludf_json.so模块
mysql> create function json_object returns string soname
-> ‘lib_mysqludf_json.so‘; ##注册UDF函数
7.安装gearman-mysql-udf来管理调用gearman
tar zxf gearman-mysql-udf-0.6.tar.gz
cd gearman-mysql-udf-0.6/
yum install -y gcc-c++
./configure --libdir=/usr/lib64/mysql/plugin/
make && make install
mysql
mysql> create function gman_do_background returns string soname ‘lib_mysqludf_json.so‘;
##注册UDF函数
mysql> create function gman_servers_set returns string soname ‘lib_mysqludf_json.so‘;
mysql> select * from mysql.func;
+--------------------+-----+-------------------------+----------+
| name | ret | dl | type |
+--------------------+-----+-------------------------+----------+
| json_object | 0 | lib_mysqludf_json.so | function |
| gman_do_background | 0 | libgearman_mysql_udf.so | function |
| gman_servers_set | 0 | libgearman_mysql_udf.so | function |
+--------------------+-----+-------------------------+----------+
3 rows in set (0.00 sec) ##查看注册的函数
mysql> select gman_servers_set(‘127.0.0.1:4730‘); ##指定Gearman的服务信息
+------------------------------------+
| gman_servers_set(‘127.0.0.1:4730‘) |
+------------------------------------+
| 127.0.0.1:4730 |
+------------------------------------+
1 row in set (0.00 sec)
mysql> quit
Bye
vim /root/redis/test.sql
use test;
#CREATE TABLE `test` (`id` int(7) NOT NULL AUTO_INCREMENT, `name` char(8)
DEFAULT NULL, PRIMARY KEY (`id`)) ENGINE=InnoDB DEFAULT CHARSET=utf8;
#INSERT INTO `test` VALUES (1,‘test1‘),(2,‘test2‘),(3,‘test3‘),(4,‘test4‘),(5,‘test5‘),(6,‘test6‘),(7,‘test7‘),(8,‘test8‘),(9,‘test9‘);
DELIMITER $$ ##编写Mysql的触发器
CREATE TRIGGER datatoredis AFTER UPDATE ON test FOR EACH ROW BEGIN
SET @RECV=gman_do_background(‘syncToRedis‘, json_object(NEW.id as `id`, NEW.name as `name`));
END$$
DELIMITER ;
mysql < test.sql ##将检测数据库导入mysql
mysql
mysql> SHOW TRIGGERS FROM test; ##查看触发器
+-------------+--------+-------+----------------------------------------------------------------------------------------------------------------+--------+---------+----------+----------------+----------------------+----------------------+--------------------+
| Trigger | Event | Table | Statement | Timing | Created | sql_mode | Definer | character_set_client | collation_connection | Database Collation |
+-------------+--------+-------+----------------------------------------------------------------------------------------------------------------+--------+---------+----------+----------------+----------------------+----------------------+--------------------+
| datatoredis | UPDATE | test | BEGIN
SET @RECV=gman_do_background(‘syncToRedis‘, json_object(NEW.id as `id`, NEW.name as `name`));
END | AFTER | NULL | | [email protected] | latin1 | latin1_swedish_ci | latin1_swedish_ci |
+-------------+--------+-------+----------------------------------------------------------------------------------------------------------------+--------+---------+----------+----------------+----------------------+----------------------+--------------------+
1 row in set (0.00 sec)
8.配置Gearman的worker端
cp worker.php /usr/local/bin/
cd /usr/local/bin/
nohup php worker.php &
[1] 9068 ##将worker打入后台运行
vim worker.php
<?php
$worker = new GearmanWorker();
$worker->addServer();
$worker->addFunction(‘syncToRedis‘, ‘syncToRedis‘);
$redis = new Redis();
$redis->connect(‘127.0.0.1‘, 6379);
while($worker->work());
function syncToRedis($job)
{
global $redis;
$workString = $job->workload();
$work = json_decode($workString);
if(!isset($work->id)){
return false;
}
$redis->set($work->id, $work->name);
}
?>
9.更新Mysql中的数据并检测
mysql
mysql> use test;
mysql> update test set name=‘hello‘ where id=1;
Query OK, 1 row affected (0.12 sec)
mysql> update test set name=‘world‘ where id=2;
Query OK, 1 row affected (0.06 sec)
mysql> select * from test;
+----+-------+
| id | name |
+----+-------+
| 1 | hello |
| 2 | world |
| 3 | test3 |
| 4 | test4 |
| 5 | test5 |
| 6 | test6 |
| 7 | test7 |
| 8 | test8 |
| 9 | test9 |
+----+-------+
9 rows in set (0.00 sec)
mysql> quit
Bye
如图:
10.主从复制
master:172.25.35.6
slave:172.25.35.7
172.25.35.8
在slave端:
vim /etc/redis/6379.conf
slaveof 172.25.35.6 6379
测试:
[[email protected] bin]# redis-cli
127.0.0.1:6379> set 1 westos
OK
127.0.0.1:6379> get 1
"westos"
[[email protected] redis-3.0.2]# redis-cli
127.0.0.1:6379> get 1
"westos"
[[email protected] ~]# redis-cli
127.0.0.1:6379> get 1
"westos"
本文出自 “jeffrey13” 博客,谢绝转载!
以上是关于Redis的主要内容,如果未能解决你的问题,请参考以下文章