search
HomeDatabaseMysql TutorialMemcached服务端安装

libevent安装 cd /tmpwget http://monkey.org/~provos/libevent-1.4.14b-stable.tar.gztar -zxf libevent-1.4.14b-stable.tar.gzcd libevent-1.4.14b-stable./configure --prefix=/opt/libevent/makemake install Memcached安装 cd /tmpwget -N http://www.m

libevent安装

cd /tmp
wget http://monkey.org/~provos/libevent-1.4.14b-stable.tar.gz
tar -zxf libevent-1.4.14b-stable.tar.gz
cd libevent-1.4.14b-stable
./configure --prefix=/opt/libevent/
make
make install

Memcached安装

cd /tmp
wget -N http://www.memcached.org/files/memcached-1.4.17.tar.gz
tar -zxf memcached-1.4.17.tar.gz
cd memcached-1.4.17
./configure --prefix=/opt/memcached --enable-64bit --with-libevent=/opt/libevent
make
make install

编写Memcached启动脚本

vi /etc/init.d/memcached
#!/bin/sh
# chkconfig: - 50 50
# description: memcached
# 以下要自己配置
CMD=/opt/memcached/bin/memcached
MEMORY=128
USER=root
HOST=master
PORT=11211
CONNECTION=32
PID=/tmp/memcached.pid
start() {
    echo -n $"memcached starting... "  
    $CMD -d -m $MEMORY -u $USER -l $HOST -p $PORT -c $CONNECTION -P $PID
    echo
}
stop() {
    echo -n $"memcached shutting down... "  
    kill `cat $PID`
    echo
}
case "$1" in
    start)
        start
        ;;
    stop)
        stop
        ;;
    restart)
        stop
        start
        ;;
    *)
    echo $"Usage: $0 {start|stop|restart}"  
    exit 1
esac
exit 0

将memcached加入系统启项

chmod +x /etc/init.d/memcached
chkconfig --add /etc/init.d/memcached
chkconfig --level 35 memcached on
chkconfig --level 35 iptables off

启动Memcached

/opt/memcached/bin/memcached -d -m 128 -u root -l master -p 11211 -c 32 -P /tmp/memcached.pid
service memcached restart

基于上面,其他一些选项的含义

-d  指定memcached进程作为一个守护进程启动
-m <num>    指定分配给memcached使用的内存,单位是MB
-u <username>   运行memcached的用户
-l <ip_addr>    监听的服务器IP地址,如果有多个地址的话,使用逗号分隔,格式可以为“IP地址:端口号”,例如:-l 指定192.168.0.184:19830,192.168.0.195:13542;端口号也可以通过-p选项指定
-p <num>    Memcached监听的端口,要保证该端口号未被占用
-c <num>    设置最大运行的并发连接数,默认是1024
-R <num>    为避免客户端饿死(starvation),对连续达到的客户端请求数设置一个限额,如果超过该设置,会选择另一个连接来处理请求,默认为20
-k  设置锁定所有分页的内存,对于大缓存应用场景,谨慎使用该选项
-P  保存memcached进程的pid文件
-s <file>   指定Memcached用于监听的UNIX socket文件
-a <perms>  设置-s选项指定的UNIX socket文件的权限
-U <num>    指定监听UDP的端口,默认11211,0表示关闭
-M  当内存使用超出配置值时,禁止自动清除缓存中的数据项,此时Memcached不可以,直到内存被释放
-r  设置产生core文件大小
-f <factor> 用于计算缓存数据项的内存块大小的乘数因子,默认是1.25
-n  为缓存数据项的key、value、flag设置最小分配字节数,默认是48
-C  禁用CAS
-h  显示Memcached版本和摘要信息
-v  输出警告和错误信息
-vv 打印信息比-v更详细:不仅输出警告和错误信息,也输出客户端请求和响应信息
-i  打印libevent和Memcached的licenses信息
-t <threads>    指定用来处理请求的线程数,默认为4
-D <char>   用于统计报告中Key前缀和ID之间的分隔符,默认是冒号“:”
-L  尝试使用大内存分页(pages)
-B <proto>  指定使用的协议,默认行为是自动协商(autonegotiate),可能使用的选项有auto、ascii、binary。
-I <size>   覆盖默认的STAB页大小,默认是1M
-F  禁用flush_all命令
-o <options>    指定逗号分隔的选项,一般用于用于扩展或实验性质的选项

