The London Perl and Raku Workshop takes place on 26th Oct 2024. If your company depends on Perl, please consider sponsoring and/or attending.


Net::MySQL - Pure Perl MySQL network protocol interface.


  use Net::MySQL;
  my $mysql = Net::MySQL->new(
      # hostname => '',   # Default use UNIX socket
      database => 'your_database_name',
      user     => 'user',
      password => 'password'

  # INSERT example
      INSERT INTO tablename (first, next) VALUES ('Hello', 'World')
  printf "Affected row: %d\n", $mysql->get_affected_rows_length;

  # SLECT example
  $mysql->query(q{SELECT * FROM tablename});
  my $record_set = $mysql->create_record_iterator;
  while (my $record = $record_set->each) {
      printf "First column: %s Next column: %s\n",
          $record->[0], $record->[1];


Net::MySQL is a Pure Perl client interface for the MySQL database. This module implements network protocol between server and client of MySQL, thus you don't need external MySQL client library like libmysqlclient for this module to work. It means this module enables you to connect to MySQL server from some operation systems which MySQL is not ported. How nifty!

Since this module's final goal is to completely replace DBD::mysql, API is made similar to that of DBI.

From perl you activate the interface with the statement

    use Net::MySQL;

After that you can connect to multiple MySQL daemon and send multiple queries to any of them via a simple object oriented interface.

There are two classes which have public APIs: Net::MySQL and Net::MySQL::RecordIterator.

    $mysql = Net::MySQL->new(
        hostname => $host,
        database => $database,
        user     => $user,
        password => $password,

Once you have connected to a daemon, you can can execute SQL with:

        INSERT INTO foo (id, message) VALUES (1, 'Hello World')

If you want to retrieve results, you need to create a so-called statement handle with:

        SELECT id, message FROM foo
    if ($mysql->has_selected_record) {
        my $a_record_iterator = $mysql->create_record_iterator;
        # ...

This Net::MySQL::RecordIterator object can be used for multiple purposes. First of all you can retreive a row of data:

    my $record = $a_record_iterator->each;

The each() method takes out the reference result of one line at a time, and the return value is ARRAY reference.


    use Net::MySQL;
    use strict;

    my $mysql = Net::MySQL->new(
        unixsocket => $path_to_socket,
        hostname   => $host,
        database   => $database,
        user       => $user,
        password   => $password,

The constructor of Net::MySQL. Connection with MySQL daemon is established and the object is returned. Argument hash contains following parameters:


Path of the UNIX socket where MySQL daemon. default is /tmp/mysql.sock. Supposing hostname is omitted, it will connect by UNIX Socket.


Name of the host where MySQL daemon runs. Supposing hostname is specified, it will connect by INET Socket.


Port where MySQL daemon listens to. default is 3306.


Name of the database to connect.

user / password

Username and password for database authentication.


The waiting time which carries out a timeout when connection is overdue is specified.


The exchanged packet will be outputted if a true value is given.


A create_DATABASE() method creates a database by the specified name.

    die $mysql->get_error_message if $mysql->is_error;

A drop_database() method deletes the database of the specified name.

    die $mysql->get_error_message if $mysql->is_error;

A query() method transmits the specified SQL string to MySQL database, and obtains the response.


When SELECT type SQL is specified, Net::MySQL::RecordIterator object which shows the reference result is returned.

    $mysql->query(q{SELECT * FROM table});
    my $a_record_iterator = $mysql->create_record_iterator();

Net::MySQL::RecordIterator object is applicable to acquisition of a reference result. See "Net::SQL::RecordIterator API"" in " for more.


returns the number of records finally influenced by specified SQL.

    my $affected_rows = $mysql->get_affected_rows_length;

MySQL has the ability to choose unique key values automatically. If this happened, the new ID will be stored in this attribute.


TRUE will be returned if the error has occurred.


TRUE will be returned if it has a reference result by SELECT.


return the number of column.


return column names by ARRAY.


transmits an end message to MySQL daemon, and closes a socket.

Net::MySQL::RecordIterator API

Net::MySQL::RecordIterator object is generated by the query() method of Net::MySQL object. Thus it has no public constructor method.


each() method takes out only one line from a result, and returns it as an ARRAY reference. undef is returned when all the lines has been taken out.

    while (my $record = $a_record_iterator->each) {
        printf "Column 1: %s Column 2: %s Collumn 3: %s\n",
            $record->[0], $record->[1], $record->[2];


libmysql, IO::Socket


Hiroyuki OYAMA <>


Takuya Tsuchida <tsucchi at>



Copyright (C) 2002-2011 Hiroyuki OYAMA. Japan. All rights reserved.

Copyright (C) 2011 Takuya Tsuchida

This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself.