由于自考的实践考核要求有需要用到 mysql 进行考核,故记录一下在 mac 环境下试手的笔记。
初始环境
首先在 mysql 官网中下载你想要的版本。可以直接下载 dmg 安装包,按照安装指示一步一步安装,并设置 mysql 的密码。
下载完毕后,一般情况下直接通过命令行使用 mysql 命令会找不到对应的命令:
1  | ➜ ~ mysql -v  | 
因此需要对当前的命令行工具配置对应的环境变量,比如笔者使用的是 zsh,则打开 ~/.zshrc 文件添加以下配置:
1  | export PATH=${PATH}:/usr/local/mysql/bin/  | 
若使用 bash 的用户同理,直接在 ~/.bashrc 添加相同代码。添加完毕后通过 source 命令重新加载对应的环境变量: source ~/.zshrc
接着就可以在命令行直接使用 mysql 了。输入 mysql -u root -p 登录 mysql,密码是在安装阶段时设置的密码。
1  | ➜ ~ mysql -u root -p  | 
数据库操作
DATABASE 可以不区分大小写,但只能要么全小写,要么全大写。一般会将这些参数用大写写出。
创建数据库
1  | -- 还可以通过 DEFAULT CHARACTER SET 选项设置默认的编码集  | 
查看现有的数据库
1  | mysql> SHOW DATABASES;  | 
切换到指定数据库
1  | mysql> USE DANNY_DATABASE  | 
数据库的查看与删除
1  | -- 创建数据库: 准备稍后移除的数据库  | 
查看当前使用的数据库
1  | -- 未选择的情况下  | 
数据表操作
创建数据表
1  | -- 创建名为 customers 的数据表  | 
其中 IF NOT EXISTS 参数是可选的,它的意思为若 customers 表不存在则创建它。
查看数据表与表列
1  | -- 查看当前用户在当前数据库中可以访问的数据表  | 
删除数据表
1  | -- 添加一个数据表用于演示删除  | 
数据表添加新列
1  | -- 插入新列  | 
数据表修改表列
修改整列: 将列名 cust_sex 修改 sex,并修改默认值
1  | mysql> alter TABLE customers  | 
仅修改列的类型
1  | mysql> ALTER TABLE customers  | 
修改指定列的指定字段
1  | mysql> ALTER TABLE customers  | 
移除数据表列: 移除 cust_contact 数据表项
1  | mysql> ALTER TABLE danny_database.customers  | 
数据项操作
添加数据
默认情况下在命令行中 mysql 是不能直接插入中文的,这个跟字符集有关。可输入下面命令修改数据库或表的字符集:
1  | 
  | 
