How to using the mysql client program

1.How to setting up a mysql user account.

Problem

  • Need an account for connecting to your MySQL server

Solution

  • Use CREATE USER and GRANT statements to set up the account.Then use the account name and password to make connection to the server.

[root@DBAMAXWELL ~]# mysql -h localhost -u root -p
Enter password: 
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 53
Server version: 5.7.37 MySQL Community Server (GPL)

Copyright (c) 2000, 2022, Oracle and/or its affiliates.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql> CREATE USER 'cbuser'@'localhost' IDENTIFIED BY 'cbpass';
ERROR 1290 (HY000): The MySQL server is running with the --skip-grant-tables option so it cannot execute this statement
mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)

mysql> CREATE USER 'cbuser'@'localhost' IDENTIFIED BY 'cbpass';
Query OK, 0 rows affected (0.00 sec)

mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)

mysql> GRANT ALL ON cookbook.* to 'cbuser'@'localhost';
Query OK, 0 rows affected (0.00 sec)

mysql> quit
Bye
[root@DBAMAXWELL ~]# 

[root@DBAMAXWELL ~]# mysql -h localhost -u cbuser -p
Enter password: cbpass
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 54
Server version: 5.7.37 MySQL Community Server (GPL)

Copyright (c) 2000, 2022, Oracle and/or its affiliates.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
+--------------------+
1 row in set (0.00 sec)

mysql> 

 2. How to creating a Database and a sample table?

Problem

  • Create a database and set up tables within it

Solution

  • CREATE DATABASE
  • CREATE TABLE
  • INSERT

mysql> CREATE DATABASE cookbook;
Query OK, 1 row affected (0.00 sec)

mysql> use cookbook;
Database changed

mysql> CREATE TABLE limbs (thing VARCHAR(20), legs INT, arms INT);
Query OK, 0 rows affected (0.02 sec)

mysql> INSERT INTO limbs (thing,legs,arms) VALUES('insect',6,0);
Query OK, 1 row affected (0.00 sec)

mysql> INSERT INTO limbs (thing,legs,arms) VALUES('squid',0,10);
Query OK, 1 row affected (0.02 sec)

mysql> INSERT INTO limbs (thing,legs,arms) VALUES('fish',0,0);
Query OK, 1 row affected (0.01 sec)

mysql> INSERT INTO limbs (thing,legs,arms) VALUES('centipede',100,0);
Query OK, 1 row affected (0.00 sec)

mysql> INSERT INTO limbs (thing,legs,arms) VALUES('table',4,0);
Query OK, 1 row affected (0.00 sec)

mysql> INSERT INTO limbs (thing,legs,arms) VALUES('armchair',4,2);
Query OK, 1 row affected (0.00 sec)

mysql> INSERT INTO limbs (thing,legs,arms) VALUES('phonograph',0,1);
Query OK, 1 row affected (0.00 sec)

mysql> INSERT INTO limbs (thing,legs,arms) VALUES('tripod',3,0);
Query OK, 1 row affected (0.00 sec)

mysql> INSERT INTO limbs (thing,legs,arms) VALUES('Peg Leg Pete',1,2);
Query OK, 1 row affected (0.00 sec)

mysql> INSERT INTO limbs (thing,legs,arms) VALUES('space alien',NULL,NULL);
Query OK, 1 row affected (0.01 sec)

mysql> 

2.How to executing sql statements interactively?

[root@DBAMAXWELL ~]# mysql -h localhost -u cbuser -p -e "select count(*) from limbs" cookbook
Enter password: 
+----------+
| count(*) |
+----------+
|       10 |
+----------+
[root@DBAMAXWELL ~]# mysql -h localhost -u cbuser -p -e "select count(*) from limbs;select now()" cookbook
Enter password: 
+----------+
| count(*) |
+----------+
|       10 |
+----------+
+---------------------+
| now()               |
+---------------------+
| 2022-03-19 17:33:17 |
+---------------------+
[root@DBAMAXWELL ~]# 

 3. How to executing SQL Statements Read from  a File or Program

Problem 

  • mysql to read statements stored in a file so that you need not enter them manually.
  • mysql to read the output from another program.

Solution

To execute the statements in this SQL script file, change location into the tables directory of the recipes distribution and run this command.

