Tuesday, April 19, 2011

Enable Remote Access To Mysql Database Server

By default remote access to MySQL database server is disabled for security reasons. However, some time you need to provide remote access to database server from home or a web server.

Working on fedora 10, php 5.3.2 and mysql Server version: 5.1.47

Edit my.cnf File
Red Hat Linux/Fedora/Centos Linux file is located at /etc/my.cnf location

opne this file and search [mysqld]
and add bellow line
port = 3306
bind-address = your system ip

service mysqld restart


Open port 3306


open TCP port 3306 using iptables.
A sample iptables rule to open Linux iptables firewall

/sbin/iptables -A INPUT -i eth0 -p tcp --destination-port 3306 -j ACCEPT

save all rules

service iptables save

Example

<?php
$link = mysql_connect('host ip', 'username', 'password');
if (!$link) {
die('Could not connect: ' . mysql_error());
exit;
}

$db_selected = mysql_select_db('database',$link);
if (!$db_selected) {
die ('Can\'t use database : ' . mysql_error());
exit;
}

$sql = "SELECT id as col1, col2, col3 FROM sometable";

$result = mysql_query($sql);

if (!$result) {
echo "Could not successfully run query ($sql) from DB: " . mysql_error();
exit;
}

if (mysql_num_rows($result) == 0) {
echo "No rows found, nothing to print so am exiting";
exit;
}

while ($row = mysql_fetch_assoc($result)) {
echo $row["col1"];
echo " ".$row["col2"];
echo " ".$row["col3"];
}

mysql_free_result($result);

mysql_close($link);
?>

Base on : http://www.cyberciti.biz/tips/how-do-i-enable-remote-access-to-mysql-database-server.html

No comments:

Post a Comment

how to see xml preview in browser php

xml preview view in browser $xml = new DOMDocument('1.0', 'UTF-8');                 $xml->preserveWhiteSpace = false; ...