Memcached服务安装

简介:
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服务器端========================
[[email protected] ~]# mkdir /abc
[[email protected] ~]# mount.cifs //192.168.100.10/rhel7 /abc
Password for [email protected]//192.168.100.10/rhel7:
[[email protected] ~]# cd /abc/memcached/
[[email protected] memcached]# tar zxvf libevent-2.1.8-stable.tar.gz -C /opt/
[[email protected] memcached]# tar zxvf memcached-1.5.6.tar.gz -C /opt/
[[email protected] memcached]# cd /opt/
[[email protected] opt]# yum install gcc gcc-c++ make -y
[[email protected] opt]# cd libevent-2.1.8-stable/
[[email protected] libevent-2.1.8-stable]# ./configure --prefix=/usr/local/libevent
[[email protected] libevent-2.1.8-stable]# make && make install
[[email protected] libevent-2.1.8-stable]# cd /opt/memcached-1.5.6/
[[email protected] memcached-1.5.6]# ./configure --prefix=/usr/local/memcached --with-libevent=/usr/local/libevent/
[[email protected] memcached-1.5.6]# make && make install
[[email protected] memcached-1.5.6]# ln -s /usr/local/memcached/bin/ /usr/local/bin/
[[email protected] memcached-1.5.6]# memcached -d -m 32m -p 11211 -u root
[[email protected] 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
[[email protected] memcached-1.5.6]# systemctl stop firewalld.service
[[email protected] memcached-1.5.6]# setenforce 0

----------------------------数据操作--------------------------------------------------
[[email protected] memcached-1.5.6]# yum install telnet -y
[[email protected] 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========================
第二台
[[email protected] ~]# mkdir /abc
[[email protected] ~]# mount.cifs //192.168.100.10/rhel7 /abc
[[email protected] ~]# cd /abc/memcached/LAMP-php5.6/
[[email protected] LAMP-php5.6]# tar zxvf apr-1.6.2.tar.gz -C /opt/
[[email protected] LAMP-php5.6]# tar zxvf apr-util-1.6.0.tar.gz -C /opt/
[[email protected] LAMP-php5.6]# tar jxvf httpd-2.4.29.tar.bz2 -C /opt/
[[email protected] opt]# mv apr-1.6.2/ httpd-2.4.29/srclib/apr
[[email protected] opt]# mv apr-util-1.6.0/ httpd-2.4.29/srclib/apr-util
[[email protected] opt]# yum -y install \
gcc \
gcc-c++ \
make \
pcre-devel \
expat-devel \
perl

[[email protected] opt]# cd httpd-2.4.29
[[email protected] httpd-2.4.29]# ./configure \
--prefix=/usr/local/httpd \
--enable-so \
--enable-rewrite \
--enable-charset-lite \
--enable-cgi

[[email protected] httpd-2.4.29]# make && make install
[[email protected] httpd-2.4.29]# cp /usr/local/httpd/bin/apachectl /etc/init.d/httpd
[[email protected] httpd-2.4.29]# vim /etc/init.d/httpd
#chkconfig: 35 85 21 //第二行插入

[[email protected] httpd-2.4.29]# chkconfig --add httpd
[[email protected] 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行)

[[email protected] httpd-2.4.29]# ln -s /usr/local/httpd/conf/httpd.conf /etc/
[[email protected] httpd-2.4.29]# ln -s /usr/local/httpd/bin/ /usr/local/bin/
[[email protected] httpd-2.4.29]# apachectl -t
Syntax OK
[[email protected] httpd-2.4.29]# service httpd start
[[email protected] httpd-2.4.29]# netstat -ntap | grep 80
tcp 0 0 192.168.120.135:80 0.0.0.0:
LISTEN 71464/httpd

------------------------------安装Mysql------------------------------------------
[[email protected] httpd-2.4.29]# yum install -y ncurses-devel autoconf cmake
[[email protected] httpd-2.4.29]# cd /abc/memcached/LAMP-php5.6/
[[email protected] LAMP-php5.6]# tar xzvf mysql-5.6.26.tar.gz -C /opt/
[[email protected] 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

[[email protected] LAMP-php5.6]#make && make install
(等待很长时间)
[[email protected] mysql-5.6.26]# cp support-files/my-default.cnf /etc/my.cnf
cp:是否覆盖"/etc/my.cnf"? yes
[[email protected] mysql-5.6.26]# cp support-files/mysql.server /etc/init.d/mysqld
[[email protected] mysql-5.6.26]# chmod 755 /etc/init.d/mysqld
[[email protected] mysql-5.6.26]# chkconfig --add /etc/init.d/mysqld
[[email protected] mysql-5.6.26]# chkconfig mysqld --level 235 on
[[email protected] mysql-5.6.26]# echo "PATH=$PATH:/usr/local/mysql/bin" >> /etc/profile
[[email protected] mysql-5.6.26]# source /etc/profile
[[email protected] mysql-5.6.26]# useradd -s /sbin/nologin mysql
[[email protected] mysql-5.6.26]# chown -R mysql:mysql /usr/local/mysql/
[[email protected] mysql-5.6.26]# /usr/local/mysql/scripts/mysql_install_db \
--user=mysql \
--ldata=/var/lib/mysql \
--basedir=/usr/local/mysql \
--datadir=/home/mysql

[[email protected] mysql-5.6.26]# ln -s /var/lib/mysql/mysql.sock /home/mysql/mysql.sock
[[email protected] mysql-5.6.26]# vim /etc/init.d/mysqld

basedir=/usr/local/mysql //第46行
datadir=/home/mysql //第47行

[[email protected] mysql-5.6.26]# service mysqld start
Starting MySQL. SUCCESS!
[[email protected] mysql-5.6.26]# netstat -anpt | grep 3306
tcp6 0 0 :::3306 :::* LISTEN 86823/mysqld
[[email protected] mysql-5.6.26]# mysqladmin -u root -p password "abc123"
Enter password:
[[email protected] mysql-5.6.26]# mysql -uroot -pabc123

------------------------------------------------以下安装PHP----------------------------

[[email protected] mysql-5.6.26]#yum -y install \
gd \
libpng \
libpng-devel \
pcre \
pcre-devel \
libxml2-devel \
libjpeg-devel

[[email protected] mysql-5.6.26]# cd /abc/memcached/LAMP-php5.6/
[[email protected] LAMP-php5.6]# tar xjvf php-5.6.11.tar.bz2 -C /opt/
[[email protected] LAMP-php5.6]# cd /opt/php-5.6.11/
[[email protected] 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

[[email protected] php-5.6.11]# make && make install
[[email protected] php-5.6.11]# cp php.ini-development /usr/local/php5/php.ini
[[email protected] php-5.6.11]# ln -s /usr/local/php5/bin/ /usr/local/bin/
[[email protected] php-5.6.11]# ln -s /usr/local/php5/sbin/
/usr/local/sbin/
[[email protected] 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行

[[email protected] php-5.6.11]#vim /usr/local/httpd/htdocs/index.php
<?php
phpinfo();
?>

[[email protected] htdocs]# mv index.html index.php
mv:是否覆盖"index.php"? yes
[[email protected] htdocs]# service httpd restart
[[email protected] htdocs]# systemctl stop firewalld.service
[[email protected] htdocs]# setenforce 0

在网页测试“http://192.168.120.135/index.php

[[email protected] htdocs]# vim index.php

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

[[email protected] 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)

[[email protected] htdocs]# service httpd restart
在网页测试“http://192.168.120.135/index.php

#测试php能否解析连接mysql

[[email protected] htdocs]# cd /abc/memcached/
[[email protected] memcached]# tar zxvf memcache-2.2.7.tgz -C /opt/
[[email protected] memcached]# cd /opt/memcache-2.2.7/
[[email protected] 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

[[email protected] memcache-2.2.7]# ./configure \
--enable-memcache \
--with-php-config=/usr/local/php5/bin/php-config

[[email protected] memcache-2.2.7]# make && make install
[[email protected] 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行

[[email protected] memcache-2.2.7]# cd /usr/local/httpd/htdocs/
[[email protected] 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;
?>

[[email protected] htdocs]# service httpd restart
在网页测试“http://192.168.120.135/index.php

基础Memcached服务就搭建完毕了,下一篇将介绍Memcached群集服务。

原文地址:http://blog.51cto.com/13756916/2178767

时间: 2024-10-12 16:13:45

Memcached服务安装的相关文章

企业级Memcached服务应用实践。

企业级Memcached服务应用实践 Memcached服务 一, Memcached介绍 1.1 Memcached与常见同类软件对比 (1)Memcached是什么? Memcached是一个开源的,支持高性能,高并发的分布式内存缓存系统,由C语言编写,总共2000多行代码.从软件名称上看,前3个字符"Mem"就是内存的意思,而接下来的后面5个字符"cache"就是缓存的意思,最后一个字符d,是daemon的意思,代表是服务器端守护进程模式服务. Memcach

Linux安装Memcached服务

环境: CentOS 6.4 libevent-1.4.14b-stable memcached-1.4.21 查看是否安装libevent[[email protected] ~]# rpm -qa |grep libevent 一.下载Memcache服务器端 memcache官网:http://memcached.org/ libevent官网:http://www.monkey.org/~provos/libevent/ Memcache用到了libevent这个库用于Socket的处理

linux下编译安装memcached服务实例教程

分享下linux下编译安装memcached服务的步骤,一起来看看. 系统:Ubuntu 13.10第一步:安装libevent-dev$aptitude search libevent-dev$aptitude install libevent-dev 第二步:下载memcached并安装(www.jbxue.com)官网地址:http://memcached.org/$wget -c http://www.memcached.org/files/memcached-1.4.17.tar.gz

CentOS 6.4 安装Memcached服务

环境: CentOS 6.4 libevent-1.4.14b-stable memcached-1.4.21 查看是否安装libevent[[email protected] ~]# rpm -qa |grep libevent 一.下载Memcache服务器端 memcache官网:http://memcached.org/ libevent官网:http://www.monkey.org/~provos/libevent/ Memcache用到了libevent这个库用于Socket的处理

在Linux上安装Memcached服务(转)

下载并安装Memcache服务器端服务器端主要是安装memcache服务器端.下载:http://www.danga.com/memcached/dist/memcached-1.2.2.tar.gz另外,Memcache用到了libevent这个库用于Socket的处理,所以还需要安装libevent,libevent的最新版本是libevent-1.3.(如果你的系统已经安装了libevent,可以不用安装)官网:http://www.monkey.org/~provos/libevent/

Mac linux 安装memcached服务

今天在Mac上安装memcached服务的时候 由于安装memcached之前需要安装libevent依赖包 所以使用brew install libevent 安装过程中报错 Warning: You are using OS X 10.11. We do not provide support for this pre-release version. You may encounter build failures or other breakage. ==> Downloading ht

LNMP 添加 memcached服务

LNMP 添加 memcached服务 由于memcached具有更多的功能和服务,已经不推荐使用memcache了.(缺少个字母d) 1. 首先安装memcached服务端. 这里使用yum源安装 # yum -y install memcached 启动服务(命令默认在/usr/bin/memcached.主要的启动参数-d守护进程,-u使用用户,-m分配内存,-l机器ip,-p端口默认11211 # /usr/bin/memcached -d -uroot -p 11211 -m 200

Nagios监控Memcached服务

说到Memcached服务,其实作为运维人员用的还是很多的: 1.在做LB的时候,为了保证同一台机器的请求的session信息防止丢失,我们用Memcached对session做分布式存储. 2.做mysql缓存的时候,我们常常吧mysql查询的结果缓存到Memcached中,这样能够较少php程序与mysql的交互,也能大大减轻数据库的压力. 从以上来看,Memcached其实也是蛮重要的,那么我们更有必要对其进行时刻的监控,接下来引入正题 Nagios监控Memcached服务是否正常运行.

Memcached的安装与使用

Windows下的Memcache安装 1. 下载memcache的windows稳定版,解压放某个盘下面,比如在c:\memcached 2. 在终端(也即cmd命令界面)下输入 c:\memcached\memcached.exe -d install            --安装memcached成为服务,这样才能正常运行,否则运行失败! 3. 再输入: c:\memcached\memcached.exe -d start    --启动memcached的. 以后memcached将