MySQL service installation and command usage

1. Introduction to the experiment

The follow-up experiments in this section to Section 17 are the advanced chapters of this course, which are based on the MySQL official reference manual, and are adjusted and adapted for testing according to the experimental building environment. Thanks to the developers of MySQL, the maintainers of official documents and Chinese documents.

List of references:

  1. MySQL official documentation , the current experiment in the laboratory building is adapted to MySQL 5.5 version.
  2. MySQL Reference Manual Chinese Version , Translator's Note: This is the MySQL Reference Manual. The original Reference Manual is in English. Compared with the English Reference Manual, this translated version may not be the latest. It refers to MySQL 5.1 to 5.1.2-alpha versions.

Thanks to the developers of MySQL, the maintainers of the official documentation, and the translators of the Chinese version. Portions are drawn from these references and adapted from the lab building.

1.1 Experiment content

The experiments in this section mainly describe and practice the installation, startup and running commands of MySQL services.

1.2 Experimental knowledge points

  • MySQL service

1.3 Experimental environment

The experimental environment used in the course is Ubuntu Linux 14.04 64-bit version. The program will be used in the experiment:

  • Mysql 5.5.50
  • Xfce Terminal

2. Experimental steps

2.1 Introduction to MySQL

  • MySQL is a relational database management system developed by the Swedish company MySQLAB and currently belongs to Oracle. MySQL is the most popular relational database management system. In terms of WEB applications, MySQL is one of the best RDBMS (Relational Database Management System: relational database management system) application software.
  • MySQL is a relational database management system, relational databases keep data in different tables instead of keeping all data in one big warehouse, which increases speed and improves flexibility.
  • The SQL language used by MySQL is the most commonly used standardized language for accessing databases. Because of its small size, high speed, low total cost of ownership, especially open source, MySQL is generally chosen as the website database for the development of small and medium-sized websites. Due to the excellent performance of its community edition, it can form a good development environment with PHP and Apache.

2.2 Install MySQL

The experimental building environment has already installed MySQL for everyone, and you do not need to install it again. The following installation is only for everyone to learn and use

2.2.1 Installation under Windows

You can download the MySQL installation package from the official website . When installing MySQL in Windows, new users can use the MySQL Installation Help and MySQL Configuration Wizard(Configuration Wizard).

When installing MySQL in Windows, there are 3 MySQL 5.1 installation packages to choose from:

  • Basic installation: This installation package has a filename like that mysql-essential-5.1.2-alpha-win32.msiand contains the minimum files required to install MySQL on Windows, including the configuration wizard. The installation package does not include optional components such as the embedded server and benchmark kit.

  • Full installation: This installation package has a filename like that mysql-5.1.2-alpha-win32.zipand contains all the files needed to install MySQL on Windows, including the configuration wizard. The installation package includes optional components such as an embedded server and benchmark kit.

  • Non-automatic installation files: This installation package has a similar file name mysql-noinstall-5.1.2-alpha-win32.zipand contains all the files in the full installation package, except that the configuration wizard is not included. The installation package does not include an automatic installer and must be installed and configured manually.

For most users, the basic installation is recommended.

If the installation package you downloaded is in the Zipfile, you need to unzip the file first. If there is a setup.exefile, double-click it to start the installation process. If there is a .msifile, double-click it to start the installation process.

2.2.2 Installation under Linux

RPMIt is recommended to use ( RedHatseries) or DEB( series) packages to install MySQL in Linux Debian/Ubuntu. In most cases, you only need to install MySQL via command line apt-getinstall MySQL-serverand MySQL-clientpackages. No other packages are required in a standard installation. MySQL-MaxIt should also be installed if you want to run a server with more features MySQL-Max RPM. M MySQL-server RPMThis module must be installed first .

2.3 Start the mysql server

In the environment configured by the laboratory building, mysql is not started by default. We first start the mysql server on the command line:

$ sudo service mysql start

  

2.4 Connecting and disconnecting servers

In order to connect to the server, when invoking mysql, a MySQL username and probably a password are usually required. Here we use the rootuser to connect to the server (the password environment is set to empty, no need for us to enter a password), enter the following command to connect to the server:

