zoukankan      html  css  js  c++  java
  • memcached随笔练习

    实验环境:

    RHEL 6.5 (已关闭selinux,iptables)

    首先部署LNMP环境,该步骤采用源码编译安装

    安装Nginx-1.8.0

    准备软件包:nginx-1.8.0.tar.gz

    下载地址:

    http://nginx.org/en/download.html 官方网址

    http://mirrors.sohu.com/nginx/(搜狐开源镜像站)

    [root@slave-147 ~]# mkdir tar      //存放tar包文件

    [root@slave-147 ~]# cd tar/

    安装依赖包

    [root@slave-147 ~]# yum install -y gcc gcc-c++ autoconf automake zlib zlib-devel openssl openssl-devel -y
    
    [root@slave-147 ~]# cd tar
    
    [root@slave-147 tar]# tar -xf pcre-8.37.tar.bz2     //只需要解压出来就好
    
    [root@slave-147 tar]# tar -xf nginx-1.8.0.tar.gz        //解压
    
    [root@slave-147 tar]# cd nginx-1.8.0
    [root@slave-147 nginx-1.8.0]# ./configure --prefix=/usr/local/nginx 
    
    --with-http_dav_module 
    
    --with-http_stub_status_module 
    
    --with-http_addition_module 
    
    --with-http_sub_module 
    
    --with-http_flv_module 
    
    --with-http_mp4_module 
    
    --with-pcre=/root/tar/pcre-8.37
    [root@slave-147 nginx-1.8.0]# make -j 4 && make install
    
    配置nginx
    
    [root@slave-147 nginx-1.8.0]# vim /usr/local/nginx/conf/nginx.conf
    
    将#user  nobody;
    
    改为 user  nginx nginx;
    
    然后将
    location ~ .php$ {
                 root           html;
                 fastcgi_pass   127.0.0.1:9000;
                 fastcgi_index  index.php;
                 fastcgi_param  SCRIPT_FILENAME  /script$fastcgi_script_name;
                 include        fastcgi_params;
             }
    
    改为
    location ~ .php$ {
                 root           html;
                 fastcgi_pass   127.0.0.1:9000;
                 fastcgi_index  index.php;
                 fastcgi_param  SCRIPT_FILENAME  /usr/local/nginx/html$fastcgi_script_name;
                 include        fastcgi_params;
             }
    保存退出!
    
    [root@slave-147 nginx-1.8.0]# /usr/local/nginx/sbin/nginx   //启动测试
    
    [root@slave-147 nginx-1.8.0]# yum install -y elinks
    
    [root@slave-147 nginx-1.8.0]# elinks 127.1 --dump   //看到如下信息证明nginx没问题
                                    Welcome to nginx!
    
       If you see this page, the nginx web server is successfully installed and
        working. Further configuration is required.
    
       For online documentation and support please refer to [1]nginx.org.
        Commercial support is available at [2]nginx.com.
    
       Thank you for using nginx.
    
    References
    
       Visible links
        1. http://nginx.org/
        2. http://nginx.com

    安装mysql

    准备软件包:mysql-5.6.26.tar.gz

    下载地址:

    http://ftp.ntu.edu.tw/MySQL/Downloads/MySQL-5.6/      //以前下载还有5.6.26,现在没有了

    http://dev.mysql.com/get/Downloads/MySQL-5.6/mysql-5.6.26.tar.gz  //   官网下载地址

    http://mirrors.sohu.com/mysql/MySQL-5.6/     //搜狐之前也有的,现在没有了

    开始解压安装

    [root@slave-147 tar]# tar -xf mysql-5.6.26.tar.gz
    [root@slave-147 tar]# cd mysql-5.6.26
    [root@slave-147 mysql-5.6.26]# useradd -M -u 8000 -s /sbin/nologin mysql 
    [root@slave-147 mysql-5.6.26]# cmake 
    -DMAKE_INSTALL_PREFIX=/usr/local/mysql 
    -DMYSQL_UNIX_ADDR=/tmp/mysql.sock 
    -DDEFAULT_CHARSET=utf8 
    -DDEFAULT_COLLATION=utf8_general_ci 
    -DWITH_EXTRA_CHARSETS=all 
    -DWITH_MYISAM_STORAGE_ENGINE=1 
    -DWITH_INNOBASE_STORAGE_ENGINE=1 
    -DWITH_MEMORY_STORAGE_ENGINE=1 
    -DWITH_READLINE=1 
    -DENABLED_LOCAL_INFILE=1 
    -DMYSQL_DATADIR=/usr/local/mysql/data 
    -DMYSQL_USER=mysql
    [root@slave-147 mysql-5.6.26]# make -j 4 && make install
    [root@slave-147 mysql-5.6.26]# chown -R mysql:mysql /usr/local/mysql/

    开始配置MySQL

    [root@slave-147 mysql-5.6.26]# cd /usr/local/mysql/support-files/
    [root@slave-147 mysql-5.6.26]# mv /etc/my.cnf /etc/my.cnf.bak
    [root@slave-147 mysql-5.6.26]# cp my-default.cnf /etc/my.cnf
    [root@slave-147 mysql-5.6.26]# cp mysql.server /etc/init.d/mysql5.6
    [root@slave-147 mysql-5.6.26]# vim /etc/init.d/mysql5.6   //找到basedir和datadir修改为日如下(路径要根据自己的实际情况而定)

    basedir=/usr/local/mysql datadir=/usr/local/mysql/data [root@slave-147 mysql-5.6.26]# ./mysql_install_db --defaults-file=/etc/my.cnf --basedir=/usr/local/mysql --datadir=/usr/local/mysql/data/ --user=mysql [root@slave-147 mysql-5.6.26]# ln -s /usr/local/mysql/bin/* /bin/ [root@slave-147 mysql-5.6.26]# service mysql5.6 start [root@slave-147 mysql-5.6.26]# vim /usr/local/mysql/bin/mysql_secure_installation [root@slave-147 mysql-5.6.26]# mkdir /var/lib/mysql [root@slave-147 mysql-5.6.26]# mysql_secure_installation //安全初始化,设置数据库root密码及其相关选项,按照自己需求而定,可以输入123456,回车,再次输入123456,然后接下来的选项直接输入yes回车 [root@slave-147 mysql-5.6.26]# ln -s /tmp/mysql.sock /var/lib/mysql/mysql.sock [root@slave-147 mysql-5.6.26]# mysql -uroot -p123456 //弹出如下信息,证明数据库正常 Welcome to the MySQL monitor. Commands end with ; or g. Your MySQL connection id is 1 Server version: 5.6.26 Source distribution Copyright (c) 2000, 2013, Oracle and/or its affiliates. All rights reserved. Oracle is a registered trademark of Oracle Corporation and/or its affiliates. Other names may be trademarks of their respective owners. Type 'help;' or 'h' for help. Type 'c' to clear the current input statement. mysql> q Bye

    安装PHP

    [root@slave-147 mysql-5.6.26]# cd /root/tar/
    [root@slave-147 tar]# tar -xf libmcrypt-2.5.8.tar.bz2 
    [root@slave-147 tar]# cd libmcrypt-2.5.8
    [root@slave-147 libmcrypt-2.5.8]# ./configure --prefix=/usr/local/libmcrypt
    [root@slave-147 libmcrypt-2.5.8]# make -j 4
    [root@slave-147 libmcrypt-2.5.8]# make install

    解决依赖:

    [root@slave-147 libmcrypt-2.5.8]#yum install -y php-pear
    [root@slave-147 libmcrypt-2.5.8]#vim /etc/ld.so.conf     //添加如下两行内容
    /usr/local/libmcrypt/lib
    /usr/local/mysql/lib
    [root@slave-147 libmcrypt-2.5.8]# ldconfig
    [root@slave-147 libmcrypt-2.5.8]# yum install -y libxml2-devel libcurl-devel libjpeg-devel libpng-devel freetype freetype-devel
    [root@slave-147 libmcrypt-2.5.8]# tar -xf php-5.6.13.tar.bz2 
    [root@slave-147 libmcrypt-2.5.8]# cd php-5.6.13
    [root@slave-147 php-5.6.13]#./configure 
    --prefix=/usr/local/php 
    --with-config-file-path=/usr/local/php 
    --with-mysql=/usr/local/mysql 
    --with-mysqli=/usr/local/mysql/bin/mysql_config 
    --with-iconv-dir 
    --with-freetype-dir 
    --with-jpeg-dir 
    --with-png-dir 
    --with-zlib 
    --with-libxml-dir=/usr 
    --enable-xml 
    --disable-rpath 
    --enable-bcmath 
    --enable-shmop 
    --enable-sysvsem 
    --enable-inline-optimization 
    --with-curl 
    --enable-mbregex 
    --enable-fpm 
    --enable-mbstring 
    --with-gd 
    --enable-gd-native-ttf 
    --with-openssl 
    --with-mhash 
    --enable-pcntl 
    --enable-sockets 
    --with-xmlrpc 
    --enable-zip 
    --enable-soap 
    --with-mcrypt=/usr/local/libmcrypt
    [root@slave-147 php-5.6.13]# make -j 4 && make install
    [root@slave-147 php-5.6.13]# cd /usr/local/php/etc/
    [root@slave-147 php-5.6.13]# cp php-fpm.conf.default php-fpm.conf
    [root@slave-147 php-5.6.13]# cd /root/tar/php-5.6.13
    [root@slave-147 php-5.6.13]# cp sapi/fpm/init.d.php-fpm /etc/init.d/php-fpm
    [root@slave-147 php-5.6.13]# cp php.ini-production /usr/local/php/php.ini
    [root@slave-147 php-5.6.13]# chmod +x /etc/init.d/php-fpm
    [root@slave-147 php-5.6.13]# echo "<?php phpinfo(); ?>" > /usr/local/nginx/html/index.php //创建测试页面
    [root@slave-147 php-5.6.13]# /etc/init.d/php-fpm start
    [root@slave-147 php-5.6.13]# /usr/local/nginx/sbin/nginx -s reload
    [root@slave-147 php-5.6.13]# firefox 192.168.75.147/index.php  //弹出如下页面证明php正常

     image

    安装libevent

    [root@slave-147 php-5.6.13]# cd ../
    [root@slave-147 tar]# tar -xf libevent-2.0.22-stable.tar.g
    [root@slave-147 tar]# cd libevent-2.0.22-stable
    [root@slave-147 libevent-2.0.22-stable]# ./configure --prefix=/usr/local/libevent
    [root@slave-147 libevent-2.0.22-stable]# make -j 4 && make install

    安装memcached

    准备软件包:

    memcache-2.2.7.tgz

    memcached-1.4.25.tar.tar

    相关软件下载地址:

    http://pecl.php.net/package/memcache                               // memcached-1.4.25.tar.tar
    http://libevent.org/                                                                    // libevent-2.0.22-stable.tar.gz
    http://www.memcached.org/                                                  // memcache-2.2.7.tgz
    http://php.net/downloads.php                                              // php-5.6.13.tar.bz2
    http://mcrypt.hellug.gr/index.html                                      // libmcrypt-2.5.8.tar.bz2
    http://pcre.org/                                                                        // pcre-8.37.tar.bz2

    [root@slave-147 libevent-2.0.22-stable]# cd ../
    [root@slave-147 tar]# tar -xf memcache-2.2.7.tgz
    [root@slave-147 tar]# cd memcached-1.4.25
    [root@slave-147 memcached-1.4.25]# ./configure 
    --prefix=/usr/local/memcached 
    --with-libevent=/usr/local/libevent/
    [root@slave-147 memcached-1.4.25]# make -j 4 && make install
    [root@slave-147 memcached-1.4.25]# /usr/local/memcached/bin/memcached                                                         //-m 内存缓存大小 ;-c 最大并发 ; -d 作为守护进程在后台启动
    -u root -p 11211 -l 192.168.75.147 -P /var/run/memcached.pid -m 128m -c 2048 -d
    [root@slave-147 memcached-1.4.25]# echo "/usr/local/memcached/bin/memcached -u root -p 11211 -l       //设置开机启动
    192.168.75.147 -P /var/run/memcached.pid -m 128m -c 2048 -d" >> /etc/rc.local
    [root@slave-147 memcached-1.4.25]# netstat -antup | grep 11211
    tcp        0      0 192.168.75.147:11211        0.0.0.0:*                   LISTEN      80047/memcached     
    udp        0      0 192.168.75.147:11211        0.0.0.0:*                               80047/memcached 
    [root@slave-147 memcached-1.4.25]# yum install -y telnet
    测试:(第一次测试的时候,晕圈了,没有按照设置的字符串长度来设值,所以报错了)
    [root@slave-147 ~]# telnet 192.168.75.147 11211
    Trying 192.168.75.147...
    Connected to 192.168.75.147.
    Escape character is '^]'.
    set name 4 300 6                                 #赋值格式:set key flag exptime bytes,这里设置name的值为Mkkkkkkkkkkkk,过期时间为300秒,字符串长度6,flag是一个无符号整数标记,这里可以随便给值
    Mkkkkkkkkkkkk
    CLIENT_ERROR bad data chunk
    ERROR
    
    set sishen 2 10 5
    12345
    STORED                                   //STORED表示存储成功
    get sishen
    VALUE sishen 2 5
    12345
    END
    get sishen                              //时间超过设置期限值时,就会自动删除,所以get不到值
    END
    quit                                         //退出
    [root@slave-147 ~]# /usr/local/memcached/bin/memcached –h    可以查看memcached的帮助信息

    安装memcache-2.2.7

    [root@slave-147 memcached-1.4.25]# cd ..
    [root@slave-147 tar]# tar -xf memcache-2.2.7.tgz
    [root@slave-147 tar]# cd memcache-2.2.7
    [root@slave-147 memcache-2.2.7]# /usr/local/php/bin/phpize 
    Configuring for:
    PHP Api Version:         20131106
    Zend Module Api No:      20131226
    Zend Extension Api No:   220131226
    [root@slave-147 memcache-2.2.7]# ./configure 
    > --enable-memcache 
    > --with-php-config=/usr/local/php/bin/php-config
    [root@slave-147 memcache-2.2.7]# make && make install
    [root@slave-147 memcache-2.2.7]# ls /usr/local/php/lib/php/extensions/no-debug-non-zts-20131226/
    memcache.so  opcache.a  opcache.so                                                                           //编译出来的模块
    [root@slave-147 memcache-2.2.7]# vim /usr/local/php/php.ini
    734 ; extension_dir = "./"                             //在734行下面添加如下两行内容
      735 ;       extension_dir='/usr/local/php/lib/php/extensions/no-debug-non-zts-2     0131226/'
      736 extension=memcache.so
    [root@slave-147 memcache-2.2.7]# /etc/init.d/php-fpm restart
    Gracefully shutting down php-fpm  done
    Starting php-fpm  done
    [root@slave-147 memcache-2.2.7]# /usr/local/nginx/sbin/nginx -s reload
    [root@slave-147 memcache-2.2.7]# firefox 192.168.75.147/index.php

    image

    测试memcache连接memcached

    [root@slave-147 memcache-2.2.7]# cd
    [root@slave-147 ~]# vim /usr/local/nginx/html/mem.php
    <?php
    $memcache = memcache_connect('192.168.75.147',11211);
    $memcache->set('test','hello world !',0,12);
    $val = $memcache->get('test');
    echo $val;
    ?>

    image

    测试memcached缓存数据库

    [root@slave-147 ~]# service mysql5.6 restart 
    Shutting down MySQL.                                       [  OK  ]
    Starting MySQL.                                            [  OK  ]
    [root@slave-147 ~]# seq 1 9999 > /tmp/sum   //seq命令,用于产生一组连续的有序数列
    [root@slave-147 ~]# cat /tmp/sum
    
    1
    2
    3
    4
    
    ………………
    9995
    9996
    9997
    9998
    9999
    [root@slave-147 ~]# mysql -uroot -p123456 -e "create database db01"
    [root@slave-147 ~]# mysql -uroot -p123456 -e "use db01;create table T1(ID int)ENGINE=innodb"
    [root@slave-147 ~]# mysql -uroot -p123456 -e "use db01;load data infile '/tmp/sum' into table T1"
    
    创建测试页面:
    
    [root@slave-147 html]# vim test.html
    
    <html>
    <head>
    <title> Memcache study test </title>
    </head>
    <body>
    <h1 align="center"> Welcome to sishen.xueji.com </h1>
    <br />
    <br />
    <a href=./read.html>Read Test</a>
    <br /> <br />
    <a href=./write.html> Write test</a>
    </body>
    </html>
    
    [root@slave-147 html]# vim read.html
    
    <html>
    <head>
    <title> Memcached Read Test </title>
    </head>
    <body>
    <h1 align="center"> Memcached Read Test Page </h1>
    <br />
    <br />
    <h3>please input a number</h3>
    <form action="read.php" method="post">
    NUM:
    <input type="text" name="num" />
    <input type="submit" value="Submit" />
    </form>
    </body>
    </html>
    
    [root@slave-147 html]# vim write.html
    
    <html>
    <head>
    <title> Memcached Wead Test </title>
    </head>
    <body>
    <h1 align="center"> Memcached Write Test Page </h1>
    <br />
    <br />
    <h3>please input a number and click submit</h3>
    <form action="write.php" method="post">
    NUM:
    <input type="text" name="num" />
    <input type="submit" value="Submit" />
    </form>
    </body>
    </html
    
    [root@slave-147 html]# vim read.php
    
    <?php
    $memcachehost = '192.168.75.147';
    $memcacheport = 11211;
    $memcachelife = 60;
    $memcache = new Memcache;
    $memcache->connect($memcachehost,$memcacheport) or die ("Could not connect");
    $num=$_POST["num"];
    $db=db01;
    $tb=T1;
    $query="select * from $tb where ID=$num";
    #$key=md5($query);
    $key=md5($num);
    if(!$memcache->get($key))
    {
    $conn=mysql_connect("127.0.0.1","root","123456");
    mysql_select_db($db);
    $result=mysql_query($query);
    # echo "mysql $num";
    while ($row=mysql_fetch_assoc($result))
    {
    $arr[]=$row;
    }
    $f = 'mysql';
    $memcache->add($key,serialize($arr),0,30); //mysql 查询后,插入 memcached
    $data = $arr ;
    }
    else{
    $f = 'memcache';
    
    $data_mem=$memcache->get($key);
    $data = unserialize($data_mem);
    }
    echo "$f $num";
    echo "key is $key";
    echo "<br>";
    ?>
    
    [root@slave-147 html]# vim write.php
    <?php
    $memcachehost = '192.168.75.147';
    $memcacheport = 11211;
    $memcachelife = 60;
    $memcache = new Memcache;
    
    $memcache->connect($memcachehost,$memcacheport) or die ("Could not connect");
    $num=$_POST["num"];
    $db=db01;
    $tb=T1;
    $query="insert into $tb values($num)";
    #$key=md5($query);
    $key=md5($num);
    if(!$memcache->get($key))
    {
    $conn=mysql_connect("192.168.75.147","root","123456");
    mysql_select_db($db);
    $result=mysql_query($query);
    while ($row=mysql_fetch_assoc($result))
    {
    $arr[]=$row;
    }
    $f = 'mysql';
    $memcache->add($key,serialize($arr),0,30); //mysql 插入成功后,插入 memcached
    $data = $arr ;
    #}
    #else{
    $f1 = 'memcache';
    $data_mem=$memcache->get($key);
    $data = unserialize($data_mem);
    }
    echo "$f $f1 $num";
    echo "<br>";
    ?>
    [root@slave-147 html]# /usr/local/nginx/sbin/nginx -s reload
    [root@slave-147 html]# firefox 192.168.75.147/mysql_connect.php

    image

    测试memcache

    [root@slave-147 html]# firefox 192.168.75.147/test.html

    image

    点击Read Test输入一个存在的字段查询

    image

    点击Submit,结果如下

    image

    写测试:

    输入一个测试值:

    image

    image

    去数据库查看插入的数据

    image

    查询成功~

  • 相关阅读:
    变量在原型链中的查找顺序
    new 运算符干了什么
    一道关于变量升级问题的题目
    【c#.Net】c#.Net基础入门(数组和循环)
    【C#.Net】c#.Net基础入门(运算符和分支结构)
    【Python】【Django】查询所有学生信息
    【Python】【Django】admin后台管理类操作数据库
    【Python】【Django】用户注册功能
    【Python】【Django】PythonWeb框架基本知识
    【转】【接口测试】接口测试与postman
  • 原文地址:https://www.cnblogs.com/zd520pyx1314/p/8665849.html
Copyright © 2011-2022 走看看