安装
apt install memcached php-memcached
php -i | grep memcached
service memcached status
默认配置
/etc/memcached.conf
-d # 以守护进程运行
-u memcache # 以该用户权限运行 默认 root
-l 127.0.0.1 # 监听来自该IP请求
-p 11211 # 监听端口
-m 64 # 分配内存 MB
-c 1024 # 允许并发连接数
在Linux服务器实际应用中,经常会有需要长时间执行的任务。这类任务若在前台运行,用户无法进行其他操作或者断开与服务器的连接,否则任务将被中止。此时适合使用守护进程。
ctrl + C暂停前台运行任务ctrl + Z暂停前台任务转后台保留command &后台运行任务fg $jobID切换后台任务到前台继续bg $jobID后台任务继续运行jobs查看当前后台任务nohub command守护进程 运行任务 断开连接后继续运行ps -aux | grep xxx查看后台守护进程kill $jobID / $pid杀任务 / 进程
或通过命令行启动
$ memcached -d -m 1024 -u root -l 127.0.0.1 -p 11211 -c 1024 -P /tmp/memcached.pid
安全配置
当公开memcahced服务时候,设置iptables规则阻挡入侵
// 放行
$ iptables -A INPUT -p tcp -s 127.0.0.1 --dport 11211 -j ACCEPT
$ iptables -A INPUT -p udp -s 127.0.0.1 --dport 11211 -j ACCEPT
// 阻止
$ iptables -I INPUT -p tcp --dport 11211 -j DROP
$ iptables -I INPUT -p udp --dport 11211 -j DROP
$ service iptables save
$ service iptables restart
# 当局域网内被放行
$ talnet 192.168.1.xxx 11211
$ status # 查看memcached状态
$ status items # 查看所有缓存项目
$ status cachedump 32 0 # 获得地址
$ get :status:264861539228401373:261588 # 读取值
set key flags exptime bytes [noreply]
value
key键flags压缩内容 MEMCACHE_COMPRESSEDexptime缓存时间 秒 0=永远bytes申请内存字节数noreply不需要返回数据 默认成功返回STORED,失败ERRORvalue值
get key key2 key3
delete key [noreplay]
DELETED删除成功ERROR语法错误或删除失败NOT_FOUNDkey 不存在
stats # 系统状态
stats items