Name

connect( )

Synopsis

DBI->connect(DBI:server:database[:host:port],
             username, password[, \%attributes])

Use this to establish a connection to MySQL and to select a database. The first argument includes a list of settings separated by colons. The name of the module (DBI), the type of server (mysql), and the name of the database to use are required. The hostname or IP address and port number are optional. The second argument is the username and the third is the user’s password. You can substitute any of these settings or values with variables—just be sure to enclose each argument containing variables with double quotes so that the values will be interpolated. Finally, attributes may be given in the fourth argument.

my $dbh = DBI->connect('DBI:mysql:bookstore:localhost',
                       'jacinta','richardson',
                      {AutoCommit=>0});

In this excerpt, Perl is connecting to the MySQL server with the username of jacinta and the password of richardson, with the database bookstore. The attribute of AutoCommit is set to off so that changes to the data may be undone using rollback( ). See the end of this chapter for a list of attributes.

Get MySQL in a Nutshell now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.