您的位置:首页 > 数据库 > Memcache

LAMP+memcache +(mysql)搭建

2019-07-25 22:15 1026 查看
版权声明:本文为博主原创文章,遵循 CC 4.0 by-sa 版权协议,转载请附上原文出处链接和本声明。 本文链接:https://blog.csdn.net/weixin_44717011/article/details/97261654

LAMP+memcache +(mysql)搭建:

一、基础环境配置
1、环境规划:
pc1 192.168.224.141 : mysql
pc2 192.168.224.142 : web
pc3 192.168.224.143 : memcache
2、时间同步:

yum -y install ntp ntpdate		//安装ntpdate工具
ntpdate cn.pool.ntp.org		//设置系统时间与网络时间同步
hwclock --systohc		//将系统时间写入硬件时间

3、关闭防火墙:

systemctl stop firewalld
systemctl disable firewalld
sed -i 's/SELINUX=enforcing/SELINUX=disabled/' /etc/selinux/config
setenforce 0

4、卸载Mariadb:

rpm -e mariadb-libs postfix

二、web服务器配置
1、安装软件,启动服务:

[root@pc2 ~]# wget https://downloads.mysql.com/archives/get/file/mysql-community-libs-compat- 8.0.16-2.el7.x86_64.rpm
[root@pc2 ~]# yum localinstall mysql-community-client-8.0.16-2.el7.x86_64.rpm mysql-community-server-8.0.16-2.el7.x86_64.rpm mysql-community-libs-8.0.16-2.el7.x86_64.rpm mysql-community-common-8.0.16-2.el7.x86_64.rpm mysql-community-libs-compat-8.0.16-2.el7.x86_64.rpm
[root@pc2 ~]# yum install http php php-gb php-mysql php-memcache
[root@pc2 ~]# systemctl restart httpd
[root@pc2 ~]# systemctl enable httpd
[root@pc2 ~]# systemctl restart mysql
[root@pc2 ~]# systemctl enable mysql

2、在服务端创建用户:

mysql> create user 'memcache'@'%' identified by 'ABC123.com';
mysql> ALTER USER 'memcache'@'%' IDENTIFIED WITH mysql_native_password BY 'ABC123.com';
mysql> flush privileges;

3、测试http功能:

[root@pc2 ~]# vim /var/www/html/index.html
[root@pc2 ~]# cat /var/www/html/index.html
this is a thml...


4、测试PHP链接功能:

vim /var/www/html/index.php
cat /var/www/html/index.php
<?php
phpinfo();
?>


5、测试mysql:

vim /var/www/html/mysql.php
cat /var/www/html/mysql.php
<?php
link=mysql_connect('192.168.42.181','superadmin','Cloudbu@123');
if(link) echo "<h1>Success!!</h1>";
else echo "Fail!!";
mysql_close();
?>


三、memcache服务配置
1、安装:
(1)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

(2)memcached 稳定版
wget https://memcached.org/files/memcached-1.5.16.tar.gz

tar -zxvf memcached-1.4.5.tar.gz
cd memcached-1.4.5
./configure --prefix=/usr/local/memcached --with-libevent=/usr/local/libevent/
make
make install

2、启动:

[root@pc3 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
[root@pc3 init.d]# ps -ef |grep memcached
root      11018      1  0 17:53 ?        00:00:00 /usr/local/memcached/bin/memcached -d -l 127.0.0.1 -p 11211 -u root -m 64 -c 1024 -p /var/run/memcached.
pid
root      11033  10997  0 17:55 pts/1    00:00:00 grep --color=auto memcached

3、链接测试:

[root@pc3 init.d]# telnet 127.0.0.1 11211 Trying 127.0.0.1...
Connected to 127.0.0.1.
Escape character is '^]'.
stats
...
STAT pid 17143 进程id
STAT uptime 6177628 总的运行时间,秒数

4、测试web和memcache的连通性:

[root@pc3 ~]# /usr/local/memcached/bin/memcached -d -l 0.0.0.0 -p 11211 -u root -m 64 -c 1024 -P /var/run/memcached.pid
[root@pc3 ~]# ps -ef |grep memcached
root 16629 1 0 02:38 ? 00:00:00
/usr/local/memcached/bin/memcached -d -l 0.0.0.0 -p 11211 -u root -m 64 -c 1024
-P /var/run/memcached.pid
root 16640 16561 0 02:38 pts/1 00:00:00 grep --color=auto memcached

5、代码测试:

<?php
$memcache = new Memcache;
$memcache->connect('192.168.224.143', 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);
?>

6、配置session:

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

session.save_handler:设置session的储存方式为memcache。默认以文件方式存取session数据,如果想要使用自定义的处理来存取session数据,比如memcache方式则修为session.save_handler =
memcache
session.save_path:设置session储存的位置,多台memcache用逗号隔开
memcache实现session共享也可以在某个一个应用中设置:
ini_set(“session.save_handler”, “memcache”);
ini_set(“session.save_path”, “tcp://192.168.0.9:11211”);
测试memcache的可用性:

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

7、创建测试数据库:

create database testab1;
use testab1;
create table test1(id int not null auto_increment,name varchar(20) default null,primary key(id)) engine=innodb auto_increment=1 default charset=utf8;
insert into test1(name) values ('tom1'),('tom2'),('tom3'),('tom4'),('tom5');
select * from test1;
grant select on testab1.* to user@'%' identified by '123456';

8、测试memcache是否缓存数据库成功:

<?php
$memcachehost = '192.168.224.143';
$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.224.142","user","123456");
mysql_select_db(testdb1);
$result=mysql_query($query);
while ($row=mysql_fetch_assoc($result))
{ $
arr[]=$row;
} $
f = 'mysql';
$memcache->add($key,serialize($arr),0,30);
$data = $arr ;
} e
lse{
$f = 'memcache';
$data_mem=$memcache->get($key);
$data = unserialize($data_mem);
} e
cho $f;
echo "<br>";
echo "$key";
echo "<br>";
//print_r($data);
foreach($data as $a)
{ e
cho "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>";
}
?>

如果出现mysql表示memcached中没有内容,需要memcached从数据库中取得。
再刷新页面,如果有memcache标志表示这次的数据是从memcached中取得的。
memcached有个缓存时间默认是1分钟,过了一分钟后,memcached需要重新从数据库中取得数据。

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: