How To Connect To A Remote Database Server

  • Posted on November 16, 2014
  • sams.folkAsked by sams.folk

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.

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.

Show comments

Submit an answer

This textbox defaults to using Markdown to format your answer.

You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!

Sign In or Sign Up to Answer

These answers are provided by our Community. If you find them useful, show some love by clicking the heart. If you run into issues leave a comment, or add your own answer to help others.

Want to learn more? Join the DigitalOcean Community!

Join our DigitalOcean community of over a million developers for free! Get help and share knowledge in Q&A, subscribe to topics of interest, and get courses and tools that will help you grow as a developer and scale your project or business.

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.