


How to install and configure memcache under Linux
基本原理:
① 客户端第一次访问应用程序时,会到数据库(rdbms)中取出数据,返回给客户端;同时也将取出的数据保存到memcached中。
② 第二次访问时,因为数据已经缓存,就不用去数据库查询了,直接从memcached中取。
rdbms是文件型的数据库,最终还是以文件的形式保存在磁盘上;而memcached则不一样,它是key:value关系型的数据库,是保存在内存中的。内存的读写速度要比磁盘的读写速度快得多,前者是后者的10的6次方倍。
memcached是基于libevent的事件处理。libevent是个程序库,它将linux的epoll、bsd类操作系统的kqueue等事件处理功能封装成统一的接口。即使对服务器的连接数增加,也能发挥o(1)的性能。 memcached使用这个libevent库,因此能在linux、bsd、solaris等操作系统上发挥其高性能。关于事件处理这里就不再详细介绍,可以参考dan kegel的the c10k problem。
编译安装memcached
1、由于memcached是基于libevent的,因此需要安装libevent,libevent-devel
# yum install libevent libevent-devel -y
2、下载并解压memcached-1.4.6.tar.gz
memcached官方网站是:
# tar -xvzf memcached-1.4.6.tar.gz
3、编译安装memcached-1.4.6
# cd memcached-1.4.6
# ./configure --prefix=/etc/memcached
# make
# make install
4、配置环境变量(这一步可忽略...)
进入用户宿主目录,编辑.bash_profile,为系统环境变量ld_library_path增加新的目录,需要增加的内容如下:
# vi .bash_profile
memcached_home=/etc/memcached
export ld_library_path=$ld_library_path:$memcached_home/lib
刷新用户环境变量:# source .bash_profile
5、编写memcached服务启停脚本
# cd /etc/init.d
vi memcached,脚本内容如下:
#!/bin/sh # # startup script for the server of memcached # # processname: memcached # pidfile: /etc/memcached/memcached.pid # logfile: /etc/memcached/memcached_log.txt # memcached_home: /etc/memcached # chkconfig: 35 21 79 # description: start and stop memcached service # source function library . /etc/rc.d/init.d/functions retval=0 prog="memcached" basedir=/etc/memcached cmd=${basedir}/bin/memcached pidfile="$basedir/${prog}.pid" #logfile="$basedir/memcached_log.txt" ipaddr="192.168.1.200" # 绑定侦听的ip地址 port="11211" # 服务端口 username="root" # 运行程序的用户身份 max_memory=64 # default: 64m | 最大使用内存 max_simul_conn=1024 # default: 1024 | 最大同时连接数 #maxcon=51200 #growth_factor=1.3 # default: 1.25 | 块大小增长因子 #thread_num=6 # default: 4 #verbose="-vv" # 查看详细启动信息 #bind_protocol=binary # ascii, binary, or auto (default) start() { echo -n $"starting service: $prog" $cmd -d -m $max_memory -u $username -l $ipaddr -p $port -c $max_simul_conn -p $pidfile retval=$? echo [ $retval -eq 0 ] && touch /var/lock/subsys/$prog } stop() { echo -n $"stopping service: $prog " run_user=`whoami` pidlist=`ps -ef | grep $run_user | grep memcached | grep -v grep | awk '{print($2)}'` for pid in $pidlist do # echo "pid=$pid" kill -9 $pid if [ $? -ne 0 ]; then return 1 fi done retval=$? echo [ $retval -eq 0 ] && rm -f /var/lock/subsys/$prog } # see how we were called. case "$1" in start) start ;; stop) stop ;; #reload) # reload # ;; restart) stop start ;; #condrestart) # if [ -f /var/lock/subsys/$prog ]; then # stop # start # fi # ;; status) status memcached ;; *) echo "usage: $0 {start|stop|restart|status}" exit 1 esac exit $retval
6、赋予执行权限
#chmod +x memcached
7、设置memcached随系统启动
# chkconfig --add memcached
# chkconfig --level 35 memcached on
启动memcached
# service memcached start
//启动的时候实际上是调用了下面的这个命令,以守护进程的方式来启动memcached
/etc/memcached/bin/memcached -d -m 64 -u root -l 192.168.1.201 \
-p 11211 -c 1024 -p /etc/memcached/memcached.pid
查看memcached是否启动
# ps -ef | grep memcached
安装memcache的php扩展
1.在 选择相应想要下载的memcache版本。
2.安装php的memcache扩展
tar vxzf memcache-2.2.5.tgz
cd memcache-2.2.5
/usr/local/php/bin/phpize
./configure --enable-memcache --with-php-config=/usr/local/php/bin/php-config --with-zlib-dir
make
make install
3.上述安装完后会有类似这样的提示:
installing shared extensions: /usr/local/php/lib/php/extensions/no-debug-non-zts-20060613/
4.把php.ini中的extension_dir = “./”修改为
extension_dir = “/usr/local/php/lib/php/extensions/no-debug-non-zts-20060613/”
5.添加一行来载入memcache扩展:extension=memcache.so
接下来重启php就可以了,可以通过phpinfo测试页面查看
The above is the detailed content of How to install and configure memcache under Linux. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

VS Code system requirements: Operating system: Windows 10 and above, macOS 10.12 and above, Linux distribution processor: minimum 1.6 GHz, recommended 2.0 GHz and above memory: minimum 512 MB, recommended 4 GB and above storage space: minimum 250 MB, recommended 1 GB and above other requirements: stable network connection, Xorg/Wayland (Linux)

The five basic components of the Linux system are: 1. Kernel, 2. System library, 3. System utilities, 4. Graphical user interface, 5. Applications. The kernel manages hardware resources, the system library provides precompiled functions, system utilities are used for system management, the GUI provides visual interaction, and applications use these components to implement functions.

vscode built-in terminal is a development tool that allows running commands and scripts within the editor to simplify the development process. How to use vscode terminal: Open the terminal with the shortcut key (Ctrl/Cmd). Enter a command or run the script. Use hotkeys (such as Ctrl L to clear the terminal). Change the working directory (such as the cd command). Advanced features include debug mode, automatic code snippet completion, and interactive command history.

To view the Git repository address, perform the following steps: 1. Open the command line and navigate to the repository directory; 2. Run the "git remote -v" command; 3. View the repository name in the output and its corresponding address.

Although Notepad cannot run Java code directly, it can be achieved by using other tools: using the command line compiler (javac) to generate a bytecode file (filename.class). Use the Java interpreter (java) to interpret bytecode, execute the code, and output the result.

Writing code in Visual Studio Code (VSCode) is simple and easy to use. Just install VSCode, create a project, select a language, create a file, write code, save and run it. The advantages of VSCode include cross-platform, free and open source, powerful features, rich extensions, and lightweight and fast.

The main uses of Linux include: 1. Server operating system, 2. Embedded system, 3. Desktop operating system, 4. Development and testing environment. Linux excels in these areas, providing stability, security and efficient development tools.

Causes and solutions for the VS Code terminal commands not available: The necessary tools are not installed (Windows: WSL; macOS: Xcode command line tools) Path configuration is wrong (add executable files to PATH environment variables) Permission issues (run VS Code as administrator) Firewall or proxy restrictions (check settings, unrestrictions) Terminal settings are incorrect (enable use of external terminals) VS Code installation is corrupt (reinstall or update) Terminal configuration is incompatible (try different terminal types or commands) Specific environment variables are missing (set necessary environment variables)