为数据表插入数据,显式设置字段
1  | mysql> INSERT INTO danny_database.customers(cust_id, cust_name, sex, cust_address)  | 
由于 cust_id 是自增的,因此可以将此字段的值设置为 0 或 NULL 会自动自增。上例 “李四” 的 cust_id 在创建后就被自增为 902。
还可以通过 SET 语句设置部分值:
1  | mysql> INSERT INTO danny_database.customers SET cust_name='王五', cust_address='武汉市', sex=DEFAULT;  | 
查询数据
可通过 SELECT 语句查询数据:
1  | mysql> SELECT * FROM customers;  | 
仅展示指定字段:
1  | +---------+-----------+------+  | 
通过 WHERE 子句设置查询条件,筛选出符合查询条件的数据:
1  | mysql> SELECT cust_id,cust_name,cust_address FROM customers  | 
删除数据
1  | -- 添加几项测试数据  | 
更新数据
1  | -- 更新数据  | 
实践
以一个 eShop 的需求为例做个简单的测试吧。
创建 eshop 数据库
在 MySQL 中创建一个名为 eshop 的数据库,选择字符集为 utf8mb4
1  | mysql> CREATE DATABASE IF NOT EXISTS eshop DEFAULT CHARACTER SET utf8mb4;  | 
创建数据表及相关记录
相关表信息如下
表名:用户(t_user)
| 字段名 | 类型 | 大小 | 
|---|---|---|
| 用户ID (id) | 自增类型 | |
| 姓名 (user_name) | 文本 | 50,非空 | 
| 联系电话 (phone_no) | 文本 | 20,非空 | 
表名:商品(product)
| 字段名 | 类型 | 大小 | 
|---|---|---|
| 商品ID(id) | 自增类型 | |
| 商品名称(product_name) | 文本 | 50,非空 | 
| 价格(price) | 数值类型 | (整数位9位,小数位2位),非空 | 
表名:购物车 (shopping_cart)
| 字段名 | 类型 | 大小 | 
|---|---|---|
| 用户id(user_id) | 整数 | 非空,主键,参考用户表主键 | 
| 商品id(product_id) | 整数 | 非空,主键,参考商品表主键 | 
| 商品数量(quantity) | 整数 | 非空 | 
1  | -- 用户表  | 
录入用户数据
用户信息
1  | 1;张三; 13333333333;  | 
商品信息
1  | 1; C++程序设计教程; 45.5  | 
购物车
1  | 1; 1; 5  | 
录入数据:
1  | -- 插入用户表数据  | 
数据的查询与更新
使用 SQL 语句列出「张三」购买商品清单信息,以购买数量升序排列:
1  | mysql> SELECT u.user_name, p.product_name, u.phone_no, p.price, s.quantity FROM t_user u, product p, shopping_cart s  | 
使用 SQL 语句选出李四购买商品的总价:
1  | mysql> SELECT u.user_name, p.product_name, p.price, s.quantity, p.price*s.quantity AS total_price FROM t_user u, product p, shopping_cart s  | 
使用 SQL 语句列出购买数量排前两位的商品名称:
1  | mysql> SELECT p.product_name, p.price, s.quantity FROM product p, shopping_cart s  | 
忘记密码
若忘记数据库密码后可通过 mysqld_safe 来修改密码:
在系统偏好设置中关闭 mysql 服务
打开终端,输入命令:
1
2➜ ~ cd /usr/local/mysql/bin
➜ ~ sudo su命令行变成以
sh-3.2#开头后继续输入命令:1
2
3
4sh-3.2# ./mysqld_safe --skip-grant-tables &
mysqld_safe Logging to '/usr/local/mysql/data/DannydeMBP.err'.
mysqld_safe Starting mysqld daemon with databases from /usr/local/mysql/data新开个命令行窗口,进入
mysql:1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22➜ ~ /usr/local/mysql/bin/mysql
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 30
Server version: 5.7.31
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>
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更新密码
1
2
3
4mysql> update user set authentication_string=password('admin') where Host='localhost' and User='root';
Query OK, 1 row affected, 1 warning (0.01 sec)
Rows matched: 1 Changed: 1 Warnings: 1输入
exit命令退出mysql,查出mysqld_safe进程号并杀掉:1
2
3
4
5
6
7
8
9
10mysql> exit
Bye
➜ ~ ps -ax | grep mysql
8553 ttys004 0:00.03 /bin/sh ./mysqld_safe --skip-grant-tables
8623 ttys004 0:00.92 /usr/local/mysql-5.7.31-macos10.14-x86_64/bin/mysqld --basedir=/usr/local/mysql-5.7.31-macos10.14-x86_64 --datadir=/usr/local/mysql-5.7.31-macos10.14-x86_64/data --plugin-dir=/usr/local/mysql-5.7.31-macos10.14-x86_64/lib/plugin --user=mysql --skip-grant-tables --log-error=host-3-187.can.danny1.network.err --pid-file=host-3-187.can.danny1.network.pid
# 杀掉 mysql 的进程
➜ ~ kill -9 8553
➜ ~ kill -9 8623此时返回系统偏好设置中看到 mysql 被关闭后就算正确退出了。接着继续输入
mysql -u root -p命令连接数据库,再输入刚才修改的密码即可。
参考资料

