实验环境为三台主机,其中server1为master端,server2,3为slave,均安装redis,首先准备redis安装包
tar zxf redis-6.2.1.tar.gz
cd redis-6.2.1
make
make install
./install_server.sh
发现安装失败,提示使用systemd
注释掉安装文件的systemd
vim /root/redis-6.2.1/utils/install_server.sh
重新安装,成功
启动redis
/etc/init.d/redis_6379 start
server1,添加master网段
vim /etc/redis/6379.conf
重新启动
/etc/init.d/redis_6379 restart
redis-cli
server2,server3配置文件
vim /etc/redis/6379.conf
server1查看slave数量
info
当集群某节点出现故障,可以进行故障转移操作
server1修改sentinel文件,指向主机master端
cp /root/redis-6.2.1/sentinel.conf /etc/red
cd /etc/redis/
vim sentinel.conf
将该配置文件传给两台slave,三台机器均开始监控
redis-sentinel /etc/redis/sentinel.conf
模拟master端故障,可以看到master切换
redis-cli
SHUTDOWN
server2端查看info
重启server1后变为slave
虽然做了主从复制,但单个master的容量受机器内存限制,往往要做集群来提升容量。
redis集群特点:
创建集群目录
cd /usr/local/
mkdir cluster
cd cluster
mkdir 7000 7001 7002 7003 7004 7005
编辑各个节点配置文件
cd 7000
vim redis.conf
port 7000
cluster-enabled yes
cluster-config-file nodes.conf
cluster-node-timeout 5000
appendonly yes
daemonize yes
启动集群
redis-server redis.conf
ps ax
以此类推,各节点编辑同样的配置文件,启动
安装tcl, 创建集群并配对
yum install tcl
redis-cli --cluster create --cluster-replicas 1 127.0.0.1:7000 127.0.0.1:7001 127.0.0.1:7002 127.0.0.1:7003 127.0.0.1:7004 127.0.0.1:7005
测试
redis-cli -c -p 7000
重新启动集群需要进入指定集群目录!!!
关掉其中的一个master,集群会自动分配
redis-cli --cluster check 127.0.0.1:7000
重新启动后变为slave
生成集群
cd /root/redis-6.2.1/utils/create-cluster
./create-cluster start
集群配对
./create-cluster create
需要三台主机,由于前面的实验server2成为了master,其余节点变为slave
server1,3中查看系统文件
vim .bash_profile
source .bash_profile
server3作为数据库,若有已存在的数据库,关闭,安装MariaDB数据库
/etc/init.d/mysqld stop
yum install -y mariadb-server
修改配置文件,启动
vim /etc/my.cnf
systemctl start mariadb
编写一个测试文件,导入mysql中
vim 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 $$
#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
使用该数据库
在server1中,安装php-fpm (需从网上下载)httpd psmisc
yum install -y php-fpm httpd psmic
cd /etc/php-fpm.d/
关闭上次实验开启的集群
killall redis-server
编写test.php,连接master与数据库
<?php
$redis = new Redis();
$redis->connect('172.25.34.2',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.34.3','redis','westos');
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 "<br>";
for ($key = 1; $key < 10; $key++)
{
echo "number is <b><font color=#FF0000>$key</font></b>";
echo "<br>";
echo "name is <b><font color=#FF0000>$data[$key]</font></b>";
echo "<br>";
}
?>
server3授权
grant all on test.* to redis@'%' identified by 'westos';
将test.php放在apache发布目录下,开启httpd服务,测试访问172.25.34.1/test.php
server2的redis中,同步数据
刷新网页
gearman是支持分布式任务分发的框架,运行在后台
在server3上gearman安装步骤:
准备lib_mysqludf_json-master.zip文件,解压并进入解压后的目录
unzip lib_mysqludf_json-master.zip
cd lib_mysqludf_json-master/
安装mariadb开发包
yum install -y mariadb-devel
重新编译
gcc $(mysql_config --cflags) -shared -fPIC -o lib_mysqludf_json.so lib_mysqludf_json.c
将编译好的文件复制到/usr/lib64/mysql/plugin/
cp lib_mysqludf_json.so /usr/lib64/mysql/plugin/
模块目录可在数据库查看
cp lib_mysqludf_json.so /usr/lib64/mysql/plugin/
注册udf函数
CREATE FUNCTION json_object RETURNS STRING SONAME 'lib_mysqludf_json.so';
server1中,安装gearman,开启并查看端口
systemctl start gearmand
netstat -antlp|grep :4730
server3中,制定路径解压安装gearman
tar zxf gearman-mysql-udf-0.6.tar.gz
cd gearman-mysql-udf-0.6/
./configure --libdir=/usr/lib64/plugin
发现需要依赖性
安装相关依赖性libevent-devel libgearman.x86_64 libgearman-devel,重新编译gearman
./configure --libdir=/usr/lib64/plugin --with-mysql=/usr/bin/mysql_config
make && make install
进入mysql
CREATE FUNCTION gman_do_background RETURNS STRING SONAME 'libgearman_mysql_udf.so';
CREATE FUNCTION gman_servers_set RETURNS STRING SONAME 'libgearman_mysql_udf.so';
查看
编写触发器,输入数据库
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 ;
mysql < test.sql
进入数据库查看触发器
SHOW TRIGGERS FROM test;
也可以这样看
SHOW TRIGGERS FROM test\G;
server1编写worker.php,指向master
<?php
$worker = new GearmanWorker();
$worker->addServer();
$worker->addFunction('syncToRedis', 'syncToRedis');
$redis = new Redis();
$redis->connect('172.25.34.2', 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);
}
?>
打入后台运行
nohup php worker.php &
进入server3数据库,更新名称为redhat
use test;
update test set name='redhat' where id=1;
进入网页查看更新
在server2查看更新
版权说明 : 本文为转载文章, 版权归原作者所有 版权申明
原文链接 : https://blog.csdn.net/weixin_47401638/article/details/120377965
内容来源于网络,如有侵权,请联系作者删除!