total 0
[maxwell@DBAMAXWELL mysql_learn]$ vi limbs.sql
DROP TABLE IF EXISTS limbs;
CREATE TABLE limbs
(
         thing VARCHAR(20), # what the thing is
         legs INT, # number of legs it has
         arms INT # number of arms it has
);      
INSERT INTO limbs (thing,legs,arms) VALUES('human',2,2);
INSERT INTO limbs (thing,legs,arms) VALUES('insect',6,0);
INSERT INTO limbs (thing,legs,arms) VALUES('squid',0,10);
INSERT INTO limbs (thing,legs,arms) VALUES('fish',0,0);
INSERT INTO limbs (thing,legs,arms) VALUES('centipede',100,0);
INSERT INTO limbs (thing,legs,arms) VALUES('table',4,0);
INSERT INTO limbs (thing,legs,arms) VALUES('armchair',4,2);
INSERT INTO limbs (thing,legs,arms) VALUES('phonograph',0,1);
INSERT INTO limbs (thing,legs,arms) VALUES('tripod',3,0);
INSERT INTO limbs (thing,legs,arms) VALUES('Peg Leg Pete',1,2);
INSERT INTO limbs (thing,legs,arms) VALUES('space alien',NULL,NULL);

[maxwell@DBAMAXWELL mysql_learn]$ mysql -h localhost -u cbuser -p cookbook < limbs.sql
Enter password: 

The command just shown illustrates how to specify an input file for mysql on the command line.Alternatively,to read a file of SQL statements from within a mysql session.use a source filename command (\.filename)

mysql> source limbs.sql
Query OK, 0 rows affected (0.01 sec)

Query OK, 0 rows affected (0.01 sec)

Query OK, 1 row affected (0.01 sec)

Query OK, 1 row affected (0.00 sec)

Query OK, 1 row affected (0.00 sec)

Query OK, 1 row affected (0.01 sec)

Query OK, 1 row affected (0.00 sec)

Query OK, 1 row affected (0.00 sec)

Query OK, 1 row affected (0.00 sec)

Query OK, 1 row affected (0.00 sec)

Query OK, 1 row affected (0.01 sec)

Query OK, 1 row affected (0.00 sec)

Query OK, 1 row affected (0.00 sec)

mysql> \. limbs.sql
Query OK, 0 rows affected (0.01 sec)

Query OK, 0 rows affected (0.02 sec)

Query OK, 1 row affected (0.00 sec)

Query OK, 1 row affected (0.00 sec)

Query OK, 1 row affected (0.01 sec)

Query OK, 1 row affected (0.00 sec)

Query OK, 1 row affected (0.00 sec)

Query OK, 1 row affected (0.00 sec)

Query OK, 1 row affected (0.01 sec)

Query OK, 1 row affected (0.00 sec)

Query OK, 1 row affected (0.00 sec)

Query OK, 1 row affected (0.00 sec)

Query OK, 1 row affected (0.00 sec)

mysql> 

 4. How to controlling mysql output destination and format.

Problem

you want mysql output to go somewhere other than your screen.

Solution

Redirect the output to a file,or use a pipe to send the output to a program.

  • Producing tabular or tab-delimited output
  • Producing HTML or XML output
  • Suppressing column headings in query output
  • Specifying the output column delimiter

[maxwell@DBAMAXWELL mysql_learn]$ mysql -h localhost -u cbuser -p
Enter password: 
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 81
Server version: 5.7.37 MySQL Community Server (GPL)

Copyright (c) 2000, 2022, Oracle and/or its affiliates.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql> use cookbook
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A

Database changed
mysql> select * from limbs where legs=0;
+------------+------+------+
| thing      | legs | arms |
+------------+------+------+
| squid      |    0 |   10 |
| fish       |    0 |    0 |
| phonograph |    0 |    1 |
+------------+------+------+
3 rows in set (0.00 sec)

mysql> 

For noninteractive use (when the input or output is redirected)


[maxwell@DBAMAXWELL mysql_learn]$ echo "SELECT * FROM limbs where legs=0" | mysql -h localhost -u cbuser -p cookbook
Enter password: 
thing   legs    arms
squid   0       10
fish    0       0
phonograph      0       1
[maxwell@DBAMAXWELL mysql_learn]$ 

mysql generates an HTML table from each query result set if you use the -H(or --html) option.This enables you to eaily produce output for inclusion in a web page that shows a query result.

[maxwell@DBAMAXWELL mysql_learn]$ mysql -H -e "SELECT * FROM limbs where legs=0" -h localhost -u cbuser -p cookbook;
Enter password: 
<TABLE BORDER=1><TR><TH>thing</TH><TH>legs</TH><TH>arms</TH></TR><TR><TD>squid</TD><TD>0</TD><TD>10</TD></TR><TR><TD>fish</TD><TD>0</TD><TD>0</TD></TR><TR><TD>phonograph</TD><TD>0</TD><TD>1</TD></TR></TABLE>[maxwell@DBAMAXWELL mysql_learn]$ 


to generate an XML document instead of HTML,use the -X (or --xml) option:

[maxwell@DBAMAXWELL mysql_learn]$ mysql -X -e "SELECT * FROM limbs where legs=0" -h localhost -u cbuser -p cookbook;
Enter password: 
<?xml version="1.0"?>

