If MYSQL is running on the server and you need to connect on the client, you need to connect as follows: shell>mysql -h host -u user -p Enter password:************ host and user respectively represent the host name and MySQL account name where the MySQL server is running. When adding, write the name of the corresponding machine and the MySQL user name. ********* represents your password. If MYSQL is running on the server , and you need to connect on the client side, you need to connect as follows:
shell>mysql -h host -u user -p
Enter password:************
host and user respectively represent the host name and MySQL account name where the MySQL server is running. When adding, write the name of the corresponding machine and MySQL user name. ********* represents your password.
If it works, you will see the following prompt:
Welcome to the MySQL monitor. Commands end with ; or g.
Your MySQL connection id is 7 to server version: 5.0.18-nt
Type 'help;' or 'h' for help. Type 'c' to clear the buffer.
mysql>
The mysql> prompt tells you that it is time to enter a command.
Sometimes MySQL will allow you to log in to the server anonymously. In this case, just enter mysql directly.
shell> mysql
After successful connection, you can enter quit at the mysql> prompt to exit at any time:
mysql> quit
Bye
A simple query example is shown below. This is a simple command that requires the version number and current date of the server:
mysql> select version(), current_date;
+-----------+--------------+
version() current_date
+-----------+--------------+
5.0.18-nt 2006-07-29
+-----------+--------------+
1 row in set (0.00 sec)
mysql>
Here we need to understand:
A command usually consists of a SQL statement, followed by a semicolon.
MySQL's display results are output in a table (rows and columns). The first line is the label of the column, and the subsequent lines are the results of the query. Usually the column label is the column name of the database table you query, but if you are retrieving an expression instead of Column values (as in the previous example), usually the expression itself is used to label the column.
It then displays how many rows were returned and the query time, which provides a rough estimate of server performance. It represents clock time, not CPU or machine time.
Keywords can be entered in upper and lower case, that is, upper and lower case are equivalent in MySQL, but in a query statement, upper and lower case must be consistent.
Here is another query:
mysql> SELECT SIN(PI()/4), (4+1)*5;
+------------------+---------+
SIN(PI()/4) (4+1)*5
+------------------+---------+
0.70710678118655 25
+------------------+---------+
1 row in set (0.02 sec)
mysql>
You can enter multiple statements on one line, such as:
mysql> SELECT VERSION(); SELECT NOW();
+-----------------+
VERSION()
+-----------------+
5.1.2-alpha-log
+-----------------+
1 row in set (0.00 sec)
+---------------------+
NOW()
+---------------------+
2005-10-11 15:15:00
+---------------------+
1 row in set (0.00 sec)
mysql>
Long commands can be entered in multiple lines. MySQL uses semicolons to determine whether the statement ends, rather than new lines.
Here is an example of a simple multi-line statement:
mysql> select
-> user(),
-> current_date;
+---------------+--------------+
USER() CURRENT_DATE
+---------------+--------------+
jon@localhost 2005-10-11
+---------------+--------------+
mysql>
In this example, after entering the first line of the multi-line query, you can see that the prompt changes to ->, which means that MySQL has not found the end-of-statement identifier and is waiting for the remaining input. If you do not want to execute a command during the input process , enter c to cancel it:
mysql> select
-> user()
-> c
mysql>
Shown below are the various prompts and the status of MySQL they represent:
Prompt meaning
mysql> is ready to accept new commands.
-> Wait for the next line of a multi-line command.
'> Waits for the next line, waiting for the end of the string starting with a single quote ("'").
"> Wait for the next line, waiting for the end of the string starting with a double quote (""").
`> Waits for the next line, waiting for the end of the identifier starting with a backslash dot (''').
/*> Wait for the next line, waiting for the end of the comment starting with /*.
-