$ mysql -u root

  

If it works, you should see some introductory information:

Enter image description here

mysql>The prompt tells you that mysql is ready to enter commands for you.

Once successfully connected, you can exit at any time mysql>by entering QUIT (or ) at the prompt:\q

2.5 Entering a query

This is a simple command that asks the server to tell MySQL the version number and current date. Enter the following command at the mysql>prompt and press Enter:

mysql> SELECT VERSION(), CURRENT_DATE;

  

Enter image description here

Ability to enter keywords in upper and lower case (it is recommended to capitalize keywords, you will thank me later~). The following queries are equivalent:

mysql> SELECT VERSION(), CURRENT_DATE;
mysql> select version(), current_date;
mysql> SeLeCt vErSiOn(), current_DATE;

  

Here's another query that shows you can use mysql as a simple calculator:

mysql> SELECT SIN(PI()/4), (4+1)*5;

  

Enter image description here

The commands shown so far are fairly short one-line statements. You can enter multiple statements on a single line by separating each statement with a semicolon:

mysql> SELECT VERSION(); SELECT NOW();

It is not necessary to give a command all on one line, and longer commands can be entered on multiple lines. MySQL determines where the statement ends by looking for the terminating semicolon rather than the end of the input line. (In other words, mysql accepts free-form input: it collects input lines but does not execute them until it sees a semicolon.)

Here is an example of a simple multi-line statement:

mysql> SELECT
    -> USER()
    -> ,
    -> CURRENT_DATE;

  

In this example, after entering the first line of the multiline query, notice how the prompt changes from mysql>to ->, which is how mysql indicates that it has not seen the complete statement and is waiting for the rest. The prompt is your friend because it provides valuable feedback that, if used, will always know what mysql is waiting for.

If you decide you don't want to execute a command while you're typing, \ccancel it by typing:

mysql> SELECT
    -> USER()
    -> \c
mysql>

  

Note the prompt here too, after you type \cit, it switches back mysql>, providing feedback that mysql is ready to accept a new command.

The following table shows the various prompts that can be seen and briefly describes the state of mysql they represent:

Enter image description here

Multi-line statements are often "by chance" when you intend to issue a command on a single line, but without a terminating semicolon. In this case, mysql waits for further input:

mysql> SELECT USER()
    ->

  

If this happens (you think you've finished typing the statement, but only one ->prompt responds), it's likely that mysql is waiting for a semicolon. If you don't pay attention to the prompt, you may sit for a while before realizing what you need to do. Enter a semicolon to complete the statement and mysql will execute:

mysql> SELECT USER()
    -> ;

  

'> The sum "> prompt will appear during string collection  (indicating that MySQL is waiting for the end of the string). In MySQL, it is possible to write strings enclosed by 'or characters (for example, or ), and MySQL allows input of strings that span multiple lines. When you see an  or   prompt, it means that a line containing a string beginning with the or bracket character has been entered, but the matching quotation mark terminating the string has not been entered. This shows that you carelessly omitted a quote character. E.g:"'hello'"goodbye"'>">'"

mysql> SELECT * FROM my_table WHERE name = 'Smith AND age < 30;
    '>

  

If you type SELECTthe statement, then press Enter and wait for the result, nothing comes up. Don't be surprised, "Why is this query so long?", pay attention ">to the clues provided by the prompt. It tells you that mysql expects to see the remainder of an unterminated string. (Do you see the error in the statement? The string is Smithmissing the second quote.)

At this point, what should you do? The easiest is to cancel the command. In this case, however, you can't just type \c, because mysql interprets it as part of the string it's collecting! Instead, enter the closing quote character (so mysql knows you're done with the string), then enter \c:

mysql> SELECT * FROM my_table WHERE name = 'Smith AND age < 30; '> '\c mysql> 

The prompt returns mysql>, showing that mysql is ready to accept a new command.

It's important to know the meaning of the '>and ">prompt, because if you enter an unterminated string by mistake, any lines entered after it will be ignored by mysql -- including QUITlines that contain ! This can be quite confusing, especially if you don't know you need to provide the terminating quote before canceling the current command.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326521051&siteId=291194637