How to Connect to a MySQL or MariaDB Database
Traducciones al EspañolEstamos 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.
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
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.
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.
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.
NoteThe steps in this guide are written for a non-root user. Commands that require elevated privileges are prefixed withsudo
. If you’re not familiar with thesudo
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
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 from192.0.2.0
and hasSELECT
,INSERT
,UPDATE
, andDELETE
permissions on theexample_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';
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 [...]
Using your preferred text editor, locate the
[mysqld]
section and abind-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.Once you locate the
bind-address
parameter, change it from the default127.0.0.1
to0.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 [...]
Restart the MySQL service.
sudo systemctl restart mysqld
Access the Database
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.
- If your local machine is running a Linux distribution, you can follow our How to Install MySQL guide.
- For other distributions, refer to the official MySQL installation documentation.
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
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