- 服务器一般都用linux,性能和可靠性方面比windows强太多了,资源占用还少
- 查看系统中是否已安装 MySQL 服务,删除已有的,现在都是自带mariadb,建议也删除
- linux安装最简单了yum搞定,这里系统用centos 7
- 据说8.0很快,内存要求1G以上
# 先查找系统上有没有旧的残余的漏网之鱼,yum干掉,建议安装之前先把国内基本源先做了
rpm -qa | grep mysql
yum list installed | grep mysql
yum -y remove mysql*
# 下载源安装
cd /tmp
wget https://repo.mysql.com//mysql80-community-release-el7-1.noarch.rpm
rpm -ivh mysql80-community-release-el7-1.noarch.rpm
yum install mysql-community-server
# 开启mysql服务,查看初始密码,登录
systemctl start mysql
systemctl enable mysql
grep 'temporary password' /var/log/mysqld.log
mysql -uroot -p
输入密码,如果前面没有找到密码就直接回车
# 进去后先修改密码,新命令
旧5.7 grant all privileges on testDB.* to test@localhost identified by '密码';
新8.0 ALTER USER 'root'@'localhost' IDENTIFIED BY '密码';
flush privileges;
# 修改配置
vim /etc/my.cnf.d/server.cnf
数据库操作
- 我因为学习的原因这里命令都还是5.6版本的,如果8.0有变化自行百度
- 关于mysql登录用户的数据库是mysql里面的user表
- 要给别的用户授权,只有用root
# 一些基本命令,创建数据库时候用utf8,创建表的时候也是,引擎innodb才支持事物
show databases;
create databases default charset utf8;
# 一个表里只能有一个自增列和主键,主键就是非空,不重复,查找加速的功能
create table test(id int not null auto_increment primary key,name char(10)) charset=utf8;
use mysql;
selcect * from 表名;
grant all privileges on testDB.* to test@"192.168.1.%" identified by '密码';
# all privileges所有权限,testDB.*数据testDB里所有,test这个用户,192.168.1里的所有IP都可以登录,
drop 用户名;
drop table 表名;
drop database 数据库名;
Mysql的数据类型
- 数字类型 tinyint,int,bigint表示的长度不一样,float,doublefloat,都是表示不精准的浮点数,精准的用decimal
- 字符类型 char,varchar() char(10)是10个字符,不够后面全部填充。varchar(10)里面是填多少放多少,例如“root”char就是10字符,varchar就是4字符,速度没有char快,这两种最多是255个字符
- text((216-1),mediutext((224-1),longtext(2**32-1)存一大串文字的
- 时间类型 datatime
- 外键 一张表里的数据,与另外一张表里的某列数据产生关系,就是外键
增
# 增加多条,就是多行
insert into tb1(name,age) values('alex',18),('egon',19),('yuan',20);
# tb11表里所有数据插入到tb12表中
insert into tb12(name,age) select name,age from tb11;
删
# 底下的命令是物理删除(不建议),常用是创建一个字段isdelete(bool),用0和1来代替物理删除
delete from tb12;
delete from tb12 where id !=2
delete from tb12 where id =2
delete from tb12 where id > 2
delete from tb12 where id >=2
delete from tb12 where id >=2 or name='alex'
改
update tb12 set name='alex' where id>12 and name='xx'
update tb12 set name='alex',age=19 where id>12 and name='xx'
查
select * from tb12;
select id,name from tb12;
select id,name from tb12 where id > 10 or name ='xxx';
# 将表头取了别名
select id,name as cname from tb12 where id > 10 or name ='xxx';
select name,age,11 from tb12;
select * from tb12 where id != 1
# 后面条件是在1,5,12当中的任意一个元素
select * from tb12 where id in (1,5,12);
select * from tb12 where id not in (1,5,12);
# 条件是tb11当中的id这列
select * from tb12 where id in (select id from tb11)
# 条件是5到12
select * from tb12 where id between 5 and 12;
通配符:
# a开头
select * from tb12 where name like "a%"
# a开头,后面只能有一位
select * from tb12 where name like "a_"
分页:
# 前10条,分页对于数据大时,越往后的页面越来越慢,因为数据库需要查找量越来越大,业界现在的做法是记录ID,使用ID往后偏移多少量查询数据,这样查询就会较快
select * from tb12 limit 10;
# 从0开始往后数10条
select * from tb12 limit 0,10;
select * from tb12 limit 10,10;
select * from tb12 limit 20,10;
select * from tb12 limit 10 offset 20;
排序:order
select * from tb12 order by id desc; 大到小
select * from tb12 order by id asc; 小到大
select * from tb12 order by age desc,id desc;
分组:group by
# 将查询结果按照某个字段或多个字段进行分组,字段中值相等的为一组,每个分组只会出现一行
# 按部门ID分组,统计每个部门里面人员ID数
select count(id) from tb11 group by port_id;
# 对聚合函数使用条件查询的要使用having关键字,不能使用where
连表查询
# 一种写法
select * from userinfo5,department5 where userinfo5.part_id = department5.id
# 这种写法left左边的表会全部显示
select * from userinfo5 left join department5 on userinfo5.part_id = department5.id
select * from department5 left join userinfo5 on userinfo5.part_id = department5.id
# department5右边全部显示
select * from userinfo5 right join department5 on userinfo5.part_id = department5.id
select * from userinfo5 innder join department5 on userinfo5.part_id = department5.id
将出现null时一行隐藏
# 连接多张表
select * from
department5
left join userinfo5 on userinfo5.part_id = department5.id
left join userinfo6 on userinfo5.part_id = department5.id
事物
# 当数据被更改时(如插入,更新,删除),对于整个业务逻辑来说,全部语句完成才算完成,只要有一步错误都回退,类似银行转账
begin
正常的mysql语句
commit
索引
索引类型:hash类型(查单一值块,范围就慢了),btree类型(二叉树算法,常用)
主键索引:加速查找 + 不能为空 + 不能重复
- 普通索引:加速查找
- 唯一索引:加速查找 + 不能重复
- 联合索引(多列):
- 联合主键索引
- 联合唯一索引
- 联合普通索引
索引本身是需要占用硬盘空间的,原理像是字典里的拼音目录和生字
创建普通索引 create index 索引名xxx on 表xxx(列名);
创建唯一索引 create unique index 索引名xxx on 表xxx(列名);
创建联合唯一索引 create unique index 索引名xxx on 表xxx(列名,列名);
联合遵循:最左前缀匹配原则,要匹配最左边的列名才走索引
几种不会命中索引的情况:
like,or,函数,!=,>(主键和数字类型还是会走索引),order by(主键还是会走索引)
慢日志
慢日志:就是sql语句执行慢的,将其记录下来,分析为什么慢。一般我们将执行时间大于多少秒的,未命中索引的,记录到某个日志文件里。
配置方式:内存,配置文件这两种
内存
show variable like “”%query“”;
set global xxx = on; 修改内存的参数
配置文件加载顺序如下:/etc/my.cnf /etc/mysql/my.cnf ~/.my.cnf 一般我们直接去第一个里修改
配置文件里具体内容解释查看网络搜索,这里修改是会影响全部的数据库
下面的内容不常用,可能引起不适,除了时间格式化输出
视图:将某个查询语句设置别名,实例:create view 视图名称 as SQL
触发器:自己看教程,不常用
函数:自己定义函数实现某些特定的功能。重点是时间函数的格式化
# 取消:作用,使用\\作为终止符号
delimiter \\
# 创建函数f1
create function f1(
i1 int,
i2 int)
returns int
BEGIN
declare num int default 0;
set num = i1 + i2;
return(num);
END \\
delimiter ;
select f1(3,4);
# 时间DATE_FORMAT(ctime, "%Y-%m"),ctime是时间,后面是要输出的格式
select DATE_FORMAT(ctime, "%Y-%m")
select DATE_FORMAT(2019-08-31, "%Y-%m") 结果就是2019-08
存储过程
定义一个别名,用来定义一大堆sql语句,程序员调用时只要call 函数名()就行了,不是很常用
# 创建一个过程p1,()里面写参数,有in,out,inout,注意int是输入,out是输入后,外部还能接收执行后的值
delimiter //
create procedure p1(int a int,in b int)
BEGIN
select * from student;
insert into teacher(tname) values("a"),("b");
END //
delimiter ;
# mysql调用p1
call p1()
# pymysql调用
cursor.callproc("p1",(a,b))