memcached内存数据库——部署及操作

此生再无相见时 提交于 2020-01-08 01:56:53

概述

 Memcached 是一个高性能的分布式内存对象缓存系统,用于动态Web应用以减轻数据库负载。它通过在内存中缓存数据和对象来减少读取数据库的次数,从而提高动态、数据库驱动网站的速度。Memcached基于一个存储键/值对的hashmap。其守护进程(daemon )是用C写的,但是客户端可以用任何语言来编写,并通过memcached协议与守护进程通信。

特点

1、协议简单;
2、基于libevent的事件处理;
3、内置内存存储方式;
4、memcached不互相通信的分布式。

存储方式

为了提高性能,memcached中保存的数据都存储在memcached内置的内存存储空间中。由于数据仅存在于内存中,因此重启memcached、重启操作系统会导致全部数据消失。另外,内容容量达到指定值之后,就基于LRU(Least Recently Used)算法自动删除不使用的缓存。memcached本身是为缓存而设计的服务器,因此并没有过多考虑数据的永久性问题。

实验环境

memcached服务器 192.168.13.128 (memcached-1.5.6.tar.gz、libevent-2.1.8-stable.tar.gz)
memcache客户端 192.168.13.129 (memcache-2.2.7.tgz 、LAMP)

1,部署memcached服务器

[root@server ~]# yum install -y gcc gcc-c++ make  ##安装环境组件
[root@server ~]# mount.cifs //192.168.100.3/LNMP-C7 /mnt/
Password for root@//192.168.100.3/LNMP-C7:  
[root@server ~]# cd /mnt/memcached/
[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]# 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   ##编译安装
[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  ##关联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/  ##建立软连接
[root@server memcached-1.5.6]# memcached -d -m 32m -p11211 -uroot  ##开启服务
##-d守护进程 ;-m缓存大小32M ;-p端口11211
[root@server memcached-1.5.6]# netstat -ntap | grep memcached  ##查看端口
[root@server memcached-1.5.6]# systemctl stop firewalld.service 
[root@server memcached-1.5.6]# setenforce 0
[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 7   ##0:不设置序列号0:不设置过期事件7:字节长度
1234567
STORED
add users 0 0 7
123
ERROR
get username    ##查看
VALUE username 0 7
1234567
END
gets username  ##查看
VALUE username 0 7 1   ##1:更新次数
1234567
END

2,在memcache客户端安装lamp结构

##lamp结构见前博客有具体操作
##测试数据工作是否正常
[root@client php-5.6.11]# mysql -u root -pabc123   //进入数据库

CREATE DATABASE sky;   //创建一个数据库为 sky
GRANT all ON sky.* TO 'skyuser'@'%' IDENTIFIED BY 'admin123';  //提权
flush privileges;   //刷新数据库
##修改PHP首页
[root@client php-5.6.11]# vim /usr/local/httpd/htdocs/index.php

        <?php
        $link=mysql_connect('192.168.13.129','skyuser','admin123');
        if($link) echo "<h1>Success!!</h1>";
        else echo "Fail!!";
        mysql_close();
        ?>

3,在客户端安装memcache客户端

[root@client ~]# yum install autoconf -y
[root@client ~]# cd /mnt/memcached/
[root@client memcached]# tar zvxf memcache-2.2.7.tgz -C /opt/
[root@client memcached]# cd /opt/memcache-2.2.7/
[root@client memcache-2.2.7]# /usr/local/php5/bin/phpize  ##增加PHP模块生成脚本
[root@client memcache-2.2.7]# ./configure \
> --enable-memcache \   ##开启memcache
> --with-php-config=/usr/local/php5/bin/php-config  ##关联PHP配置文件
[root@client memcache-2.2.7]# make && make install
/usr/local/php5/lib/php/extensions/no-debug-zts-20131226/  ##共享文件位置,后面需要用到
[root@client memcache-2.2.7]# vim /usr/local/php5/php.ini  ##修改php配置文件
extension_dir = "/usr/local/php5/lib/php/extensions/no-debug-zts-20131226/" ##共享文件位置
extension = memcache.so  ##指向memcache模块

4,用客户端检测服务端是否正常连接

[root@client memcache-2.2.7]# vim /usr/local/httpd/htdocs/index.php
<?php
$memcache=new Memcache();
$memcache->connect('192.168.13.128',11211);  ##连接Memcached服务器地址
$memcache->set('key','Memcache test Successfull!',0,60);
$result=$memcache->get('key');
unset($memcache);
echo$result;
?>
[root@client memcache-2.2.7]# service httpd stop  ##重启
[root@client memcache-2.2.7]# service httpd start
##浏览器访问

memcached内存数据库——部署及操作

5,memcache数据库基本操作

新建数据:

add username 0 0 7  
//添加数据(两个0表示:不进行压缩和序列化标识,数据过期时间为永不过期;标识号是7就需要输入7位数。)
allways   //输入一个7位数

查询数据:

get username  //查询数据
gets username

更新数据:

set username 0 0 10       //更新信息,若键名不存在,则自行添加
everything

replace username 0 0 8    //更新信息,若键名不存在,则报错
12345678

检测/查看 更新数据:

gets username  //检测更新
VALUE username 0 8 4
12345678

追加数据:

append username 0 0 7       //键值后追加数据
example

prepend username 0 0 2     //键值前追加数据
un

清除数据:

delete username     //清除指定的键值数据
flush_all           //清除所有缓存数据
OK

查看服务器统计信息:

stats                  //显示状态信息
stats items            //返回所有键值对的统计信息
stats cachedump 1 0    //返回指定存储空间的键值对 
stats slabs            //显示各个slab的信息
stats sizes           //输出所有item的大小和个数
stats reset           //清空统计数据

谢谢阅读!

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!