创建数据库
语法
CREATE DATABASE [IF NOT EXISTS] db_name [create_specification [,
create_specification] ...]
create_specification:
[DEFAULT] CHARACTER SET charset_name
[DEFAULT] COLLATE collation_name
说明:
- 大写的表示关键字
- [] 是可选项
- CHARACTER SET: 指定数据库采用的字符集
- COLLATE: 指定数据库字符集的校验规则
基本操作
- 查看数据库列表
show databases
![![[Pasted image 20260423151039.png]]](https://i-blog.csdnimg.cn/direct/852268cf46304a5abab6aabda875ec1a.png)
- 创建数据库–本质就是在/var/lib/mysql创建一个目录
create database database1
![![[Pasted image 20260423151136.png]]](https://i-blog.csdnimg.cn/direct/e9f1c1c0ff4948178ac4e7389f0c0f50.png)
![![[Pasted image 20260423151154.png]]](https://i-blog.csdnimg.cn/direct/b94c536864db4aa88237de406667b94c.png)
- 删除数据库–删除目录
drop database database1
![![[Pasted image 20260423151253.png]]](https://i-blog.csdnimg.cn/direct/f6d8e127ef1d4c6dbdfcb03bafcad42d.png)
![![[Pasted image 20260423151301.png]]](https://i-blog.csdnimg.cn/direct/f31b1a6f3ff84c95818bde930f7112d3.png)
create database if not exists database1
![![[Pasted image 20260423151457.png]]](https://i-blog.csdnimg.cn/direct/9c6fe65a87ba4aff9c8ca300cc468518.png)
字符集和校验规则
创建数据库的时候,有两个编码集:
- 数据库编码集 – 数据库未来存储数据
- 数据库校验集 – 支持数据库,进行字段比较使用的编码,本质也是一种读取数据库中数据的采用的编码格式
数据库无论对数据做任何操作,都必须保证操作和编码必须是编码一致的!
查看系统默认字符集以及校验规则
show variables like 'character_set_database';
show variables like 'collation_database';
![![[Pasted image 20260423152143.png]]](https://i-blog.csdnimg.cn/direct/9b59522539b640629d6b4c3294970b23.png)
查看数据库支持的字符集
show charset;
![![[Pasted image 20260423152209.png]]](https://i-blog.csdnimg.cn/direct/cdd69901ad984ebeab3da338c75ae21d.png)
查看数据库支持的字符集校验规则
show collation;
![![[Pasted image 20260423152345.png]]](https://i-blog.csdnimg.cn/direct/355d5fb7809a49b3b2ad1fa0042a5489.png)
创建数据库案例
- 创建名为 d1 的数据库
create database d1;
![![[Pasted image 20260423152546.png]]](https://i-blog.csdnimg.cn/direct/21b3ac1149ae47bcabf23381dc1829fd.png)
![![[Pasted image 20260423152559.png]]](https://i-blog.csdnimg.cn/direct/54fde8188ea74b87b7c33130e4458bfd.png)
当我们创建数据库没有指定字符集和校验规则时,系统使用默认字符集:utf8,校验规则是:utf8_ general_ ci
- 创建一个使用utf8字符集的 d2 数据库
create database d2 charset=utf8;
![![[Pasted image 20260423152754.png]]](https://i-blog.csdnimg.cn/direct/352a0fca587f409b84bd0023cf3274b6.png)
- 创建一个使用utf8字符集的 d3 数据库
create database d3 character set utf8;
![![[Pasted image 20260423153014.png]]](https://i-blog.csdnimg.cn/direct/57949cfa307645369b9854d69c1e4246.png)
![![[Pasted image 20260423153032.png]]](https://i-blog.csdnimg.cn/direct/0959e11c4e834a3a8e18731780d62411.png)
- 创建一个使用utf字符集,并带校对规则的 d4 数据库
create database d4 charset=utf8 collate utf8_general_ci;
![![[Pasted image 20260423153200.png]]](https://i-blog.csdnimg.cn/direct/21d07da553c14541a04a854967390606.png)
![![[Pasted image 20260423153217.png]]](https://i-blog.csdnimg.cn/direct/5305753c50304af8a29ecc281e2094a3.png)
- 创建一个使用gbk字符集,并带校对规则的 d5 数据库
![![[Pasted image 20260423153330.png]]](https://i-blog.csdnimg.cn/direct/bdb4e26803034455b3e32a03f8f63b79.png)
![![[Pasted image 20260423153355.png]]](https://i-blog.csdnimg.cn/direct/7a3f44077ecf436696403041feb2ab37.png)
校验规则对数据库的影响
- 不区分大小写
创建一个数据库,校验规则使用utf8_ general_ ci[不区分大小写] - 区分大小写
创建一个数据库,校验规则使用utf8_ bin[区分大小写]
![![[Pasted image 20260423153630.png]]](https://i-blog.csdnimg.cn/direct/b7677af5e1504807a37372540b17a355.png)
![![[Pasted image 20260423153637.png]]](https://i-blog.csdnimg.cn/direct/b6616c0b40814c64a2fe0a76122fdff3.png)
![![[Pasted image 20260423153825.png]]](https://i-blog.csdnimg.cn/direct/88ba2d895aa94fa989cfec15aeb7aa46.png)
![![[Pasted image 20260423153838.png]]](https://i-blog.csdnimg.cn/direct/216af4908c684100a85f9a85d45c9075.png)
不区分大小写的查询以及结果
![![[Pasted image 20260423154003.png]]](https://i-blog.csdnimg.cn/direct/786137a5458744059196fc7e2862716b.png)
不区分大小写排序以及结果
![![[Pasted image 20260423154311.png]]](https://i-blog.csdnimg.cn/direct/0bea807bbb974a298bf280eaec243639.png)
![![[Pasted image 20260423154112.png]]](https://i-blog.csdnimg.cn/direct/6ca569a54ae64267ad885f04a6c5120c.png)
![![[Pasted image 20260423154119.png]]](https://i-blog.csdnimg.cn/direct/a0f8bd01837c43228a56d385d2967904.png)
区分大小写的查询以及结果
![![[Pasted image 20260423154207.png]]](https://i-blog.csdnimg.cn/direct/9b83f563e63d4fce857668b10482dc85.png)
区分大小写排序以及结果
![![[Pasted image 20260423154250.png]]](https://i-blog.csdnimg.cn/direct/613578ed577f4a828d530e0c75fb4a6f.png)
操纵数据库
查看数据库
show databases;
![![[Pasted image 20260423155301.png]]](https://i-blog.csdnimg.cn/direct/04e6068a5d704c1593f827ab6d87c600.png)
显示创建语句
show create database 数据库名;
![![[Pasted image 20260423155812.png]]](https://i-blog.csdnimg.cn/direct/74f6275a34324ec78c6d2ee1e29b6217.png)
![![[Pasted image 20260423155846.png]]](https://i-blog.csdnimg.cn/direct/3b8df9317b004704945b1f7ce2762de5.png)
说明:
- MySQL 建议我们关键字使用大写,但是不是必须的。
- 数据库名字的反引号``,是为了防止使用的数据库名刚好是关键字
/*!40100 default.... */这个不是注释,表示当前mysql版本大于4.01版本,就执行这句话
修改数据库
语法:
ALTER DATABASE db_name
[alter_spacification [,alter_spacification]...]
alter_spacification:
[DEFAULT] CHARACTER SET charset_name
[DEFAULT] COLLATE collation_name
说明:
- 对数据库的修改主要指的是修改数据库的字符集,校验规则
实例: 将 mytest 数据库字符集改成 gbk
![![[Pasted image 20260423155602.png]]](https://i-blog.csdnimg.cn/direct/950e9b4d52184e4dbd39de14e64bcada.png)
![![[Pasted image 20260423155614.png]]](https://i-blog.csdnimg.cn/direct/427d03ef71ac4eb48fc9eaaf5a1e69ce.png)
数据库删除
DROP DATABASE [IF EXISTS] db_ name;
![![[Pasted image 20260423155123.png]]](https://i-blog.csdnimg.cn/direct/0720c9db099643d2aee8a9e7b1b34a64.png)
执行删除之后的结果:
- 数据库内部看不到对应的数据库
- 对应的数据库文件夹被删除,级联删除,里面的数据表全部被删
注意:不要随意删除数据库
备份和恢复
- 备份
# mysqldump -P3306 -u root -p 密码 -B 数据库名 > 数据库备份存储的文件路径
![![[Pasted image 20260423160245.png]]](https://i-blog.csdnimg.cn/direct/f75ce2918c9144b7af348c20ce07b6c9.png)
这时,可以打开看看 test1.sql 文件里的内容,其实把我们整个创建数据库,建表,导入数据的语句都装载这个文件中。
![![[Pasted image 20260423160308.png]]](https://i-blog.csdnimg.cn/direct/507c5685d43e460f840afbe9b70be3c5.png)
![![[Pasted image 20260423160403.png]]](https://i-blog.csdnimg.cn/direct/2165d36383004b0d8c403641d6b0fde4.png)
- 恢复
![![[Pasted image 20260423160454.png]]](https://i-blog.csdnimg.cn/direct/1dcec99187264cccbc1156053d937be8.png)
![![[Pasted image 20260423160535.png]]](https://i-blog.csdnimg.cn/direct/5a275ee82677446181b5a1c2b5f75796.png)
![![[Pasted image 20260423160546.png]]](https://i-blog.csdnimg.cn/direct/09f40ab81bed4dcfa90979d5e6ebc7cc.png)
- 注意事项
如果备份的不是整个数据库,而是其中的一张表,怎么做?
# mysqldump -u root -p 数据库名 表名1 表名2 > D:/mytest.sql
同时备份多个数据库
# mysqldump -u root -p -B 数据库名1 数据库名2 ... > 数据库存放路径
如果备份一个数据库时,没有带上-B参数, 在恢复数据库时,需要先创建空数据库,然后使用数据库,再使用source来还原。
查看连接情况
语法:
show processlist
![![[Pasted image 20260423160857.png]]](https://i-blog.csdnimg.cn/direct/e5ea5e5da4d948f4aecb79cf876e4005.png)
可以告诉我们当前有哪些用户连接到我们的MySQL,如果查出某个用户不是你正常登陆的,很有可能你的数据库被人入侵了。以后大家发现自己数据库比较慢时,可以用这个指令来查看数据库连接情况
458



被折叠的 条评论
为什么被折叠?



