SitePoint Sponsor

User Tag List

Results 1 to 7 of 7
  1. #1
    SitePoint Zealot
    Join Date
    Feb 2009
    Posts
    152
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Connect to Local Database from Remote Server

    Hi,
    I have a mysql database on my local installation of WAMP. To access this database I would use something like this:

    $link = mysql_connect('localhost', 'root', 'root');

    If I wanted to access this same database from a remote server what would I use? Do I just replace localhost with the ip address of my machine?

    Thanks!

  2. #2
    Follow Me On Twitter: @djg gold trophysilver trophybronze trophy Dan Grossman's Avatar
    Join Date
    Aug 2000
    Location
    Philadephia, PA
    Posts
    20,578
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Yes, and ensure two things:

    1) There is no firewall blocking port 3306 on either side

    2) The user you connect as (root in your example) has permission to connect from that host. Refer to the manual for how to manage user access permissions:

    http://dev.mysql.com/doc/refman/5.1/...ing-users.html

  3. #3
    SitePoint Zealot
    Join Date
    Feb 2009
    Posts
    152
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    okay thanks! My SQL port in MAMP is currently set to 8889. Should I change this to 3306?

  4. #4
    Follow Me On Twitter: @djg gold trophysilver trophybronze trophy Dan Grossman's Avatar
    Join Date
    Aug 2000
    Location
    Philadephia, PA
    Posts
    20,578
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Unless you have a reason to use a different port, it'll make your life easier to keep it at MySQL's default. That's what mysql_connect() will try to use.

  5. #5
    SitePoint Zealot
    Join Date
    Feb 2009
    Posts
    152
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Ok, i've changed it to 3306 now.

    Thanks!

  6. #6
    SitePoint Zealot
    Join Date
    Feb 2009
    Posts
    152
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I am having some issues trying to make this remote connection. Here is what I've done so far:

    1. Created a user in phpmyadmin and granted them all global privleges and from any host.
    2. Used the following script to try and connect to the database from my remote server:

    Code:
    <?php
    $dbServer1='99.99.999.999';
    
    // username and password to log onto db server
    $dbUser1='username';
    $dbPass1='password';
    
    // name of database
    $dbName1='databasename';
    
        $link = mysql_connect("$dbServer1", "$dbUser1", "$dbPass1") or die("Could not connect");
        print "Connected successfully to your_db_name1<br>";
        mysql_select_db("$dbName1") or die("Could not select database your_db_name1");
        print "Database your_db_name1 selected successfully<br><br><br>";
    
    // close connection
    mysql_close($link);
    
    ?>
    I added the ip address of my local machine and the username and password of the user I created in phpmyadmin to the appropriate places.

    When I run the script I get the following error:

    Warning: mysql_connect() [function.mysql-connect]: Can't connect to MySQL server on '99.99.999.999' (4) in /home/toomany2/public_html/testdbconnection.php on line 29
    Could not connect

    When testing I turned off my firewall and plugged my computer directly into the modem.

    If I change the server to localhost and run it locally it connects no problem.

    The remote machine is running OS X if that makes any difference.

    Have I missed something to allow the remote connection?

    Thanks

  7. #7
    Follow Me On Twitter: @djg gold trophysilver trophybronze trophy Dan Grossman's Avatar
    Join Date
    Aug 2000
    Location
    Philadephia, PA
    Posts
    20,578
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Change die("Could not connect") to die(mysql_error()) in your code. This will give you the error message produced by the MySQL library, which can help you identify whether this is a network issue (it couldn't reach the other computer at all) or an authentication issue (a network connection was made, but the user wasn't allowed to connect).


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •