Articles in MySQL Category

Using MySQL Command-line

MySQL comes with a command-line interface that let you run MySQL commands and SQL queries. While there are graphical tools like phpMyAdmin and MySQL Workbench, command-line interface will come in handy when you manage more databases and when you get more familiar with MySQL.

Based on your operating system, add MySQL to a global path variable (environment variable) so that your operating system’s command-line will identify mysql as a command from any directory (not only in installation directory).

Logging into MySQL

You can login to MySQL as root user (generally the user with all the privileges) by typing below command.

mysql -u root -p

Above command will prompt you to enter the password for user root. If your MySQL installation is new and you haven’t changed root password, most of the time root password is blank (just type Enter key).

After that, you would see a prompt like below that let you type commands. Prior to the prompt, you would see few instructions and MySQL version number.


Executing Commands

All SQL commands you type in MySQL prompt should have a semicolon (;) at the end. It won’t run the command till you enter a semicolon.

In addition to the SQL commands, MySQL has got its own set of commands. To see these commands, type help in MySQL prompt as below. These commands aren’t required to have a semicolon at the end. After typing a command hit Enter key to execute the command.

mysql> help

Multiple Line Commands

To achieve the clarity, you can span a command over multiple lines. Just hit Enter key after each line and MySQL will prompt an arrow indicating a new line. Following is a multiple line SQL command to create a data table.

mysql> create table `employee` (
    ->   `id` int(10),
    ->   `first_name` varchar(40),
    ->   `last_name` varchar(40),
    ->   `age` tinyint(3),
    ->   `joined_date` date,
    ->   `records` text,
    ->   primary key  (`id`)
    -> );

Logging out of MySQL

Use exit command to log out of MySQL.

mysql> exit
Where to Head from Here...
Share with Your Peers...

We Value Your Feedback...

We love to hear what you think about this article. Please provide your opinion, suggestions and improvements using following form. Note that submitted feedback is not displayed but we will get back to you if it needs a reply.