MySQL terminal (Terminal) management MySQL

Source: Internet
Author: User
Tags administrator password mysql login

MySQL has a lot of visual management tools, such as "Mysql-workbench" and "sequel-pro-". Now I am writing mysql Terminal command Operation article, is to strengthen my understanding of MySQL, always better than the use of graphical understanding, because I would like to write code. At the same time write these articles, is want to give you a reference, hope can also help you, have improved, this is why I write terminal operation MySQL article.

The following commands are provided based on the author's testing on Mac systems.

1. mysql Service status

1.1 Starting MySQL

1.2 Stop MySQL Service

1.3 Restart MySQL Service

1.4 Viewing the current MySQL version

2. Change the root administrator password for MySQL

3. mysql Terminal login

3.1 Terminal login (tedious)

3.2 Terminal Login (Simplified)

1. mysql Service status

1.1 Starting MySQL

/ Library/startupitems/mysqlcom/mysqlcom start

You need to enter an administrator password.

1.2 Stop MySQL Service

/ Library/startupitems/mysqlcom/mysqlcom stop

Just change the start to stop.

1.3 Restart MySQL Service

/ Library/startupitems/mysqlcom/Database Server

On my Mac, I usually just need to use the restart command.

Of course, in addition to the above use terminal to start MySQL outside. There is also an unexpected simple way to find the MySQL service in "Settings" and set its status, turn it on or off.

1.4 Viewing the current MySQL version

Mysql> Selectversion ();+-----------+|Version ()|+-----------+| 5.6. -    |+-----------+1Rowinch Set(0.00Sec

2. Change the root administrator password for MySQL

Example: Change the root account password to ' 123456 ':

123456 Enter password:Warning:Using A password on the command line interface can is insecure.

Note: You need to know your account's original password to make changes.

3. mysql Terminal login

3.1 Terminal login (tedious)

Start by using the following command to see if there is a path to add MySQL:

$ echo $PATH

MySQL Run path:/usr/local/mysql/bin, if you can find this character in the query results, then it is added in the path. If not, you need to add the MySQL run path.

To add a MySQL run path:

$ PATH="$PATH":/usr/local/mysql/bin

Add is successful, we can use which to view:

$ which MySQL /usr/local/mysql/bin/MySQL

If there is a path, it will output the running path of MySQL, and if it does not exist, nothing is output.

Once added, we will be able to log in normally:

- -P

Here will be asked to enter the password, it should be noted that the password here is the MySQL login password, not the system administrator login password .

Note: Each time you close the terminal, and then re-open the terminal, you have to re-add the path, you can take these commands as temporary. In other words, these commands expire after the terminal is closed.

3.2 Terminal Login (Simplified)

We can use the alias command to simplify MySQL terminal login operation, of course, if you just want to temporarily, you can enter alias < simplified name > < execute command directly at the terminal, so as soon as you close the terminal, Commands that have just been simplified will fail. If you want to keep it there, you need to add the alias directive to ~/.BASHRC (Ubuntu) or ~/.bash_profile (MacOS).

In addition to the above 3.1 of the terminal login methods, we can also use the MySQL run path to log in, like this:

$/usr/local/mysql/bin/mysql-u Root-

After entering the correct password, you will be able to operate MySQL normally.

Wouldn't it be too tedious to enter such a long string of characters every time? And it's not easy to remember. In the terminal, we can use the alias command to simplify:

$ alias Mysql=/usr/local/mysql/bin/mysql

Its format is: Alias < simplified name >=< ' specific directive >

When we use it we can be very simple:

$ mysql-u Root-

It was so convenient.

But this is not enough, because this is temporary, as long as we close the current terminal window, all the simplified instructions will be invalidated. So we need to define alias as global, we can add instructions in ~/.bash_profile, if we go to the ~/.bash_profile file:

Vi

Before editing:

Export path="/users/baijiawei/library/application support/goodsync": $PATH

After editing:

Export path="/users/baijiawei/library/application support/goodsync": $PATH             # Mysqlalias mysql='/usr/local/mysql/bin/mysql';

#那一行代表是注释, we usually add single quotes to specific commands, the "Alias < simplified name >=< ' specific directive >" that we mentioned earlier.

Finally, to make the ~/.bash_profile file effective, we must use the source command:

$ source ~/.bash_profile

In the future when we use, there is no need to enter so many troublesome instructions, so easy.

We can enter alias directly on the terminal to view the existing simplified commands:

$ alias alias mysql='/usr/local/mysql/bin/mysql'

4, the MySQL database import and Export

To import and export the database, we need to use the Mysqldump tool, and then I'll introduce its import and export capabilities. This tool is attached when you install MySQL, and we need to find its running path before using it. Fortunately, I have found:

/usr/local/mysql/bin/mysqldump

We don't have to worry about how to use it first, let's use alias to simplify the command:

Alias mysqldump='/usr/local/mysql/bin/mysqldump';

4.1 Exporting a database

OK, now let's introduce its usage, first look at the format when exporting the database:

Mysqldump-u root-p < database name > < table name > > < exported name >.sql

Here's an example:

$ mysqldump-u root-p Test CLASS >

Of course these operations are required to enter the MySQL administrator password, which is the root password. In addition, it is the current directory that is exported to the terminal. Tip: "We can use PWD to view the current directory."

4.2 Importing a database

To import data into an existing database, first enter the database with the use command, and then:

Mysql> Source/users/baijiawei/documents/code/class.sql

Format of the imported database: source/< path >/. <sql>.sql, you can also export to a. dump file

4.3 Restoring a database

If you delete some data by mistake, or if you forget to specify the where condition when updating the data, or if you have deleted the data tables, you can also restore them:

$ mysql-u Root-p TestDB <

Of course, you also need to enter the root password.

Then this section will be here, the end of the terminal operation of SQL statements in the basic part of the Ministry, please pay attention to my blog, thank you.

Blog Garveycalvin

Blog Source: http://www.cnblogs.com/GarveyCalvin/

This article copyright belongs to the author and the blog Garden altogether, welcome reprint, but must retain this paragraph statement, and gives the original link, thanks cooperation!

MySQL terminal (Terminal) management MySQL

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.