一、基础知识
知名数据库排名查询地址:https://db-engines.com/en/ranking
Mysql下载地址:https://dev.mysql.com/downloads/mysql/
1.1 历史
- 2005年10月,mysql5.0版本发布
- 2008年1月16日,mysql被sun公司收购
- 2009年4月20日,Oracle收购Sun公司,Mysql转入Oracle公司
- 2013年2月5日:5.6首个正式版本发布
- 2015年10.21日,mysql5.7首个正式版5.7.9发布
- 2016年9月12日,8.0开发版发布
1.2 版本介绍
- Alpha版本:内部测试版,一般不向外发布
- Beta版本:功能开发完和测试之后的产品,不会存在较大的功能和性能bug
- RC版本:生成环境之前的一个小版本,根据Beta版本测试结果打补丁后的版本
- GA版本:正式发布版本
1.3 mysql相关概念
- 实体(Entity):客观存在并且可以相互区别的事务,一本书,一支笔
- 属性(Attribute):描述实体特性
- 码(Key):唯一标识实体的属性或属性的集合称为码
- 域(Domain):属性的取值范围
- 实体型(Entity Type):具有相同属性的实体必然具有共同的特征和性质,用实体名或属性名的集合来抽象刻画同类实体
- 实体集(Entity Set):同类学生的集合称为实体集
- 联系(Relationship)
- 实体内部的联系:指组成实体的各属性之间的联系
- 实体间的联系:不同实体集之间的联系
- 模型:对现实世界特征的模拟和抽象,数据模型也是一种模型
- 数据模型
- 概念数据模型
- 结构数据模型
实体类型及实体间的联系,独立于计算机的模型,现实世界的第一层抽象,概念模型(E-R图)
矩形:实体
椭圆:实体的属性
棱形:实体间的联系
二、Mysql的安装与管理
2.1 mysql8.0的安装
打开网址:下载
- 选择最新版本下载
- 将下载好的软件上传到Linux的/root目录
[root@db01 ~]#pwd
/root
[root@db01 ~]#ll
total 307360
-rw-r--r-- 1 root root 314736640 Jun 23 21:43 mysql-8.0.29-linux-glibc2.17-x86_64-minimal.tar
- 解压并删除没用的文件
[root@db01 ~]#tar xf mysql-8.0.29-linux-glibc2.17-x86_64-minimal.tar
[root@db01 ~]#ll
total 614720
-rw-r--r-- 1 root root 314736640 Jun 23 21:43 mysql-8.0.29-linux-glibc2.17-x86_64-minimal.tar
-rw-r--r-- 1 7155 31415 54767572 Mar 24 18:55 mysql-8.0.29-linux-glibc2.17-x86_64-minimal.tar.xz
-rw-r--r-- 1 7155 31415 6888372 Mar 24 18:59 mysql-router-8.0.29-linux-glibc2.17-x86_64-minimal.tar.xz
-rw-r--r-- 1 7155 31415 253076824 Mar 24 19:07 mysql-test-8.0.29-linux-glibc2.17-x86_64-minimal.tar.xz
[root@db01 ~]#tar xf mysql-8.0.29-linux-glibc2.17-x86_64-minimal.tar.xz
[root@db01 ~]#ll
total 614720
drwxr-xr-x 9 root root 129 Jun 23 22:04 mysql-8.0.29-linux-glibc2.17-x86_64-minimal
-rw-r--r-- 1 root root 314736640 Jun 23 21:43 mysql-8.0.29-linux-glibc2.17-x86_64-minimal.tar
-rw-r--r-- 1 7155 31415 54767572 Mar 24 18:55 mysql-8.0.29-linux-glibc2.17-x86_64-minimal.tar.xz
-rw-r--r-- 1 7155 31415 6888372 Mar 24 18:59 mysql-router-8.0.29-linux-glibc2.17-x86_64-minimal.tar.xz
-rw-r--r-- 1 7155 31415 253076824 Mar 24 19:07 mysql-test-8.0.29-linux-glibc2.17-x86_64-minimal.tar.xz
[root@db01 ~]#rm -rf *.tar
[root@db01 ~]#rm -rf *.xz
[root@db01 ~]#ll
total 0
drwxr-xr-x 9 root root 129 Jun 23 22:04 mysql-8.0.29-linux-glibc2.17-x86_64-minimal
- 创建目录并将包移动到相应目录
mkdir /linie/{softwares,data,log} -p
mkdir /linie/data/mysql -p #真实数据
mkdir /linie/data/mysql-bin -p #二进制日志
cp -r mysql-8.0.29-linux-glibc2.17-x86_64-minimal /linie/softwares/mysql
- 配置环境变量
vim /etc/profile.d/mysql.sh
#!/bin/bash
export MYSQL=/linie/softwares/mysql
export PATH=$PATH:$MYSQL/bin
#加载环境变量
source /etc/profile.d/mysql.sh
#测试环境变量
[root@db01 ~]#mysql -V
mysql Ver 8.0.29 for Linux on x86_64 (MySQL Community Server - GPL)
- 创建用户及数据目录,并添加权限
useradd -r -s /sbin/nologin -d /linie/data/mysql -c 'MySQL DataBase Server User' mysql
chown -R mysql.mysql /linie/data/mysql
- 初始化
#进入目录
cd /linie/softwares/mysql
(1)不设置密码
mysqld --initialize-insecure --user=mysql --basedir=/linie/softwares/mysql --datadir=/linie/data/mysql
(2)设置一个12位的4种密码复杂度的临时管理用户
mysqld --initialize --user=mysql --basedir=/linie/softwares/mysql80 --datadir=/linie/data/mysql80
[root@db01 mysql]#mysqld --initialize-insecure --user=mysql --basedir=/linie/softwares/mysql --datadir=/linie/data/mysql
2022-06-23T15:21:37.533909Z 0 [System] [MY-013169] [Server] /linie/softwares/mysql/bin/mysqld (mysqld 8.0.29) initializing of server in progress as process 3251
2022-06-23T15:21:37.541401Z 1 [System] [MY-013576] [InnoDB] InnoDB initialization has started.
2022-06-23T15:21:37.827584Z 1 [System] [MY-013577] [InnoDB] InnoDB initialization has ended.
2022-06-23T15:21:38.478605Z 6 [Warning] [MY-010453] [Server] root@localhost is created with an empty password ! Please consider switching off the --initialize-insecure option.
- 配置启动方式
[root@db01 mysql]#cp -r /linie/softwares/mysql/support-files/mysql.server /etc/init.d/mysqld
cp: overwrite ‘/etc/init.d/mysqld’? y
[root@db01 mysql]#sed -r -i "s#^(datadir=)#\1/linie/data/mysql#g" /etc/init.d/mysqld
[root@db01 mysql]#sed -r -i "s#^(basedir=)#\1/linie/softwares/mysql#g" /etc/init.d/mysqld
#重载配置
chkconfig --add mysqld #设置开机自启脚本
systemctl daemon-reload #重载
- 启动并查看结果
[root@db01 mysql]#systemctl start mysqld
[root@db01 mysql]#netstat -lntp
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name
tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN 1414/sshd
tcp6 0 0 :::3306 :::* LISTEN 3486/mysqld
tcp6 0 0 :::22 :::* LISTEN 1414/sshd
tcp6 0 0 :::33060 :::* LISTEN 3486/mysqld
[root@db01 mysql]#mysql
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 8
Server version: 8.0.29 MySQL Community Server - GPL
Copyright (c) 2000, 2022, 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.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql> exit
Bye
2.2 设置初始密码
#进入数据库设置密码
[root@db01 mysql]#mysql
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 9
Server version: 8.0.29 MySQL Community Server - GPL
Copyright (c) 2000, 2022, 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.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql> alter user root@localhost identified by '123';
Query OK, 0 rows affected (0.00 sec)
mysql> exit
Bye
#使用设置的密码加入数据库
[root@db01 mysql]#mysql -uroot -p123
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 13
Server version: 8.0.29 MySQL Community Server - GPL
Copyright (c) 2000, 2022, 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.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql>
2.3 Mysql重置密码
- 停止数据库
systemctl stop mysqld