MySQL 简介
mysql-server #提供服务
mysql-client #提供SQL命令
官网地址:https://dev.mysql.com/downloads/
https://downloads.mysql.com/archives/community/
查看帮助文档
mysql> ? contents
一、安装MySQL
1.1、二进制安装mysql5.7-linux
安装前准备
shell> rpm -qa | grep mysql
shell> rpm -qa | grep mariadb
mariadb-libs-5.5.56-2.el7.x86_64
shell> yum -y remove mariadb-libs.x86_64
创建mysql用户
shell> useradd -M -s /sbin/nologin -r mysql
# -M不创建主目录,-s /sbin/nologin不允许登录,-r创建的是系统用户
安装依赖包
shell> yum -y install libaio
开始安装
shell> tar -xvf mysql-5.7.34-el7-x86_64.tar.gz -C /erp/
shell> cd /erp
shell> mv mysql-5.7.34 mysql;cd mysql
shell> mkdir data;mkdir logs
shell> touch /erp/mysql/logs/mysqld.log
shell> chown -R mysql.mysql /erp/mysql/
shell> pwd
/erp/mysql
开始编译安装–初始化目录
shell> ./bin/mysqld --initialize \
--user=mysql \
--basedir=/erp/mysql \
--datadir=/erp/mysql/data
....
root@localhost: a(kdUreTc8W0 #初始化密码,注意保存
编辑配置文件/etc/my.cnf
shell> vim /etc/my.cnf
[mysqld]
basedir=/erp/mysql
datadir=/erp/mysql/data
port=3306
socket=/erp/mysql/mysql.sock
character-set-server=utf8
log-error=/erp/mysql/logs/mysqld.log
pid-file=/tmp/mysqld.pid
[mysql]
socket=/erp/mysql/mysql.sock
[client]
socket=/erp/mysql/mysql.sock
配置环境变量,并刷新
shell> vim /etc/profile.d/mysql.sh
export PATH=/erp/mysql/bin:$PATH
shell> . /etc/profile.d/mysql.sh 或 source /etc/profile.d/mysql.sh
配置启动脚本,并启动MySQL
shell> pwd
/erp/mysql
shell> cp support-files/mysql.server /etc/init.d/mysqld
shell> chmod +x /etc/init.d/mysqld
shell> vim /etc/init.d/mysqld
basedir=/erp/mysql //在第46~47行
datadir=/erp/mysql/data
shell> /etc/init.d/mysqld start
Starting MySQL. SUCCESS!
1.2、二进制安装-windows
- 下载地址
https://cdn.mysql.com/archives/mysql-5.7/mysql-5.7.37-winx64.zip
- 安装
1、解压
2、管理员身份启动cmd,切换到解压目录,并进入bin目录
例如:C:\Windows\system32>cd /d D:\app\mysql57\bin
3、初始化:mysqld --initialize --console ,会生成一个随机密码
4、安装:mysqld -install #一定要用管理员身份安装
5、启动:net start mysql
6、登录:mysql -p初始化密码
7、修改密码:set password=password("123456");
8、在MySQL目录下创建一个my.ini或my.cnf配置文件,指定一些MySQL的基本配置
9、如果添加完配置文件启动失败,则删除data下数据,重新执行 初始化
[mysqld]
# 设置3306端口
port=3306
# 设置mysql的安装目录
basedir=D:\app\mysql57
# 设置mysql数据库的数据的存放目录
datadir=D:\app\mysql57\data
# 允许最大连接数
max_connections=200
# 允许连接失败的次数。这是为了防止有人从该主机试图攻击数据库系统
max_connect_errors=10
# 服务端使用的字符集默认为UTF8
character-set-server=utf8
# 创建新表时将使用的默认存储引擎
default-storage-engine=INNODB
# 默认使用“mysql_native_password”插件认证
default_authentication_plugin=mysql_native_password
character-set-server=utf8mb4
[mysql]
# 设置mysql客户端默认字符集
default-character-set=utf8
[client]
# 设置mysql客户端连接服务端时默认使用的端口
port=3306
default-character-set=utf8
1.3、yum安装
[root@master-51 mysql-5.7.17]# rpm -Uvh mysql-community-*.rpm
warning: mysql-community-client-5.7.17-1.el7.x86_64.rpm: Header V3 DSA/SHA1 Signature, key ID 5072e1f5: NOKEY
error: Failed dependencies:
perl(JSON) is needed by mysql-community-test-5.7.17-1.el7.x86_64
[root@master-51 mysql-5.7.17]# rpm -Uvh mysql-community-*.rpm
warning: mysql-community-client-5.7.17-1.el7.x86_64.rpm: Header V3 DSA/SHA1 Signature, key ID 5072e1f5: NOKEY
error: Failed dependencies:
perl(JSON) is needed by mysql-community-test-5.7.17-1.el7.x86_64
- 出现如下报错
[root@master-51 mysql-5.7.17]# rpm -Uvh mysql-community-*.rpm
perl(JSON) is needed by mysql-community-test-5.7.17-1.el7.x86_64
- 解决方法
[root@master-51 ~]# yum -y install perl-JSON
[root@master-51 ~]# rpm -Uvh mysql-community-*.rpm
- 默认配置
默认路径:/var/lib/mysql
传输协议:TCP
进程所有者与所有组:mysql
1.4、麒麟V10安装MYSQL
> 安装mysql5.7,参考如下连接,待测试验证
https://blog.csdn.net/sjk2lq/article/details/125335599
CPU架构:aarch64
MySQL版本:8.0
下载地址:https://cdn.mysql.com/archives/mysql-8.0/mysql-8.0.30-1.el7.aarch64.rpm-bundle.tar
安装: rpm -ivh mysql-community-*.rpm --nodeps --force
参考:https://blog.csdn.net/abcdint/article/details/126310947
二、简单配置
- MySQL 口令
0 or LOW 长度默认为8位,最低密码策略
1 or MEDIUM(系统默认) 长度;数字,大小写字母,特殊字符
2 or STRONG 长度;空令1+字典文件
- 登录mysql
[root@master-51 ~]# grep password /var/log/mysqld.log #复制初始化密码
[root@master-51 mysql]# mysql -p
Enter password:
mysql> set global validate_password_policy=0; #修改密码策略及长度
Query OK, 0 rows affected (0.00 sec)
mysql> set global validate_password_length=6;
Query OK, 0 rows affected (0.00 sec)
mysql> alter user root@'localhost' identified by "123456"; #设置新密码
Query OK, 0 rows affected (0.00 sec)
- 操作指令类型
DDL: 数据定义语言。create alter drop
DML: 数据操作语言。insert update delete
DCL: 数据控制语言。grant revoke
DTL: 数据事务语言。commit rollback savepoint
三、字符集
* 数值型
* 字符型
* 枚举型
* 日期时间型
- 数值型
[整形,浮点型]
[整形:正整数,负整数],整形存小数时会四舍五入
[浮点型:float(默认为4字节),double(默认为8字节)][float(m,n)代表总长度为m,小数位占n位]
[有符号{+,-},只留正数(unsigned)]
int类型默认长度为11。
int(5),#设定字符长度为5,插入值长度不足时使用空格补齐,插入数值可超过指定长度
设置长度是为了节省空间。
[使用关键字zerofill时,用0代替空格补位]
案例:注意先后顺序
mysql> alter table user add age int(2) unsigned default 21 after sex;
- 字符型
char:默认宽度为1,最长为255
varchar:必须制定字符长度
text/bob:大文本类型,字符数大于65535存储时使用,无需指定长度
char(15) 定长:固定字符数长度15个,若存入字符未到15个,用空格补齐15个。浪费存储空间但速度快
varchar(15) 变长:存多少字符就占多少字符空间,需要CPU计算存入多少个字符。浪费CPU
varchar存储时,需要加上一个用来记录字符长度的字节(即L+1)
对于经常变更的数据来说,char比varchar更好,varchar容易产生碎片
- varchar碎片管理
碎片:网上解释:
在MySQL中,如果我们删除了表中的大量数据,或者我们对含有可变长度文本数据类型(VARCHAR,TEXT或BLOB)的表进行了
很多更改,不过被删除的数据记录仍然被保持在MySQL的链接清单中,因此数据存储文件的大小并不会随着数据的删除而减小。
需要执行如下命令来进行碎片整理:
OPTIMIZE [LOCAL | NO_WRITE_TO_BINLOG] TABLE table_name1 [, table_name2] ...
此外,OPTIMIZE TABLE语句有两个可选的关键字:LOCAL和NO_WRITE_TO_BINLOG。
在默认情况下,OPTIMIZE TABLE语句将会被记录到二进制日志中,如果我们指定了LOCAL或NO_WRITE_TO_BINLOG关键字,
则不会记录。当然,一般情况下,我们也无需关注这两个关键字。
- 枚举类型
enum(v1,v2,v3), 单选 值不少于64个
set(v1,v2,v3), 多选
枚举类型在插入数值时,序号可以代表数值。
- 日期时间类型
年 year YYYY 2021,占用1个字符,存储范围为1901--2155
日期 date YYYYMMDD 20210119,占用4个字符,存储范围:0001-01-01--9999-12-31
时间 time HHMMSS 151146,占用3个字符
日期时间类型 time YYYYMMDDHHMMSS 20210119151146
datetime 不获取系统时间
timestamp 没有赋值时,自动获取系统时间
yyyymmddhhmmss
datetime 1000-01-01 00:00:00:00~9999-12-31 23:59:59/不获取系统时间
timestamp 1970-01-01 00:00:00~2038-01-19 00:00:00/没有赋值时自动获取系统时间
date 0001-01-01 ~ 9999-12-31 yyyymmdd
year 1901 ~ 2155 yyyy
time HH:MM:SS
01~69 ---> 2001~2069
70~99 ---> 1970~1999
案例:
ysql> create table t7(
-> name char(15),
-> syear year,
-> birthday date,
-> up_class time,
-> party datetime
-> );
mysql> insert into t7 values("bob",1990,20181210,083000,20180601120000);
mysql> select * from t7;
+------+-------+------------+----------+---------------------+
| name | syear | birthday | up_class | party |
+------+-------+------------+----------+---------------------+
| bob | 1990 | 2018-12-10 | 08:30:00 | 2018-06-01 12:00:00 |
+------+-------+------------+----------+---------------------+
1 row in set (0.00 sec)
mysql> select curdate(),curtime();
+------------+-----------+
| curdate() | curtime() |
+------------+-----------+
| 2021-01-19 | 02:51:20 |
+------------+-----------+
1 row in set (0.00 sec)
mysql> select now(),date(now()),time(now()),year(now()),month(now()),day(now());
+---------------------+-------------+-------------+-------------+--------------+------------+
| now() | date(now()) | time(now()) | year(now()) | month(now()) | day(now()) |
+---------------------+-------------+-------------+-------------+--------------+------------+
| 2021-01-19 02:51:25 | 2021-01-19 | 02:51:25 | 2021 | 1 | 19 |
+---------------------+-------------+-------------+-------------+--------------+------------+
1 row in set (0.00 sec)
mysql> select year(sysdate()),month(sysdate()),day(sysdate()),time(sysdate());
+-----------------+------------------+----------------+-----------------+
| year(sysdate()) | month(sysdate()) | day(sysdate()) | time(sysdate()) |
+-----------------+------------------+----------------+-----------------+
| 2021 | 1 | 19 | 03:00:50 |
+-----------------+------------------+----------------+-----------------+
1 row in set (0.00 sec)
- 插入日期
mysql>
mysql> desc t9;
+-------+---------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+---------+------+-----+---------+-------+
| y | year(4) | YES | | NULL | |
| t | time | YES | | NULL | |
| d | date | YES | | NULL | |
+-------+---------+------+-----+---------+-------+
3 rows in set (0.00 sec)
mysql> insert into t9 values(now(),now(),now());
Query OK, 1 row affected, 1 warning (0.00 sec)
mysql> select * from t9;
+------+----------+------------+
| y | t | d |
+------+----------+------------+
| 2021 | 03:08:39 | 2021-01-19 |
+------+----------+------------+
1 row in set (0.00 sec)
mysql>
mysql> show create table t1; #查看表创建命令,以及默认字符集等等
mysql> create table t2 () default charset=utf8; #指定默认字符集为utf8
四、MySQL增删改查
- 增
mysql> alter table t8 add email varchar(40),add qq char(11);
- 删
mysql> alter table t1 drop name,drop text;
- 改
modify
change
rename
- 查
show desc select
五、索引
- 算法:btree b+tree hash
- 网站:
算法索引详解1
算法讲解2
算法:btree b+tree hash
索引:是对记录集的多个字段进行排序的方法
- btree模型
默认索引类型;索引信息存储在表对应的文件中
- b+tree模型
- 索引优缺点
优点:加快查询速度
缺点:减慢写入数据的速度,占用磁盘空间
六、索引增删
- desc 查看索引,看key列
mysql> desc t1;
+--------+----------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+--------+----------+------+-----+---------+-------+
| id | int(10) | YES | | NULL | |
| name | char(10) | YES | | NULL | |
| gender | char(10) | YES | | NULL | |
| email | char(15) | YES | | NULL | |
+--------+----------+------+-----+---------+-------+
4 rows in set (0.00 sec)
- 创建索引【Index索引遵循最左前缀,详情看5(算法索引详解1)】
表已存在时使用如下格式:
mysql> create index 索引名 on 表名(字段名);
mysql> create index id_index on t1(id);
在创建表格时添加索引
mysql> create table t2(
-> id int(10),
-> name char(20),
-> age char(2),
-> sex char(10),
-> index(id),index(name)); #默认情况下,索引名和字段名相同
Query OK, 0 rows affected (0.06 sec)
- 查看索引
mysql> desc t1;
+--------+----------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+--------+----------+------+-----+---------+-------+
| id | int(10) | YES | MUL | NULL | |
| name | char(10) | YES | | NULL | |
| gender | char(10) | YES | | NULL | |
| email | char(15) | YES | | NULL | |
+--------+----------+------+-----+---------+-------+
4 rows in set (0.00 sec)
MUL 代表key值
mysql> show create table t1\G
*************************** 1. row ***************************
Table: t1
Create Table: CREATE TABLE `t1` (
`id` int(10) DEFAULT NULL,
`name` char(10) DEFAULT NULL,
`gender` char(10) DEFAULT NULL,
`email` char(15) DEFAULT NULL,
KEY `id_index` (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1
1 row in set (0.00 sec)
mysql> show index from t1\G
*************************** 1. row ***************************
Table: t1
Non_unique: 1
Key_name: id_index #索引键名称
Seq_in_index: 1
Column_name: id
Collation: A
Cardinality: 2
Sub_part: NULL
Packed: NULL
Null: YES
Index_type: BTREE #默认为btree算法
Comment:
Index_comment:
1 row in set (0.00 sec)
- 删除索引
mysql> drop index 索引名 on 表名;
mysql> drop index id on t2;
Query OK, 0 rows affected (0.00 sec)
Records: 0 Duplicates: 0 Warnings: 0
mysql>
row ***************************
Table: t1
Non_unique: 1
Key_name: id_index #索引键名称
Seq_in_index: 1
Column_name: id
Collation: A
Cardinality: 2
Sub_part: NULL
Packed: NULL
Null: YES
Index_type: BTREE #默认为btree算法
Comment:
Index_comment:
1 row in set (0.00 sec)
* 删除索引
```shell
mysql> drop index 索引名 on 表名;
mysql> drop index id on t2;
Query OK, 0 rows affected (0.00 sec)
Records: 0 Duplicates: 0 Warnings: 0
mysql>