192.168.42.186 :memcache
192.168.42.187 :web
192.168.42.18:mysql
一、时间同步
二、关闭防火墙、selinux、卸载mariadb
systemctl stop firewalld
systemctl disable firewalld
sed -i 's/SELINUX=enforcing/SELINUX=disabled/' /etc/selinux/config setenforce 0
rpm -e mariadb-libs postfix
三、配置web服务器
1、安装mysql
1、下载mysql包
wget http://repo.mysql.com/mysql-community-release-el7-5.noarch.rpm
rpm -ivh mysql-community-release-el7-5.noarch.rpm
yum update
3、装载mysql服务端客户端
yum install mysql-server mysql-client -y
4、权限设置
chown mysql:mysql -R /var/lib/mysql
5、数据库初始化
mysqld --initialize
6、开启数据库
systemctl start mysqld
7、查看启动状态
systemctl status mysqld
8、创建mysql用户备用
mysql> create user 'memcache'@'%' identified by 'Cloudbu@123';
mysql> ALTER USER 'memcache'@'%' IDENTIFIED WITH mysql_native_password BY 'Cloudbu@123'; mysql> flush privileges;
2、安装apache
yum install http php php-gb php-mysql php-memcache
systemctl restart httpd
3、web端测试
测试apache
[root@localhost ~]# vim /var/www/html/index.html
[root@localhost ~]# cat /var/www/html/index.html
this is a test form 187..
测试php
vim /var/www/html/index.php
cat /var/www/html/index.php
<?
php phpinfo();
?>
四、配置MySQL服务器 (同上)
五、测试mysql与php链接(在web服务器)
编写mysql.php测试脚本,浏览器测试
[root@web html]# more mysql.php
<?php
$link=mysql_connect('192.168.214.129','memcache','Cloudbu@123');
if($link) echo "<h1>Success!!</h1>";
else echo "Fail!!";
mysql_close();
?>
六、配置memcache服务器
yum install make gcc gcc-c++ -y
1、配置libevent库
wget http://monkey.org/~provos/libevent-1.4.14b-stable.tar.gz
tar zxvf libevent-1.4.14b-stable.tar.gz
cd libevent-1.4.14b-stable
./configure --prefix=/usr/local/libevent/
make
make install
2、配置memcache
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
3、启动memcache
[root@localhost ~]# /usr/local/memcached/bin/memcached -d -l 0.0.0.0 -p 11211 -u root -m 64 -c 1024
[root@memcache ~]# ps -ef | grep memcache
root 57656 1 0 13:24 ? 00:00:01 /usr/local/memcached/bin/memcached -d -l 0.0.0.0 -p 11211 -u root -m 64 -c 1024
root 59240 1800 0 15:39 pts/0 00:00:00 grep --color=auto memcache
七、测试memcache与php链接
编写memcache.php测试脚本,浏览器测试
[root@web html]# more memcache.php
<?php
$memcache = new Memcache;
$memcache->connect('192.168.214.130', 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);
?>
八、配置session
1、配置session
vim /etc/php.ini
session.save_handler = memcache
session.save_path = "tcp://192.168.42.145:11211"
2、重启apache,编写测试脚本,浏览器测试
[root@web html]# more session.php
<?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 />";
?>
九、创建数据库测试
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;
编写测试脚本,浏览器测试
[root@web html]# vim test.php
<?php
$memcachehost = '192.168.214.130';
$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.214.129","memcache","Cloudbu@123");
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>";
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>";
}
?>