停止Memcached

ps -ef | grep memcached
kill -9 <PID>

Java客户端 - XMemcached客户端

spring.xml

<!-- 启动参数加入 -Dxmemcached.jmx.enable=true 可启动jmx支持 -->
<!-- 监控端可通过service:jmx:rmi:///jndi/rmi://host:7077/xmemcachedServer连接监控 -->
<bean name="memcachedClient"
    class="net.rubyeye.xmemcached.utils.XMemcachedClientFactoryBean"
    destroy-method="shutdown">
    <!-- servers节点列表格式 主节点1:port,主节点1备份:port 主节点2:port,主节点2备份:port -->
    <!--可以不设置备份节点,主备节点都好分割,不同的主备分组用空格分隔 -->
    <property name="servers">
        <value>${cache.memcacheHost}</value>
    </property>
    <!-- 权重 -->
    <property name="weights">
        <list>
            <value>${cache.memcacheHostWeights1}</value>
        </list>
    </property>
    <!-- 开启sasl验证需要配置此部分 -->
    <!-- <bean name="server1" class="java.net.InetSocketAddress"> <constructor-arg> 
        <value>host1</value> </constructor-arg> <constructor-arg> <value>port1</value> 
        </constructor-arg> </bean> <property name="authInfoMap"> <map> <entry key-ref="server1"> 
        <bean class="net.rubyeye.xmemcached.auth.AuthInfo" factory-method="typical"> 
        <constructor-arg index="0"> <value>cacheuser</value> </constructor-arg> <constructor-arg 
        index="1"> <value>123456</value> </constructor-arg> </bean> </entry> </map> 
        </property> -->
    <!-- nio线程数最好是cpu核数-1 -->
    <property name="connectionPoolSize">
        <value>${cache.memcacheConnectionPoolSize}</value>
    </property>
    <!--默认超时时间 -->
    <property name="opTimeout">
        <value>${cache.opTimeout}</value>
    </property>
    <!-- 使用二进制保存 -->
    <property name="commandFactory">
        <!-- <bean class="net.rubyeye.xmemcached.command.BinaryCommandFactory"></bean> -->
        <bean class="net.rubyeye.xmemcached.command.TextCommandFactory"></bean>
    </property>
    <!-- 分布式hash一致策略 -->
    <property name="sessionLocator">
        <bean class="net.rubyeye.xmemcached.impl.KetamaMemcachedSessionLocator"></bean>
    </property>
    <!-- 序列化转换 -->
    <property name="transcoder">
        <bean class="net.rubyeye.xmemcached.transcoders.SerializingTranscoder" />
    </property>
    <!-- buffer分配策略 -->
    <property name="bufferAllocator">
        <bean class="net.rubyeye.xmemcached.buffer.SimpleBufferAllocator"></bean>
    </property>
    <!-- 在hash一致模式下节点失败路由到下一节点,关闭快速失败模式 -->
    <property name="failureMode" value="false" />
</bean>

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Explain the InnoDB Buffer Pool and its importance for performance.Explain the InnoDB Buffer Pool and its importance for performance.Apr 19, 2025 am 12:24 AM

InnoDBBufferPool reduces disk I/O by caching data and indexing pages, improving database performance. Its working principle includes: 1. Data reading: Read data from BufferPool; 2. Data writing: After modifying the data, write to BufferPool and refresh it to disk regularly; 3. Cache management: Use the LRU algorithm to manage cache pages; 4. Reading mechanism: Load adjacent data pages in advance. By sizing the BufferPool and using multiple instances, database performance can be optimized.

MySQL vs. Other Programming Languages: A ComparisonMySQL vs. Other Programming Languages: A ComparisonApr 19, 2025 am 12:22 AM

Compared with other programming languages, MySQL is mainly used to store and manage data, while other languages ​​such as Python, Java, and C are used for logical processing and application development. MySQL is known for its high performance, scalability and cross-platform support, suitable for data management needs, while other languages ​​have advantages in their respective fields such as data analytics, enterprise applications, and system programming.

Learning MySQL: A Step-by-Step Guide for New UsersLearning MySQL: A Step-by-Step Guide for New UsersApr 19, 2025 am 12:19 AM

MySQL is worth learning because it is a powerful open source database management system suitable for data storage, management and analysis. 1) MySQL is a relational database that uses SQL to operate data and is suitable for structured data management. 2) The SQL language is the key to interacting with MySQL and supports CRUD operations. 3) The working principle of MySQL includes client/server architecture, storage engine and query optimizer. 4) Basic usage includes creating databases and tables, and advanced usage involves joining tables using JOIN. 5) Common errors include syntax errors and permission issues, and debugging skills include checking syntax and using EXPLAIN commands. 6) Performance optimization involves the use of indexes, optimization of SQL statements and regular maintenance of databases.

MySQL: Essential Skills for Beginners to MasterMySQL: Essential Skills for Beginners to MasterApr 18, 2025 am 12:24 AM

MySQL is suitable for beginners to learn database skills. 1. Install MySQL server and client tools. 2. Understand basic SQL queries, such as SELECT. 3. Master data operations: create tables, insert, update, and delete data. 4. Learn advanced skills: subquery and window functions. 5. Debugging and optimization: Check syntax, use indexes, avoid SELECT*, and use LIMIT.

MySQL: Structured Data and Relational DatabasesMySQL: Structured Data and Relational DatabasesApr 18, 2025 am 12:22 AM

MySQL efficiently manages structured data through table structure and SQL query, and implements inter-table relationships through foreign keys. 1. Define the data format and type when creating a table. 2. Use foreign keys to establish relationships between tables. 3. Improve performance through indexing and query optimization. 4. Regularly backup and monitor databases to ensure data security and performance optimization.

MySQL: Key Features and Capabilities ExplainedMySQL: Key Features and Capabilities ExplainedApr 18, 2025 am 12:17 AM

MySQL is an open source relational database management system that is widely used in Web development. Its key features include: 1. Supports multiple storage engines, such as InnoDB and MyISAM, suitable for different scenarios; 2. Provides master-slave replication functions to facilitate load balancing and data backup; 3. Improve query efficiency through query optimization and index use.

The Purpose of SQL: Interacting with MySQL DatabasesThe Purpose of SQL: Interacting with MySQL DatabasesApr 18, 2025 am 12:12 AM

SQL is used to interact with MySQL database to realize data addition, deletion, modification, inspection and database design. 1) SQL performs data operations through SELECT, INSERT, UPDATE, DELETE statements; 2) Use CREATE, ALTER, DROP statements for database design and management; 3) Complex queries and data analysis are implemented through SQL to improve business decision-making efficiency.

MySQL for Beginners: Getting Started with Database ManagementMySQL for Beginners: Getting Started with Database ManagementApr 18, 2025 am 12:10 AM

The basic operations of MySQL include creating databases, tables, and using SQL to perform CRUD operations on data. 1. Create a database: CREATEDATABASEmy_first_db; 2. Create a table: CREATETABLEbooks(idINTAUTO_INCREMENTPRIMARYKEY, titleVARCHAR(100)NOTNULL, authorVARCHAR(100)NOTNULL, published_yearINT); 3. Insert data: INSERTINTObooks(title, author, published_year)VA

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment