一、基础环境配置
1、环境规划
(1)关于分配
memcache: 192.168.142.134
mysql: 192.168.142.110
web:192.168.142.111
按照下面的方法,修改主机名,利于分辨三台机器
(2)同步时间(三台机器都要运行)
[root@mysql ~]# yum install ntp ntpdate -y
[root@mysql ~]# ntpdate cn.pool.ntp.org
[root@mysql ~]# hwclock --systohc
(3)关闭防火墙(三台机器均运行)
2、web服务器配置(安装软件,启动服务)
①、卸载
[root@web mysql]# rpm -e mariadb-libs postfix
②、将MySQL安装时的压缩包放入web机器中
③、创建MySQL目录,并将解压后的全部放入该目录中。
[root@web ~]# tar xf mysql-8.0.16-2.el7.x86_64.rpm-bundle.tar -C mysql
④、安装MySQL的相关包。
[root@web mysql]# 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@web mysql]# yum install http php php-gb php-mysql php-memcache
⑥、启动http
3、MySQL配置(MySQL需提前安装)
4、测试http功能(web上进行测试)
①、 内容:
②、网络上测试:
5、测试PHP链接功能
①、内容:
②、网络上测试:
6、测试mysql
①、内容:
②、网络上测试:
7、memcache服务配置 (提前安装memcache)
(1)安装memcache
①、安装Libvent。
[root@memcache ~]# wget http://monkey.org/~provos/libevent-1.4.14b-stable.tar.gz
[root@memcache ~]# tar zxf libevent-1.4.14b-stable.tar.gz
[root@memcache ~]# yum install make gcc-c++
[root@memcache ~]# cd libevent-1.4.14b-stable
[root@memcache libevent-1.4.14b-stable]# ./configure --prefix=/usr/local/libevent/
[root@memcache libevent-1.4.14b-stable]# make && make install
②、安装memcache。
[root@memcache ~]# wget https://memcached.org/files/memcached-1.5.16.tar.gz
[root@memcache ~]# tar zxf memcached-1.5.16.tar.gz
[root@memcache ~]# cd memcacged-1.5.16.tar.gz
[root@memcache memcached-1.5.16]# ./configure --prefix=/usr/local/memcached --with libevent=/usr/local/libevent/
[root@memcache memcached-1.5.16]# make && make install
(2)功能测试
①、修改客户端监听所有地址
[root@memcache ~]# /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@memcache ~]# ps -ef | grep memcached
root 21282 1 7 11:51 ? 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 21293 8183 0 11:51 pts/0 00:00:00 grep --color=auto memcached
②、测试代码
③、代码测试(错误,一行未输出)
8、测试memcache的可用性
①、配置session:
[root@web html]# vim /etc/php.ini
session.save_handle = memcache
session.save_path = "tcp://192.168.142.112:11211?persistent=1&weight=1&timeout=1&retry_interval=15"
②、测试代码:
③、测试结果:
9、测试memcache是否缓存数据库成功
①、创建测试数据库
mysql> create database testab1;
mysql> use testab1;
mysql> 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;
mysql> insert into test1(name) values ('tom1'),('tom2'),('tom3'),('tom4'),('tom5');
mysql> select * from test1;
+----+------+
| id | name |
+----+------+
| 1 | tom1 |
| 2 | tom2 |
| 3 | tom3 |
| 4 | tom4 |
| 5 | tom5 |
+----+------+
mysql> grant select on testab1.* to memcache@'%';
②、测试代码:
③、测试结果:
(注:第一次显示为MySQL,后来刷新为memcache,等待一段时间后重新显示MySQL)
即: