diff --git a/README.markdown b/README.markdown
index 3d8f091..85c1ef9 100644
--- a/README.markdown
+++ b/README.markdown
@@ -21,6 +21,7 @@ Table of Contents
     * [read_result](#read_result)
     * [query](#query)
     * [server_ver](#server_ver)
+    * [thread_id](#thread_id)
     * [set_compact_arrays](#set_compact_arrays)
 * [SQL Literal Quoting](#sql-literal-quoting)
 * [Multi-Resultset Support](#multi-resultset-support)
@@ -332,6 +333,16 @@ You should only call this method after successfully connecting to a MySQL server
 
 [Back to TOC](#table-of-contents)
 
+thread_id
+----------
+`syntax: tid = db:thread_id()`
+
+Returns the MySQL connection thread id, like `"758"`.
+
+You should only call this method after successfully connecting to a MySQL server, otherwise `nil` will be returned.
+
+[Back to TOC](#table-of-contents)
+
 set_compact_arrays
 ------------------
 `syntax: db:set_compact_arrays(boolean)`
diff --git a/lib/resty/mysql.lua b/lib/resty/mysql.lua
index a629daa..e888131 100644
--- a/lib/resty/mysql.lua
+++ b/lib/resty/mysql.lua
@@ -551,6 +551,8 @@ function _M.connect(self, opts)
     self._server_ver = server_ver
 
     local thread_id, pos = _get_byte4(packet, pos)
+    
+    self._thread_id = thread_id
 
     --print("thread id: ", thread_id)
 
@@ -694,6 +696,9 @@ function _M.server_ver(self)
     return self._server_ver
 end
 
+function _M.thread_id(self)
+    return self._thread_id
+end
 
 local function send_query(self, query)
     if self.state ~= STATE_CONNECTED then
diff --git a/t/sanity.t b/t/sanity.t
index 7a7dd5c..2b6310f 100644
--- a/t/sanity.t
+++ b/t/sanity.t
@@ -121,7 +121,7 @@ GET /t
                 return
             end
 
-            ngx.say("connected to mysql ", db:server_ver())
+            ngx.say("connected to mysql ", db:server_ver(), " thread ", db:thread_id())
 
             db:close()
         ';
@@ -129,7 +129,7 @@ GET /t
 --- request
 GET /t
 --- response_body_like
-connected to mysql \d\.\S+
+connected to mysql \d\.\S+\sthread\s\d+
 --- no_error_log
 [error]