Tutorial

How To Set Up a Remote Database to Optimize Site Performance with MySQL

Published on April 17, 2014
How To Set Up a Remote Database to Optimize Site Performance with MySQL
Not using Ubuntu 12.04?Choose a different version or distribution.
Ubuntu 12.04

Introduction

As your application or website grows, you may come to the point where you’ve outgrown your current setup. If you are currently hosting your web server and database backend on the same VPS, a good idea may be to separate these two functions so that each can operate and grow on its own machine.

In this guide, we’ll discuss how to configure a remote database server that your web server can connect to for dynamic content. We will be using WordPress as an example so that we have something to work with. We’ll configure Nginx on our web server and then connect it to a MySQL database on a remote machine. We’ll be doing all of this on an Ubuntu 12.04 VPS instance for our demo.

Install MySQL on the Database Server

To get started, we’ll configure one VPS instance as the MySQL server. Having our data stored on a separate computer is a good way to expand gracefully when you’ve hit the ceiling on a one-machine configuration. It also provides the basic structure necessary to load balance and expand your set up even more at a later time.

To get started, we’ll need to install some basic packages on our database server. These will basically be most of the same steps you’d take for setting up a database for a traditional LEMP stack, but we won’t need all of the components (some will be on the other server).

Start off by updating your package cache and installing the MySQL server:

sudo apt-get update
sudo apt-get install mysql-server

You will be asked to select and confirm a root password for MySQL during the installation procedure.

When you are finished, you need to run the database installation command, which will generate the appropriate directory structure to manage your components.

sudo mysql_install_db

Afterwards, we should tighten up security a little bit by running a script that will ask us about disabling some insecure defaults:

sudo mysql_secure_installation

You will have to enter the MySQL administrator’s password that you set in the steps above. Afterwards, it will ask if you want to change that password. Type “N” for no if you’re happy with your current password.

To all of the additional questions, you should just hit ENTER to select the default options which will remove some test databases and lock down access.

Configure MySQL to Allow Remote Access

Now that you have your database up and running, we need to change some values to allow connections from other computers.

Open up the main configuration file for MySQL with root privileges in your editor:

sudo nano /etc/mysql/my.cnf

This file is divided into sections denoted by words in brackets ([ and ]). Find the section labeled mysqld:

[mysqld]

Within this section (in the area between this tag and the next section marker), you’ll need to find a parameter called bind-address. This basically tells the database software which network address to listen to connections on.

Currently, MySQL is configured to only look for connections from its own computer. We need to change that to reference an external IP address that your server can be reached at.

If you are hosting this in a datacenter with private networking capabilities, use your server’s private network IP. Otherwise, you can use the public IP address here:

bind-address        = your_database_IP

Save and close the file when you are finished.

To force MySQL to read the new change that we’ve just implemented, we can restart the database:

sudo service mysql restart

Set Up Remote WordPress Credentials and Database

Now that we have MySQL configured listening on an external address, we need to create a database and establish a remote user. Even though MySQL itself is now listening on an IP address that other machines can connect to, there are not currently any databases that it can access.

This is also an opportunity for us to establish different privileges based on where a user is connecting from. We can create two “users” that can actually just be the same username, but associated with different hosts.

What I mean by this is that we can create a user that is bound to the database server itself and grant it very broad permissions. We can then use the same username, but associated with our web server and give it only the permissions that WordPress requires.

This will allow us to do heavy duty work while logged into our database server, while only providing our web server with the bare minimum of permissions it needs to accomplish its job. This is a good security policy that will partially shield the database server in the event that the web server is compromised.

Begin by connecting to MySQL using the root account and administrative password you configured:

mysql -u root -p

You will be asked for your MySQL root password and then you’ll be given a MySQL prompt.

To get this started, let’s create the database that WordPress will use. We will just call this wordpress so that we can easily identify it in later:

CREATE DATABASE wordpress;

Now that we have a database, we need to create our local user, which will be used to do more intense database operations if ever need be. We will call this user wordpressuser and make this account only match connection attempts originating from the database server itself by using localhost in the declaration:

CREATE USER 'wordpressuser'@'localhost' IDENTIFIED BY 'password';

Let’s go ahead and grant this account full access to our database:

GRANT ALL PRIVILEGES ON wordpress.* TO 'wordpressuser'@'localhost';

This user can now do any operation on the database for WordPress, but this account cannot be used remotely, as it only matches connections from the local machine.

Let’s create a companion account that will match connections exclusively from our web server. For this, you’ll need your web server’s IP address. We could name this account anything, but for a more consistent experience, we’re going to use the exact same username as we did above, with only the host portion modified.

Keep in mind that you must use an IP address that utilizes the same network that you configured in your my.cnf file. This means that if you used a private networking IP, you’ll want to create the rule below to use the private IP of your web server. If you configured MySQL to use the public internet, you should match that with the web server’s public IP address.

CREATE USER 'wordpressuser'@'web_server_IP' IDENTIFIED BY 'password';

Now that we have our remote account, we can give it the subset of available privileges that WordPress requires to operate under normal circumstances. These are select, delete, insert, and update.

While this is the final goal, we are actually not able to implement this at this point. This is because during certain operations, you will have to adjust permissions temporarily to allow more access. One of these is actually the initial installation. It is easiest to just grant all privileges at the moment and then we will restrict them after we are finished with the installation.

For reference, the command we will be using to lock down the account (don’t worry, we will give you this command again when you need it) is this:

