Using Perl to access your MySQL database

This document contains information for connecting a Perl script with a hosting MySQL database using the DBI module: what data you need, where to place the script, and how to resolve the most common errors.

Your site's Perl scripts can read and store information in a hosting MySQL database using the DBI module. Perl and DBI are already available on the account, so nothing needs to be installed.

Data you need

Where to find them: log in to cPanel from the client portal → Log in to cPanel, or at https://yourdomain.com/cpanel. Then open Databases → MySQL Databases.

  • Database name: the full name, with the account prefix. For example, account_store.
  • MySQL username: also with the prefix. For example, account_app. It must be added to the database with the necessary privileges.
  • MySQL user password: the one you defined when creating it. It is not the cPanel password.
  • Server: localhost, because the script runs on the same hosting. Remote access is only needed for programs that connect from outside the hosting.

How the script is put together

Every script that uses DBI follows the same four steps:

  1. Connect to the database using the mysql driver, the database name, localhost, the username, and its password.
  2. Prepare the query, using placeholders (?) for the values instead of writing them directly inside the query text. This prevents SQL injection.
  3. Execute the query by passing the values and, if it returns results, iterate through them row by row.
  4. Close the query and the connection.

Complete code examples are in the official DBI documentation and the DBD::mysql driver documentation.

Where to place the script

  • If it runs from the browser, save it in the cgi-bin folder, with 755 permissions, and with Linux line endings.
  • If a scheduled task runs it, you can save it outside public_html.
  • A script that responds to the browser must first send the header indicating the content type, before displaying any text.

Security: the script stores the database password. Never leave it in a public folder with another extension or make copies like .bak or .txt, because they could be downloaded with the visible password. Use a MySQL user with the minimum permissions the script needs, and back up the database before testing changes that modify or delete data.

Common issues

  • Cannot connect: check the full name of the database and the user, the MySQL password, and that the user is added to that database. The server must be localhost.
  • Error "install_driver(mysql) failed": the DBD::mysql driver is not available on the account. Contact support.
  • Table does not exist: the name used in the script does not match the database name, including uppercase and lowercase letters.
  • The browser downloads the file or displays the code: the script is not in cgi-bin or does not have execution permission.
  • 500 Error: check the permissions (755), the Linux line endings, and that the content header is sent first.

If you need help, open a ticket from the client portal or ask via the site's online chat.

  • perl para acceder a una base de datos mysql
  • 41 Users Found This Useful
Was this answer helpful?

Related Articles

How to access a MySQL database remotely (home or work)

This article explains how to enable remote access to MySQL to connect from your computer, or...

MySQL databases

A MySQL database stores information for applications like WordPress, online stores, or forums. In...

Basic commands in MySQL

This article gathers basic MySQL commands to run in the SQL tab of phpMyAdmin. They are useful...

How to create a MySQL database

This article explains how to create a MySQL database in cPanel, how to import a .sql file, and...

How to manage MySQL databases

This article explains how to manage a MySQL database with phpMyAdmin from cPanel: searching data,...