# Connect to a `TiDB` Server Instance Using `Python`
+ Importing the **`Connector/Python`** database driver module - **`mysql.connector`**
+ Create **`MySQLConnection`** objects by passing connection parameters to the driver's **`connect()`** function
+ Closing a connection by calling a **`close()`** function on a **`MySQLConnection`** object
```
import ~!@mysql.connector@!~

conn = ~!@mysql.connector.connect@!~(
    database="test",
    host="127.0.0.1",
    port=4000,
    user="root",
    password=""
)
print(
    f"Connected to {conn.server_host}:{conn.server_port} "
    f"as {conn.user} (Connection ID: {conn.connection_id})"
)
print(f"The server is running {conn.get_server_info()}")
conn.close()

------------------~!@SAMPLE OUTPUT@!~-------------------------
Connected to 127.0.0.1:4000 as root (Connection ID: 413)
The server is running 5.7.25-TiDB-v6.1.0

```
-----------------------------------------
If the MySQL Connector/Python C extension is installed, mysql.connector.connect() method will return a CMySQLConnection object instead of a MySQLConnection object.

Because Connector/Python requires named arguments in connect(), the order in which you supply those arguments is unimportant. 

If the connection fails, Connector/Python raises an exception. If an exception occurs outside of a try/catch block, the default exception handler prints a stack trace and exits the program. 

To specify the port number for TCP/IP connections, include the host parameter and provide an integer port parameter:

```
conn_params = {
"database" : "dname", "host": "127.0.0.1", "port": 3307,
"user": "mysqluser", "password": "mysqlpass"
}
```

To specify the path to a socket file for local host connections on UNIX, omit the host and port parameters and provide a unix_socket string parameter with the path to the UNIX socket file. 

Note: This course covers exception handling in the lesson titled “Handling Exceptions.”
