sams.folk
By:
sams.folk

How To Connect To A Remote Database Server

November 16, 2014 15.9k views

Overall, I'm trying to connect to a remote database server from another server.

One of my servers has LAMP installed. It can connect and send data to its local MySQL database and works fine.

The other server has only Linux and MySQL (LM) installed, in order to make a dedicated database server, and is setup using the following articles.

https://www.digitalocean.com/community/tutorials/how-to-set-up-a-remote-database-to-optimize-site-performance-with-mysql
https://www.digitalocean.com/community/tutorials/how-to-migrate-a-mysql-database-to-a-new-server-on-ubuntu-14-04

It has a database to use, if I could only connect to it.

Within the LM/database server's /etc/mysql/my.cnf , I've adjusted the bind-address from localhost to the IP address of this server.

I've set up a new username with …

CREATE USER 'newusername'@'111.222.333.444' IDENTIFIED BY 'password';

Show grants reveals …

GRANT USAGE ON *.* TO 'newusername'@'111.222.333.444' IDENTIFIED BY PASSWORD '*abc123...'

This new username and password are the credentials, I'd like to connect with instead of using root. Additionally, when installing MySQL, to secure the database I chose to disallow root login remotely.

At the CLI I can connect remotely with ...

mysql -u newusername -h 111.222.333.444 -p

However, when using a browser to send data from an HTML page to the PHP application portion of my LAMP server, and it trying to remote connect to the LM/database server to insert the data; I get my coded error message of not being able to connect to the LM/database server.

$db_connect = @mysql_connect("111.222.333.444", "newusername", "password");  
if (!$db_connect)                                             
     exit("<p>Unable to connect to the database server at this time.</p>");

When I can get things to work, I'll remove MySQL from the LAMP server, so that this server becomes only an application server.

Passwords and IP addresses are all written correctly. I've granted All Privileges on newusername in the guess that would do something. Any suggestions of what to do? Thanks for any assistance.

3 comments
  • As you can connect remotely via the command line, then it's quite likely the error is in the PHP code not the MySql configuration. Either way though, it would be great to get some real debugging output. Doing something like this should provide some more info:

    $db_connect = mysql_connect("db.ip.address", "database_user", "password");
    if (!$db_connect)
        {     
            die('Unable to connect: ' . mysql_error()); 
        } else {
    # snip
    
  • I wish I could have thought of that.

    The recoding of the mysql_connect block did give me an error message of ...

    Host “....” is not allowed to connect to this MySQL server.

    This led me to do a search with that statement. Turns out, when I was creating a new user (or root) and adding an IP address, I was using the database server's IP and not the application server's IP. Also a '%' can be used in place of an IP address.

    Thanks once more for your response and help getting me unstuck!

  • Although this is a late reply, it might help someone in future
    I had the same issue. I did everything i could to allow remote connection.

    When I was doing changes i kept on restarting the apache2 server using sudo service apache2 restart. However, please keep in mind that you need to restart the MySql server incase you want the change from the configuration.

1 Answer

This question was answered by @sams.folk:

I wish I could have thought of that.

The recoding of the mysql_connect block did give me an error message of ...

Host “....” is not allowed to connect to this MySQL server.

This led me to do a search with that statement. Turns out, when I was creating a new user (or root) and adding an IP address, I was using the database server's IP and not the application server's IP. Also a '%' can be used in place of an IP address.

Thanks once more for your response and help getting me unstuck!

You can see the comment here.

Have another answer? Share your knowledge.