Memcached原理与部署

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Memcached原理与部署相关的知识,希望对你有一定的参考价值。

Memcached概述

一套开源的高性能分布式内存对象缓存系统
所有的数据都存储在内存中
支持任意存储类型的数据
提高网站的访问速度

Memcached路由算法

●求余数hash算法
先用key做hash运算得到一个整数,再去做hash算法,根据余数进行路
由,这种算法适合大多数据需求,但是不适合用在动态变化的环境中
●一致性hash算法
按照hash算法把对应的key通过一定的hash算法处理后映射形成一一个首
尾相接闭合循环,然后通过使用与对象存储一样的hash算法将机器也映
射到环中,顺时针方向计算将所有对象存储到里自己最近的机器中
适合在动态变化的环境中使用

环境部署

Memcached服务器
192.168.149.139

Memcache客户端
192.168.149.130

实验步骤

配置Memcached服务器

安装编译环境

[root@server ~]# yum install gcc gcc-c++ make -y

远程挂载获取资源包

[root@server ~]# mount.cifs //192.168.142.1/memcached /mnt

解压资源包

[root@server memcached]# tar zxvf libevent-2.1.8-stable.tar.gz -C /opt
[root@server memcached]# tar zxvf memcached-1.5.6.tar.gz -C /opt
[root@server memcached]# tar zxvf magent-0.5.tar.gz -C /opt

配置并编译安装libevent

[root@server memcached]# cd /opt/libevent-2.1.8-stable
[root@server libevent-2.1.8-stable]# ./configure --prefix=/usr/local/libevent
[root@server libevent-2.1.8-stable]# make && make install

编译安装memcached

[root@server libevent-2.1.8-stable]# cd /opt/memcached-1.5.6/
[root@server memcached-1.5.6]# ./configure --prefix=/usr/local/memcached --with-libevent=/usr/local/libevent/
[root@server memcached-1.5.6]# make && make install

建立服务命令的软链接到系统,便于操作

[root@server memcached-1.5.6]# ln -s /usr/local/memcached/bin/* /usr/local/bin

启动服务,-d守护进程 ;-m缓存大小32M ;-p端口11211; -u用户

[root@server memcached-1.5.6]# memcached -d -m 32m -p 11211 -u root

安装与使用telnet连接memcached

[root@server memcached-1.5.6]# yum install telnet -y

#连接使用telnet
[root@server memcached-1.5.6]# telnet 127.0.0.1 11211

#添加键名,不设置序列号,不设置过期时间,即将输入的字节长度
add username 0 0 0

#查看键值
gets username

#最后一个数字是跟新因子
VALUE username 0 7 1

#更新,若键名不存在,则报错
replace username 0 0 9

#更新,对不存在的进行添加
set username 0 0 10

#检查更新,更新因子相等则更新否则返回EXISTS
cas username 0 0 7 4

#键后追加数据 最后数字是追加的字节数
apppend username 0 0 4

#键值前追加数据
prepend username 0 0 2

#删除键
delete username

#清除所有缓存数据
flush_all

#显示状态信息
stats

#返回所有键值对的统计信息
stats items

#返回指定存储空间的键值对
stats cachedump 1 0

#显示各个slab的信息
stats slabs

#输出所有item的大小和个数
stats sizes

#退出
quit

配置客户端的LAMP架构

使用脚本安装架构

[root@client ~]# vim lamp.sh

#!/bin/bash
#请使用source执行此脚本
#数据库默认密码为123123

#远程挂载
df -hT | grep -o "cifs"
if [ $? -ne 0 ];then
echo "Start mount"
yum install expect -y
read -p "请输入提供资源主机IP:" gongip
/usr/bin/expect <<-EOF
spawn mount.cifs //$gongip/LAMP /mnt/
expect {
"Password*"
{send " "}
}
expect eof
EOF
fi

#安装apache服务
cd /mnt/

tar zxf apr-1.6.2.tar.gz -C /opt
tar zxf apr-util-1.6.0.tar.gz -C /opt/
tar jxf httpd-2.4.29.tar.bz2 -C /opt/

cd /opt/

mv apr-1.6.2/ httpd-2.4.29/srclib/apr
mv apr-util-1.6.0/ httpd-2.4.29/srclib/apr-util

yum -y install gcc
gcc-c++
make
pcre-devel
expat-devel
perl

cd /opt/httpd-2.4.29/

./configure --prefix=/usr/local/httpd
--enable-so
--enable-rewrite
--enable-charset-lite
--enable-cgi

make && make install

cp /usr/local/httpd/bin/apachectl /etc/init.d/httpd

sed -i -e ‘2a# chkconfig: 35 85 21 # description: Apache is a World Wide Web server‘ /etc/init.d/httpd

chkconfig --add httpd

sed -i -e ‘197d‘ -e ‘196aServerName www.kgc.com:80‘ /usr/local/httpd/conf/httpd.conf
ip=ifconfig ens33 | awk ‘NR==2{print $2}‘

sed -i -e ‘52s/^/#/‘ -e ‘51d‘ -e "50aListen $ip:80" /usr/local/httpd/conf/httpd.conf

