Jump to content

trying to connect to mysql databse in another machine


suyesh.amatya

Recommended Posts

I have a php script in my machine and via it I want to access the database in the other machine with IP 192.168.0.104.

The code is:

 

        mysql_connect("192.168.0.104","root","");

mysql_select_db("dbtest");

$sql="select * from dbtest";

$result=mysql_query($sql);

while($data=mysql_fetch_array($result)){

echo $data['name'];

}

but i am not being able to do this.Need help..

Use this code, and see if any errors appear:

 

mysql_connect("192.168.0.104","root","") or die(mysql_error());
mysql_select_db("dbtest") or die(mysql_error());
$sql="select * from dbtest";
$result=mysql_query($sql) or die(mysql_error());
while($data=mysql_fetch_array($result)){
    echo $data['name'];
}

first off, PUT A PASSWORD ON YOUR ROOT ACCOUNT

 

Secondly,

log into the remote server and add a user with remote connection privileges:

 

CREATE USER 'test'@ '192.168.3.198' ......

 

where the IP or hostname is the ip or hostname of the computer that will be connecting to the server.  If you wish to allow any computer to connect, use %.  Finish the CREATE statement with the required privileges and database restrictions.

 

DO NOT:

 

1. grant root remote connections (especially with no password)

2. grant the remote user more privileges then he needs (if he doesn't need to be able to alter or drop tables, don't let him)

3. give the remote user admin rights (create users, grant permissions, lock tables, etc...)

4. give the remote user a null or weak password 

 

This query string should do what you want and be reasonably secure:

 

CREATE USER '<user>'@'<host>' IDENTIFIED BY '<passwd>';
GRANT SELECT , INSERT , UPDATE , DELETE ON `<database>` . * TO '<user>'@'<host>';

change the stuff identified by < > tags

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.