GRANT SELECT,DELETE,INSERT,UPDATE ON wordpress.* TO 'wordpressuser'@'web_server_ip';

But for now, we will temporarily grant all privileges, which makes it effectively identical to the local account for the time being:

GRANT ALL PRIVILEGES ON wordpress.* TO 'wordpressuser'@'web_server_ip';

We will come back to this after we have configured WordPress. If you are not actually installing WordPress and are using this guide simply to learn how to separate your web server from your database, you may be able to use the more restrictive settings now. It depends on your web application, so look up the minimum database privileges needed for your application.

Flush the privileges to write them to disk and begin using them:

FLUSH PRIVILEGES;

Now, you can exit the MySQL prompt by typing:

exit

Test Remote and Local Connections

Before we continue, it’s best to verify that you can connect to your database from both the local machine and from your web server using the wordpressuser accounts.

First, test the connection from your database machine by attempting to log in with our new account:

mysql -u wordpressuser -p

Type in the password that you set up for this account when prompted.

If you are given a MySQL prompt, then the local connection was successful. You can exit out again by typing:

exit

Log into your web server to test remote connections.

On your web server, you’ll need to install some client tools for MySQL in order to access the remote database. Update your local package cache, and then install the client utilities:

sudo apt-get update
sudo apt-get install mysql-client

Now, we can connect to our database server using the following syntax:

mysql -u wordpressuser -h database_server_IP -p

Again, you must make sure that you are using the correct IP address for the database server. If you configured MySQL to listen on the private network, enter your database’s private network IP, otherwise enter your database server’s public IP address.

You should be asked for the password for your wordpressuser account, and if all went well, you should be given a MySQL prompt.

If this is successful, then you can go ahead and exit out of the prompt, as you’ve now verified that you can connect remotely.

For an additional check, you can try doing the same thing from a third server to make sure that this other server is not granted access. You have verified local access and access from the web server, but you have not verified that other connections will be refused.

Go ahead and try that same procedure on a server that you did not configure a specific user account for. You may have to install the client utilities as you did above:

mysql -u wordpressuser -h database_server_IP -p

This should not complete successfully. It should throw back an error that looks something like:

ERROR 1130 (HY000): Host '11.111.111.111' is not allowed to connect to this MySQL server

This is what we expect and what we want.

Set Up the Web Server

Now that we have verified that our web server can access the WordPress database, we need to actually make this a web server by configuring Nginx, PHP and the necessary components.

Since you updated your package index to run the test above, we don’t need to do that again. Let’s install all of the packages we need:

sudo apt-get install nginx php5-fpm php5-mysql

When everything is installed, you can begin configuring the software.

Configure PHP

Let’s begin with PHP since that is quite easy.

Open the PHP configuration file for php-fpm, which will handle our dynamic content. We just need to modify one value in this:

sudo nano /etc/php5/fpm/php.ini

Search for the cgi.fix_pathinfo parameter. It will likely be commented out with the “;” character and set to “1”. We need to reverse both of these conditions by uncommenting the line and setting it to “0”:

cgi.fix_pathinfo=0

This is a security measure. By setting this option, we tell PHP not to try to guess the file that the user was trying to access if an exact match is not found. If we didn’t set this, a malicious user could take advantage of this opportunity and get our server to execute code that we don’t want it to.

Save and close the file when you are finished.

Next, we’ll need to open another file to modify how our PHP processor and web server communicate:

sudo nano /etc/php5/fpm/pool.d/www.conf

Look for the listen directive, which should be set to 127.0.0.1:9000. Rather than using a port, we’re going to set this to a unix domain socket:

listen = /var/run/php5-fpm.sock

Save and close the file when you are finished.

Now that we have our values, restart our PHP processor:

sudo service php5-fpm restart

Configure Nginx

Now we’re ready to configure Nginx. We can start by copying the default virtual host file to a new file that we can work with. We’ll name this after the domain of our site. I’m going to use the placeholder “example.com”:

sudo cp /etc/nginx/sites-available/default /etc/nginx/sites-available/example.com

Now, open the file we just copied over:

sudo nano /etc/nginx/sites-available/example.com

Inside, we’ll modify our server block (the sections contained within the server brackets). Begin by uncommenting the directive to listen to port 80. We’re also going to change the root directory and make Nginx serve a PHP index file by default:

