Memcached note

本文提供了一步一步的指导,详细解释了如何在Linux系统上安装和配置Memcache服务器端,包括下载必要的软件包、安装libevent和memcached,以及启动服务的步骤。此外,还提供了解决安装过程中可能出现的问题的方法,如加载共享库失败,并介绍了如何将Memcache服务集成到系统的启动项中,确保其在系统重启后能够自动启动。

下载并安装Memcache服务器端
服务器端主要是安装memcache服务器端.
下载:http://www.danga.com/memcached/dist/memcached-1.2.2.tar.gz
另外,Memcache用到了libevent这个库用于Socket的处理,所以还需要安装libevent,libevent的最新版本是libevent-1.3。(如果你的系统已经安装了libevent,可以不用安装)
官网:http://www.monkey.org/~provos/libevent/
下载:http://www.monkey.org/~provos/libevent-1.3.tar.gz

用wget指令直接下载这两个东西.下载回源文件后。
1.先安装libevent。这个东西在配置时需要指定一个安装路径,即./configure –prefix=/usr;然后make;然后make install;
2.再安装memcached,只是需要在配置时需要指定libevent的安装路径即./configure –with-libevent=/usr;然后make;然后make install;
这样就完成了Linux下Memcache服务器端的安装。详细的方法如下:

 

1.分别把memcached和libevent下载回来,放到 /tmp 目录下:
# cd /tmp
# wget http://www.danga.com/memcached/dist/memcached-1.2.0.tar.gz
# wget http://www.monkey.org/~provos/libevent-1.2.tar.gz

2.先安装libevent:
# tar zxvf libevent-1.2.tar.gz
# cd libevent-1.2
# ./configure –prefix=/usr
# make
# make install

3.测试libevent是否安装成功:
# ls -al /usr/lib | grep libevent
lrwxrwxrwx 1 root root 21 11?? 12 17:38 libevent-1.2.so.1 -> libevent-1.2.so.1.0.3
-rwxr-xr-x 1 root root 263546 11?? 12 17:38 libevent-1.2.so.1.0.3
-rw-r–r– 1 root root 454156 11?? 12 17:38 libevent.a
-rwxr-xr-x 1 root root 811 11?? 12 17:38 libevent.la
lrwxrwxrwx 1 root root 21 11?? 12 17:38 libevent.so -> libevent-1.2.so.1.0.3
还不错,都安装上了。

4.安装memcached,同时需要安装中指定libevent的安装位置:
# cd /tmp
# tar zxvf memcached-1.2.0.tar.gz
# cd memcached-1.2.0
# ./configure –with-libevent=/usr
# make
# make install
如果中间出现报错,请仔细检查错误信息,按照错误信息来配置或者增加相应的库或者路径。
安装完成后会把memcached放到 /usr/local/bin/memcached ,

5.测试是否成功安装memcached:
# ls -al /usr/local/bin/mem*
-rwxr-xr-x 1 root root 137986 11?? 12 17:39 /usr/local/bin/memcached
-rwxr-xr-x 1 root root 140179 11?? 12 17:39 /usr/local/bin/memcached-debug

 

启动Memcached服务
1.启动Memcache的服务器端:
# /usr/local/bin/memcached -d -m 10 -u root -l 192.168.141.64 -p 12000 -c 256 -P /tmp/memcached.pid

-d选项是启动一个守护进程,
-m是分配给Memcache使用的内存数量,单位是MB,我这里是10MB,
-u是运行Memcache的用户,我这里是root,
-l是监听的服务器IP地址,如果有多个地址的话,我这里指定了服务器的IP地址192.168.0.200,
-p是设置Memcache监听的端口,我这里设置了12000,最好是1024以上的端口,
-c选项是最大运行的并发连接数,默认是1024,我这里设置了256,按照你服务器的负载量来设定,
-P是设置保存Memcache的pid文件,我这里是保存在 /tmp/memcached.pid,

2.如果要结束Memcache进程,执行:

# kill `cat /tmp/memcached.pid`

也可以启动多个守护进程,不过端口不能重复。

 

测试Memcached:

 

复制代码
[root@localhost /]# telnet 192.168.141.64 12000
Trying 
192.168.141.64...
Connected to 
192.168.141.64 (192.168.141.64).
Escape character is '^]'
.
set key1 0 60 4
zhou
STORED
get key1
VALUE key1 
0 4
zhou
END
复制代码


至此Memcached安装成功!

 

 

常见问题:

1.如果启动Memcached服务的时候遇到了

/usr/local/bin/memcached: error while loading shared libraries: libevent-1.2.so.1: cannot open shared object file: No such file or directory;

解决方案:

