mysql – MySQL server client tools

The mysql command is a client tool for MySQL server, which is used for managing databases, executing queries, and performing various operations on MySQL databases. It is a command-line tool that allows users to interact with the MySQL server directly.

Overview

To use the mysql command, you need to have a MySQL server installed and running on your system. Once you have installed MySQL, you can start the MySQL server by running the following command:

sudo systemctl start mysql

After starting the MySQL server, you can connect to it using the mysql command as follows:

mysql -u <username> -p

Replace <username> with the username of the MySQL user you want to connect as. You will be prompted to enter the password for the user.

Once you are connected to the MySQL server, you can execute SQL queries, create and manage databases, and perform various other operations.

Examples

  1. Create a new database:
CREATE DATABASE mydatabase;
  1. Use a database:
USE mydatabase;
  1. Create a table:
CREATE TABLE mytable (
  id INT(6) UNSIGNED AUTO_INCREMENT PRIMARY KEY,
  firstname VARCHAR(30) NOT NULL,
  lastname VARCHAR(30) NOT NULL,
  email VARCHAR(50),
  reg_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP
);
  1. Insert data into a table:
INSERT INTO mytable (firstname, lastname, email)
VALUES ('John', 'Doe', 'john.doe@example.com');
  1. Select data from a table:
SELECT * FROM mytable;

Options

The mysql command provides several options that you can use to customize its behavior. The following table lists the available options:

Option Description
-u Specifies the MySQL username to use for the connection.
-p Prompts for the MySQL user password to use for the connection.
-h Specifies the hostname or IP address of the MySQL server to connect to.
-P Specifies the port number of the MySQL server to connect to.
-D Specifies the name of the default database to use.
-e Executes the specified SQL statement and exits.
-t Displays query output in tabular format.
-v Displays verbose output.
-V Displays the MySQL client version and exits.

Troubleshooting tips

  • If you are unable to connect to the MySQL server using the mysql command, make sure that the MySQL server is running and that you have provided the correct username and password.
  • If you are experiencing issues with SQL queries, make sure that your syntax is correct and that you are using the correct database and table names.
  • If you are experiencing performance issues, try optimizing your SQL queries, or consider upgrading your hardware or MySQL server configuration.

Notes

  • The mysql command is a powerful tool for managing MySQL databases, but it can also be dangerous if used improperly. Be sure to backup your data regularly and use caution when executing SQL queries.
  • The mysql command is just one of many MySQL client tools available. Other tools include mysqldump for backing up MySQL databases, and mysqladmin for managing MySQL server configuration.