How CONNECTION_ID() Works in MySQL

The MySQL CONNECTION_ID() function returns the connection ID (thread ID) for the connection.

In MySQL, every connection has an ID that is unique among the set of currently connected clients. We can use the CONNECTION_ID() function to return that ID.

Syntax

The syntax goes like this:

CONNECTION_ID()

So, no arguments are required or accepted.

Example

Here’s an example to demonstrate:

SELECT CONNECTION_ID();

Result:

+-----------------+
| CONNECTION_ID() |
+-----------------+
|               9 |
+-----------------+

In my case, the connection ID is 6.

If I connect to MySQL from a new terminal window:

mariadb

And run the statement again:

SELECT CONNECTION_ID();

I get a different connection ID:

+-----------------+
| CONNECTION_ID() |
+-----------------+
|              10 |
+-----------------+

No Arguments are Accepted

CONNECTION_ID() doesn’t accept any arguments. Passing any arguments results in an error:

SELECT CONNECTION_ID( 20 );

Result:

ERROR 1582 (42000): Incorrect parameter count in the call to native function 'CONNECTION_ID'