memcache

1.环境配置:

192.168.239.140 :memcache
192.168.239.141:web
192.168.239.142:mysql

2.进行时间同步:

yum install ntp ntpdate
ntpdate cn.pool.ntp.org

3.关闭防火墙:

[root@localhost ~]# systemctl stop firewalld
[root@localhost ~]# systemctl disable firewalld
[root@localhost ~]# sed -i 's/SELINUX=enforcing/SELINUX=disabled/' /etc/selinux/config
[root@localhost ~]# setenforce 0

前提条件:
rpm -e mariadb-libs postfix

4.在web端服务器配置:

yum install mysql-community-libs-compat-8.0.16-2.el7.x86_64.rpm 

yum install http  php php-gb php-mysql php-memcache

systemctl restart httpd
systemctl enable httpd
systemctl restart httpd
systemctl enable mysql

5.在MySQL服务端创建用户

create user 'memcache'@'%' identified by 'WANGYUFENG@mysql123';
alter user 'memcache'@'%' identified with mysql_native_password by 'WANGYUFENG@mysql123';
flush privileges;

6.在web服务端测试http功能:

vim /var/www/html/index.html
this is a test 

去百度开启无痕浏览模式测试

7.在web服务端测试php连接功能:

<?php
phpinfo();
?>

~
8.在web服务端测试MySQL

<?php
$link=mysql_connect('192.168.239.142','superadmin','WANGYUFENG@mysql123');
if($link) echo "<h1>Success!!</h1>";
else echo "Fail!!";
mysql_close();
?>

9.在memcache端进行软件下载安装:

libevent安装:
wget http://monkey.org/~provos/libevent-1.4.14b-stable.tar.gz
tar zxvf libevent-1.4.14b-stable.tar.gz 
yum install make gcc gcc-c++
cd libevent-1.4.14b-stable
 ./configure --prefix=/usr/local/libevent/
make
make install
memcache安装:
wget https://memcached.org/files/memcached-1.5.16.tar.gz
tar -zxvf memcached-1.5.16.tar.gz 
cd memcached-1.5.16
./configure --prefix=/usr/local/memcached --with-libevent=/usr/local/libevent/
make
make install

启动:

cd /etc/rc.d/init.d/
/usr/local/memcached/bin/memcached -d -l 127.0.0.1 -p 11211 -u root -m 64 -c 1024 -P /var/run/memcached.pid
-d:启动一个守护进程
-l:监听的服务器IP地址
-p:是设置memcache的TCP监听的端口,最好是1024以上的端口
-u:是运行memcache的用户,如果当前为root的话,需要使用此参数指定用户
-m:是分配给memcache使用的内存数量,单位是MB,默认是64MB
-c:选项是最大与模型的最大连接数,默认是1024
-P:是设置保存memcache的pid文件
-M:return error on memory exhausted(rather than removing items)
-f:chunk size growth factor (default : 1.25)
-I override the size of each sllab page. Adjusts max item size(1.4.2版本新增)
也可以启动多个守护进程,但是端口不能重复
<?php
$memcache = new Memcache;
$memcache->connect('192.168.239.140',11211) or die ("could not connect");
$version = $memcache->getVersion();
echo "Server's version: ".$version."<br/>";
$tmp_object = new stdClass;
$tmp_object->str_attr = 'test';
$tmp_object->int_attr = 123;
$memcache->set('key', $tmp_object,false,10) or die ("Failed to save data at the server");
echo "Store data in the cache(data will expire in 10 seconds)<br/>";
$get_result = $memcache->get('key');
echo "Data from the cache:<br/>";
var_dump($get_result);
?>

测试成功结果如下:

Server's version: 1.5.16
Store data in the cache(data will expire in 10 seconds)
Data from the cache:
object(stdClass)#3 (2) { ["str_attr"]=> string(4) "test" ["int_attr"]=> int(123) }

在web端配置session:

vim /etc/php.ini
session.save_handler = memcache
session.save_path = "tcp://192.168.239.141:11211?persistent=1&weight=1&timeout=1&retry_interval=15"

测试memcache的可用性:

<?php
session_start();
if (!isset($——SESSION['session_time']))
{
$_SESSION['session_time']=time();
}
echo "session_time:".$_SESSION['session_time']."<br/>";
echo "now_time:".time()."<br/>";
echo "session_id:".session_id()."<br/>";
?>

在MySQL端创建测试数据库:

create user ‘user’@‘%’ identified by ‘WANGYUFENG@mysql123’;

create database testab1;

use testab1;

create table test1(
id int not null auto_increment,
name int not null,
primary key(id))
innodb=engine


测试memcache存储数据库

<?php
$memcachehost='192.168.239.140';
$memcacheport=11211;
$memcachelife=60;
$memcache=new Memcache;
$memcache->connect($memcachehost,$memcacheport) or die ("Could not connect");
$query="select * from test1 limit 10";
$key=md5($query);
if(!$memcache->get($key))
{
$conn=mysql_connect("192.168.239.142","user","WANGYUFENG@mysql123");
mysql_select_db(testab1);
$result=mysql_query($query);
while($row=mysql_fetch_assoc($result))
{
$arr[]=$row;
}
$f='mysql';
$memcache->add($key,serialize($arr),0,30);
$data=$arr;
}
else{
$f='memcache';
$data_mem=$memcache->get($key);
$data=unserialize($data_mem);
}
echo $f;
echo "<br/>";
echo "$key";
echo "<br/>";
//print_r($data);
foreach($data as $a)
{
echo "number is <b><font color=#FF0000>$a[id]</font></b>";
echo "<br/>";
echo "name is <b><font color=#FF0000>$a[name]</font></b>";
echo "<br>";
}
?>
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值