How to connect to a database on another server?

Discussion in 'MySQL' started by hhheng, Nov 6, 2007.

  1. #1
    Though I can setup an almost same database as another server, I'd like to directly connect to the other server. To do like this, one edit will affect 2 sites.

    Can we use like this:

     
    hhheng, Nov 6, 2007 IP
  2. birdsq

    birdsq Peon

    Messages:
    810
    Likes Received:
    16
    Best Answers:
    0
    Trophy Points:
    0
    #2
    $dbhost = 'yourdbhost.com:3306';

    this should work :)
     
    birdsq, Nov 6, 2007 IP
  3. garbageman

    garbageman Peon

    Messages:
    29
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Remember to get the other site's server to allow the connection through its firewall
     
    garbageman, Nov 6, 2007 IP
  4. phper

    phper Active Member

    Messages:
    247
    Likes Received:
    17
    Best Answers:
    0
    Trophy Points:
    60
    #4
    hhheng, if the host is in the form of a domain name, it would be something like 'anotherdomain.com' (without 'http://').

    Port number is optional if it's the standard 3306.

    On the network level, you'll need to make sure the firewall doesn't block you, as garbageman suggested.

    On database level, you may also need to grant the privileges to a user from the location you're connecting from (unless if the the user is already granted the privileges to connect from anywhere, which is not recommended for security reasons).
    For more info on granting privileges: http://dev.mysql.com/doc/en/GRANT.html
     
    phper, Nov 6, 2007 IP
  5. hhheng

    hhheng Banned

    Messages:
    2,633
    Likes Received:
    37
    Best Answers:
    0
    Trophy Points:
    0
    #5
    $db_host = "anotherdomain.com:3306" is not working.

    Actually i have 2 websites run on 2 different servers, and both sites need to connect the same database on one of the server.

    How to know whether the firewall block outside connection or not?
     
    hhheng, Nov 8, 2007 IP
  6. Boulder

    Boulder Well-Known Member

    Messages:
    806
    Likes Received:
    46
    Best Answers:
    0
    Trophy Points:
    118
    #6
    Try this -

    $db_host = '192.160.X.X' ;

    Using the server IP address and the authorized user and password of that database..

    Boulder
     
    Boulder, Nov 8, 2007 IP
  7. phper

    phper Active Member

    Messages:
    247
    Likes Received:
    17
    Best Answers:
    0
    Trophy Points:
    60
    #7
    If the database is on IP2 and you need to connect from IP1, run mysql from IP1 from the command line and connect to IP2.
    e.g.: mysql -h IP2 -u yourusername -p

    If it hangs there for a long time and then timeout or gives you an error message saying 'Connection refused' (or something like that), it means it's blocked by the firewall.

    If you can connect, but when you do 'use yourDbName' it gives you a 'permission denied' error message, then you need to run the 'GRANT' query on IP2.

    Note:
    - The error msg may differ significantly. I haven't used MySQL for quite a while so can't really remember anymore.
    - Copy the error msg that you get here, if you still find problems.
     
    phper, Nov 8, 2007 IP
  8. kewlchat

    kewlchat Well-Known Member

    Messages:
    1,779
    Likes Received:
    45
    Best Answers:
    0
    Trophy Points:
    110
    #8
    Ask your server admin what teh db host is cause some times its different on the server i use they simply have you put the word mysql as the db host..
     
    kewlchat, Nov 8, 2007 IP
  9. Xtrm2Matt

    Xtrm2Matt Active Member

    Messages:
    129
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    58
    #9
    You need to configure the MySQL server to accept non-localhost connections, by doing so:

    GRANT ALL PRIVILEGES ON *.* TO USER@"%" IDENTIFIED BY 'PASS'

    *.* <- You can change this to a specific database (*.) or a specific table (.*).
    USER <- Username to connect to the database with.
    % <- % allows connections from ANY IP. If you want to accept connections from a specfic IP, replace the %.
    PASS <- Username's password to connect to the database with.

    I wouldn't really suggest giving a "GRANT ALL PRIVILEDGES" option, perhaps just "insert,update,delete,select" should be fine.
     
    Xtrm2Matt, Nov 11, 2007 IP