linux memcache installation configuration method

  • 2020-05-09 19:46:42
  • OfStack

Basic principles:

When the client accesses the application program for the first time, it will fetch the data from the database (RDBMS) and return it to the client. The retrieved data is also saved to memcached.
The second access, because the data has been cached, you do not have to go to the database query, directly from memcached.

RDBMS is a file-based database that is ultimately saved as a file on disk. memcached, on the other hand, is a relational database of key:value, which is stored in memory. Memory reads and writes much faster than disk reads and writes, which is 10 to the sixth times faster.

memcached is event handling based on libevent. libevent is a program library, which encapsulates Linux's epoll, BSD's kqueue and other event handling functions into a unified interface. The performance of O(1) can be achieved even if the number of connections to the server is increased. memcached USES the libevent library, so it can perform well on operating systems like Linux, BSD, Solaris, and so on. For details of event handling, please refer to The C10K Problem of Dan Kegel. Learn more about libevent: http:// monkey. org/~provos/libevent/.

Compile and install Memcached

1. Since memcached is based on libevent, you need to install libevent, libevent-devel

# yum install libevent libevent-devel -y

2. Download and unzip memcached-1.4.6.tar.gz

The official memcached website is: http:// memcached.org /
# tar -xvzf memcached-1.4.6.tar.gz

3. Compile and install memcached-1.4.6

# cd memcached-1.4.6
# ./configure --prefix=/etc/memcached
# make
# make install

4. Configure environment variables (this step can be ignored...)

Enter the user host directory and edit.bash_profile. Add a new directory for the system environment variable LD_LIBRARY_PATH.
# vi .bash_profile
MEMCACHED_HOME=/etc/memcached
export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:$MEMCACHED_HOME/lib
Refresh the user environment variable: # source.bash_profile

5. Write stop/start scripts for memcached service

# cd /etc/init.d
vi memcached, the script is as follows:


#!/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"		  #  Bind listening IP address 
	port="11211"					  #  Service port 
	username="root"				 #  The user identity of the running program 
	max_memory=64				  # default: 64M |  Maximum memory usage 
	max_simul_conn=1024			 # default: 1024 |  Maximum number of simultaneous connections 
	#maxcon=51200
	#growth_factor=1.3			 # default: 1.25 |  Block size growth factor 
	#thread_num=6				  # default: 4
	#verbose="-vv"				  #  See startup details 
	#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. Grant execution authority
#chmod +x memcached

7. Set memcached to start with the system

# chkconfig --add memcached
# chkconfig --level 35 memcached on
Start the memcached
# service memcached start
// when you start, you actually call the following command, starting memcached as a daemon
/etc/memcached/bin/memcached -d -m 64 -u root -l 192.168.1.201 \
-p 11211 -c 1024 -P /etc/memcached/memcached.pid

See if memcached is started
# ps -ef | grep memcached

Install the PHP extension for Memcache

1. In http: / / pecl php. net package/memcache memcache choose corresponding to download version.

2. Install the memcache extension for PHP

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. After the above installation, there will be a prompt like this:

Installing shared extensions: /usr/local/php/lib/php/extensions/no-debug-non-zts-20060613/

4. Change extension_dir = "./ "in php.ini to

extension_dir = "/ usr local/php/lib/php/extensions/no - debug - non - zts - 20060613 /"

5. Add 1 line to load the memcache extension: extension= memcache.so

Next, restart php and you can view it from the phpinfo test page


Related articles: