Install memcached in linux, linuxmemcached

Source: Internet
Author: User
Tags php memcached

Install memcached in linux, linuxmemcached
System image and environment requirements:

1) For windows series and developer-related tutorials, refer to section 1.0 to start installation steps.

2) For Centos 6 series and Aliyun Linux 6 series and later versions, refer to section 2.0 to start installation.

3) For Centos 5 series and Aliyun Linux 5 Series, refer to section 3.0 to start installation.

4) for Ubuntu Debian and other series versions, see section 4.0 installation steps.

Note: If you already have a php memcache environment, pay attention to the prompts In the tutorial to avoid overwriting the production environment and making your business unavailable, back up the environment before upgrading and re-compiling the environment!

1.0 Installation Steps

If the standard php memcached extension cannot be set up successfully, you can consider using a manual package to access OCS. For the connection method, refer to the following link. The sample code is very simple, the difference between memcached and php is that it only supports mainstream interfaces. You need to add some specific interfaces by yourself. The installation and usage methods are as follows:

Https://github.com/ronnywang/PHPMemcacheSASL

2.0 Installation Steps

Centos and Aliyun Linux 6 Series

First, check whether gcc-c ++ and other components are installed. If not, run the command,

yum  install gcc+ gcc-c++

Use gcc-v to check whether the GCC version is 4.2 or above. (You must install GCC 4.2 or later]

1) run the rpm-qa | grep php Command to check whether the PHP environment exists in the system. If not, install it. If PHP is available, do not install it. We recommend that you use php5.3 and later versions. For some PHP5.2 versions, the zend_parse_parameters_none function may cause errors in the source code. For details, refer to the official php documentation [PHP with source code compilation]

yum install php-devel,php-common,php-cli  

[If the source code is compiled, follow the official php compilation and upgrade method]

2) install SASL-related environments

yum install cyrus-sasl-plain cyrus-sasl  cyrus-sasl-devel  cyrus-sasl-lib

[Check whether these packages have been installed first. If yes, you do not need to install them]

3) install the source package libmemcached [Recommended version of libmemcached-1.0.16]

Check whether these packages have been installed, including the source package. If yes, you do not need to install these packages.

wget https://launchpad.net/libmemcached/1.0/1.0.16/+download/libmemcached-1.0.16.tar.gz tar zxvf libmemcached-1.0.16.tar.gzcd libmemcached-1.0.16./configure --prefix=/usr/local/libmemcached --enable-saslmakemake installcd ..

4) install the source package memcached [Recommended version for memcached-2.2.0]

Before installing MEMCACHED, check whether the zlib-devel package is not required.

yum install zlib-devel

Check whether the memcached client package (including the source package) has been installed. If yes, you do not need to install it, but you need to re-compile and add the-enable-memcached-sasl extension.

Wget http://pecl.php.net/get/memcached-2.2.0.tgztar zxvf memcached-2.2.0.tgzcd memcached-2.2.0phpize (if there are two PHP environments in the system, you need to call the command in absolute path/usr/bin/phpize, the path is the PHP environment path using OCS ). /configure -- with-libmemcached-dir =/usr/local/libmemcached -- enable-memcached-sasl (pay attention to this parameter) makemake install

Finally, modify the php. ini file (locate to find this file. If there are two sets of PHP environments in the system, find the PHP environment path using OCS and modify it accordingly) and add

extension=memcached.somemcached.use_sasl = 1

Dependency:

Memcached 2.2.0 extensions must use libmemcached 1.0.x libraries. Libraries lower than 1.0 cannot be compiled successfully. When compiling libmemcached, GCC must be above 4.2.

Use the test code at the end of the page to test whether the environment has been deployed successfully. Modify the corresponding address and port username and password in the code.

3.0 Installation Steps

Centos and Aliyun Linux 5 Series [64-bit version]

First, check whether gcc-c ++ and other components are installed. If not, execute

yum  install gcc+ gcc-c++

1) Run rpm-qa | grep php to check whether a PHP environment exists in the system. If not, install it. If PHP is available, do not install it! We recommend that you use php5.3 and later versions. For some PHP5.2 versions, the zend_parse_parameters_none function may cause errors in the source code. For details, refer to the official php documentation [PHP with source code compilation]

yum install php53 php53-devel

2) install SASL-related environments

yum install cyrus-sasl-plain cyrus-sasl  cyrus-sasl-devel  cyrus-sasl-lib

3) install the source package libmemcached. [libmemcached1.0.2 is recommended]

Check whether these packages have been installed, including the source package. If yes, you do not need to install these packages.

wget http://launchpad.net/libmemcached/1.0/1.0.16/+download/libmemcached-1.0.16.tar.gztar -zxvf libmemcached-1.0.16.tar.gzcd libmemcached-1.0.16cd libmemcached-1.0.16./configure --prefix=/usr/local/libmemcached --enable-saslmakemake installcd .. 

4) install the source code package memcached [Recommended version: memcached2.0]

Before installing MEMCACHED, check whether the zlib-devel package is not required.

yum install zlib-devel

Check whether the memcached client package (including the source package) has been installed. If yes, you do not need to install it, but you need to re-compile and add the-enable-memcached-sasl extension.

Wget http://pecl.php.net/get/memcached-2.2.0.tgztar-zxvf memcached-2.0.0.tgzcd memcached-2.0.0phpize (if there are two PHP environments in the system, you need an absolute path to call this command/usr/bin/phpize, which is the PHP environment path using OCS, execute phpize in the memcached source code directory ). /configure -- with-libmemcached-dir =/usr/local/libmemcached -- enable-memcached-sasl (pay attention to this parameter) makemake instal

Finally, modify php. the INI file (locate to find this file, yum is usually installed in/etc/php. ini if there are two PHP environments in the system, you need to find the path of the PHP environment using OCS and modify it accordingly), add

extension=memcached.so memcached.use_sasl = 1

Execute php-m | grep memcached. If memcache is displayed, memcache is supported in the environment.

Use the test code at the end of the page to test whether the environment has been deployed successfully. Modify the corresponding address and port username and password in the code.

4.0 Installation Steps

1) Change the ubuntu Source

Solution 1: vim/etc/apt/source. list

Add content at the beginning

Deb http://mirrors.aliyun.com/ubuntu/ precise main restricted universe restricted http://mirrors.aliyun.com/ubuntu/ precise-security main restricted universe restricted http://mirrors.aliyun.com/ubuntu/ precise-updates main restricted universe restricted http://mirrors.aliyun.com/ubuntu/ precise-proposed main restricted universe restricted http://mirrors.aliyun.com/ubuntu/ precise-backports main restricted universe internal- src http://mirrors.aliyun.com/ubuntu/ precise main restricted universe regular-src http://mirrors.aliyun.com/ubuntu/ precise-security main restricted universe regular-src http://mirrors.aliyun.com/ubuntu/ precise-updates main restricted universe regular-src http://mirrors.aliyun.com/ubuntu/ precise-proposed main regular universe regular-src http://mirrors.aliyun.com/ubuntu/ precise-backports main restricted universe multiverseapt-get update // update the list

Solution 2: Download the compressed package of update_source through wget http://oss.aliyuncs.com/aliyunecs/update_source.tgz, decompress the package, execute the chmod 777 file name, and then execute the script to automatically change the Source Operation

2) Configure GCC, G ++ through ape-get

Run the dpkg-s installation package name (for example, dpkg-s gcc) to check whether gcc-c ++ and other components are installed. If not, run

apt-get build-dep gccapt-get install build-essentialapt-get install pkg-config

3) install php5, php5-dev

First, run the dpkg-s installation package name (for example, dpkg-s php) to check whether php and other components are installed. If not, run

Apt-get install php5 php5-dev [php5-cli and php5-common are automatically installed]

4) install and configure sasl support

First, run the dpkg-s installation package name (for example, dpkg-s libsasl2) to check whether libsasl2 cloog-ppl and other components are installed. If not, run

apt-get install  libsasl2-dev cloog-pplcd /usr/local/src

5) install the specified version of libmemcache

Check whether these packages have been installed, including the source package. If yes, you do not need to install these packages.

wget https://launchpad.net/libmemcached/1.0/1.0.16/+download/libmemcached-1.0.16.tar.gztar -zxvf libmemcached-1.0.16.tar.gzcd libmemcached-1.0.16./configure --prefix=/usr/local/libmemcachedmakemake installcd ..

6) install the specified version of memcached.

Check whether the memcached client package (including the source package) has been installed. If yes, you do not need to install it, but you need to re-compile and add the-enable-memcached-sasl extension.

wget http://pecl.php.net/get/memcached-2.2.0.tgztar zxvf memcached-2.2.0.tgzcd memcached-2.2.0phpize5./configure --with-libmemcached-dir=/usr/local/libmemcached --enable-memcached-saslmakemake install

7) Configure php to support memcached and then test

echo "extension=memcached.so" >>/etc/php5/conf.d/pdo.iniecho "memcached.use_sasl = 1" >>/etc/php5/conf.d/pdo.ini

