How To Connect to a MySQL Database

How to connect to your MySQL database via Perl or PHP.

 

Standard MySQL Connect Parameters

Perl MySQL Connect Example

PHP MySQL Connect Example

 

 

Standard MySQL Connect Parameters

 

To connect to your database, you will need to know:

 

  • Your account username
  • Your MySQL password
  • The name of your database

 

The rest of the information that you need is:

 

  • MySQL Server: mysql5.datarealm.com
  • MySQL Port: 3306 (The default)

 

Please note - Connections to older mysql 4 dbs can happen via mysql.datarealm.com. Connections to newer mysql 5 dbs need to happen through mysql5.datarealm.com. The rest of this article assumes you are using mysql5. 

 

Also note, for security reasons, we only allow connections to our MySQL servers to come from our Web servers. If you are trying to connect to mysql.datarealm.com or myql5.datarealm.com directly from your computer, or other location, it will not work. If you have questions about our MySQL database, please contact us.

 

 

Perl MySQL Connect Example

 

As with all programing languages, there is more than one way to go about doing something. This example is only one way of performing the database connect, but should be handy for those looking to learn more about using MySQL with Perl. To use this example, just cut and paste the code below into your script, making the appropriate edits to the $username, $password, and $db_name values.



#Invoke the Perl Database Libraries
use DBI;


#Specify the MySQL server
my $host = "mysql5.datarealm.com";


#Edit these so that the values have
#your account username and MySQL password
my $username = "your_user";
my $password = "your_MySQL_password";


#Edit this so that the value of db_name is
#the name of your database that you want to connect to
my $db_name = "your_database";


#
#The following lines do not need to be edited
#
#put the database and server in to the connect statement
$dsn = "DBI:mysql5:database=$db_name;host=$host";


#Generate the full connect statement
$dbh = DBI->connect($dsn, $username, $password);


#run the connect statement
$self->dbh = $dbh;

 

 

If you have any questions about this connect statement, please contact us.

 

 

PHP MySQL Connect Example

 

As with all programing languages, there is more than one way to go about doing something. This example is only one way of performing the database connect, but should be handy for those looking to learn more about using MySQL with PHP. To use this example, just cut and paste the code below into your script, making the appropriate edits to the $username, $password, and $db_name values.

 

#specify the connection information
$db_server ="mysql5.datarealm.com";
$db_name = "your_database";
$username = "your_user";
$password = "your_MySQL_password";        


#
#The following lines do not need to be edited
#
#make the connection.  If there is a problem, print out a helpful error message
$dbh = @mysql_connect($db_server,$username,$password) or die 
("Connection to $db_server with login '$username'/'$password' failed.");

#select the database.  If the database is not found on the server, let us know
$db = @mysql_select_db($db_name) or die 
("Connection made. But database '$db_name' was not found.");

 

 

If you have any questions about using this connect statement, please contact us.

Was this answer helpful?

 Print this Article

Also Read

Logging into the phpMyAdmin MySQL interface

How to access the phpMyAdmin web based MySQL interface.   What is phpMyAdmin? Accessing...

Requesting a MySQL 5 Database

How to create a MySQL 5 Database in your account.   Requesting a MySQL 5 Database A note about...