您的位置:首页 > 数据库 > MySQL

测试 mysql 实际连接数

2017-11-20 20:40 204 查看
最近公司有部分产品从自己机房迁移到网易云,由虚拟机转到网易云的容器服务,另外本次迁移还涉及到几个 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%以下,查询未受影响。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: