How to Connect to a MySQL or MariaDB Database

Traducciones al Español
Estamos traduciendo nuestros guías y tutoriales al Español. Es posible que usted esté viendo una traducción generada automáticamente. Estamos trabajando con traductores profesionales para verificar las traducciones de nuestro sitio web. Este proyecto es un trabajo en curso.
Create a Linode account to try this guide with a $100 credit.
This credit will be applied to any valid services used during your first 60 days.

This guide shows you how to connect to a remote MySQL or MariaDB database using the command line. You can also learn how to connect to a remote database via MySQL Workbench by using the referenced links at the end of this guide.

Before You Begin

  1. If you have not already done so, create a Linode account and Compute Instance. See our Getting Started with Linode and Creating a Compute Instance guides.

  2. Follow our Setting Up and Securing a Compute Instance guide to update your system. You may also wish to set the timezone, configure your hostname, create a limited user account, and harden SSH access.

  3. Install MySQL or MariaDB on your server. You can follow our guide on How to Install MySQL or on How to Install MariaDB. Use the Distribution drop down at the top of each guide to select the Linux distribution you want to install on.

Note
The steps in this guide are written for a non-root user. Commands that require elevated privileges are prefixed with sudo. If you’re not familiar with the sudo command, see the Linux Users and Groups guide.

How to Connect to a Remote Database from the Command Line

This section shows you how to connect to your database without an SSH connection. This can be useful if you want to limit SSH access but still permit database access.

Refer to our Create an SSH Tunnel for MySQL Remote Access to learn how to connect to your database using an SSH tunnel.

Configure the Database Server

  1. Make sure your database has a user set up to allow connections from your local machine’s IP address.

    The example below displays a series of commands to create a new MySQL/MariaDB user named example_user. The user accepts connections from 192.0.2.0 and has SELECT, INSERT, UPDATE, and DELETE permissions on the example_db database:

     CREATE user 'example_user'@'192.0.2.0' IDENTIFIED BY 'password';
     GRANT SELECT,INSERT,UPDATE,DELETE ON example-db.* TO 'example_user' IDENTIFIED BY 'password';
    
  2. Locate you database’s configuration files using the command below. The following command lists the files' default locations. The locations returned by the command may be different than those in the example shown below:

     sudo mysql --help
    
    [...]
    
    Default options are read from the following files in the given order:
    /etc/my.cnf /etc/mysql/my.cnf ~/.my.cnf
    
    [...]
  3. Using your preferred text editor, locate the [mysqld] section and a bind-address parameter.

    If you see any !includedir parameters in the files, you may also need to check the files in the locations those parameters designate.

  4. Once you locate the bind-address parameter, change it from the default 127.0.0.1 to 0.0.0.0. This enables external connections on the database.

    Also, if the file contains a skip-networking parameter, comment it out with a #.

    File: /etc/mysql/mysql.conf.d/mysqld.conf
     1
     2
     3
     4
     5
     6
     7
     8
     9
    10
    11
    12
    
    [...]
    
    [mysqld]
    
    [...]
    
    # skip-networking
    
    bind-address = 0.0.0.0
    
    [...]
        
  5. Restart the MySQL service.

     sudo systemctl restart mysqld
    

Access the Database

  1. You need to have the MySQL command-line, or CLI tool installed on your local machine to connect to the database. The installation methods below work for both MySQL and MariaDB.

  2. Issue the command below from your local machine to connect to the database. Replace 198.51.100.0 with the IP address for your database server.

     mysql -u example_user -p -h 198.51.100.0
    

    You can also specify the port to connect to the database. This is required if the database server is set up to use anything other than the default port (3306).

     mysql -u example_user -p -h 198.51.100.0 -P 3312
    
  3. You can verify your connection using the following command. This command fetches a list of databases that your current user has access to.

     SHOW DATABASES;
    
    +--------------------+
    | Database           |
    +--------------------+
    | example_db         |
    | information_schema |
    +--------------------+

How to Connect to a Database Remotely Using the MySQL Workbench Tool

Follow our Install MySQL Workbench for Database Administration guide for steps to install the MySQL Workbench tool on your local machine. This guide also shows you how to connect to a remote database via MySQL Workbench. These steps work whether your target database server is MySQL or MariaDB.

For more information, take a look at the official MySQL Workbench manual. You may also refer to MariaDB’s documentation on using the MySQL Workbench with MariaDB.

Conclusion

Now that you have your remote database connection, you may want to learn more about using MySQL/MariaDB and working with more advanced database operations. You can refer to our extensive list of MySQL guides and specific MariaDB guides to build your database management skills.

More Information

You may wish to consult the following resources for additional information on this topic. While these are provided in the hope that they will be useful, please note that we cannot vouch for the accuracy or timeliness of externally hosted materials.

This page was originally published on


Your Feedback Is Important

Let us know if this guide made it easy to get the answer you needed.


Join the conversation.
Read other comments or post your own below. Comments must be respectful, constructive, and relevant to the topic of the guide. Do not post external links or advertisements. Before posting, consider if your comment would be better addressed by contacting our Support team or asking on our Community Site.