简介: Memcached 是一个高性能的分布式内存对象缓存系统,用于动态Web应用以减轻数据库负载。它通过在内存中缓存数据和对象来减少读取数据库的次数,从而提高动态、数据库驱动网站的速度。Memcached基于一个存储键/值对的hashmap。其守护进程(daemon )是用C写的,但是客户端可以用任何语言来编写,并通过memcached协议与守护进程通信。 特点: memcached作为高速运行的分布式缓存服务器,具有以下的特点。 · 协议简单 · 基于libevent的事件处理 · 内置内存存储方式 · memcached不互相通信的分布式 具体实验: 实验环境:两台CentOS7.3系统完成(一台Memcached服务器,一台基于LAMP架构进行的Memcached API客户端)
Memcached服务器 IP:192.168.120.128
Memcached API客户端 IP:192.168.120.135
**实验所需软件包:**
Memcached压缩包链接:https://pan.baidu.com/s/1pekiFGA0rv3XyM7KY58jUA
提取码:6uff
=========================memcached服务器端========================
[root@localhost ~]# mkdir /abc
[root@localhost ~]# mount.cifs //192.168.100.10/rhel7 /abc
Password for root@//192.168.100.10/rhel7:
[root@localhost ~]# cd /abc/memcached/
[root@localhost memcached]# tar zxvf libevent-2.1.8-stable.tar.gz -C /opt/
[root@localhost memcached]# tar zxvf memcached-1.5.6.tar.gz -C /opt/
[root@localhost memcached]# cd /opt/
[root@localhost opt]# yum install gcc gcc-c++ make -y
[root@localhost opt]# cd libevent-2.1.8-stable/
[root@localhost libevent-2.1.8-stable]# ./configure --prefix=/usr/local/libevent
[root@localhost libevent-2.1.8-stable]# make && make install
[root@localhost libevent-2.1.8-stable]# cd /opt/memcached-1.5.6/
[root@localhost memcached-1.5.6]# ./configure --prefix=/usr/local/memcached --with-libevent=/usr/local/libevent/
[root@localhost memcached-1.5.6]# make && make install
[root@localhost memcached-1.5.6]# ln -s /usr/local/memcached/bin/* /usr/local/bin/
[root@localhost memcached-1.5.6]# memcached -d -m 32m -p 11211 -u root
[root@localhost memcached-1.5.6]# netstat -ntap | grep memc
tcp 0 0 0.0.0.0:11211 0.0.0.0:* LISTEN 58189/memcached
tcp6 0 0 :::11211 :::* LISTEN 58189/memcached
[root@localhost memcached-1.5.6]# systemctl stop firewalld.service
[root@localhost memcached-1.5.6]# setenforce 0
----------------------------数据操作-------------------------------------------------- [root@localhost memcached-1.5.6]# yum install telnet -y [root@localhost memcached-1.5.6]# telnet 127.0.0.1 11211 Trying 127.0.0.1... Connected to 127.0.0.1. Escape character is '^]'.
add username 0 0 7 //不进行压缩和序列化标识 ,数据过期时间为永不过期 ,即将输入的字符串长度(0 0 7含义)
example //输入数据
get username //获取数据 VALUE username 0 7 example
gets username VALUE username 0 7 1 //最后一位时更新因子会自增1 example
set username 0 0 10 //更新信息,若键名不存在,则自行添加 everything
replace username 0 0 8 //更新信息,若键名不存在,则报错 12345678
gets username VALUE username 0 8 4 12345678
cas username 0 0 7 //检查更新,更新因子相等则更新否则返回EXISTS(不能更新) logging STORED
append username 0 0 7 //键值后追加数据 example STORED
prepend username 0 0 2 //键值前追加数据 un STORED
delecte username
flush_all //清除所有缓存数据 OK
stats //显示状态信息
quit //退出
=====================以下安装客户端--需要LAMP========================
第二台
[root@localhost ~]# mkdir /abc
[root@localhost ~]# mount.cifs //192.168.100.10/rhel7 /abc
[root@localhost ~]# cd /abc/memcached/LAMP-php5.6/
[root@localhost LAMP-php5.6]# tar zxvf apr-1.6.2.tar.gz -C /opt/
[root@localhost LAMP-php5.6]# tar zxvf apr-util-1.6.0.tar.gz -C /opt/
[root@localhost LAMP-php5.6]# tar jxvf httpd-2.4.29.tar.bz2 -C /opt/
[root@localhost opt]# mv apr-1.6.2/ httpd-2.4.29/srclib/apr
[root@localhost opt]# mv apr-util-1.6.0/ httpd-2.4.29/srclib/apr-util
[root@localhost opt]# yum -y install
gcc
gcc-c++
make
pcre-devel
expat-devel
perl
[root@localhost opt]# cd httpd-2.4.29
[root@localhost httpd-2.4.29]# ./configure
--prefix=/usr/local/httpd
--enable-so
--enable-rewrite
--enable-charset-lite
--enable-cgi
[root@localhost httpd-2.4.29]# make && make install [root@localhost httpd-2.4.29]# cp /usr/local/httpd/bin/apachectl /etc/init.d/httpd [root@localhost httpd-2.4.29]# vim /etc/init.d/httpd #chkconfig: 35 85 21 //第二行插入
[root@localhost httpd-2.4.29]# chkconfig --add httpd [root@localhost httpd-2.4.29]# vim /usr/local/httpd/conf/httpd.conf
ServerName www.yun.com:80 //第197行 Listen 192.168.120.135:80 //第51行(注释第52行)
[root@localhost httpd-2.4.29]# ln -s /usr/local/httpd/conf/httpd.conf /etc/ [root@localhost httpd-2.4.29]# ln -s /usr/local/httpd/bin/* /usr/local/bin/ [root@localhost httpd-2.4.29]# apachectl -t Syntax OK [root@localhost httpd-2.4.29]# service httpd start [root@localhost httpd-2.4.29]# netstat -ntap | grep 80 tcp 0 0 192.168.120.135:80 0.0.0.0:* LISTEN 71464/httpd
------------------------------安装Mysql------------------------------------------ [root@localhost httpd-2.4.29]# yum install -y ncurses-devel autoconf cmake [root@localhost httpd-2.4.29]# cd /abc/memcached/LAMP-php5.6/ [root@localhost LAMP-php5.6]# tar xzvf mysql-5.6.26.tar.gz -C /opt/ [root@localhost LAMP-php5.6]# 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
[root@localhost LAMP-php5.6]#make && make install
(等待很长时间)
[root@localhost mysql-5.6.26]# cp support-files/my-default.cnf /etc/my.cnf
cp:是否覆盖"/etc/my.cnf"? yes
[root@localhost mysql-5.6.26]# cp support-files/mysql.server /etc/init.d/mysqld
[root@localhost mysql-5.6.26]# chmod 755 /etc/init.d/mysqld
[root@localhost mysql-5.6.26]# chkconfig --add /etc/init.d/mysqld
[root@localhost mysql-5.6.26]# chkconfig mysqld --level 235 on
[root@localhost mysql-5.6.26]# echo "PATH=$PATH:/usr/local/mysql/bin" >> /etc/profile
[root@localhost mysql-5.6.26]# source /etc/profile
[root@localhost mysql-5.6.26]# useradd -s /sbin/nologin mysql
[root@localhost mysql-5.6.26]# chown -R mysql:mysql /usr/local/mysql/
[root@localhost mysql-5.6.26]# /usr/local/mysql/scripts/mysql_install_db
--user=mysql
--ldata=/var/lib/mysql
--basedir=/usr/local/mysql
--datadir=/home/mysql
[root@localhost mysql-5.6.26]# ln -s /var/lib/mysql/mysql.sock /home/mysql/mysql.sock [root@localhost mysql-5.6.26]# vim /etc/init.d/mysqld
basedir=/usr/local/mysql //第46行 datadir=/home/mysql //第47行
[root@localhost mysql-5.6.26]# service mysqld start
Starting MySQL. SUCCESS!
[root@localhost mysql-5.6.26]# netstat -anpt | grep 3306
tcp6 0 0 :::3306 :::* LISTEN 86823/mysqld
[root@localhost mysql-5.6.26]# mysqladmin -u root -p password "abc123"
Enter password:
[root@localhost mysql-5.6.26]# mysql -uroot -pabc123
------------------------------------------------以下安装PHP----------------------------
[root@localhost mysql-5.6.26]#yum -y install
gd
libpng
libpng-devel
pcre
pcre-devel
libxml2-devel
libjpeg-devel
[root@localhost mysql-5.6.26]# cd /abc/memcached/LAMP-php5.6/
[root@localhost LAMP-php5.6]# tar xjvf php-5.6.11.tar.bz2 -C /opt/
[root@localhost LAMP-php5.6]# cd /opt/php-5.6.11/
[root@localhost 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
[root@localhost php-5.6.11]# make && make install [root@localhost php-5.6.11]# cp php.ini-development /usr/local/php5/php.ini [root@localhost php-5.6.11]# ln -s /usr/local/php5/bin/* /usr/local/bin/ [root@localhost php-5.6.11]# ln -s /usr/local/php5/sbin/* /usr/local/sbin/ [root@localhost php-5.6.11]# vim /etc/httpd.conf
AddType application/x-httpd-php .php //第394行 AddType application/x-httpd-php-source .phps //第395行
DirectoryIndex index.html index.php //第256行
[root@localhost php-5.6.11]#vim /usr/local/httpd/htdocs/index.php <?php phpinfo(); ?>
[root@localhost htdocs]# mv index.html index.php mv:是否覆盖"index.php"? yes [root@localhost htdocs]# service httpd restart [root@localhost htdocs]# systemctl stop firewalld.service [root@localhost htdocs]# setenforce 0
在网页测试“http://192.168.120.135/index.php” [root@localhost htdocs]# vim index.php
<?php $link=mysql_connect('192.168.120.135','skyuser','admin123'); if($link) echo "Success!!"; else echo "Fail!!"; mysql_close(); ?>
[root@localhost htdocs]# mysql -uroot -p Enter password:
mysql> CREATE DATABASE sky; Query OK, 1 row affected (0.00 sec)
mysql> GRANT all ON sky.* TO 'skyuser'@'%' IDENTIFIED BY 'admin123'; Query OK, 0 rows affected (0.12 sec)
mysql> flush privileges; Query OK, 0 rows affected (0.09 sec)
[root@localhost htdocs]# service httpd restart 在网页测试“http://192.168.120.135/index.php” #测试php能否解析连接mysql
[root@localhost htdocs]# cd /abc/memcached/ [root@localhost memcached]# tar zxvf memcache-2.2.7.tgz -C /opt/ [root@localhost memcached]# cd /opt/memcache-2.2.7/ [root@localhost memcache-2.2.7]# /usr/local/php5/bin/phpize //增加为PHP的模块后再对memcache进行配置 Configuring for: PHP Api Version: 20131106 Zend Module Api No: 20131226 Zend Extension Api No: 220131226
[root@localhost memcache-2.2.7]# ./configure
--enable-memcache
--with-php-config=/usr/local/php5/bin/php-config
[root@localhost memcache-2.2.7]# make && make install [root@localhost memcache-2.2.7]# vim /usr/local/php5/php.ini
extension_dir="/usr/local/php5/lib/php/extensions/no-debug-zts-20131226/" //第737行 extension=memcache.so //第738行
[root@localhost memcache-2.2.7]# cd /usr/local/httpd/htdocs/ [root@localhost htdocs]# vim index.php
<?php $memcache = new Memcache(); $memcache->connect('192.168.120.128',11211); $memcache->set('key','Memcache test Successfull!',0,60); $result = $memcache->get('key'); unset($memcache); echo $result; ?>
[root@localhost htdocs]# service httpd restart 在网页测试“http://192.168.120.135/index.php”
基础Memcached服务就搭建完毕了,下一篇将介绍Memcached群集服务。