13.3 mysql常用命令

时间:2022-04-27
本文章向大家介绍13.3 mysql常用命令,主要内容包括mysql常用命令目录概要、mysql常用命令、创建库、基本概念、基础应用、原理机制和需要注意的事项等,并结合实例形式分析了其使用技巧,希望通过本文能帮助到大家理解应用这部分内容。

mysql常用命令目录概要

  • 查询库 show databases;
  • 切换库 use mysql;
  • 查看库里的表 show tables;
  • 查看表里的字段 desc tb_name;
  • 查看建表语句 show create table tb_nameG;
  • 查看当前用户 select user();
  • 查看当前使用的数据库 select database();
  • 创建库 create database db1;
  • 创建表 use db1; create table t1(id int(4), name char(40));
  • 查看当前数据库版本 select version();
  • 查看数据库状态 show status;
  • 查看各参数 show variables; show variables like 'max_connect%';
  • 修改参数 set global max_connect_errors=1000;
  • 查看队列 show processlist; show full processlist;

mysql常用命令

  • mysql内部命令和linux系统命令不通用
  • 库是由表组成,表是由字段组成
  • 在mysql 中使用的命令需要使用分号 ; 结尾,否则会出问题
  1. 登录到mysql
[root@hf-01 ~]# mysql -uroot -p'hanfeng'
Warning: Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor.  Commands end with ; or g.
Your MySQL connection id is 11
Server version: 5.6.35 MySQL Community Server (GPL)

Copyright (c) 2000, 2016, Oracle and/or its affiliates. All rights reserved.

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> 
  1. 查看数据库 show databases;
mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| test               |
+--------------------+
4 rows in set (0.00 sec)

mysql> 
  1. 切换库 use mysql; ——>切换到哪一个库下面
mysql> use mysql;
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> 
  1. 列出所有的表 show tables;
mysql> show tables;
+---------------------------+
| Tables_in_mysql           |
+---------------------------+
| columns_priv              |
| db                        |
| event                     |
| func                      |
| general_log               |
| help_category             |
| help_keyword              |
| help_relation             |
| help_topic                |
| innodb_index_stats        |
| innodb_table_stats        |
| ndb_binlog_index          |
| plugin                    |
| proc                      |
| procs_priv                |
| proxies_priv              |
| servers                   |
| slave_master_info         |
| slave_relay_log_info      |
| slave_worker_info         |
| slow_log                  |
| tables_priv               |
| time_zone                 |
| time_zone_leap_second     |
| time_zone_name            |
| time_zone_transition      |
| time_zone_transition_type |
| user                      |
+---------------------------+
28 rows in set (0.00 sec)

mysql> 
  1. 查看表里的字段 desc tb_name;
  2. 例子
    • desc user; //查看user表里的所有字段;需要进入相关的库以后才能查看对应的表
  3. 查看建表语句 show create table tb_nameG;——>G 表示 竖排显示
  4. 例子
    • show create table userG;
  5. 查看当前用户 select user();
mysql> select user();
+----------------+
| user()         |
+----------------+
| root@localhost |
+----------------+
1 row in set (0.00 sec)

mysql> 
  • 这里的localhost 表示主机名,会反解析你的IP所属主机名
  1. 在mysql里面使用 方向键 可以查看之前使用的命令,和shell的命令类似,并且也有记录命令历史的功能,默认的缓存文件在 / 下,名字为“.mysql_history”
  • 支持ctrl+l 清屏
  1. 查看当前使用的数据库 select database();
mysql> select database();
+------------+
| database() |
+------------+
| mysql      |
+------------+
1 row in set (0.00 sec)

mysql> 

创建库

  1. 创建库 create database db1;
mysql> create database db1;
Query OK, 1 row affected (0.00 sec)

mysql> 
  1. 查看db1库
mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| db1                |
| mysql              |
| performance_schema |
| test               |
+--------------------+
5 rows in set (0.00 sec)

mysql> 
  1. 切换到db1库下面去
mysql> use db1;
Database changed
mysql> 
  1. 创建表 create table t1(id int(4), name char(40));——>定义 id 和 name ,并用反引好括起来
mysql> create table t1(`id` int(4), `name` char(40));
Query OK, 0 rows affected (0.03 sec)

mysql> 
  1. 查看创建的表t1,默认使用的是InnoDB 引擎
mysql> show create table t1G;
*************************** 1. row ***************************
       Table: t1
Create Table: CREATE TABLE `t1` (
  `id` int(4) DEFAULT NULL,
  `name` char(40) DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1
1 row in set (0.00 sec)

ERROR: 
No query specified

mysql> 
  1. 在数据库里面取消命令,即在命令的最前面加一个 # 号就不会生效了
  2. 删除表 drop table t1;
mysql> drop table t1;
Query OK, 0 rows affected (0.02 sec)

mysql> 
  1. 创建t1表
mysql> create table t1(`id` int(4), `name` char(40)) ENGINE=InnoDB DEFAULT CHARSET=utf8;
Query OK, 0 rows affected (0.01 sec)

mysql> 
  1. 查看表,会看到变成了utf8
mysql> show create table t1G;                                          
*************************** 1. row ***************************
       Table: t1
Create Table: CREATE TABLE `t1` (
  `id` int(4) DEFAULT NULL,
  `name` char(40) DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8
1 row in set (0.01 sec)

ERROR: 
No query specified

mysql> 
  1. 查看当前数据库版本 select version();
mysql> select version();
+-----------+
| version() |
+-----------+
| 5.6.35    |
+-----------+
1 row in set (0.00 sec)

mysql> 
  1. 查看数据库状态 show status; 它会把常用的数据都列出来
  2. 查看各参数
  • show variables;
  • show variables like 'max_connect%'; //mysql下 % 为通配符
mysql> show variables like 'max_connect%'; 
+--------------------+-------+
| Variable_name      | Value |
+--------------------+-------+
| max_connect_errors | 100   |
| max_connections    | 151   |
+--------------------+-------+
2 rows in set (0.00 sec)

mysql>
  1. 修改参数 set global max_connect_errors=1000; ——>仅在内存中生效
mysql> set global max_connect_errors=1000;
Query OK, 0 rows affected (0.01 sec)

mysql> show variables like 'max_connect%';
+--------------------+-------+
| Variable_name      | Value |
+--------------------+-------+
| max_connect_errors | 1000  |
| max_connections    | 151   |
+--------------------+-------+
2 rows in set (0.00 sec)

mysql> 
  • 若想重启依然生效,那需要修改配置文件/etc/my.cnf
  1. 查看队列
  • show processlist; //查看库的状况,比如,那些用户在连,做了些什么操作,是否锁表
mysql> show processlist; 
+----+------+-----------+------+---------+------+-------+------------------+
| Id | User | Host      | db   | Command | Time | State | Info             |
+----+------+-----------+------+---------+------+-------+------------------+
| 14 | root | localhost | db1  | Query   |    0 | init  | show processlist |
+----+------+-----------+------+---------+------+-------+------------------+
1 row in set (0.00 sec)

mysql> 
  • show full processlist; //查看到的对列,最后一个会非常完成的显示出来
mysql> show full processlist;
+----+------+-----------+------+---------+------+-------+-----------------------+
| Id | User | Host      | db   | Command | Time | State | Info                  |
+----+------+-----------+------+---------+------+-------+-----------------------+
| 14 | root | localhost | db1  | Query   |    0 | init  | show full processlist |
+----+------+-----------+------+---------+------+-------+-----------------------+
1 row in set (0.00 sec)

mysql>