Learn how to a build a cloud-first strategyRegister Now


Connect to a mysql database on another webserver?

Posted on 2009-12-31
Medium Priority
Last Modified: 2013-12-12
Hello, I have a web server running a php web app off a MySQL database that I created a while back. (the database is constantly being updated every minute or so) There's a second website hosted on another server (different location and ip) that I want to pull some information from the first server and display (and possibly modify). I thought I'd just open the mysql port on the first server's firewall (available only to the second server's IP), and connect via PHP directly to the first server from the second, but I've never done this before so I wanted to see if anyone who's had experience doing this type of thing knows if this is the most secure/efficient way to do what I'm trying to do, or if there's a better way to do this.
Question by:Matt Kendall
  • 2
LVL 32

Accepted Solution

DrDamnit earned 2000 total points
ID: 26156474
You've got the right idea. It's pretty simple and striaght forward. The onlything to check is to make sure that the OUTBOUND firewall of server A can connect to server B (where B is the remote  box).

I have done this with PHP and with VB6 / ODBC. It's really straight forward.

Expert Comment

ID: 26157479
You can setup MySQL to take connections on any port, you don't need to use the standard 3306.

Also you can setup to only allow connections from certain IP addresses, so here you could specify your other webserver.

Setup a new user on your MySQL server with only access allowed from the second IP.

Usually MySQL users are localhost only.

$link = mysql_connect('otherdomain.com:14307', 'mysql_user', 'mysql_password');
if (!$link) {
    die('Could not connect: ' . mysql_error());
echo 'Connected successfully';

Open in new window


Expert Comment

ID: 26157488
You can also restrict was statements the remote user can execute.

"The GRANT statement specifies that this user is only allowed to run a limited amount of statements on the MySQL server. This user will be allowed to: select records, insert records, update records, delete records, create databases, and DROP DATABASEs. More importantly, this user is not permitted to create users and set privileges.

You could further reduce a user's privileges by removing other items from the GRANT statement. The best policy here is that the user should only be given permission to access the functions that are necessary to perform their tasks."



Author Closing Comment

by:Matt Kendall
ID: 31671708

Featured Post

 [eBook] Windows Nano Server

Download this FREE eBook and learn all you need to get started with Windows Nano Server, including deployment options, remote management
and troubleshooting tips and tricks

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In this blog post, we’ll look at how using thread_statistics can cause high memory usage.
It’s a season to be thankful, and we’re thankful for users like you who engage on site, solve technology problems, and network with others in the industry. What tech are we most thankful for? Keep reading.
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …
Suggested Courses
Course of the Month21 days, 3 hours left to enroll

810 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question