redis安装及应用
Redis安装及主从配置
server2,3,4.
安装
tar zxf redis-4.0.1.tar.gz
cd redis-4.0.1
yum install -y gcc
make
make install
cd utils/
./install_server.sh
cd /etc/redis/
vim 6379.conf
/etc/init.d/redis_6379 restart
redis-cli
server3,4
主从配置(2主,3,4从)
vim /etc/redis/6379.conf
/etc/init.d/redis_6379 restart
此时在server2中redis写入,server2,3会自动复制
Redis高可用
[root@server2 ~]# cd redis-4.0.1
[root@server2 redis-4.0.1]# cp sentinel.conf /etc/redis/
[root@server2 redis-4.0.1]# cd /etc/redis/
[root@server2 redis]# vim sentinel.conf
[root@server2 redis]# scp sentinel.conf root@172.25.8.3:/etc/redis/
[root@server2 redis]# scp sentinel.conf root@172.25.8.4:/etc/redis/
[root@server2 redis]# redis-sentinel /etc/redis/sentinel.conf ##将三台机子都启动sentinel服务
server3
[root@server3 redis]# redis-sentinel /etc/redis/sentinel.conf
server4
[root@server4 ~]# redis-sentinel /etc/redis/sentinel.conf
server2:
[root@server2 ~]# redis-cli -p 26379
127.0.0.1:26379> info
此时,停止server2redis服务:
[root@server2 ~]# redis-cli
127.0.0.1:6379> SHUTDOWN
not connected>
会发现server3选举成为新的master
[root@server2 ~]# redis-cli -p 26379
127.0.0.1:26379> info
[root@server2 ~]# vim /etc/redis/6379.conf 将2作为从再次开启
[root@server2 redis]# /etc/init.d/redis_6379 start
[root@server2 redis]# redis-cli
127.0.0.1:6379> info
redis集群
[root@server2 ~]# cd /usr/local/
[root@server2 local]# mkdir cluster ##建立集群目录
[root@server2 local]# cd cluster/
[root@server2 cluster]# mkdir 700{1..6}
[root@server2 cluster]# cd 7001/
[root@server2 7001]# vim redis.conf(其余依法操作,只需修改相应port参数)
[root@server2 7001]# redis-server redis.conf ##打开集群服务
[root@server2 7001]# cd ..
[root@server2 cluster]# cp 7001/redis.conf 7002/
[root@server2 cluster]# cp 7001/redis.conf 7003
[root@server2 cluster]# cp 7001/redis.conf 7004
[root@server2 cluster]# cp 7001/redis.conf 7005
[root@server2 cluster]# cp 7001/redis.conf 7006
[root@server2 cluster]# cd 7002/
[root@server2 7002]# ls
redis.conf
[root@server2 7002]# vim redis.conf
[root@server2 7002]# redis-server redis.conf
[root@server2 7002]# cd ..
[root@server2 cluster]# cd 7003/
[root@server2 7003]# vim redis.conf
[root@server2 7003]# redis-server redis.conf
[root@server2 7003]# cd ..
[root@server2 cluster]# cd 7004/
[root@server2 7004]# vim redis.conf
[root@server2 7004]# redis-server redis.conf
[root@server2 7004]# cd ..
[root@server2 cluster]# cd 7005/
[root@server2 7005]# vim redis.conf
[root@server2 7005]# redis-server redis.conf
[root@server2 cluster]# cd 7006/
[root@server2 7006]# vim redis.conf
[root@server2 7006]# redis-server redis.conf
[root@server2 7006]# cd
[root@server2 ~]# cd redis-4.0.1
[root@server2 redis-4.0.1]# cd src/
[root@server2 src]# cp redis-trib.rb /usr/local/bin/
[root@server2 src]# cd
[root@server2 ~]# yum install rubygems-1.3.7-5.el6.noarch.rpm -y
[root@server2 ~]# yum install libyaml-0.1.3-4.el6_6.x86_64.rpm ruby-2.2.3-1.el6.x86_64.rpm ##安装依赖包
[root@server2 ~]# gem install --local redis-4.0.1.gem
[root@server2 ~]# redis-trib.rb create --replicas 1 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 127.0.0.1:7006 ##创建集群
[root@server2 ~]# redis-trib.rb check 127.0.0.1
[root@server2 ~]# redis-cli -c -p 7001
127.0.0.1:7001> set name westos
-> Redirected to slot [5798] located at 127.0.0.1:7002 ##提示内容会转到7002上
OK[root@server2 ~]# redis-cli -c -p 7001
127.0.0.1:7001> get name
-> Redirected to slot [5798] located at 127.0.0.1:7002
"westos"
[root@server2 ~]# redis-cli -c -p 7002
127.0.0.1:7002> get name
"westos"
127.0.0.1:7002>
[root@server2 ~]# redis-cli -c -p 7006
127.0.0.1:7006> get name
-> Redirected to slot [5798] located at 127.0.0.1:7002
"westos"
127.0.0.1:7002>
[root@server2 ~]# redis-cli -p 7002
127.0.0.1:7002> SHUTDOWN ##关闭一个master,会发现集群会继续运行
not connected>
[root@server2 ~]# redis-trib.rb check 127.0.0.1:7001 ##查看集群状态
[root@server2 ~]# redis-cli -c -p 7001
127.0.0.1:7001> get name
-> Redirected to slot [5798] located at 127.0.0.1:7005
"westos"
127.0.0.1:7005>
root@server2 ~]# redis-cli -c -p 7005 ##再关闭一个master,这时集群会down掉
127.0.0.1:7005> SHUTDOWN
not connected>
[root@server2 ~]# redis-cli -c -p 7001
127.0.0.1:7001> get name
(error) CLUSTERDOWN The cluster is down
Redis用作mysql缓存
Server3:redis -master
Server4:redis-slave lnmp
Server1:mysql
server1:
[root@server1 ~]# yum install -y mysql-server
[root@server1 ~]# /etc/init.d/mysqld start
[root@server1 ~]# mysql
mysql> show databases;
mysql> grant all on test.* to redis@'%' identified by 'westos'; ##远程登陆用户授权
server4;
[root@server4 ~]# cd redis/
[root@server4 redis]# rpm -ivh nginx-1.8.0-1.el6.ngx.x86_64.rpm 安装nginx与php
[root@server4 redis]# yum install -y php*
[root@server4 redis]# cd /etc/nginx/
[root@server4 nginx]# cd
[root@server4 ~]# vim /etc/php.ini
修改时区
[root@server4 ~]# cd /etc/php.d/
[root@server4 php.d]# /etc/init.d/php-fpm start
[root@server4 php.d]# /etc/init.d/php-fpm reload
[root@server4 php.d]# cd /etc/
[root@server4 etc]# cd php-fpm.d/
[root@server4 php-fpm.d]# vim www.conf( 将29行和41行后的改为nginx)
[root@server4 php-fpm.d]# /etc/init.d/php-fpm reload
配置nginx
[root@server4 php-fpm.d]# cd /etc/nginx/
[root@server4 nginx]# cd conf.d/
[root@server4 conf.d]# vim default.conf
[root@server4 conf.d]# /etc/init.d/nginx start
[root@server4 conf.d]# cd /usr/share/nginx/html/
[root@server4 html]# vim index.php (编辑一个PHP默认访问页面)
访问172.25.8.4
[root@server4 ~]# cd redis/ ##(这里有一个写好的测试页面)
[root@server4 redis]# cp test.php /usr/share/nginx/html/
[root@server4 redis]# cd /usr/share/nginx/html/
[root@server4 html]# vim test.php
[root@server4 html]# yum install unzip -y
[root@server4 ~]# cd redis/
[root@server4 redis]# unzip phpredis-master.zip
[root@server4 redis]# cd phpredis-master
[root@server4 phpredis-master]# phpize
Configuring for:
PHP Api Version: 20090626
Zend Module Api No: 20090626
Zend Extension Api No: 220090626
[root@server4 phpredis-master]# ./configure
[root@server4 phpredis-master]# make
[root@server4 phpredis-master]# make install
cd /etc/php.d/
[root@server4 php.d]# ls
curl.ini json.ini mysql.ini pdo_sqlite.ini zip.ini
fileinfo.ini mbstring.ini pdo.ini phar.ini
gd.ini mysqli.ini pdo_mysql.ini sqlite3.ini
[root@server4 php.d]# cp mysql.ini redis.ini
[root@server4 php.d]# vim redis.ini
[root@server4 php.d]# /etc/init.d/php-fpm reload
[root@server4 php.d]# php -m |grep redis
[root@server4 ~]# cd redis/ ##有一个自己写好用来测试的test.sql
[root@server4 redis]# mysql -h 172.25.8.1 -u redis -p < test.sql ##导入到数据库中
Enter password:
server1:
mysql> use test
mysql> show tables;
mysql> select * from test;
访问web页面
至此,Redeis用作mysql缓存已完成,但会有一个问题,此时如果在数据库中更新表中内容,redis却不会更新,会出现数据不同步的现象,此时就需要mysql通过触发器改变数据同步到redis。
通过gearman实现数据同步
server4:
[root@server4 redis]# yum install -y gearmand-1.1.8-2.el6.x86_64.rpm libgearman-* libevent-*
[root@server4 redis]# /etc/init.d/gearmand start
[root@server4 redis]# scp lib_mysqludf_json-master.zip gearman-mysql-udf-0.6.tar.gz root@172.25.8.1:
server1:
[root@server1 ~]# yum install unzip -y
[root@server1 ~]# unzip lib_mysqludf_json-master.zip
[root@server1 ~]# ls
gearman-mysql-udf-0.6.tar.gz lib_mysqludf_json-master.zip
lib_mysqludf_json-master
[root@server1 ~]# cd lib_mysqludf_json-master
[root@server1 lib_mysqludf_json-master]# ls
lib_mysqludf_json.c lib_mysqludf_json.so README.md
lib_mysqludf_json.html lib_mysqludf_json.sql
[root@server1 lib_mysqludf_json-master]# yum install gcc -y
[root@server1 lib_mysqludf_json-master]# yum install -y mysql-devel
[root@server1 lib_mysqludf_json-master]# gcc $(mysql_config --cflags) -shared -fPIC -o lib_mysqludf_json.so lib_mysqludf_json.c
[root@server1 ~]# ls
gearman-mysql-udf-0.6.tar.gz lib_mysqludf_json-master.zip
lib_mysqludf_json-master
[root@server1 ~]# cd lib_mysqludf_json-master
[root@server1 lib_mysqludf_json-master]# cp lib_mysqludf_json.so /usr/lib64/mysql/plugin/
[root@server1 lib_mysqludf_json-master]# mysql
mysql> CREATE FUNCTION json_object RETURNS STRING SONAME 'lib_mysqludf_json.so';
##注册udf函数
mysql> select * from mysql.func;
[root@server1 ~]# tar zxf gearman-mysql-udf-0.6.tar.gz
[root@server1 ~]# cd gearman-mysql-udf-0.6
[root@server1 ~]# yum install -y libgearman-* libevent-*
[root@server1 gearman-mysql-udf-0.6]# ./configure --with-mysql --libdir=/usr/lib64/mysql/plugin/
[root@server1 gearman-mysql-udf-0.6]# make
[root@server1 gearman-mysql-udf-0.6]# make install
[root@server1 ~]# mysql ##注册udf函数
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';
mysql> select * from mysql.func;
mysql> SELECT gman_servers_set('172.25.8.4:4730');
server4:
[root@server4 redis]# vim test.sql
[root@server4 redis]# mysql -h 172.25.8.1 -u redis -p < test.sql
server1:
mysql> SHOW TRIGGERS FROM test; ##查看触发器
server4:
[root@server4 redis]# tar zxf gearman-1.1.2.tgz
[root@server4 redis]# cd gearman-1.1.2
[root@server4 gearman-1.1.2]# phpize
[root@server4 gearman-1.1.2]# ./configure
[root@server4 gearman-1.1.2]# make
[root@server4 gearman-1.1.2]# make install
[root@server4 gearman-1.1.2]# cd /etc/php.d/
[root@server4 php.d]# cp redis.ini gearman.ini
[root@server4 php.d]# vim gearman.ini
[root@server4 php.d]# /etc/init.d/php-fpm reload
[root@server4 php.d]# cd
[root@server4 ~]# cd redis/
[root@server4 redis]# cp worker.php /usr/local/
[root@server4 redis]# cd /usr/local/
[root@server4 local]# vim worker.php
[root@server4 local]# nohup php /usr/local/worker.php &
server1:
mysql> use test;
mysql> update test set name='hello' where id=1; ##更新数据
mysql> select * from test;
server3:
[root@server3 redis]# redis-cli
127.0.0.1:6379> get 1
"hello"
页面访问;
172.25.8.4/test.php
Ok。