红茶的个人站点

  • 首页
  • 专栏
  • 开发工具
  • 其它
  • 隐私政策
Awalon
Talk is cheap,show me the code.
  1. 首页
  2. 专栏
  3. MySQL学习笔记
  4. 正文

MySQL 从入门到精通 14:MySQL 管理

2025年8月7日 7点热度 0人点赞 0条评论

系统数据库

MySQL 安装好后自带四个系统数据库:

数据库 含义
mysql 存储 MySQL 运行时所需的各种信息(时区、主从、用户、权限等)
information_schema 提供了访问数据库元数据的各种表和视图,包含数据库、表、字段类型及访问权限等
performance_schema 为 MySQL 运行时状态提供了一个底层监控功能,主要用于收集数据库服务器性能参数
sys 包含了一系列方便 DBA 和开发人员利用 performance_schema 性能数据库进行性能调优和诊断的视图

常用工具

mysql

自带的命令行客户端。

语法:

mysql  [options]  [database]
-U, --user=name         #指定用户名
-P, --password[=name]   #指定密码
-h, --host=name         #指定服务器IP或域名
-P, --port=port         #指定连接端口
-e, --execute=name      #执行SQL语句并退出

使用 -e 参数可以直接执行指定 SQL 并且不需要进入交互式界面:

mysql -u root -pmysql123abc itcast -e "select * from student"
mysql: [Warning] Using a password on the command line interface can be insecure.
+----+-----------+------------+-----+
| id | name      | no         | age |
+----+-----------+------------+-----+
|  1 | joke      | 2000100101 |   3 |
|  2 | 谢逊      | 2000100102 |  15 |
|  3 | 殷天正    | 2000100103 |  20 |
|  4 | 韦一笑    | 2000100104 |  20 |
| 12 | Lucy      | NULL       |  16 |
| 22 | Tom       | NULL       |  22 |
| 23 | Tom       | NULL       |  35 |
| 24 | Tom       | NULL       |  20 |
| 31 | Tom       | NULL       |  99 |

利用这一点可以在 Shell 脚本中执行 SQL 并获取结果。

通过-p显式指定密码时-p与密码之间不能有空格,否则无法正常执行。

mysqladmin

mysqladmin 是一个执行管理操作的客户端程序,可以通过它来检查服务器配置和当前状态,创建或删除数据库等。

查看帮助文档:

mysqladmin --help

查看版本信息:

mysqladmin -uroot -pmysql123abc version
mysqladmin: [Warning] Using a password on the command line interface can be insecure.
mysqladmin  Ver 8.0.41 for Linux on x86_64 (Source distribution)
Copyright (c) 2000, 2025, 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.
​
Server version          8.0.41
Protocol version        10
Connection              Localhost via UNIX socket
UNIX socket             /var/lib/mysql/mysql.sock
Uptime:                 3 days 5 hours 21 min 58 sec

查看 MySQL 系统变量:

mysqladmin -uroot -pmysql123abc variables| grep innodb

创建数据库:

mysqladmin -uroot -pmysql123abc create db02

查看数据库是否创建成功:

mysql -u root -pmysql123abc itcast -e "show databases"
mysql: [Warning] Using a password on the command line interface can be insecure.
+--------------------+
| Database           |
+--------------------+
| db02               |
| information_schema |
| itcast             |
| itheima            |
| mysql              |
| performance_schema |
| sys                |
| test               |
+--------------------+

删除数据库:

mysqladmin -uroot -pmysql123abc drop db02
mysqladmin: [Warning] Using a password on the command line interface can be insecure.
Dropping the database is potentially a very bad thing to do.
Any data stored in the database will be destroyed.
​
Do you really want to drop the 'db02' database [y/N] y

mysqlbinlog

由于服务器生成的二进制日志文件以二进制格式保存,如果要查看这些文件,就需要使用 mysqlbinlog。

语法:

mysqlbinlog [options] log-files1 log-files2 ..
选项:
-d, --database=name     指定数据库名称,只列出指定的数据库相关操作。
-O, --offset=#          忽略掉日志中的前n行命令。
-r, --result-file=name  将输出的文本格式日志输出到指定文件。
-S, --short-form        显示简单格式,省略掉一些信息。
--start-datatime=date1--stop-datetime=date2 指定日期间隔内的所有日志。
--start-position=pos1 --stop-position=pos2  指定位置间隔内的所有日志。

示例:

sudo mysqlbinlog -s binlog.000011

mysqlshow

客户端对象查找工具,用于快速查找数据库、表、表中的列和索引。

语法:

mysqlshow [options] [db_name [table_name [col_name]]]
选项:
--count 显示数据库及表的统计信息(数据库,表均可以不指定)
-i      显示指定数据库或者指定表的状态信息

统计当前数据库中有多少张表,有多少行数据:

mysqlshow -uroot -pmysql123abc --count
mysqlshow: [Warning] Using a password on the command line interface can be insecure.
+--------------------+--------+--------------+
|     Databases      | Tables |  Total Rows  |
+--------------------+--------+--------------+
| information_schema |     79 |        33423 |
| itcast             |     11 |           77 |
| itheima            |      2 |      1000000 |
| mysql              |     37 |         3991 |
| performance_schema |    111 |       492496 |
| sys                |    101 |         7859 |
| test               |      2 |     10000000 |
+--------------------+--------+--------------+

只查看某个数据库的统计信息:

mysqlshow -uroot -pmysql123abc itcast --count
mysqlshow: [Warning] Using a password on the command line interface can be insecure.
Database: itcast
+------------------+----------+------------+
|      Tables      | Columns  | Total Rows |
+------------------+----------+------------+
| course           |        2 |          4 |
| score            |        5 |          2 |
| student          |        4 |          9 |
| student_course   |        3 |          6 |
| student_v_1      |        2 |          5 |
| student_v_2      |        2 |          1 |
| student_v_3      |        2 |          1 |
| tb_user          |        9 |         24 |
| tb_user_ext      |        4 |          2 |
| user_logs        |        5 |          8 |
| user_v_pro_count |        2 |         15 |
+------------------+----------+------------+

只查看某张表的统计信息:

mysqlshow -uroot -pmysql123abc itcast tb_user --count
mysqlshow: [Warning] Using a password on the command line interface can be insecure.
Database: itcast  Wildcard: tb_user
+---------+----------+------------+
| Tables  | Columns  | Total Rows |
+---------+----------+------------+
| tb_user |        9 |         24 |
+---------+----------+------------+

查看某张表的状态信息:

mysqlshow -uroot -pmysql123abc itcast tb_user -i
mysqlshow: [Warning] Using a password on the command line interface can be insecure.
Database: itcast  Wildcard: tb_user
+---------+--------+---------+------------+------+----------------+-------------+-----------------+--------------+-----------+----------------+---------------------+---------------------+------------+--------------------+----------+----------------+-----------------+
| Name    | Engine | Version | Row_format | Rows | Avg_row_length | Data_length | Max_data_length | Index_length | Data_free | Auto_increment | Create_time         | Update_time         | Check_time | Collation          | Checksum | Create_options | Comment         |
+---------+--------+---------+------------+------+----------------+-------------+-----------------+--------------+-----------+----------------+---------------------+---------------------+------------+--------------------+----------+----------------+-----------------+
| tb_user | InnoDB | 10      | Dynamic    | 25   | 655            | 16384       | 0               | 16384        | 0         | 26             | 2025-08-05 14:58:16 | 2025-08-06 12:01:54 |            | utf8mb4_0900_ai_ci |          |                | 系统用户表 |
+---------+--------+---------+------------+------+----------------+-------------+-----------------+--------------+-----------+----------------+---------------------+---------------------+------------+--------------------+----------+----------------+-----------------+

mysqldump

实现数据备份与数据迁移,备份内容包含创建表、插入表的 SQL 语句。

语法:

mysqldump [options] db_name [tables] 
mysqldump [options] --database/-B db1 [db2 db3..]
mysqldump [options] --all-databases/-A 
选项:
--add-drop-database     在每个数据库创建语句前加上 drop database 语句
--add-drop-table        在每个表创建语句前加上 drop table 语句,默认开启
--skip-add-drop-table   不开启
-n, --no-create-db      不包含数据库的创建语句
-t, --no-create-info    不包含数据表的创建语句
-d, --no-data           不包含数据
-T, --tab=name          自动生成两个文件:一个.sql文件,创建表结构的语句;一个.txt文件,数据文件

备份指定数据库:

mysqldump -uroot -pmysql123abc itcast > ~/download/sql/itcast.sql

仅备份数据,不备份表结构:

mysqldump -uroot -pmysql123abc -t itcast > ~/download/sql/itcast_data.sql

仅备份表结构,不包含数据:

mysqldump -uroot -pmysql123abc -d itcast > ~/download/sql/itcast_structure.sql

使用 -T 参数分别备份表的表结构和数据:

mysqldump -uroot -pmysql123abc -T ~/download/sql itcast student
mysqldump: [Warning] Using a password on the command line interface can be insecure.
mysqldump: Got error: 1290: The MySQL server is running with the --secure-file-priv option so it cannot execute this statement when executing 'SELECT INTO OUTFILE'

报错,原因是-T参数指定的备份文件目标目录只能是 MySQL secure-file-priv 变量指定的安全目录:

mysql> show variables like '%secure_file_priv%';
+------------------+-----------------------+
| Variable_name    | Value                 |
+------------------+-----------------------+
| secure_file_priv | /var/lib/mysql-files/ |
+------------------+-----------------------+

因此我们要备份到这个指定目录:

sudo mysqldump -uroot -pmysql123abc -T /var/lib/mysql-files/ itcast student
sudo ls -al /var/lib/mysql-files/
总用量 12
drwxr-x---.  2 mysql mysql   44  8月  7 16:11 .
drwxr-xr-x. 59 root  root  4096  7月  1 17:33 ..
-rw-r--r--.  1 root  root  1610  8月  7 16:11 student.sql
-rw-r-----.  1 mysql mysql  161  8月  7 16:11 student.txt

数据不是以 INSERT 语句,而是以制表符间隔的方式存放的:

sudo cat /var/lib/mysql-files/student.txt
1       joke    2000100101      3
2       谢逊    2000100102      15
3       殷天正  2000100103      20
4       韦一笑  2000100104      20
12      Lucy    \N      16
22      Tom     \N      22
23      Tom     \N      35
24      Tom     \N      20
31      Tom     \N      99

mysqlimport

用于导入使用 mysqldump -T 导出的文本文件(.txt)。

语法:

mysqlimport [options] db_name textfile1 [textfile2...]

示例:

 sudo mysqlimport -uroot -pmysql123abc itcast /var/lib/mysql-files/score.txt

source

要通过 sql 文件恢复数据库,需要在 mysql 客户端中执行 source 命令:

mysql> use itcast;
Database changed
​
mysql> source /home/icexmoon/download/sql/itcast.sql
Query OK, 0 rows affected (0.00 sec)
​
Query OK, 0 rows affected (0.00 sec)
...

参考资料

  • 黑马程序员 MySQL数据库入门到精通

本作品采用 知识共享署名 4.0 国际许可协议 进行许可
标签: MySQL mysqladmin mysqldump
最后更新:2025年8月7日

魔芋红茶

加一点PHP,加一点Go,加一点Python......

点赞
< 上一篇
下一篇 >

文章评论

razz evil exclaim smile redface biggrin eek confused idea lol mad twisted rolleyes wink cool arrow neutral cry mrgreen drooling persevering
取消回复

COPYRIGHT © 2021 icexmoon.cn. ALL RIGHTS RESERVED.
本网站由提供CDN加速/云存储服务

Theme Kratos Made By Seaton Jiang

宁ICP备2021001508号

宁公网安备64040202000141号