Php-m | grep mem

Memcached [This component is displayed to indicate that the installation is complete]

Configuration complete

 

Sample Code:

1. Example 1: connect to OCS and set/get:

$ Connect = new Memcached; // declare a new memcached link $ connect-> setOption (Memcached: OPT_COMPRESSION, false ); // disable the compression function $ connect-> setOption (Memcached: OPT_BINARY_PROTOCOL, true); // use the binary Protocol $ connect-> addServer ('aaaaaaaaaa .m.yyyyyyyyy.ocs.aliyuncs.com ', 11211 ); // Add the OCS instance address and port number $ connect-> setSaslAuthData ('aaaaaaaaaa, 'Password'); // set the OCS account password for authentication. If the password-free function is enabled, skip this step $ connect-> set ("hello", "world"); echo 'Hello: ', $ connect-> get ("hello "); $ connect-> quit ();

2. Example 2: cache an array in OCS

$ Connect = new Memcached; // declare a new memcached link $ connect-> setOption (Memcached: OPT_COMPRESSION, false ); // disable the compression function $ connect-> setOption (Memcached: OPT_BINARY_PROTOCOL, true); // use the binary Protocol $ connect-> addServer ('xxxxxxxx .m.yyyyyy.ocs.aliyuncs.com ', 11211 ); // Add the OCS instance address and port number $ connect-> setSaslAuthData ('xxxxxxxx', 'bbbbbbbbbb'); // set the OCS account password for authentication. If the password-free function is enabled, this step is not required. $ User = array ("name" => "ocs", "age" => 1, "sex" => "male "); // declare an array $ expire = 60; // set the expiration time test ($ connect-> set ('your _ name', $ user, $ expire), true, 'set cache failed'); if ($ connect-> get ('your _ name') {$ result = $ connect-> get ('your _ name ');} else {echo "Return code:", $ connect-> getResultCode (); echo "Retucn Message:", $ connect-> getResultMessage (); // if an error occurs, parse the return code $ result = "";} print_r ($ result); $ connect-> quit () ; Function test ($ val, $ expect CT, $ msg) {if ($ val! = $ Expect CT) throw new Exception ($ msg );}

3. Example 3: Use OCS in combination with the MySQL database

$ Connect = new Memcached; // declare a new memcached link $ connect-> setOption (Memcached: OPT_COMPRESSION, false ); // disable the compression function $ connect-> setOption (Memcached: OPT_BINARY_PROTOCOL, true); // use the binary Protocol $ connect-> addServer ('xxxxxx .m.yyyyyy.ocs.aliyuncs.com ', 11211 ); // Add the instance address and port $ connect-> setSaslAuthData ('xxxxxx', 'My _ passwd'); // set the OCS account and password for authentication. If the password-free function is enabled, this step $ user = array ("name" => "ocs", "age" => 1, "sex" => "male") is not required "); // Define an array if ($ connect-> get ('your _ name') {$ result = $ connect-> get ('your _ name '); print_r ($ result); echo "Found in OCS, get data from OCS"; // if data is obtained, print the data from OCS exit ;} else {echo "Return code:", $ connect-> getResultCode (); echo "Retucn Message:", $ connect-> getResultMessage (); // return code $ db_host = 'zzzzzz .mysql.rds.aliyuncs.com '; // database address $ db_name = 'my _ db'; // database name $ db_username = 'db _ user '; // used for database Account name $ db_password = 'db _ passwd'; // Database User Password $ connection = mysql_connect ($ db_host, $ db_username, $ db_password); if (! Mysql_select_db ($ db_name, $ connection) {echo 'could not select database'; // if the database connection fails, an error message exit is thrown;} $ SQL = "SELECT name, age, sex FROM test1 WHERE name = 'ocs' "; $ result = mysql_query ($ SQL, $ connection); while ($ row = mysql_fetch_assoc ($ result )) {$ user = array ("name" => $ row ["name"], "age" => $ row ["age"], "sex" => $ row ["sex"],); $ expire = 5; // set the data expiration time in the cache test ($ connect-> set ('your _ name', $ user, $ Expire), true, 'set cache failed'); // write to OCS cache} mysql_free_result ($ result); mysql_close ($ connection );} print_r ($ connect-> get ('your _ name'); // print the obtained data echo "Not Found in OCS, get data from MySQL "; // confirm the data obtained from the database $ connect-> quit (); function test ($ val, $ expect CT, $ msg) {if ($ val! = $ Expect CT) throw new Exception ($ msg );}

Source: https://help.aliyun.com/knowledge_detail/5974954.html

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.