connect()
DBI->connect(DBI:server:database[:host:port],username,password[, \%attri])
Use this method to establish a connection to MySQL and to
select the default database. The first argument is a list of required
values separated by colons: the module (DBI), the
driver (mysql) for a MySQL server, and the database
name. 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, you may give
attributes in the fourth argument. Here is an example:
my $dbh = DBI->connect('DBI:mysql:bookstore:localhost',
'paola','caporalle1017', {AutoCommit=>0});In this excerpt, Perl is connecting to the MySQL server with the
username paola and the password
caporalle1017, with the database
bookstore. The attribute
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.
If you don’t specify the username or the user’s password (i.e.,
if undef is given instead), the value of the
environment variables, DBI_USER and
DBI_PASS, will be used if they are defined.