> ## Documentation Index
> Fetch the complete documentation index at: https://kb.hosting.com/llms.txt
> Use this file to discover all available pages before exploring further.

# Connecting to MySQL using Perl

> Learn to connect to MySQL using Perl with this guide featuring two methods, detailed instructions, code snippets, and related articles.

This article describes two methods for connecting to a MySQL database using Perl:

* DBI (Database Interface) module

* Legacy *mysql* module

<Tip>
  The MySQL databases and users must already exist before you can use these methods. For information about how to manage MySQL databases using cPanel, please see [this article](/docs/managing-mysql-databases).
</Tip>

## Connecting to MySQL using the DBI (Database Interface) module

Using the DBI module is the preferred way to connect to MySQL in Perl. The original Perl *mysql* module is deprecated.

To connect to MySQL using the DBI module, follow these steps:

1. Use the following Perl code to connect to MySQL and select a database. Replace *USERNAME* with your username, *PASSWORD* with your password, and *DBNAME* with the database name:

```
use DBI;
$myConnection = DBI->connect("DBI:mysql:DBNAME:localhost", "USERNAME", "PASSWORD");
```

2. After the code connects to MySQL and selects the database, you can run SQL queries and perform other operations. For example, the following Perl code runs a SQL query that extracts the last names from the *employees* table, and stores the result in the *\$result* variable:

```
$query = $myConnection->prepare("SELECT lastname FROM employees");
$result = $query->execute();
```

## Connecting to MySQL using the legacymysqlmodule

The original Perl *mysql* module is deprecated, and should only be used when absolutely necessary for backward compatibility. If possible, use the DBI module instead.

To connect to MySQL using the legacy *mysql* module, follow these steps:

1. Use the following Perl code to connect to MySQL and select a database. Replace *USERNAME* with your username, *PASSWORD* with your password, and *DBNAME* with the database name:

```
use Mysql;
$myConnection = Mysql->connect('localhost','DBNAME','USERNAME','PASSWORD');
```

2. After the code connects to MySQL and selects the database, you can run SQL queries and perform other operations. For example, the following Perl code runs a SQL query that extracts the last names from the *employees* table, and stores the result in the *\$result* variable:

```
$result = $myConnection->query('SELECT lastname FROM employees');
```

## More information

To view the online documentation for the DBI module, please visit [http://dbi.perl.org/docs](http://dbi.perl.org/docs).

## Related articles

* [Connecting to MySQL from the command line](/docs/connect-to-mysql-from-the-command-line)

* [Connecting to MySQL using PHP](/docs/connect-to-mysql-using-php)

* [Connecting to MySQL using Python](/docs/connecting-to-mysql-using-python)

* [Connecting to MySQL using Microsoft.NET](/docs/connecting-to-mysql-using-microsoft-net)

* [Managing MySQL databases, users, and tables from the command line](/docs/managing-mysql-databases-and-users-from-the-command-line)
