准备学习环境

安装 MySQL

Windows

对于Windows,直接官网下载一直下一步即可(下载下面的那个大的)

安装程序会要求您提供安装类型,这时请选择 Developer Default,其他直接一路默认

之后,会指导你设置root用户的密码

Linux

下载安装

对于Linux,直接apt下载安装即可

1
2
sudo apt update
sudo apt install mysql-server mysql-client

然后运行服务

1
sudo service mysql start

测试是否能进入MySQL命令行,一开始并没有root密码,直接回车即可

1
sudo mysql -u root -p

初始化

可以看见MySQL已经安装成功,接下来输入exit先退回bash,我们需要初始化一下MySQL

1
2
exit # 退出MySQL,返回bash
sudo mysql_secure_installation

初始化设计 5 个步骤:

  1. 安装验证密码插件

  2. 设置root管理员在数据库中的专有密码

  3. 随后删除匿名账户,并使用root管理员从远程登录数据库,以确保数据库上运行的业务的安全性

  4. 删除默认的测试数据库,取消测试数据库的一系列访问权限

  5. 刷新授权列表,让初始化的设定立即生效

下面是输出的信息,添加了简单的注释

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
pi@DESKTOP-LECILAQ:~$ sudo mysql_secure_installation

Securing the MySQL server deployment.

Connecting to MySQL using a blank password.

VALIDATE PASSWORD COMPONENT can be used to test passwords
and improve security. It checks the strength of password
and allows the users to set only those passwords which are
secure enough. Would you like to setup VALIDATE PASSWORD component? # 要安装验证密码插件吗?

Press y|Y for Yes, any other key for No: N # 这里我选择N
Please set the password for root here.

New password: # 输入要为root管理员设置的数据库密码

Re-enter new password: # 再次输入密码
By default, a MySQL installation has an anonymous user,
allowing anyone to log into MySQL without having to have
a user account created for them. This is intended only for
testing, and to make the installation go a bit smoother.
You should remove them before moving into a production
environment.

Remove anonymous users? (Press y|Y for Yes, any other key for No) : Y # 删除匿名账户
Success.


Normally, root should only be allowed to connect from
'localhost'. This ensures that someone cannot guess at
the root password from the network.

Disallow root login remotely? (Press y|Y for Yes, any other key for No) : N # 禁止root管理员从远程登录,这里我没有禁止

... skipping.
By default, MySQL comes with a database named 'test' that
anyone can access. This is also intended only for testing,
and should be removed before moving into a production
environment.


Remove test database and access to it? (Press y|Y for Yes, any other key for No) : Y # 删除test数据库并取消对它的访问权限
- Dropping test database...
Success.

- Removing privileges on test database...
Success.

Reloading the privilege tables will ensure that all changes
made so far will take effect immediately.

Reload privilege tables now? (Press y|Y for Yes, any other key for No) : Y # 刷新授权表,让初始化后的设定立即生效
Success.

All done!
pi@DESKTOP-LECILAQ:~$

允许远程连接

编辑 /etc/mysql/mysql.conf.d/mysqld.cnf 文件,把bind-address的值改为0.0.0.0

1
sudo nano /etc/mysql/mysql.conf.d/mysqld.cnf

之后再进入MySQL,执行下面的命令

1
2
3
4
5
mysql> CREATE USER 'root'@'%' IDENTIFIED BY '你的密码';
Query OK, 0 rows affected (0.03 sec)

mysql> grant all privileges on *.* to 'root'@'%';
Query OK, 0 rows affected (0.02 sec)

重新启动,现在允许从任何位置通过root用户连接了

安装 HeidiSQL

很多教程用的都是Navicat,但是它是付费的,而且太太太贵了,我又不敢用破解版,所以选择了HeidiSQL

前往官网或点击这里下载

使用HeidiSQL连接到你的数据库

加载样例数据

这里的样例来自《SQL必知必会》(第五版),点击此链接下载

在左侧右键新建一个数据库,用于存储样例数据,我取名为test

然后加载create.sql,再按上面的蓝色箭头运行

回到test库,右键刷新,可以看见已经出现了数据

然后再对populate.sql作相同的操作,完成样例数据导入

数据库的层次结构

由大到小,一共可以分为4层:数据库(或称为字段或记录)

数据库(datebase)

定义:保持有组织的数据的容器(通常是一个文件或一组文件)

一台主机中可以包含诺干个数据库,例如下图中共有information_schemamysqlperformance_schemasystest5个数据库,其中test是我们在上一篇添加的

注意:误用导致混淆

人们常用数据库这个术语代表他们使用的数据库软件,这是不正确的,也因此产生了许多混淆。确切地说,数据库软件应称为数据库管理系统(DBMS)。数据库是通过DBMS创建和操纵的容器,而具体它究竟是什么,形式如何,各种数据库都不一样

表(table)

定义:某种特定类型数据的结构化清单

如图,test库中包含了CustomersOrderItemsOrdersProductsVendors5个表

列(column)

定义:表中的一个字段(也就是说字段是同一个意思),所有的表都是由一个或多个列组成的

数据类型:记住,每个列都有对应的数据类型,

行(row)

定义:表中的一个记录(record),也就是说记录是同一个意思