How to connect at MySQL database

Do not forget create a new database and database’s user via Cpanel>>Mysql

Check connection here via command line

mysql -udatabaseuser -hdatabaseserver -ppassword  databasename

Where you need to:

~ replace databaseserver with the correct database servername for your site.

~ replace databaseuser with your own mysql username.

~ replace databasename with your own mysql databasename.

Example:

mysql -uvasya_hut -hlocalhost -pdfs435ksja vasya_hut

mysql -uvasya_hut -hvasya.com -pdfs435ksja vasya_hut

If you use preferences set in a .my.cnf file, the connection command would be much shorter, just:

mysql

– To connect from a PHP script, just put this in your file:

using the same substitutions for the bold terms as above, plus substituting databasepassword with your own mysql password.

Example:

mysql_connect(“vasya.com”, “vasya”, “password”);
mysql_select_db(“mikedb”);

– To connect from a perl script, put this in your file:

#!/usr/bin/perl
use DBI;

$database = “databasename”;
$hostname = “vasya.com”;
$port = “3306″;
$username = “databaseuser”;
$password = ‘databasepassword’;

$dsn = “DBI:mysql:database=$database;host=$hostname;port=$port”;

$dbh = DBI->connect($dsn, $username, $password) or die(“Could not connect!”);

$sql = “SELECT * FROM mytable”;

$sth = $dbh->prepare($sql);
$sth->execute;

while(($column1, $column2) = $sth->fetchrow_array)
{
print “C1 = $column1, C2 = $column2n”;
}

$dbh->disconnect;

where databasename, databaseuser, and databasepassword are your database name, database username and database password, and where vasya.com is your website’s domain name with no “www.” in front, for example, “mysite.com”, or if you are not hosting your own domain name with us, then “mysite.modwest.com”.

Similar Posts:

Leave a Reply

Your email address will not be published. Required fields are marked *