<resultset statement="SELECT * FROM limbs where legs=0
" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
  <row>
        <field name="thing">squid</field>
        <field name="legs">0</field>
        <field name="arms">10</field>
  </row>

  <row>
        <field name="thing">fish</field>
        <field name="legs">0</field>
        <field name="arms">0</field>
  </row>

  <row>
        <field name="thing">phonograph</field>
        <field name="legs">0</field>
        <field name="arms">1</field>
  </row>
</resultset>
[maxwell@DBAMAXWELL mysql_learn]$ 

You can reformat XML to suit a variety of purposes by running it through XSLT transforms.This enables you to use the same input to produce many output formats.

[maxwell@DBAMAXWELL mysql_learn]$ vi mysql-xml.xsl
<?xml version="1.0" encoding="ISO-8859-1"?>
<xsl:stylesheet version="1.0"
        xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
        <!-- mysql-xml.xsl: interpret XML-format output from mysql client -->
        <xsl:output method="text"/>
        <!-- Process rows in each resultset -->
        <xsl:template match="resultset">
                 <xsl:text>Query: </xsl:text>
                  <xsl:value-of select="@statement"/>
                   <xsl:value-of select="' '"/>
                    <xsl:text>Result set: </xsl:text>
                     <xsl:apply-templates select="row"/>
             </xsl:template>
<!-- Process fields in each row -->
<xsl:template match="row">
         <xsl:apply-templates select="field"/>
 </xsl:template>
 <!-- Display text content of each field -->
 <xsl:template match="field">
          <xsl:value-of select="."/>
           <xsl:choose>
                    <xsl:when test="position() != last()">
                             <xsl:text>, </xsl:text> <!-- comma after all but last field -->
                              </xsl:when>
                               <xsl:otherwise>
                                        <xsl:value-of select="' '"/> <!-- newline after last field -->
                                         </xsl:otherwise>
                                          </xsl:choose>
                                  </xsl:template>
                          </xsl:stylesheet>
~                                                                                                                                                 
~                                                                                                                                                 
"mysql-xml.xsl" [New] 30L, 1093C written                                                                              
[maxwell@DBAMAXWELL mysql_learn]$ mysql -X -u cbuser -p -e "SELECT * FROM limbs where legs=0" cookbook | xsltproc mysql-xml.xsl -
Enter password: 
Query: SELECT * FROM limbs where legs=0 
Result set:
squid, 0, 10
fish, 0, 0
phonograph, 0, 1
[maxwell@DBAMAXWELL mysql_learn]$ 

Suppressing column headings in query output.

Tab-delimited format is convenient for generating datafiles for import into other programs. To create output that contains only data values,suppress the header row with --skip-column-names option:

 5. How to using user-defined variables in SQL statements

Problem

use a value in one statement that produced by an earlier statement

Solution

Save the value in a user-defined variable to store it for later use.

mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| cookbook           |
+--------------------+
2 rows in set (0.00 sec)

mysql> use cookbook
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A

Database changed
mysql> 
mysql> select @max_limbs := MAX(arms+legs) from limbs;
+------------------------------+
| @max_limbs := MAX(arms+legs) |
+------------------------------+
|                          100 |
+------------------------------+
1 row in set (0.00 sec)

mysql> select * from limbs where arms+legs = @max_limbs;
+-----------+------+------+
| thing     | legs | arms |
+-----------+------+------+
| centipede |  100 |    0 |
+-----------+------+------+
1 row in set (0.00 sec)

mysql> select @last_id := LAST_INSERT_ID();
+------------------------------+
| @last_id := LAST_INSERT_ID() |
+------------------------------+
|                            0 |
+------------------------------+
1 row in set (0.00 sec)

mysql> select @name := thing from limbs where legs = 0;
+----------------+
| @name := thing |
+----------------+
| squid          |
| fish           |
| phonograph     |
+----------------+
3 rows in set (0.00 sec)

mysql> select @name;
+------------+
| @name      |
+------------+
| phonograph |
+------------+
1 row in set (0.00 sec)

mysql> select @name2 := thing from limbs where legs < 0;
Empty set (0.00 sec)

mysql> select @name2;
+--------+
| @name2 |
+--------+
| NULL   |
+--------+
1 row in set (0.00 sec)

mysql> set @sum = 4 + 7;
Query OK, 0 rows affected (0.00 sec)

mysql> select @sum;
+------+
| @sum |
+------+
|   11 |
+------+
1 row in set (0.00 sec)

mysql> set @max_limbs = (select max(arms+legs) from limbs);
Query OK, 0 rows affected (0.00 sec)

mysql> set @x = 1, @X = 2; select @x,@X;
Query OK, 0 rows affected (0.00 sec)

+------+------+
| @x   | @X   |
+------+------+
|    2 |    2 |
+------+------+
1 row in set (0.00 sec)

mysql> 

Guess you like

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