server {
    listen 80;
    root /var/www/example.com;
    index index.php index.hmtl index.htm;

Next, we’ll modify the server_name directive to use our domain name, ensure that our try_files is set up correctly (passing requests to PHP if no files are found) and that our error pages are configured:

server {
    listen 80;
    root /var/www/example.com;
    index index.php index.hmtl index.htm;
    server_name example.com;
    location / {
        try_files $uri $uri/ /index.php?q=$uri&$args;
    }
    error_page 404 /404.html;
    error_page 500 502 503 504 /50x.html;
    location = /50x.html {
        root /usr/share/nginx/www;
    }

Finally, we need to set up the actual PHP processing by using a location block that will match all of our PHP requests. We will immediately return a 404 if an exact match is not found. We’ll also use the socket we configured for PHP:

server {
    listen 80;
    root /var/www/example.com;
    index index.php index.hmtl index.htm;
    server_name example.com;
    location / {
        try_files $uri $uri/ /index.php?q=$uri&$args;
    }
    error_page 404 /404.html;
    error_page 500 502 503 504 /50x.html;
    location = /50x.html {
        root /usr/share/nginx/www;
    }
    location ~ \.php$ {
        try_files $uri =404;
        fastcgi_pass unix:/var/run/php5-fpm.sock;
        fastcgi_index index.php;
        include fastcgi_params;
    }
}

This is the end of our server block configuration. Save and close the file.

Now, we link this to our “enabled” directory and remove the link for the default server block file:

sudo rm /etc/nginx/sites-enabled/default
sudo ln -s /etc/nginx/sites-available/example.com /etc/nginx/sites-enabled/

Restart Nginx to make these changes happen:

sudo service nginx restart

Install WordPress

Now that we have our web server set up with PHP processing and we have a database server, we need to install an application to take advantage of this and connect out to our database. As you know by now, we’re using WordPress to demo this functionality in this tutorial.

Download the latest WordPress tarball to your home directory:

cd ~
wget http://wordpress.org/latest.tar.gz

Extract the files, which will create a directory called “wordpress” in your home directory:

tar xzvf latest.tar.gz

WordPress includes a sample configuration file, but it is not in place out-of-the-box. We will rename this file so that it will be read correctly and then we can open it in our text editor to make our changes:

cp ~/wordpress/wp-config-sample.php ~/wordpress/wp-config.php
nano ~/wordpress/wp-config.php

Inside, we need to enter the correct values for our remote database. Remember to use the same IP address you used in your remote database test earlier.

/** The name of the database for WordPress */
define('DB_NAME', 'wordpress');

/** MySQL database username */
define('DB_USER', 'wordpressuser');

/** MySQL database password */
define('DB_PASSWORD', 'password');

/** MySQL hostname */
define('DB_HOST', 'database_server_ip');

Close the file when you are finished. This is actually the only part of the entire configuration that explicitly links our web server and database server.

Next, we’ll need to create the directory structure that we set up in our Nginx server block configuration. If you recall, I’m using “example.com” for my demonstration, but you should use whatever you specified in your Nginx configuration:

sudo mkdir -p /var/www/example.com

We will then copy the files and directories found in our ~/wordpress directory to the new document root that we just created:

sudo cp -r ~/wordpress/* /var/www/example.com

Now all of our files are in place. The only thing left to do is modify our permissions and file ownership a little bit. We should start by moving into our server’s document root:

cd /var/www/example.com

We are going to give all of the files in this directory to our web server user, which is called www-data:

sudo chown -R www-data:www-data *

We still want to be able to edit these files as our normal, non-root user though, so we can add our regular, non-root account on our web server to the web server group. We can then give that group permissions to modify files in this directory:

sudo usermod -a -G www-data your_user
sudo chmod -R g+rw /var/www/example.com

Set Up the Site through the Web Interface

Now, all you need to do is complete the installation through the web interface.

Navigate to your domain name (or public IP address) associated with your web server:

http://example.com

You should see the WordPress installation screen where you need to fill out your relevant information:

WordPress admin setup

Once you have set your configuration, you will need to log into the application using the account you just created:

WordPress admin login

You will be taken to the administration dashboard, where you can get started configuring your site:

WordPress admin dashboard

Restrict Remote Database Permissions

When you are finished configuring your WordPress, you should go back and revoke some of your privileges for your remote database user.

Most of the database privileges are not needed for daily operation and will only need to be enabled during updates or with plugin installations. Keep this in mind in case you get an error when performing administrative actions after taking these steps.

Certain plugins may also require additional privileges. Investigate each plugin to see what it requires and consider choosing plugins that requires the least amount of additional access.

Log into your database server. Afterwards, log into MySQL using the MySQL root account:

mysql -u root -p

Type your password to gain access to the prompt.

You can see the current permissions of your remote user by typing:

show grants for 'wordpressuser'@'web_server_IP';
+---------------------------------------------------------------------------------------------------------------------------+
| Grants for wordpressuser@xx.xxx.xxx.xxx                                                                                   |
+---------------------------------------------------------------------------------------------------------------------------+
| GRANT USAGE ON *.* TO 'wordpressuser'@'xx.xxx.xxx.xxx' IDENTIFIED BY PASSWORD '*5FD2B7524254B7F81B32873B1EA6D681503A5CA9' |
| GRANT ALL PRIVILEGES ON `wordpress`.* TO 'wordpressuser'@'xx.xxx.xxx.xxx'                                                 |
+---------------------------------------------------------------------------------------------------------------------------+
2 rows in set (0.00 sec)

The “usage” permission actually means no real privileges, so we don’t have to worry about that. The second privilege line is the one that we set initially, allowing all privileges on the wordpress database.

The procedure for applying new privileges that are more restrictive than the current privileges is actually two steps.

First, we need to revoke all of the current privileges. Do this by typing something like this:

REVOKE ALL PRIVILEGES on wordpress.* FROM 'wordpressuser'@'web_server_IP';

If we ask for the current grants now, we’ll see that the second line is gone:

show grants for 'wordpressuser'@'web_server_IP';
+---------------------------------------------------------------------------------------------------------------------------+
| Grants for wordpressuser@10.128.213.175                                                                                   |
+---------------------------------------------------------------------------------------------------------------------------+
| GRANT USAGE ON *.* TO 'wordpressuser'@'xx.xxx.xxx.xxx' IDENTIFIED BY PASSWORD '*5FD2B7524254B7F81B32873B1EA6D681503A5CA9' |
+---------------------------------------------------------------------------------------------------------------------------+
1 row in set (0.00 sec)

Now, we can add the privileges that we would like back to the account. We need UPDATE, INSERT, SELECT, and DELETE privileges for day-to-day usage:

GRANT SELECT,DELETE,INSERT,UPDATE ON wordpress.* TO 'wordpressuser'@'web_server_ip';

If we check again, we can see our granular privileges are now set.

To tell MySQL to re-read the privileges table to implement our changes, we can type:

FLUSH PRIVILEGES;

Afterward, exit out of MySQL again:

exit

Conclusion

If you were following along, you should now have a good understanding of how to get a remote database talking with your application. While we went over some WordPress-specific steps, the general idea, especially those related to MySQL configuration and user privileges, are applicable in most remote MySQL situations.

Thanks for learning with the DigitalOcean Community. Check out our offerings for compute, storage, networking, and managed databases.

Learn more about our products

About the author(s)

Justin Ellingwood
Justin Ellingwood
See author profile
Category:
Tutorial
Tags:

Still looking for an answer?

Ask a questionSearch for more help

Was this helpful?
 
66 Comments
Leave a comment...

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!

Thank you for this tutorial, it’s exactly what I was looking for to get into the subject!

Curently, I’ve tested this between two droplets acting with the private networking addresses, so in Wordpress’ setup, I’ve entered the private IP of the mySQL droplets.

But is there any negative point in using an IP in Wordpress (or actually, any apps for that matter) rather than a properly formed URL? My guess is that there is actually nothing wrong with it (or nothing could go wrong with it!), but I just want to make sure ! :)

Andrew SB
DigitalOcean Employee
DigitalOcean Employee badge
April 30, 2014

@Jean-Philippe: Nope, nothing wrong with that at all. The main reason that we use domain names to access web sites instead of IP addresses is that we humans have a much harder time remembering strings of numbers rather than a nicely formated name! One other thing that domains names allow is having a stable identifier if the IP address behind it changes. So just remember to change the references to the IP address if you spin up a new database server with a different IP.

Why not just create a hosts file entry for the database IP so that can be referenced and/or updated if needed in one spot?

@AndrewSB thanks for that. I realized soon after that my concerns where out of this world. I had my first thought early in the morning thinking about search engines, can’t really figure why I associated adresses that are only seen by PHP and that. Well all have thoses moments I guess ;-)

One issue I see here is that you are relying solely on MySQL to handle authentication with @webserver_IP. This exposes MySQL to the world still, and allows brute-force attacks on the MySQL port. This will mostly fail, but regardless it is open to attack.

There are two ways to secure this properly.

  1. Use a firewall to ensure port 3306 is exposed to only the web server IP. Install the likes of CSF on your server, or use your cloud providers Access & Security provisions if available to enforce this.

  2. Have MySQL listen only on 127.0.0.1 and have the web server create a VPN or SSH tunnel to the database server so that it can access MySQL locally via the tunnel.

@kevin that is good advice. How would I make sure 3306 is open only to the web server IP and mysql listening only on 127.0.0.1 ? Thanks

I’ve thought about trying this but was concerned about the latency of having the database on another server. I imagine the effect is pretty small if both servers are in the same data center. But what if they’re in different data centers? Would the latency be an continuous issue for every read / write?

I used this to install opencart, everything worked fine then i shut both droplets down to make a backup and when i restarted them the site would no longer open. Apache error logs from webserver below

http://pastebin.com/EwfNad9U

In my testing, doesn’t the bind-address have to be set to 0.0.0.0 to access both from the local machine and the remote machine? I had trouble restarting mysql after changing to only the public ip and couldn’t connect to it. Researching the issue, seemed the common suggestion was to use 0.0.0.0. Or am I missing something?

Kamal Nasser
DigitalOcean Employee
DigitalOcean Employee badge
July 27, 2014

@neil: You can set bind-address to 0.0.0.0 but that means it will listen on all network interfaces including the public interface. It’s recommended that you keep it listening on the private interface and configuring the mysql client to connect to the private IP instead of 127.0.0.1.

You can do that by passing -h 1.2.3.4 to the mysql command where 1.2.3.4 is your droplet’s private IP address, or by adding the following to ~/.my.cnf:

[client]
host=1.2.3.4

YOU’RE A GENIUS and I LOVE YOU!!! thank you thank spank you.

Now, I was getting that whole not being able to restart my mysql with the ip address I was putting in. I have no idea with what you meant bypassing in the -h 1.2.3.4 thing into mysql. Can you clear that up a little for example… should we put the binder back to 121.0.0.1 and then just put host=1.2.3.4 underneath the client like you show above?

Also, to the magic of what we installed that allows us to client tools for mysql… what are those client tools and how can we read more up on them… do they have a specific name?

****Lastly, and probably most importantly… What if we are running a virtual machine farm for the cores of our sites… an availability set… Each vm would have it’s own IP… so how do we bind multiple ip’s for the mysql to listen to? and furthermore we would need to only use one ip for the username as well. is this correct? what can be done in this regard?

Kamal Nasser
DigitalOcean Employee
DigitalOcean Employee badge
August 8, 2014

@xtianus: I would recommend setting bind-address to your droplet’s private IP address which you can get by running the following command:

ifconfig | grep -C 1 eth1 | grep inet | awk -F' ' '{ print $2 }' | awk -F: '{ print $2 }'

Then, configure ~/.my.cnf as described in my previous comment.

The default MySQL CLI client is simply mysql, you can start it by running:

mysql -u username -p

You can also use a GUI MySQL client such as MySQL Workbench, you will need to use the SSH Tunnel connection mode: Set the MySQL Host to your droplet’s private IP address, and the SSH Host to your droplet’s public IP address and fill in the rest of the credentials.

What if we are running a virtual machine farm for the cores of our sites… an availability set… Each vm would have it’s own IP… so how do we bind multiple ip’s for the mysql to listen to? and furthermore we would need to only use one ip for the username as well. is this correct? what can be done in this regard?

I believe you will need to bind it to the shared virtual IP address.

the above command ipconfig… where do you do that at?

Kamal Nasser
DigitalOcean Employee
DigitalOcean Employee badge
August 9, 2014

@xtianus: On the droplet.

I am sorry what is the droplet… let me google that and find out.

Moreover, I can’t hit the DB from my other server connection. I keep getting this error

ERROR 1130 (00000): Host ‘‘xxx.xx.xxx.xxx’’ is not allowed to connect to this MySQL server

I’ve noticed something odd in your explanation of how to use the wp-config file and the way the database is setup… Follow with me. Oh and I still can only use 0.0.0.0 my.cef file setting.

The server which holds the wp-config file has a setting of the IP of which is where the mysql database is located. Ok that seems about right. The file is pointing to the external db…

With that said the next part is that I don’t actually have a setup of my mysql WP database analogous to the wp-config file. I have a localhost (which I am not using) and I setup a user account (same as wp-config file one) with the IP that is of my wp-config file location…

Point is, my DB user settings don’t have any analogous settings to my wp-config file. How does that work?

Very very good tutorial.

Short & Sweet article, now I have connected to Remote DB, to a existing webserver (magento),

1)how can I manage mysql using phpMyadmin?

  1. I am using ssl certificate for few sections of website like customer account & shopping cart etc, but now the issue is that the browser is not recognizing the ssl certificate and throwing warning, proceed with own risk, so what I need to do that my I can use the same certificate for Remote DB server as well?

Appreciate quick response. TIA

Since both droplets are in the same datacenter, I tried using the private address for both, but I could never connect. I had to use the public. Does that degrade the response time? Thank you.

Justin Ellingwood
DigitalOcean Employee
DigitalOcean Employee badge
March 30, 2015

@joshlsullivan: I’m sorry that you had trouble with the private networking. It should work if configured correctly, so it may be worth giving it another try. In reference to using a public IP address, you it might result in a longer response time. However, I think the more concerning aspect is that it is less secure to use the public network. Even if using the private network within a datacenter, you should look into securing your connections either with firewall rules, tunneling through a VPN connection, or preferably both. Good luck!

@jellingwood But if I use private IPs, that’s as secure as using VPN? I’m not quoting you on this, just making sure I’m taking all the necessary steps.

Justin Ellingwood
DigitalOcean Employee
DigitalOcean Employee badge
March 30, 2015

@joshlsullivan: No, the DigitalOcean private network is a local network for the entire datacenter. This means that any Droplets located within the datacenter will be on the same private network (regardless of the Droplet owner). Using private networking is more secure than public networking because your server is exposed to fewer machines, but the network interface will still be accessible to other machines. A VPN option would ensure that only your servers be able to talk to each other.

This comment has been deleted

    I’m trying to connect remotely from a GIU (Sequel Pro, not to name it), via an SSH Tunnel. It fails at connecting to the database (but SSH is fine of course)…

    What would be the correct way to set things up in this case?

    Justin Ellingwood
    DigitalOcean Employee
    DigitalOcean Employee badge
    April 2, 2015

    @CHYZ 94.3: How exactly is it failing? Are you unable to connect at all or is it denying you because of a permissions or an authentication problem?

    Typically, if you want to tunnel the port, you’d connect using something like this:

    ssh -L 3306:127.0.0.1:3306 user@mysql_ip_address
    

    You can then point your GUI on your local computer to 127.0.0.1:3306. If you can get that far, and it is giving you permissions errors (not connection errors), you need to make sure the user@localhost MySQL user has access to the database. If it is giving you authentication errors, make sure that you are using the password you set for the user@localhost MySQL account and not the one you set up for remote connections (an SSH tunnel is effectively a local connection).

    It’s hard to give more specific advice without more details. Hopefully that helps.

    Nicely written blog. World just looks so easy now :)

    Thank you for the great tutorial. i was able to separate my wordpress site and sql successfully. The only issue i am having is i am no longer able to connect remotely to sql using sequel pro. I use ssh tunneling to port 22 using my user name and password. for db im also using the root username and the correct mysql password. the mysql host address is set to localhost, also i tried 127.0.0.1; the port i am using is 3306; Lastly the server i setup for sql is ubuntu 14.04 running only mysql which i installed myself. How can i enable remote access to the database? Am i missing something.

    Thanks for the wonderful tutorial. I have used percona instead of mysql and magento instead of wordpress. When i am trying to connect to the database from localhost it is working fine but when i tried to connect it remote getting this below error.

    ERROR 1130 (HY000): Host ‘104.236.38.106’ is not allowed to connect to this MySQL server

    I have installed the mysql client on webserver too.

    I am using UFW and IP tables on both the droplets and port for mysql 3306 is open.

    Please let me know if i am missing something.

    Thanks in advance.

    Thanks you for this tutorial. If I have a total of 3 servers, 2 for the app and 1 for the DB. How do I then set two ip addresses in the “bind-address” line? I do have my servers in a shared network here on DO. And where can I find the shared virtual IP address? Also is DO’s private network fast enough for this use? Or should I just connect my servers with their ips outside a private network Thanks

    @jellingwood

    1. Does private networking need to be enabled on both app & db server?
    2. Does this work if I have forced site-wide SSL on my app server?
    Justin Ellingwood
    DigitalOcean Employee
    DigitalOcean Employee badge
    June 30, 2015

    @sugarhill: Yes, both servers must have private networking enabled to communicate over that interface. There’s no reason that SSL shouldn’t work over a private network connection.

    Hi thank you for the tutorial, I followed it and it works but I have a problem, I also installed fail2ban firewall following this tutorial:

    https://www.digitalocean.com/community/tutorials/how-fail2ban-works-to-protect-services-on-a-linux-server”.

    The problem is as soon as I insert a drop rule in the ip tables

    “sudo iptables -A INPUT -j DROP” or

    “sudo iptables -P INPUT DROP”

    My webserver cannot communicate with the mysql server. I tried inserting

    “-A INPUT -i eth0 -p tcp -m tcp --dport 3306 -j ACCEPT” and “-A INPUT -s mysqlserverip -i eth0 -p tcp -m tcp --dport 3306 -j ACCEPT” into iptables but still doesn’t work.

    What am I doing wrong. Why fail2ban doesn’t allow mysql connections?

    If anyone is having trouble connecting to their remote mysql server when they activate fail2ban with iptables rules I found a solution and it seems to be working: I addes following rules;

    sudo iptables -A INPUT -p tcp -s your_webserver_ip  --sport 1024:65535 -d  your_db_server_ip --dport 3306 -m state --state NEW,ESTABLISHED -j ACCEPT
    
    sudo iptables -A OUTPUT -p tcp -s  your_db_server_ip --sport 3306 -d  your_webserver_ip  --dport 1024:65535 -m state --state ESTABLISHED -j ACCEPT
    

    The rules above gives access to MySQL database server from web server only.

    This tutorial is indispensable, constantly referring back to it. Thanks Justin!

    I think I messed up and unintentionally I randomly answered the questions which come after “sudo mysql_secure_installation” and I think I answered the remote access question with No which I should just hit ENTER to select the default options which will remove some test databases and lock down access. I’m not sure what’s exactly the issue but once I’m trying to run “mysql -u root -p” and enter password I get this error: ERROR 2002 (HY000): Can’t connect to local MySQL server through socket ‘/var/run/mysqld/mysqld.sock’ (2)

    Please help!

    Justin Ellingwood
    DigitalOcean Employee
    DigitalOcean Employee badge
    September 1, 2015

    @alialani: Hello. Where are you typing mysql -u root -p? Is it on the database server or on the web server? And at what point (is this the very first time you’re trying to connect to the database)?

    The first thing I would check is whether MySQL is actually up and running. You can check by typing:

    1. sudo service mysql status

    If it reports that MySQL isn’t currently running, you’ll have to start it with:

    1. sudo service mysql start

    Re-check the status after you run that command. If it still says it’s down, check the logs by trying this command:

    1. sudo less /var/log/mysql.err

    Also, if you think you’ve accidentally answered a question incorrectly in the mysql_secure_installation prompt, you can run the command again to change your mind:

    1. sudo mysql_secure_installation

    Hello,

    I have 4 wordpress sites on my webserver now i want to move the mysql server away from the main server, how do i do this? I don’t understand your tutorial

    Can you show me how to create the complete database with the ip of my web app?

    CREATE DATABASE your-db-name;
    CREATE USER your-db-username@localhost;
    SET PASSWORD FOR your-db-username@localhost= PASSWORD("your-db-password");
    GRANT ALL PRIVILEGES ON your-db-name.* TO your-db-username@localhost IDENTIFIED BY 'your-db-password';
    
    Justin Ellingwood
    DigitalOcean Employee
    DigitalOcean Employee badge
    October 5, 2015

    @iamkingsleyf: Hello! If you need to set up access to your database from your web server, you should just need to type the last three commands again, but replace “localhost” with the IP address of your web app / web server. Afterwards, remember to flush the changes to disk by typing:

    FLUSH PRIVILEGES;
    

    Hope that helps!

    Thanks, i did but was not impressed,

    Nicely written blog. World just looks so easy now :)

    Thank you for the great tutorial. i was able to separate my wordpress site and sql successfully. The only issue i am having is i am no longer able to connect remotely to sql using sequel pro. I use ssh tunneling to port 22 using my user name and password. for db im also using the root username and the correct mysql password. the mysql host address is set to localhost, also i tried 127.0.0.1; the port i am using is 3306; Lastly the server i setup for sql is ubuntu 14.04 running only mysql which i installed myself. How can i enable remote access to the database? Am i missing something.

    Thanks for the wonderful tutorial. I have used percona instead of mysql and magento instead of wordpress. When i am trying to connect to the database from localhost it is working fine but when i tried to connect it remote getting this below error.

    ERROR 1130 (HY000): Host ‘104.236.38.106’ is not allowed to connect to this MySQL server

    I have installed the mysql client on webserver too.

    I am using UFW and IP tables on both the droplets and port for mysql 3306 is open.

    Please let me know if i am missing something.

    Thanks in advance.

    Thanks you for this tutorial. If I have a total of 3 servers, 2 for the app and 1 for the DB. How do I then set two ip addresses in the “bind-address” line? I do have my servers in a shared network here on DO. And where can I find the shared virtual IP address? Also is DO’s private network fast enough for this use? Or should I just connect my servers with their ips outside a private network Thanks

    @jellingwood

    1. Does private networking need to be enabled on both app & db server?
    2. Does this work if I have forced site-wide SSL on my app server?
    Justin Ellingwood
    DigitalOcean Employee
    DigitalOcean Employee badge
    June 30, 2015

    @sugarhill: Yes, both servers must have private networking enabled to communicate over that interface. There’s no reason that SSL shouldn’t work over a private network connection.

    Hi thank you for the tutorial, I followed it and it works but I have a problem, I also installed fail2ban firewall following this tutorial:

    https://www.digitalocean.com/community/tutorials/how-fail2ban-works-to-protect-services-on-a-linux-server”.

    The problem is as soon as I insert a drop rule in the ip tables

    “sudo iptables -A INPUT -j DROP” or

    “sudo iptables -P INPUT DROP”

    My webserver cannot communicate with the mysql server. I tried inserting

    “-A INPUT -i eth0 -p tcp -m tcp --dport 3306 -j ACCEPT” and “-A INPUT -s mysqlserverip -i eth0 -p tcp -m tcp --dport 3306 -j ACCEPT” into iptables but still doesn’t work.

    What am I doing wrong. Why fail2ban doesn’t allow mysql connections?

    If anyone is having trouble connecting to their remote mysql server when they activate fail2ban with iptables rules I found a solution and it seems to be working: I addes following rules;

    sudo iptables -A INPUT -p tcp -s your_webserver_ip  --sport 1024:65535 -d  your_db_server_ip --dport 3306 -m state --state NEW,ESTABLISHED -j ACCEPT
    
    sudo iptables -A OUTPUT -p tcp -s  your_db_server_ip --sport 3306 -d  your_webserver_ip  --dport 1024:65535 -m state --state ESTABLISHED -j ACCEPT
    

    The rules above gives access to MySQL database server from web server only.

    This tutorial is indispensable, constantly referring back to it. Thanks Justin!

    I think I messed up and unintentionally I randomly answered the questions which come after “sudo mysql_secure_installation” and I think I answered the remote access question with No which I should just hit ENTER to select the default options which will remove some test databases and lock down access. I’m not sure what’s exactly the issue but once I’m trying to run “mysql -u root -p” and enter password I get this error: ERROR 2002 (HY000): Can’t connect to local MySQL server through socket ‘/var/run/mysqld/mysqld.sock’ (2)

    Please help!

    Justin Ellingwood
    DigitalOcean Employee
    DigitalOcean Employee badge
    September 1, 2015

    @alialani: Hello. Where are you typing mysql -u root -p? Is it on the database server or on the web server? And at what point (is this the very first time you’re trying to connect to the database)?

    The first thing I would check is whether MySQL is actually up and running. You can check by typing:

    1. sudo service mysql status

    If it reports that MySQL isn’t currently running, you’ll have to start it with:

    1. sudo service mysql start

    Re-check the status after you run that command. If it still says it’s down, check the logs by trying this command:

    1. sudo less /var/log/mysql.err

    Also, if you think you’ve accidentally answered a question incorrectly in the mysql_secure_installation prompt, you can run the command again to change your mind:

    1. sudo mysql_secure_installation

    Hello,

    I have 4 wordpress sites on my webserver now i want to move the mysql server away from the main server, how do i do this? I don’t understand your tutorial

    Can you show me how to create the complete database with the ip of my web app?

    CREATE DATABASE your-db-name;
    CREATE USER your-db-username@localhost;
    SET PASSWORD FOR your-db-username@localhost= PASSWORD("your-db-password");
    GRANT ALL PRIVILEGES ON your-db-name.* TO your-db-username@localhost IDENTIFIED BY 'your-db-password';
    
    Justin Ellingwood
    DigitalOcean Employee
    DigitalOcean Employee badge
    October 5, 2015

    @iamkingsleyf: Hello! If you need to set up access to your database from your web server, you should just need to type the last three commands again, but replace “localhost” with the IP address of your web app / web server. Afterwards, remember to flush the changes to disk by typing:

    FLUSH PRIVILEGES;
    

    Hope that helps!

    Good tutorial, can you also write up tutorial with MariaDB instead of MySQL?

    Yes I second this request - as there are differences? Also how about on Ubunut 14.04. Would like to know very much!

    hello

    Thank you for your perfect article . I used this article for loadbalance my webservers and also I use 3mysql server for high availability . now how can I connect my webservers and mysql servers together? would you please help me about this?

    Followed this tutorial to the letter, and had it all going well. Then, I installed LAMP over it so that I could install PHPMyAdmin…and it screwed everything up. Is there a trick to installing PHPMyAdmin here?

    @asb @kamaln7 great tutorial. but got 2 small questions,

    actually i wan to use http2 so can i install nginx 1.9.12 at first or i need to install stable version then have to upgrade to mainline?

    its a bit foolish question, is it a must to have 2 server block on port 80 & 443 instead just one on 443? actually my site will be only on ssl so i was thinking to skip redirect from http to https and serve only https. will it work if i only have listen 443 and enable HSTS policy on ssl configuration or listen 80 & 443 both is must?

    thanks in advance

    thanks for the awesome tutorial. got few small questions though

    Q1. can i install phpmyadmin? if yes it should be installed on web server droplet or database droplet?

    Q2. when using private networking, can i connect with my database remotely through 3rd party software? if this is possible i wouldnt worry about phpmyadmin

    @jellingwood Today i just tried to follow your tutorial on ubuntu 16.04. unfortunately i got stuck when i wanted to create a database.

    root@database:~# sudo mysql_install_db
    2016-05-12 05:45:37 [WARNING] mysql_install_db is deprecated. Please consider switching to mysqld --initialize
    2016-05-12 05:45:37 [ERROR]   The data directory needs to be specified.
    
    

    could you please write a separate tutorial for ubuntu 16.04? i would really appreciate.

    @asb

    hello bro, i cant connect to mysql from my webserver. but from database server i can without any issue. actually i did enabled firewall after creating the droplet. can that make any issue here? if yes how can i disable it?

    this is the error massage i get when i try to connect from my webserver

    admin@droplet:~$ mysql -u admin -h DB_PRIVATE_IP -p
    Enter password:
    ERROR 2003 (HY000): Can't connect to MySQL server on 'DB_PRIVATE_IP' (110)
    
    

    Know can you guys do the same but for node and postgresql ;D

    This comment has been deleted

      Great post.

      I came here not because of WP but because I wanted to allow MySql to accept remote connections.

      Particularly MySQL Workbench running on a MAC OS El Capitan.

      At first I got confused by the command

      GRANT USAGE ON *.* TO 'wordpressuser'@'xx.xxx.xxx.xxx' IDENTIFIED BY PASSWORD 'password' 
      

      as I tried to set xx.xxx.xxx.xxx as the IP to the server address where the MySQL is,

      but as I tried to reach it from other server with ip address ‘zzz.zzz.zzz.zzz’ I got the error

      Host 'zzz.zzz.zzz.zzz' is not allowed to connect to this MySQL server
      

      What I wanted to achieve and maybe some people here too, I got through the alternative I found here

      CREATE USER 'username'@'%' IDENTIFIED BY 'password';
      
      GRANT ALL PRIVILEGES ON 'database'.* TO 'username'@'%' ;
      

      When I tried this in Centos 7 with mariadb (5.5.50), I could not find the bind-address parameter. The configuraltion files were found as /etc/my.cnf /etc/my.cnf.d/client.cnf /etc/my.cnf.d/mysql-clients.cnf /etc/my.cnf.d/server.cnf

      None of the files have this bind-address parameter. Can I add these parameters in any of these files?

      Trying to setup mysql. I pretty much don’t know anything about command line. I try to change the 127.0.0.1 to the public IP but I can’t even type it in. I type and nothing showsup. I’m on a laptop, shouldnt matter I guess. Also when it comes to exiting the text editor, I press N, write No but nothing happens. What is up.

      I see only this “Error establish database connection” i am on lamp Any help thank you

      I’m stuck in #2, Configure MySQL to Allow Remote Access. I run the command sudo nano /etc/mysql/my.cnf and don’t get any lists in brackets. Only a bunch of ^G ^X choices that don’t work when you type them. Any recommendations?

      Thanks! These instructions still worked for me with one change:

      include fastcgi_params; should be include fastcgi.conf;

      my my.cnf does not have a bind-address variable, or the mentioned sections in this article…

      How would this guide change if we need to setup 2 webserver to connect to the same db server, for use with a load balanced architecture?

      Hi Guys,

      Thank you for this tutorial, I would like to set up the same configuration on CentOS. How do we configure it? Can you please let me know.

      Thanks Suresh

      Thanks for the tutorial.

      I want to know about droplet sizes, which one needs more power, web server or mysql server? I mean, does web server use more cpu & ram than mysql or vice versa?

      Hello there, Can we use Load Balancer with 2 DB server ? Is it possible ? And can use load balancer IP as remote db ip ?

      When I execute: sudo nano /etc/mysql/my.cnf, my file doesn’t have [mysqld] section. Any reason this would happen?

      If the MySQL server is for several servers with WordPress how do we ensure the cpanel backups are in sync with the database server?

      Say we replicated every hour, the data on the server could be up to 59 minutes older?

      I get following error when i type sudo mysql_install_db:

      2018-05-27 06:05:11 [WARNING] mysql_install_db is deprecated. Please consider switching to mysqld --initialize 2018-05-27 06:05:11 [ERROR] The data directory needs to be specified.

      You guys should update your articles

      This comment has been deleted

        Hello, great article. Im having issues with this for 7 days and can’t figure it out. I have read many articles. I think my issue is the last step. My question is, if I already have an existing site and created a new droplet for mysql and I get up to the point of testing connections as in your article, do I still need to do the “Set Up Your Web Server” portion if all those directories are already created on my current droplet? My site does not connect and eventho I can do it thru ssh from server to server, since files are on one droplet and database on the other, there seems something that is blocking files or connection as I get “Error Establishing Connection” not sure if the “Set Up Webserver” settings what allows files to communicate. Thank You

        Try DigitalOcean for free

        Click below to sign up and get $200 of credit to try our products over 60 days!

        Sign up

        Join the Tech Talk
        Success! Thank you! Please check your email for further details.

        Please complete your information!

        Become a contributor for community

        Get paid to write technical tutorials and select a tech-focused charity to receive a matching donation.

        DigitalOcean Documentation

        Full documentation for every DigitalOcean product.

        Resources for startups and SMBs

        The Wave has everything you need to know about building a business, from raising funding to marketing your product.

        Get our newsletter

        Stay up to date by signing up for DigitalOcean’s Infrastructure as a Newsletter.

        New accounts only. By submitting your email you agree to our Privacy Policy

        The developer cloud

        Scale up as you grow — whether you're running one virtual machine or ten thousand.

        Get started for free

        Sign up and get $200 in credit for your first 60 days with DigitalOcean.*

        *This promotional offer applies to new accounts only.