(HY000/2003): Can't connect to MySQL server on '192.168.2.98' (99)

本文针对高并发LNMP环境下PHP连接MySQL异常的问题进行了深入分析,并提供了详细的解决方案,包括调整系统参数以增加连接数、优化内核设置及调整limits配置。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

线上lnmp环境,高并发(qps几十k)下报,php连mysql异常,问题分析,解决。

一.问题描述

PHP Warning:  mysqli::real_connect(): (HY000/2003): Can't connect to MySQL server on '192.168.2.98' (99) in


二.问题分析

php连mysql失败,看到99,99是 os的错误,对应错误信息用perror看看。

perror 99

OS error code  99:  Cannot assign requested address

给出,Cannot assign requested address


出现这个错误信息,说明client,没有可以使用的连接,无法创建tcp连接到 mysql server,说明达到了client的最大可用连接限制。

知道这个,就需要调优,调大限制client连接的对应参数。


三.问题解决

1.修改local 可用端口数.local端口,一个ip作为tcp源ip,最多使用65535个端口,如果想变多,需要配置多个网卡ip。做成cluster。

net.ipv4.ip_local_port_range = 10000    65535
2.系统的TIME-WAIT sockets进行快速回收

net.ipv4.tcp_tw_recycle = 1 

3.重用TIME_WAIT状态的端口

net.ipv4.tcp_tw_reuse = 1
4.调大相关内核限制

# Controls IP packet forwarding
net.ipv4.ip_forward = 0


# Controls source route verification
net.ipv4.conf.default.rp_filter = 1


# Do not accept source routing
net.ipv4.conf.default.accept_source_route = 0


# Controls the System Request debugging functionality of the kernel
kernel.sysrq = 0


# Controls whether core dumps will append the PID to the core filename.
# Useful for debugging multi-threaded applications.
kernel.core_uses_pid = 1


# Controls the use of TCP syncookies
net.ipv4.tcp_syncookies = 1


# Disable netfilter on bridges.
net.bridge.bridge-nf-call-ip6tables = 0
net.bridge.bridge-nf-call-iptables = 0
net.bridge.bridge-nf-call-arptables = 0


# Controls the default maxmimum size of a mesage queue
kernel.msgmnb = 65536


# Controls the maximum size of a message, in bytes
kernel.msgmax = 65536


# Controls the maximum shared segment size, in bytes
kernel.shmmax = 68719476736


# Controls the maximum number of shared memory segments, in pages
kernel.shmall = 4294967296


net.ipv4.tcp_fin_timeout = 20


net.ipv4.ip_local_port_range = 10000 65000


net.ipv4.tcp_tw_recycle = 1


net.ipv4.tcp_tw_reuse = 1


net.ipv4.tcp_timestamps = 0


fs.aio-max-nr = 1048576


kernel.sem = 250 32000 100 128


net.core.rmem_default = 31457280
 
net.core.rmem_max = 12582912
 
net.core.wmem_default = 31457280
 
net.core.wmem_max = 12582912
 
fs.file-max = 6815744


net.ipv4.tcp_max_syn_backlog = 20480


net.core.somaxconn = 65536


# Increase number of incoming connections backlog
net.core.netdev_max_backlog = 65536


# Increase the maximum amount of option memory buffers
net.core.optmem_max = 25165824
 
vm.swappiness = 10


vm.dirty_ratio = 60


# Increase the maximum total buffer-space allocatable
# This is measured in units of pages (4096 bytes)
net.ipv4.tcp_mem = 65536 131072 262144
net.ipv4.udp_mem = 65536 131072 262144


# Increase the read-buffer space allocatable
net.ipv4.tcp_rmem = 8192 87380 16777216
net.ipv4.udp_rmem_min = 16384


# Increase the write-buffer-space allocatable
net.ipv4.tcp_wmem = 8192 65536 16777216
net.ipv4.udp_wmem_min = 16384


5.调大/etc/security/limits.conf相关限制(这个主要对server端有限制,client不影响)

* soft nofile 65535
* hard nofile 65535
* hard nproc  102400
* soft nproc  102400
mysql soft nproc 1024000
mysql hard nproc 1024000
mysql soft nofile 1024000
mysql hard nofile 1024000
www soft nproc 1024000
www hard nproc 1024000
www soft nofile 1024000
www hard nofile 1024000
root soft nproc 1024000
root hard nproc 1024000
root soft nofile 1024000
root hard nofile 1024000


6.总体上,把client端,server端涉及到的影响并发处理能力的,参数都调大,做压测,能达到C100K级别。就说明系统限制没太大问题。剩余就是应用代码程序方面的优化了。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值