MySql 常用命令收藏

8 篇文章 0 订阅
本文详细介绍了如何使用MySQL命令行进行登录、显示数据库、查看表结构、退出、备份和恢复数据库的操作。重点讲解了全量备份和特定数据库备份的mysqldump命令,以及如何使用source命令和gunzip工具进行数据库的恢复。同时,提到了删除数据库的注意事项。
摘要由CSDN通过智能技术生成

目录

1、登录

2、显示数据库

3、显示数据库中的表

4、显示表结构

5、退出MySQL命令行模式

6、备份数据库

1.备份所有数据库

2.备份指定数据库

7、还原备份

1.恢复所有数据库

普通还原

带压缩还原

2.恢复指定数据库

普通还原

带压缩还原

8、删除数据库


1、登录

mysql -uroot -proot

参数说明:

-u 用户名

-p 密码 

root@a233977e55c3:/# mysql -uroot -proot
mysql: [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: 8.0.19 MySQL Community Server - GPL

Copyright (c) 2000, 2020, 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> 

2、显示数据库

show databases;
mysql> show databases;
ERROR 2006 (HY000): MySQL server has gone away
No connection. Trying to reconnect...
Connection id:    8
Current database: *** NONE ***

+--------------------+
| Database           |
+--------------------+
| information_schema |
| jeecg-boot         |
| mysql              |
| nacos              |
| performance_schema |
| sys                |
| xxl_job            |
+--------------------+
7 rows in set (0.01 sec)

3、显示数据库中的表

use mysql;
show tables;

 use 数据库名称; 选择数据库

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> show tables;
+---------------------------+
| Tables_in_mysql           |
+---------------------------+
| columns_priv              |
........
| user                      |
+---------------------------+
33 rows in set (0.00 sec)

4、显示表结构

describe 表名;
mysql> describe user;
+--------------------------+-----------------------------------+------+-----+-----------------------+-------+
| Field                    | Type                              | Null | Key | Default               | Extra |
+--------------------------+-----------------------------------+------+-----+-----------------------+-------+
| Host                     | char(255)                         | NO   | PRI |                       |       |
| User                     | char(32)                          | NO   | PRI |                       |       |
.........................
| User_attributes          | json                              | YES  |     | NULL                  |       |
+--------------------------+-----------------------------------+------+-----+-----------------------+-------+
51 rows in set (0.00 sec)

5、退出MySQL命令行模式

exit
mysql> exit
Bye

6、备份数据库

1.备份所有数据库

mysqldump -u用户名 -p密码 --all-databases [|gzip] > 备份名称[.gz]

 []的部分为可选

 | gzip 压缩备份

例如:

root@a233977e55c3:/# mysqldump -uroot -proot --all-databases > allbackup.sql
mysqldump: [Warning] Using a password on the command line interface can be insecure.

 带压缩

root@a233977e55c3:/# mysqldump -uroot -proot --all-databases |gzip > allbackup.sql.gz
mysqldump: [Warning] Using a password on the command line interface can be insecure.

 可以用ls -l -h输出结果,压缩后可以节约不少空间。

root@a233977e55c3:/# ls -l -h
total 9.7M
-rw-r--r--   1 root root 6.1M Mar  6 16:03 allbackup.sql
-rw-r--r--   1 root root 1.3M Mar  6 16:14 allbackup.sql.gz

2.备份指定数据库

mysqldump -u用户名 -p密码 --databases 数据库名[可以多个数据库用空格分开] [|gzip] > 备份名称[.gz]

 例如:

root@a233977e55c3:/# mysqldump -uroot -proot mysql > mysql.sql
mysqldump: [Warning] Using a password on the command line interface can be insecure.

 带压缩

root@a233977e55c3:/# mysqldump -uroot -proot mysql |gzip > mysql.sql.gz
mysqldump: [Warning] Using a password on the command line interface can be insecure.

7、还原备份

1.恢复所有数据库

普通还原

mysql -u用户名 -p密码 -e "source 备份名称"
root@a233977e55c3:/# mysql -uroot -proot -e "source allbackup.sql"
mysql: [Warning] Using a password on the command line interface can be insecure.

带压缩还原

gunzip < 备份名称.gz | mysql -u用户名 -p密码
gunzip < allbackup.sql.gz | mysql -uroot -proot

2.恢复指定数据库

普通还原

mysql -u用户名 -p密码 数据库名称 < 备份名称
root@a233977e55c3:/# mysql -uroot -proot mysql < mysql.sql
mysql: [Warning] Using a password on the command line interface can be insecure.

带压缩还原

gunzip < 备份名称.gz | mysql -u用户名 -p密码 数据库名称
root@a233977e55c3:/# gunzip < mysql.sql.gz | mysql -uroot -proot mysql 
mysql: [Warning] Using a password on the command line interface can be insecure.

8、删除数据库

​drop database `数据库名称`;

注意:`数据库名称` 名称两边的【`】,有特殊符号的数据库名称必须加。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值