1. 程式人生 > >測試 mysql 實際連線數

測試 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%以下,查詢未受影響。