我们的服务器上运行着 150 个数据库,有时会导致“连接过多”问题,而且 MySQL 进程中还有很多休眠查询。休眠查询时间超过 200。请查看下面的服务器 my.cnf 配置。我已将 max_connections 从 500 增加到 750,但连接数有时会超过 750。将 max_connections 增加到 1000 是否安全,或者 my.cnf 中是否需要任何其他配置?如果配置中缺少任何内容,有人能帮我吗?
[mysqld]
performance-schema=0
datadir=/var/lib/mysql
socket=/var/lib/mysql/mysql.sock
symbolic-links=0
log-error=/var/log/mysqld.log
pid-file=/var/run/mysqld/mysqld.pid
query_cache_type=1
slow_query_log=1
slow_query_log_file="/var/log/mysql/mysql_slow.log"
max_connections = 750
default-storage-engine=MyISAM
innodb_file_per_table=1
max_allowed_packet=268435456
open_files_limit=43000
bind-address=0.0.0.0
innodb_buffer_pool_size= 6G
wait_timeout = 300
key_buffer_size = 2G
read_buffer_size = 16M
bulk_insert_buffer_size = 512M
myisam_sort_buffer_size = 2M
sql_mode = 'NO_ENGINE_SUBSTITUTION'
query_cache_limit = 536870912
query_cache_size = 268435456
Mysql 上运行的线程
mysql> show status like "%Threads%";
+------------------------+-------+
| Variable_name | Value |
+------------------------+-------+
| Delayed_insert_threads | 0 |
| Slow_launch_threads | 0 |
| Threads_cached | 12 |
| Threads_connected | 659 |
| Threads_created | 38451 |
| Threads_running | 1 |
+------------------------+-------+
htop 结果:
1 [| 1.3%] 11 [ 0.0%] 21 [ 0.0%] 31 [ 0.0%]
2 [|| 3.2%] 12 [|||| 29.5%] 22 [ 0.0%] 32 [ 0.0%]
3 [ 0.0%] 13 [ 0.0%] 23 [| 0.6%] 33 [ 0.0%]
4 [||| 21.3%] 14 [|||| 26.1%] 24 [|| 2.6%] 34 [ 0.0%]
5 [| 0.6%] 15 [| 0.6%] 25 [|| 1.9%] 35 [ 0.0%]
6 [|||| 26.3%] 16 [|| 3.2%] 26 [|| 1.3%] 36 [ 0.0%]
7 [|| 1.9%] 17 [|| 4.5%] 27 [ 0.0%] 37 [ 0.0%]
8 [|| 3.8%] 18 [|| 5.8%] 28 [ 0.0%] 38 [ 0.0%]
9 [| 0.6%] 19 [ 0.0%] 29 [ 0.0%] 39 [ 0.0%]
10 [|| 4.5%] 20 [|| 5.1%] 30 [ 0.0%] 40 [ 0.0%]
Mem[|||||||||||||||||||17.7G/62.7G] Tasks: 132, 473 thr; 3 running
Swp[| 89.0M/7.81G] Load average: 1.03 0.97 0.90
Uptime: 22 days, 16:31:03
PID USER PRI NI VIRT RES SHR S CPU% MEM% TIME+ Command
14706 mysql 20 0 29.3G 13.7G 10816 S 12.2 21.9 37h23:37 /usr/sbin/mysqld
数据库连接脚本(PHP):
<?php
class setting{
private $conn;
private $dbs;
function __construct($cid){
$client=$this->getclientbyid($cid);
$db=new db_bridge($client['website']);
if(!$db->exist()){
echo($client['website']." not exist in our DB");
die;
}
else{
$db->setid();
$this->setdb($db->get());
}
}
function __destruct(){
$this->dbs=null;
}
function setdb($db){
$this->conn=$db;
self::connect();
}
function connect(){
try{
$this->dbs=new PDO("mysql:host=".$this->conn['host'].";dbname=".$this->conn['db'], $this->conn['duser'],$pass);
}
catch(Exception $e){
echo "Unable to connect Database";
}
}
protected function query($query,$both=false){
$result=$this->dbs->prepare($query);
$result->execute();
if(!$both){
$rs=$result->fetch(PDO::FETCH_ASSOC);
}
else{
$rs=$result->fetch(PDO::FETCH_BOTH);
}
return $rs;
}
}
答案1
与其增加max_connections
,不如
- 让客户端在完成后关闭连接。
- 如果使用“连接池”,请不要将其数字设置得太高。
- 允许更少的客户端连接(例如,Apache 或其他 Web 服务器中的最大子连接数)
- 减少
wait_timeout
(以便睡眠连接能够更快自动断开)
休眠连接数不一定反映任何严重错误。同时运行的线程数 ( SHOW GLOBAL STATUS LIKE 'Threads_running'
) 更重要。它不应超过几十个。(您显示的“1”将是命令SHOW
本身;没有其他任何“正在运行”。)
max_connections
几百个就足够了最多系统。请提供 Wilson 所要求的信息;这可能会进一步深入了解真正的问题所在。
其他事宜...
query_cache_size = 268435456
太高。查询缓存扩展性不好;限制为 50M。
为了从 slowlog 中获取更多信息,请设置long_query_time = 1
。