Connect to a MySQL Database Using the mysql Command
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 MySQL database using mysql, the MySQL command-line client. This opens up a simple SQL shell environment, allowing you to perform SQL queries and commands on your database. If you require more advanced capabilities, consider using the MySQL Shell.
Obtain the connection details for the MySQL instance you wish to use. If you do not have a MySQL instance yet, you can create a Managed Database, deploy the MySQL Marketplace App, or install MySQL server (or MariaDB) on a Compute Instance. This instance must allow remote connections or you must run the mysql command from within same system.
Ensure mysql is installed and is compatible with the MySQL version on your database server. Run the following command on the system you intend on using to verify that mysql is installed.
This should inform you which version you are using. If the command is not found or you are not on a compatible version, see the Installing MySQL guide.
sudo. If you’re not familiar with the
sudocommand, see the Linux Users and Groups guide.
The main purpose of the mysql utility is to connect to a MySQL database server and open a simple SQL shell environment. The mysql command can be used to connect to either a local or remote database server. In the commands provided below, see the Common Command Options for information on each of the available options.
Local database server: Use this command when connecting to a MySQL Server instance running on the same machine you are using.
mysql -u [username] -p
Remote database server: In many cases, the database server is not on the same system you are using. In these cases, you can SSH in to the remote system (if permitted) and run the command above to connect to a local MySQL instance. Alternatively, you can use the mysql command to remotely connect to the database. If your MySQL server does not allow remote connections or your user cannot connect remotely, see Configure the Database Server to Allow Remote Connections.
mysql -h [host] -p [port] -u [username] -p
The following list is a collection of common options used with the mysqldump command. At minimum, the username and password is required. When connecting to a remote database server, the host (and perhaps the port) should be provided. For a full list of available options, reference the Command Options for Connecting to the Server documentation.
-u ): The username of your MySQL user. This user must have proper grants to access the database.
-p): Specifies that the user’s password is required for the connection. The password can be entered directly in the command itself (though that is not recommended due to security concerns) or the password can be omitted (by just using the
--passwordoption with no value). In the password is omitted, mysql prompts you for the password before connecting to the database. For more details about password security, see MySQL’s End-User Guidelines for Password Security.
-h ): The IP address or FQDN (fully qualified domain name) of the remote database server. You can omit this option from the command if you are connecting to a local MySQL instance on your same system.
-P ): The port number of that the MySQL database instance uses. This can be omitted if your MySQL instance uses the default port of
SSL Settings (
--ssl-mode): This controls if the connection should be encrypted. This can be set to
DISABLED(unencrypted - not recommended),
PREFERRED(tries an encrypted connection first before falling back to unencrypted), or
REQUIRED(fails if an encrypted connection can’t be established. If omitted, this option is automatically set to
PREFERRED. You can also set this to
VERIFY_IDENTITYto require an encrypted connection and either verify the CA certificate or both verify the CA certificate and the host name identity.
If you are frequently connecting to the same database, you can securely store many of these options (including the password). See the Securely Storing Credentials guide. Other options can be stored in an option file.
If you have installed the MySQL server yourself (not through a managed service) and wish to connect to a database remotely without first logging in to the database server through SSH, you may need to modify a few settings. 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.
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
DELETEpermissions on the
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 a
If you see any
!includedirparameters in the files, you may also need to check the files in the locations those parameters designate.
Once you locate the
bind-addressparameter, change it from the default
0.0.0.0. This enables external connections on the database.
Also, if the file contains a
skip-networkingparameter, comment it out with a
- File: /etc/mysql/mysql.conf.d/mysqld.conf
1 2 3 4 5 6 7 8
... [mysqld] ... # skip-networking bind-address = 0.0.0.0 ...
Restart the MySQL service.
sudo systemctl restart mysqld
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.
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.
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