Mysql : Connect to a remote mysql Server

At a shell or command (cmd for windows) prompt you type all one 1 single line:

mysql -u DB_USER_NAME -p[PASSWORD] DB_NAME -h DB_SERVER

with the following replacements of the terms above:

* Replace DB_SERVER with the correct database servername or IP address

* Replace DB_USER_NAME with your mysql username.

* Replace DB_NAME with your mysql databasename.

* [PASSWORD] is optional on command line, otherwise it will ask you to enter it later on

DB_USER_NAME = Kartook
DB_SERVER             = KartookShop.com
BD_NAME                   = shop

mysql -u bip -p shop -h KartookShop.com

If you connecting to the same computer you can use host name will be localhost or with out any host name.

mysql -u Kartook -p shop

To Connect from PHP you use the below script:
we will query cust table and get customer name.


<?
$database = “DB_NAME”;
$hostname = “DB_SERVER”;
$username = “DB_USER_NAME”;
$password = ‘DB_PASSWORD’;

$link = mysql_connect($hostname, $username, $password);
mysql_select_db($database);

$SQL = “SELECT cust_name FROM cust;
$result = $link -> mysql_query($SQL,$link);

if (mysql_num_rows($result)> 0) {
while ($tmp = mysql_fetch_row($result))
echo   $tmp[0];
}

mysql_close($link);

?>

From Perl you can use the below script:

#!/usr/bin/perl
use DBI;
$database = “DB_NAME”;
$hostname = “DB_SERVER”;
$port = “3306”;
$username = “DB_USER_NAME”;
$password = ‘DB_PASSWORD’;

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

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

$SQL = “SELECT cust_name FROM cust”;

$result = $dbh->prepare($SQL);
$result->execute;

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

$dbh->disconnect;

Leave a Reply

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.