OneProxy5.8.1資料庫連線池特性試驗

yb708發表於2016-02-01

該功能主要是用來改善外部程式對資料庫的反覆連線、以及長期不釋放的連線對資料庫帶來的效能和穩定性問題。

準備步驟:

1、 在測試之前準備了兩臺linuxredhat5.4)虛擬機器(機器1bogonIP10.122.3.73;機器2bogon_2IP10.122.3.69),分別安裝了MySQL5.6.27

2、 按照“1.OneProxy5.8.1新的目錄組織結構執行試驗”的步驟在bogon安裝OneProxy

3、 bogontest資料庫中建立測試表t,並分別插入一條記錄用於區分,資料如下:

bogon上:

mysql> select * from t;

+---+-------+

| a | b     |

+---+-------+

| 1 | bogon |

+---+-------+

1 row in set (0.00 sec)

bogon_2上:

mysql> select * from t;

+---+---------+

| a | b       |

+---+---------+

| 1 | bogon_2 |

+---+---------+

1 row in set (0.00 sec)

      

測試內容:

通過連續建立100個會話的指令碼來分別測試通過OneProxy來連線資料庫的是否可以實際減少資料會話的建立。

指令碼1(直接連MySQL)內容為:

# create by yb

# coding=gbk

# function: connect mysql test

import time

import os

import sys

import mysql.connector

 

# 建立連線

config = {

          'user':'test',

          'password':'test',

          'host':'10.122.3.73',

          'port':3306,

          'database':'test'}

         

conn_arr=[]

for i in range(100):

    conn = mysql.connector.connect(**config)

    # 建立遊標

    cur = conn.cursor()

    # 執行查詢SQL

    sql = "SELECT a,b FROM t WHERE a=1"

    cur.execute(sql)

    # 獲取查詢結果

    result_set = cur.fetchall()

    if result_set:

        for row in result_set:

            print "%d, %s" % (row[0],row[1])

    conn_arr.append(conn)

print "100 connect opened!"

time.sleep(100)

# 關閉遊標和連線       

cur.close()

conn.close()

指令碼2(通過OneProxy來連線)內容為:

# create by yb

# coding=gbk

# function: connect mysql test

import time

import os

import sys

import mysql.connector

 

# 建立連線

config = {

          'user':'test',

          'password':'test',

          'host':'10.122.3.73',

          'port':3307,

          'database':'test'}

         

conn_arr=[]

for i in range(100):

    conn = mysql.connector.connect(**config)

    # 建立遊標

    cur = conn.cursor()

    # 執行查詢SQL

    sql = "SELECT a,b FROM t WHERE a=1"

    cur.execute(sql)

    # 獲取查詢結果

    result_set = cur.fetchall()

    if result_set:

        for row in result_set:

            print "%d, %s" % (row[0],row[1])

    conn_arr.append(conn)

print "100 connect opened!"

time.sleep(100)

# 關閉遊標和連線       

cur.close()

conn.close()

 

測試步驟:

1、 執行指令碼1,可以觀察執行後資料庫的連線數直接增加了100個:

執行前會話情況:

mysql> show full processlist;

+----+------+-------------+------+---------+------+-------+-----------------------+

| Id | User | Host        | db   | Command | Time | State | Info                  |

+----+------+-------------+------+---------+------+-------+-----------------------+

| 39 | root | bogon:31777 | NULL | Query   |    0 | init  | show full processlist |

+----+------+-------------+------+---------+------+-------+-----------------------+

1 row in set (0.00 sec)

執行後會話情況:

mysql> show full processlist;

+-----+------+-------------------+------+---------+------+-------+-----------------------+

| Id  | User | Host              | db   | Command | Time | State | Info                  |

+-----+------+-------------------+------+---------+------+-------+-----------------------+

|  39 | root | bogon:31777       | NULL | Query   |    0 | init  | show full processlist |

|  49 | test | 10.122.3.11:53775 | test | Sleep   |    4 |       | NULL                  |

|  50 | test | 10.122.3.11:53776 | test | Sleep   |    4 |       | NULL                  |

|  51 | test | 10.122.3.11:53777 | test | Sleep   |    4 |       | NULL                  |

|  52 | test | 10.122.3.11:53778 | test | Sleep   |    4 |       | NULL                  |