[root@localhost bin]# LD_DEBUG=libs memcached -v 
[root@localhost bin]
# ln -s /usr/lib/libevent-1.2.so.1 /usr/lib64/libevent-1.2.so.1
[root@localhost bin]
# /usr/local/bin/memcached -d -m 100 -u root -p 12000 -c 1000 -P /tmp/memcached.pid
[root@localhost bin]
# ps -aux

可以看到启动的Memcached服务了.

 

2.把Memcached服务加载到Linux的启动项中.万一机器断电系统重启.那么Memcached就会自动启动了.

假如启动Memcache的服务器端的命令为:
# /usr/local/bin/memcached -d -m 10 -u root -l 192.168.141.64 -p 12000 -c 256 -P /tmp/memcached.pid容来自17jquery

想开机自动启动的话,只需在/etc/rc.d/rc.local中加入一行,下面命令
/usr/local/memcached/bin/memcached -d -m 10 -p 12000 -u apache -c 256
上面有些东西可以参考一下:即,ip不指定时,默认是本机,用户:最好选择是:apache 或 deamon
这样,也就是属于哪个用户的服务,由哪个用户启动。

 

 

我在cpanel 多PHP编辑器里只有这些 display_errors This determines whether errors should be printed to the screen as part of the output or if they should be hidden from the user. 已禁用 max_execution_time This sets the maximum time in seconds a script is allowed to run before it is terminated by the parser. This helps prevent poorly written scripts from tying up the server. The default setting is 30. 60 max_input_time This sets the maximum time in seconds a script is allowed to parse input data, like POST, GET and file uploads. 60 max_input_vars This sets the maximum number of input variables allowed per request and can be used to deter denial of service attacks involving hash collisions on the input variable names. 3000 memory_limit This sets the maximum amount of memory in bytes that a script is allowed to allocate. This helps prevent poorly written scripts for eating up all available memory on a server. Note that to have no memory limit, set this directive to -1. 6000M post_max_size Sets max size of post data allowed. This setting also affects file upload. To upload large files, this value must be larger than upload_max_filesize. Generally speaking, memory_limit should be larger than post_max_size. 1000M session.gc_maxlifetime This specifies the number of seconds after which data will be seen as "garbage" and potentially cleaned up. 1440 session.save_path session.save_path defines the argument which is passed to the save handler. If you choose the default files handler, this is the path where the files are created. /var/cpanel/php/sessions/ea-php74 upload_max_filesize The maximum size of an uploaded file. 1000M zlib.output_compression Whether to transparently compress pages. If this option is set to "On" in php.ini or the Apache configuration, pages are compressed if the browser sends an "Accept-Encoding: gzip" or "deflate" header. 已禁用
最新发布
09-25
你已经在 **cPanel 的 MultiPHP INI Editor** 中正确设置了: ```ini memory_limit = 6000M ``` ✅ 这意味着: > PHP 层面的内存限制已经设为 **6GB**,这是目前最权威、最高优先级的配置方式之一。 --- ## ✅ 回答你的问题: > “为什么我在 `wp-config.php` 加了 `define('WP_MEMORY_LIMIT', '256M');`,但系统健康还是显示 40M?” 尽管你在 cPanel 设置了 `memory_limit = 6000M`,WordPress 仍可能显示 `40M`,原因如下: --- ## 🔍 核心原因分析 ### ❓ 为什么 WordPress 显示的是 `40M` 而不是 `6000M` 或 `256M`? > 🚨 因为 **WordPress 没有使用 `ini_get('memory_limit')` 来获取真实值**,而是有自己的“逻辑推断”机制。 > 它会先尝试提升内存,如果失败或未定义,则退回到 **默认值(后台 40M)** --- ### ⚙️ WordPress 内存检测流程(简化版) ```text 1. 是否定义了 WP_MEMORY_LIMIT? → 是 → 尝试 ini_set('memory_limit', ...) ↓ 成功? ← 取决于运行环境是否允许修改 ↓ 成功 → 当前 memory_limit = 设定值(如 256M) ↓ 失败 → 继续使用当前 memory_limit(可能是 php.ini 的值) 2. 如果没成功设置 → WordPress 使用内置默认值: - 后台(Admin): 40M - 前台(Frontend): 32M ``` 📌 所以: - 即使 `php.ini` 是 `6000M` - 如果 WordPress 在初始化时尝试调用 `ini_set()` 失败 - 它就会认为“我不能提高内存”,于是退回到默认的 `40M` - 然后在「网站健康」中报告:“PHP 内存限制:40M” 👉 **这不是真实的限制!这只是 WordPress 自己“猜”的结果。** --- ## ✅ 如何验证真实内存限制? ### 方法 1:创建一个测试文件 `check-memory.php` ```php <?php // 不加载 WordPress,直接看 PHP 原生值 echo "<h3>PHP 原生命令查看 memory_limit</h3>"; echo "当前 memory_limit: <strong>" . ini_get('memory_limit') . "</strong><br>"; // 如果你想加载 WordPress 上下文 if (file_exists('wp-load.php')) { require_once('wp-load.php'); echo "通过 wp_max_memory_limit(): <strong>" . size_format(wp_max_memory_limit()) . "</strong><br>"; } ``` 上传到网站根目录,访问: ``` https://yoursite.com/check-memory.php ``` 你应该看到输出: ```html 当前 memory_limit: 6000M 通过 wp_max_memory_limit(): 256 MB ``` 📌 解读: - `ini_get('memory_limit') = 6000M` → ✅ 真实可用上限是 6GB - `wp_max_memory_limit() = 256M` → 是 WordPress 主动申请的值 - 「网站健康」显示 `40M` → 是误判或缓存旧值 --- ### 方法 2:检查是否真的无法 `ini_set` WordPress 只有在调用 `ini_set()` 成功后才会更新内部状态。 你可以手动测试: ```php <?php $old = ini_get('memory_limit'); $new = '512M'; @ini_set('memory_limit', $new); $current = ini_get('memory_limit'); echo "原值: $old<br>"; echo "尝试设为: $new<br>"; echo "实际变为: $current<br>"; if ($current == $new || str_replace('M','',$current) >= 512) { echo "<span style='color:green'>✅ 成功修改 memory_limit</span>"; } else { echo "<span style='color:red'>❌ 修改失败(可能被锁定)</span>"; } ``` 如果输出: ``` 原值: 6000M 尝试设为: 512M 实际变为: 6000M ✅ 成功修改 memory_limit ``` 说明虽然值没变小,但由于原本就更大,所以不影响。 但如果输出: ``` 实际变为: 40M ❌ 修改失败 ``` 那说明 `ini_set()` 被禁用了(罕见)。 --- ## ✅ 正确做法:让 WordPress “相信”内存已提升 即使 `php.ini` 已经很高,你也应该在 `wp-config.php` 中明确定义: ```php /** 提升 WordPress 内存限制 */ define('WP_MEMORY_LIMIT', '256M'); define('WP_MAX_MEMORY_LIMIT', '512M'); // 后台复杂操作可用 ``` 放在其他 `define` 之前,例如: ```php // ... define('DB_PASSWORD', 'xxx'); // --- 添加在这里 --- define('WP_MEMORY_LIMIT', '256M'); define('WP_MAX_MEMORY_LIMIT', '512M'); // -------------------- define('AUTH_KEY', '...'); ``` 保存后: 1. 清除所有缓存(对象缓存、OPCache、插件缓存) 2. 刷新「网站健康」页面 3. 查看是否变为 `256M` --- ## 🔄 为什么有时候改了也不刷新?常见原因 | 原因 | 解决方案 | |------|----------| | OPCache 缓存了旧 `php.ini` | 重启 PHP 或通过 cPanel 清除 OPCache | | 对象缓存插件(Redis/Memcached)缓存了 `getinfo()` 数据 | 清除对象缓存 | | 浏览器缓存了「网站健康」页面 | 强制刷新(Ctrl+F5)或换个浏览器 | | 多个 `.user.ini` 文件冲突 | 检查子目录是否有覆盖配置 | --- ## ✅ 总结:你现在的情况 | 项目 | 状态 | 建议 | |------|------|-------| | `memory_limit = 6000M` in cPanel | ✅ 已设置成功 | 完全足够 | | `WP_MEMORY_LIMIT = 256M` in wp-config.php | ✅ 推荐添加 | 让 WP 主动识别 | | 系统健康显示 `40M` | ⚠️ 是误报或缓存 | 验证真实值为准 | | 实际可用内存 | ✅ 高达 6GB | 安全无虞 | > 💡 **结论:你不需要担心 `40M` 的提示。只要 `ini_get('memory_limit')` 返回 `6000M`,就是安全的。** --- ## ✅ 最终建议操作清单 1. ✅ 确保 `wp-config.php` 包含: ```php define('WP_MEMORY_LIMIT', '256M'); define('WP_MAX_MEMORY_LIMIT', '512M'); ``` 2. ✅ 创建 `check-memory.php` 验证真实值 3. ✅ 清除所有缓存(包括 OPCache) 4. ✅ 刷新「网站健康」页面,观察是否更新 5. ✅ 删除测试文件(防止信息泄露) ---
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值