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

歡迎訪問 生活随笔!

生活随笔

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

数据库

redis配置mysql缓存_Redis做mysql的缓存服务器

發布時間:2024/7/23 数据库 26 豆豆
生活随笔 收集整理的這篇文章主要介紹了 redis配置mysql缓存_Redis做mysql的缓存服务器 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

一redis簡介:redis 是一個高性能的 key-value 數據庫。 redis 的出現,很大程度補償了memcached 這類 keyvalue 存儲的不足,在部分場合可以對關系數據庫起到很好的補充作用。它提供了 Python,Ruby,Erlang,PHP 客戶端,使用很方便。Redis 的所有數據都是保存在內存中,然后不定期的通過異步方式保存到磁盤上(這稱為“半持久化模式”);也可以把每一次數據變化都寫入到一個appendonly file(aof)里面(這稱為“全持久化模式”)。

二主機環境 rhel6.5 selinx and iptales disabled

1. Redis 安裝

實驗環境 ? 172.25.254.2 ? ?vm2.example.com ? 上

首先得到 ? ?zxf redis-3.0.2.tar.gz

tar zxf redis-3.0.2.tar.gz

cd redis-3.0.2

make && make install

1.1. 配置并啟動服務

cd utils/

./install_server.sh ? ? ? ? ? ? ?這里一路回車都使用的默認

點擊(此處)折疊或打開

[root@vm2 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!

1.2簡單測試,如下就證明成功

點擊(此處)折疊或打開

[root@vm2 utils]# redis-cli

127.0.0.1:6379> ping

PONG

2 安裝lnmp架構2.1 安裝以下軟件包

點擊(此處)折疊或打開

[root@vm2 lnmp]# ls

nginx-1.8.0-1.el6.ngx.x86_64.rpm php-fpm-5.3.3-38.el6.x86_64.rpm

php-5.3.3-38.el6.x86_64.rpm php-gd-5.3.3-38.el6.x86_64.rpm

php-cli-5.3.3-38.el6.x86_64.rpm php-mbstring-5.3.3-38.el6.x86_64.rpm

php-common-5.3.3-38.el6.x86_64.rpm php-mysql-5.3.3-38.el6.x86_64.rpm

php-devel-5.3.3-38.el6.x86_64.rpm php-pdo-5.3.3-38.el6.x86_64.rpm

[root@vm2 lnmp]# yum install -y *

2.2安裝 php 的 redis 擴展

首先得到 ? ?phpredis-master.zip

unzip ?phpredis-master.zip

cd ?phpredis-master

phpize

./configure

make ?&& ?make ?install

cd /etc/php.d/

cp mysql.ini ?redis.ini

vim /etc/php.ini ? ? ? ? ? ??????????#添加以下行

extension=redis.so????????? ? ? ?#加載 redis 模塊

點擊(此處)折疊或打開

[root@vm2 php.d]# /etc/init.d/php-fpm start

Starting php-fpm: [ OK ]

2.3簡單配置 nginx

vim /etc/nginx/conf.d/default.conf

location / {

root ? /usr/share/nginx/html;

index ? index.php ?index.html index.htm;

}

location ~ \.php$ {

root ? ? ? ? ? /usr/share/nginx/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;

}

啟動 nginx

點擊(此處)折疊或打開

[root@vm2 php.d]# nginx -t

nginx: the configuration file /etc/nginx/nginx.conf syntax is ok

nginx: configuration file /etc/nginx/nginx.conf test is successful

[root@vm2 php.d]# nginx

[root@vm2 php.d]# netstat -antlpe| grep nginx

tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 0 14520 6352/nginx

vim ? /etc/php-fpm.d/www.conf ? ? 將這個配置文件中user和group改為nginx ?默認是apache

user = nginx

group = nginx

/etc/init.d/php-fpm restart

點擊(此處)折疊或打開

[root@vm2 php.d]# /etc/init.d/php-fpm restart

Stopping php-fpm: [ OK ]

Starting php-fpm: [ OK ]

3 在另一臺虛擬機上安裝mysql

實驗環境 ? 172.25.254.3 ? ?vm3.example.com ? 上

yum ?install ?-y ?mysql-server

點擊(此處)折疊或打開

[root@vm3 mnt]# mysql

Welcome to the MySQL monitor. Commands end with ; or \g.

Your MySQL connection id is 2

Server version: 5.1.71 Source distribution

Copyright (c) 2000, 2013, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its

affiliates. Other names may be trademarks of their respective

owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql> grant all on test.* to redis@'172.25.254.2' identified by 'redhat';

Query OK, 0 rows affected (0.00 sec)

mysql> flush privileges;

Query OK, 0 rows affected (0.00 sec)

編寫如下代碼

點擊(此處)折疊或打開

[root@vm3 mnt]# cat 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');

[root@vm3 mnt]# mysql < test.sql

4創建 php 測試頁面在172.25.254.2上面進入nginx默認發布目錄

cd ? ??/usr/share/nginx/html

vim ?test.php

點擊(此處)折疊或打開

$redis = new Redis();

$redis->connect('127.0.0.1',6379) or die ("could net connect redis server");

# $query = "select * from test limit 9";

$query = "select * from test";

for ($key = 1; $key < 10; $key++)

{

if (!$redis->get($key))

{

$connect = mysql_connect('172.25.254.3','redis','redhat');

mysql_select_db(test);

$result = mysql_query($query);

//如果沒有找到$key,就將該查詢sql的結果緩存到redis

while ($row = mysql_fetch_assoc($result))

{

$redis->set($row['id'],$row['name']);

}

$myserver = 'mysql';

break;

}

else

{

$myserver = "redis";

$data[$key] = $redis->get($key);

}

}

echo $myserver;

echo "

";

for ($key = 1; $key < 10; $key++)

{

echo "number is $key";

echo "

";

echo "name is $data[$key]";

echo "

";

}

?>然后在瀏覽器進行測試,這時候我們已經實現了 redis 作為 mysql 的緩存服務器,但是如果更新了 mysql,redis

中仍然會有對應的 KEY,數據就不會更新,此時就會出現 mysql 和 redis 數據不一致的情

況。所以接下來就要通過 mysql 觸發器將改變的數據同步到 redis 中。

5,配置 gearman 實現數據同步

1. 安裝 gearman 軟件包:

gearmand libgearman-devel libgearman libevent libevent-devel

libevent-doc libevent-headers tokyocabinet

2,啟動服務

點擊(此處)折疊或打開

[root@vm2 html]# /etc/init.d/gearmand start

Starting gearmand: [ OK ]

3.安裝 php 的 gearman 擴展

https://pecl.php.net

yum install -y db*-devel

tar zxf gearman-1.1.2.tgz

cd gearman-1.1.2

./configure --with-php-config=/usr/bin/php-config

make && make install

# vim /etc/php.ini

extension=gearman.so

/etc/init.d/php-fpm ?restart

點擊(此處)折疊或打開

[root@vm2 html]# /etc/init.d/php-fpm restart

Stopping php-fpm: [ OK ]

Starting php-fpm: [ OK ]

4. 安裝 lib_mysqludf_json

lib_mysqludf_json UDF 庫函數將關系數據映射為 JSON 格式。通常,數據庫中的數據映

射為 JSON 格式,是通過程序來轉換的。

https://github.com/mysqludf/lib_mysqludf_json

yum install -y mysql-devel# 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 的模塊目錄:

5. 安裝 gearman-mysql-udf

這個插件是用來管理調用 Gearman 的分布式的隊列。

https://launchpad.net/gearman-mysql-udf

tar zxf gearman-mysql-udf-0.6.tar.gz

cd gearman-mysql-udf-0.6

./configure --with-mysql=/usr/bin/mysql_config

--libdir=/usr/lib64/mysql/plugin/

make# make install

拷貝 lib_mysqludf_json.so 模塊:

cp lib_mysqludf_json.so /usr/lib64/mysql/plugin/

注冊 UDF 函數

點擊(此處)折疊或打開

mysql> CREATE FUNCTION json_object RETURNS STRING SONAME

'lib_mysqludf_json.so';

mysql> CREATE FUNCTION gman_do_background RETURNS STRING SONAME

'libgearman_mysql_udf.so';

mysql> CREATE FUNCTION gman_servers_set RETURNS STRING SONAME

'libgearman_mysql_udf.so';

查看函數

指定 gearman 的服務信息

6. 編寫 mysql 觸發器(根據實際情況編寫)

點擊(此處)折疊或打開

vim test.sql

use test;

DELIMITER $$

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 ;

查看觸發器

7. 編寫 gearman 的 worker 端

點擊(此處)折疊或打開

vim worker.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); #這條語句就是將 id 作 KEY 和

name 作 VALUE 分開存儲,需要和前面寫的 php 測試代碼的存取一致。

}

?>

后臺運行 worker

nohup php worker.php &

然后在3上更新數據庫

然后再瀏覽器中進行測試

總結

以上是生活随笔為你收集整理的redis配置mysql缓存_Redis做mysql的缓存服务器的全部內容,希望文章能夠幫你解決所遇到的問題。

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