测试 mysql 实际连接数

最近公司有部分产品从自己机房迁移到网易云,由虚拟机转到网易云的容器服务,另外本次迁移还涉及到几个 mysql 实例的迁移,由于自己机房中 mysql跑在物理机上,且每天会有业务的高峰,因此需要测试一下网易云关系数据库实例(以 mysql 为例)的一些指标。

以 mysql 测试 myqsl 的实际连接数为例,mysql 版本5.5,规格 4 核 8 GB(网易云mysql默认连接数跟规格相关),此规格在网易云上的默认连接数设置为2000,具体测试代码如下:

import java.sql.Connection; 
import java.sql.DriverManager; 
import java.sql.Statement; 
import java.sql.ResultSet; 
import java.sql.SQLException; 
import java.io.IOException; 

public class main {
    public static void main(String args[]){ 
        int count=0; 
        Connection []conn = new Connection[1000]; 
        Statement []stmt = new Statement[1000]; 
        ResultSet []rs = new ResultSet[1000]; 
        try{ 
        Class.forName("com.mysql.jdbc.Driver").newInstance(); 
        for(count = 0; count < 2000; count++){ 
        conn[count] = DriverManager.getConnection("jdbc:mysql://yourIP:3306/db1", "root", "password"); 
        stmt[count] = conn[count].createStatement(); 
        rs[count] = stmt[count].executeQuery("SELECT * FROM t1"); 
        while (rs[count].next()){ 
        //System.out.println(rs.getString(1) + "t " + rs.getString(2)); 
        } 
        System.out.print(count + "t"); 
        } 

        }catch(SQLException ex1){ 
        System.out.println("n" + ex1.toString()); 
        }catch(InstantiationException ex2){ 
        System.out.println("n" + ex2.toString()); 
        }catch(ClassNotFoundException ex3){ 
        System.out.println("n" + ex3.toString()); 
        }catch(IllegalAccessException ex4){ 
        System.out.println("n" + ex4.toString()); 
        }finally{ 
        try{ 
        System.out.println("nSystem has opened " + count-- + " Mysql connections.nPress Enter key to close the connections"); 
        System.in.read(); 
        System.out.println("nClose the Connections:"); 
        for(; count >= 0; count--){ 
        rs[count].close(); 
        stmt[count].close(); 
        conn[count].close(); 
        System.out.print(count + "t"); 
        } 
        }catch(SQLException ex){ 
        System.out.println("n Close connection exception:" + ex.toString()); 
        }catch(IOException io_ex){} 
        }//end the first "try" 
        } 
}

注:表 db1.t1 中数据为10条。

经测试 mysql 实例的连接数可以达到参数 max_connections 默认的最大值2000:

且此时实例的负载不高,其中CPU 和 内存都在40%以下,查询未受影响。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值