|  53 | test | 10.122.3.11:53779 | test | Sleep   |    4 |       | NULL                  |

|  54 | test | 10.122.3.11:53780 | test | Sleep   |    4 |       | NULL                  |

|  55 | test | 10.122.3.11:53781 | test | Sleep   |    4 |       | NULL                  |

|  56 | test | 10.122.3.11:53782 | test | Sleep   |    4 |       | NULL                  |

|  57 | test | 10.122.3.11:53783 | test | Sleep   |    4 |       | NULL                  |

|  58 | test | 10.122.3.11:53784 | test | Sleep   |    4 |       | NULL                  |

|  59 | test | 10.122.3.11:53785 | test | Sleep   |    4 |       | NULL                  |

|  60 | test | 10.122.3.11:53786 | test | Sleep   |    4 |       | NULL                  |

部分省略

101 rows in set (0.00 sec)

2、 啟動bogon上的OneProxy,並執行指令碼2,可以觀察到啟動OneProxy後資料庫增加了11個會話,單執行指令碼後資料庫的連線數沒有增加,因此說明通過OneProxy連線資料庫有建立了會話連線池機制,因此可以有效的保護資料庫會話的使用:

啟動OneProxy後執行指令碼前會話情況:

mysql> show full processlist;

+-----+------+-------------+------+---------+------+-------+-----------------------+

| Id  | User | Host        | db   | Command | Time | State | Info                  |

+-----+------+-------------+------+---------+------+-------+-----------------------+

|  39 | root | bogon:31777 | NULL | Query   |    0 | init  | show full processlist |

| 149 | test | bogon:23836 | test | Sleep   |    1 |       | NULL                  |

| 150 | test | bogon:23839 | test | Sleep   |   13 |       | NULL                  |

| 151 | test | bogon:23841 | test | Sleep   |   13 |       | NULL                  |

| 152 | test | bogon:23843 | test | Sleep   |   13 |       | NULL                  |

| 153 | test | bogon:23846 | test | Sleep   |   12 |       | NULL                  |

| 154 | test | bogon:23848 | test | Sleep   |   12 |       | NULL                  |

| 155 | test | bogon:23850 | test | Sleep   |   12 |       | NULL                  |

| 156 | test | bogon:23853 | test | Sleep   |   11 |       | NULL                  |

| 157 | test | bogon:23855 | test | Sleep   |   11 |       | NULL                  |

| 158 | test | bogon:23857 | test | Sleep   |   11 |       | NULL                  |

| 159 | test | bogon:23859 | test | Sleep   |   10 |       | NULL                  |

+-----+------+-------------+------+---------+------+-------+-----------------------+

12 rows in set (0.00 sec)

執行指令碼後會話情況:

mysql> show full processlist;

+-----+------+-------------+------+---------+------+-------+-----------------------+

| Id  | User | Host        | db   | Command | Time | State | Info                  |

+-----+------+-------------+------+---------+------+-------+-----------------------+

|  39 | root | bogon:31777 | NULL | Query   |    0 | init  | show full processlist |

| 260 | test | bogon:24137 | test | Sleep   |    0 |       | NULL                  |

| 261 | test | bogon:24139 | test | Sleep   |   98 |       | NULL                  |

| 262 | test | bogon:24141 | test | Sleep   |   98 |       | NULL                  |

| 263 | test | bogon:24143 | test | Sleep   |   98 |       | NULL                  |

| 264 | test | bogon:24146 | test | Sleep   |   97 |       | NULL                  |

| 265 | test | bogon:24148 | test | Sleep   |   97 |       | NULL                  |

| 266 | test | bogon:24150 | test | Sleep   |   97 |       | NULL                  |

| 267 | test | bogon:24153 | test | Sleep   |   96 |       | NULL                  |

| 268 | test | bogon:24155 | test | Sleep   |   96 |       | NULL                  |

| 269 | test | bogon:24157 | test | Sleep   |   96 |       | NULL                  |

| 270 | test | bogon:24160 | test | Sleep   |   95 |       | NULL                  |

+-----+------+-------------+------+---------+------+-------+-----------------------+

12 rows in set (0.00 sec)


來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/7913676/viewspace-1985204/,如需轉載,請註明出處,否則將追究法律責任。

相關文章