ln -s /usr/local/httpd/conf/httpd.conf /etc/
ln -s /usr/local/httpd/bin/* /usr/local/bin/

systemctl stop firewalld.service
setenforce 0
service httpd start

netstat -ntuap | grep httpd
if [ $? -eq 0 ];then
echo Apache服务启动
else
echo 启动失败
exit 1
fi

#安装mysql服务
echo "================================="
echo "安装mysql数据库,请稍后。。。"
sleep 3

yum install -y ncurses-devel autoconf cmake
cd /mnt/
tar zxf mysql-5.6.26.tar.gz -C /opt/
cd /opt/mysql-5.6.26

cmake
-DCMAKE_INSTALL_PREFIX=/usr/local/mysql
-DDEFAULT_CHARSET=utf8
-DDEFAULT_COLLATION=utf8_general_ci
-DEXTRA_CHARSETS=all
-DSYSCONFIDIR=/etc
-DMYSQL_DATADIR=/home/mysql/
-DMYSQL_UNIX_ADDR=/home/mysql/mysql.sock

echo "进行编译安装,要很久,请稍后。。。"
make && make install

cp support-files/my-default.cnf /etc/my.cnf
cp support-files/mysql.server /etc/init.d/mysqld

chmod +x /etc/init.d/mysqld
chkconfig --add /etc/init.d/mysqld
chkconfig --level 235 mysqld on

echo "PATH=$PATH:/usr/local/mysql/bin" >> /etc/profile
source /etc/profile

echo $PATH | grep -o "mysql"
if [ $? -eq 0 ];then
echo "添加成功"
else
echo "失败"
exit 1
fi

useradd -s /sbin/nologin mysql

chown -R mysql:mysql /usr/local/mysql/

/usr/local/mysql/scripts/mysql_install_db
--user=mysql
--ldata=/var/lib/mysql
--basedir=/usr/local/mysql
--datadir=/home/mysql

sed -i -e ‘47d‘ -e ‘46d‘ -e ‘45abasedir=/usr/local/mysql datadir=/home/mysql‘ /etc/init.d/mysqld
service mysqld start

netstat -ntuap | grep mysqld
if [ $? -eq 0 ];then
echo Mysql数据库启动
else
echo 启动失败
exit 1
fi

#read -p "请输入数据库密码(自定义):" mima
/usr/bin/expect <<EOF
spawn mysqladmin -u root -p password 123123
expect {
"Enter*"
{send " "}
}
expect eof
EOF

#安装php工具
yum install -y gd
libpng
libpng-devel
pcre
pcre-devel
libxml2-devel
libjpeg-devel

cd ~
tar jxf /mnt/php-5.6.11.tar.bz2 -C /opt/
cd /opt/php-5.6.11

./configure
--prefix=/usr/local/php5
--with-gd
--with-zlib
--with-apxs2=/usr/local/httpd/bin/apxs
--with-mysql=/usr/local/mysql
--with-config-file-path=/usr/local/php5
--enable-mbstring

make && make install

cp -p php.ini-development /usr/local/php5/php.ini
ln -s /usr/local/php5/bin/* /usr/local/bin/

sed -i ‘393a AddType application/x-httpd-php .php AddType application/x-httpd-php-source .phps‘ /usr/local/httpd/conf/httpd.conf

sed -i -e ‘256d‘ -e ‘255aDirectoryIndex index.html index.php‘ /usr/local/httpd/conf/httpd.conf

cat > /usr/local/httpd/htdocs/index.php <<-EOF
<?php
phpinfo();
?>
EOF

service httpd stop
service httpd start

rm -rf /opt/*
umount /mnt

source /etc/profile
echo $PATH | grep -o "mysql"
while [ $? -ne 0 ];do
while [ "a" = "a" ]
do
echo "错误!!"
sleep 2
done
done
echo "LAMP架构已完成,欢迎下次光临!"

验证数据库运行

#进入数据库
[root@client php-5.6.11]# mysql -uroot -p123123

#创建数据库名为poke
mysql> create database poke;

#提权poke用户
mysql> grant all on poke.* to ‘pokeuser‘@‘client‘ identified by ‘admin123‘;

#刷新数据库
mysql> flush privileges;

#退出数据库
mysql> quit

修改PHP首页内容

[root@client ~]# vim /usr/local/httpd/htdocs/index.php

<?php
$link=mysqlconnect(‘192.168.142.130‘,‘pokeuser‘,‘admin123‘);
if($link) echo "<h1>Success!!</h1>";
else echo "Fail!!";
mysqlclose();
?>

测试

技术图片

客户端安装Memcache

编译安装后修改php文件

[root@client memcache-2.2.7]# vim /usr/local/php5/php.ini

#替换第736行引号中内容
extension_dir="/usr/local/php5/lib/php/extensions/no-debug-zts-20131226/"
#追加下述条目
extension = memcache.so

编写测试网页,测试memcache

[root@client memcache-2.2.7]# vim /usr/local/httpd/htdocs/index.php

<?php
$memcache = new Memcache();
#指向memcached服务器地址
$memcache->connect(‘192.168.142.139‘,11211);
$memcache->set(‘key‘,‘Memcache test Successfull!‘,0,60);
$result = $memcache->get(‘key‘);
unset($memcache);
echo $result;
?>

测试
技术图片

以上是关于Memcached原理与部署的主要内容,如果未能解决你的问题,请参考以下文章

memcached原理 部署memcached Session共享

领会Memcached高可用群集(Memcached主主复制+Keepalived)

Memcached 主主复制 + Keepalived 高可用架构附上原理

Memcached的原理与应用

在CentOS7上部署Memcached主主复制+Keepalived高可用架构

Memcached常规应用与分布式部署方案