How to set up a remote MySQL database connection on Linux

When to set up a remote database connection

This topic discusses how to set up a connection from your web server to a MySQL server on another host.

This is an advanced topic that should be used only by an experienced network administrator or database administrator. You must have root access to the file system and you must be able to log in to MySQL as root.

Prerequisites

Before you begin, you must:

High availability

Use the following guidelines to configure remote database connections if your web server or database server are clustered:

  • You must configure a connection for each web server node
  • Typically, you configure a database connection to the database load balancer; however, database clustering can be complex and configuring it is up to you. For more information, see MySQL documentation.

Resolving connection issues

If you have issues connecting to either host, first ping the other host to make sure it’s reachable. You also might need to allow connections from one host to another by modifying firewall and SELinux rules (if you use SELinux).

Create the remote connection

To create a remote connection:

  1. On your database server, as a user with root privileges, open your MySQL configuration file.To locate it, enter the following command:mysql --help The location displays similar to the following:Default options are read from the following files in the given order: /etc/my.cnf /etc/mysql/my.cnf /usr/etc/my.cnf ~/.my.cnf On Ubuntu 16, the path is typically /etc/mysql/mysql.conf.d/mysqld.cnf.
  2. Search the configuration file for bind-address.If it exists, change the value as follows.If it doesn’t exist, add it anywhere except the [mysqld] section.bind-address = <ip address of your Magento web node> See MySQL documentation, especially if you have a clustered web server.
  3. Save your changes to the configuration file and exit the text editor.
  4. Restart the MySQL service:
For CentOS: 
  service mysqld restart

For Ubuntu: 
  service mysql restart

If MySQL fails to start, look in syslog for the source of the issue. Resolve the issue using MySQL documentation or another authoritative source.

Grant access to a database user

To enable your web node to connect to the database server, you must grant a web node database user access to the database on the remote server.

This example grants the root database user full access to the database on the remote host.

To grant access to a database user:

  1. Log in to the database server.
  2. Connect to the MySQL database as the root user.
  3. Enter the following command:GRANT ALL ON <local database name>.* TO <remote web node username>@<remote web node server ip address> IDENTIFIED BY '<database user password>'; For example,GRANT ALL ON remote_database.* TO [email protected] IDENTIFIED BY 'dbuserpassword';

If your web server is clustered, enter the same command on every web server. You must use the same username for every web server.

Verify database access

On your web node host, enter the following command to verify the connection works:

mysql -u <local database username> -h <database server ip address> -p

If the MySQL monitor displays as follows, the database is ready for connection

Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 213
Server version: 5.6.26 MySQL Community Server (GPL)

Copyright (c) 2000, 2015, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

If your web server is clustered, enter the command on each web server host.

Scroll to Top