IT数码 购物 网址 头条 软件 日历 阅读 图书馆
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
图片批量下载器
↓批量下载图片,美女图库↓
图片自动播放器
↓图片自动播放器↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁
 
   -> 大数据 -> How to using the mysql client program -> 正文阅读

[大数据]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="'&#10;'"/>
? ? ? ? ? ? ? ? ? ? <xsl:text>Result set:&#10;</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="'&#10;'"/> <!-- 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>?

  大数据 最新文章
实现Kafka至少消费一次
亚马逊云科技:还在苦于ETL?Zero ETL的时代
初探MapReduce
【SpringBoot框架篇】32.基于注解+redis实现
Elasticsearch:如何减少 Elasticsearch 集
Go redis操作
Redis面试题
专题五 Redis高并发场景
基于GBase8s和Calcite的多数据源查询
Redis——底层数据结构原理
上一篇文章      下一篇文章      查看所有文章
加:2022-03-21 20:57:41  更:2022-03-21 20:59:38 
 
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁

360图书馆 购物 三丰科技 阅读网 日历 万年历 2024年11日历 -2024/11/24 6:26:02-

图片自动播放器
↓图片自动播放器↓
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
图片批量下载器
↓批量下载图片,美女图库↓
  网站联系: qq:121756557 email:121756557@qq.com  IT数码