下载mysql的rpm包:https://dev.mysql.com/downloads/repo/yum/mysql80-community-release-el7-3.noarch.rpm
安装mysql源:
yum -y localinstall mysql80-community-release-el7-3.noarch.rpm
安装mysql
yum -y install mysql-community-server
启动mysql
systemctl start mysql
初始化
mysqld --initialize --user=mysql --console
mysqld -install
查看初始化密码
grep 'tempoary password' /var/log/mysql.log |awk '{print $NF}'
进入mysql
mysql -uroot -p
>show variables like 'validate_password%'; #查看密码策略规则
>set global validate_password.policy=LOW; #关闭策略,密码简单化
>set global validate_password.length=6;
>set global validate_password.check_user_name=OFF;
>flush privileges;
>alter user 'root'@'localhost' identified by '123456'; #重新设置密码
>use mysql; #设置允许远程登录
>update user set Host='%' where User='root';
>grant all privileges on *.* to 'root'@'%' with grant option;
>use mysql; #更改加密方式,允许native连接
>alter user 'root'@'%' identified with mysql_native_password by 'root';
>select user,host from mysql,user;
>select user();
show variables like 'validate_password%'; #查看密码策略规则
>validate_password_length 8 # 密码的最小长度,此处为8。
>validate_password_mixed_case_count 1 # 至少要包含小写或大写字母的个数,此处为1。
>validate_password_number_count 1 # 至少要包含的数字的个数,此处为1。
>validate_password_policy MEDIUM # 强度等级,其中其值可设置为0、1、2。分别对应:
【0/LOW】:只检查长度。
【1/MEDIUM】:在0等级的基础上多检查数字、大小写、特殊字符。
【2/STRONG】:在1等级的基础上多检查特殊字符字典文件,此处为1。
>validate_password_special_char_count 1 # 至少要包含的个数字符的个数,此处为1
>validate_password_dictionary_file 密码策略文件,策略为STRONG才需要
#mysql_secure_installation 初始化MySQL,初始化过程中会提示输入随机密码
#查看数据库当前设置的最大连接数量
show variables like '%max_connections%';
#查看当前连接数量
show global status like 'Max_used_connections';
#设置日志文件时间
SET GLOBAL log_timestamps=SYSTEM;
#查看连接数
show status like 'Threads%';
Threads_connected #这个数值是打开的连接数
Threads_running #这个数值是激活的连接数,低于connected数值
create [tempoary] table [库名].表名(表结构定义) [选项]
tempoary 临时表,会话结束后自动消失
表结构:
字段名 数据类型
选项:
字符集:
charset=utf8 //如果不设定则使用数据库字符集
存储引擎:
engine=InnoDB
其它:MyISAM、Memory/Heap BDB,不同的引擎在保存表的结构和数据采用不同的方式
>show engines 显示存储引擎状态信息
>show engine 引擎名 { logs | status } 显示存储引擎的日志或状态信息
自增起始数:
auto_increment=行数
数据文件目录:
data directory='/mysql/data'
索引文件目录:
index directory='/mysql/index'
表注释:
comment='this is table'
>create table student(id int(10), name varchar(20),socre float);
create table student select * from school.student; #将其它数据库中的表复制到当前库
create table copy_table_construct like student; #复制一个其它表的结构,创建一个新表
create table stu (id int(5) not null auto_increment,
name varchar(20) not null,
primary key (id)
) engine=InnoDB default charset=utf8;
create table stu (id int(5) not null auto_increment,
name varchar(20) not null,
date datetime not null default now(),
sex varchar(6) default null,
)
create index 创建索引
create index index_city on student; //在student表上建立一个城市索引
查询表
>show student;
#查看表结构
>desc student;
>describe student;
Field 字段名
Type 字段类型
Null 是否可以存储NULL值
Key 是否已编制索引
Default 是否有默认值
Extra 获取相关附加信息
#查看表详细信息,输出SQL语句
>show create table student \G
show tables; 或 show tables from database_name; 显示当前数据库中所有表的名称
show databases; 显示mysql中所有数据库的名称
show columns from table_name from database_name; 或 show columns from database_name.table_name; 显示表中列名称
show grants for user_name; 显示表中列名称
show index from table_name; 显示表的索引
show status; 显示一下系统特定资源的信息,线程数量等
show variables; 显示系统变量的名称和值
show processlist; 显示系统中正在运行的所有进程,当前正在执行的查询,大多数用户查看他们自己的进程
show table status; 显示当前使用或指定的database的每个表的信息
show privileges; 显示服务器所支持的不同权限
show create database database_name; 显示create database语句是否能创建指定的数据库
show create table table_name; 显示create table是否能创建指定表
show engines; 显示安装以后可用的存储引擎和默认引擎
show innodb status; 显示innoDB存储引擎的状态
show logs; 显示BDB存储引擎的日志
show warnings; 显示最后一个执行语句所产生的错误、警告和通知
show errors; 只显示最后一个执行语句所产生的错误
表的查询操作
select * from 表 *效率低
select * from 表 where id > 1
select nid,name,gender as gg from 表 where id > 1 as相当于取别名,别名为gg
select count(id) from userinfo5; 查询表内有多少条数据
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; 多加了一列11
其他:
select * from tb12 where id != 1
select * from tb12 where id in (1,5,12);
select * from tb12 where id not in (1,5,12);
select * from tb12 where id in (select id from tb11)
select * from tb12 where id between 5 and 12; 闭区间
通配符:
select * from tb12 where name like "a%" 以a开头的所有(多个字符)
select * from tb12 where name like "a_" 以a开头的所有(一个字符)
分页:
select * from tb12 limit 10;
select * from tb12 limit 0,10; 0开始位置行,10查看的行数
select * from tb12 limit 10,10;
select * from tb12 limit 20,10;
select * from tb12 limit 10 offset 20; 20开始位置行,10查看的行数
排序:
select * from tb12 order by id desc; 大到小
select * from tb12 order by id asc; 小到大
select * from tb12 order by age desc,id desc; 按优先级安序排列
取后10条数据
select * from tb12 order by id desc limit 10;
分组:
select count(id),part_id from userinfo5 group by part_id; 统计数量
select count(id),part_id as num from userinfo5 group by part_id; 统计数量,对字段重命名
select max(id),part_id from userinfo5 group by part_id; part_id重合时,id取max
select nin(id),part_id from userinfo5 group by part_id;
聚合函数(去除重复)
count
max
min
sum
avg 平均值
**** 如果对于聚合函数结果进行二次筛选时?必须使用having ****
select count(id),part_id from userinfo5 group by part_id having count(id) > 1;
select count(id),part_id from userinfo5 where id > 0 group by part_id having count(id) > 1;
join 连表操作:
select column_name from table1 inner join table2 on table1.column_name=table2.column_name
select * from userinfo,department where userinfo.part_id = department.id #表的并集,同inner join
select * from userinfo left join department on userinfo.part_id = department.id # left join左连接:userinfo右边允许有空
select * from userinfo right join department on userinfo.part_id = department.id # right join右连接:department左边允许有空
select * from userinfo inner join department on userinfo.part_id = department.id # inner join内连接:不会出现null
select * from userinfo full join department on userinfo.part_id = department.id # full join完全连接:左右都可能出现null
union 组合两个或更多select语句的结果集
使用条件:每个select语句必须具有相同的列数,且以相同的顺序排列
组合,去除重复的部分
select nickname from A union select name from B
组合,不去除重复的部分
select nickname from A union all select name from B
select into 从另一个表中复制数据到新表中
select * into newtable from student;
select name,id into newtable from student where school='xiwang';
用户和授权
#查看用户
>select host,user from mysql.user;
>select user(); //当前用户信息
>select user() \G //不以图表格式显示,以行形式列出
#创建用户
>create user 'test'@'localhost' identified by 'password';
#删除用户username=test,host=localhost
1、>delete from mysql.user where user='test' and host='localhost';
2、>drop user 'test'@'localhost'
#grant语句,不仅可以创建用户,还能授权
使用方法:
grant [权限] on [库名].[表名] to [用户名]@'[地址]' identified by 'password';
#权限选项:
all privileges 所有权限(增、删、查、改)
insert delete update select //增、删、查、改
#库名和表名
*.* //表示所有库的所有表
name.* //name库的所有表
name.name //name库的name表
#地址
192.168.110.110 指定IP连接
localhost 不会被解析成IP,直接通过UNixsocket连接
127.0.0.1 通过TCP/IP协议连接,只能在本机访问
::1 兼容ipv6,表示同ipv4的127.0.0.1
% 所有设备
>grant all privileges on *.* to 'test'@'%' identified by 'password';
#创建test用户并授权所有库和表,允许所有设备连接
>show grants; //查看所有的授权
>show grant for 'test'@'localhost'; //只查看一个用户的权限
#撤销权限
>remove all on *.* from 'test'@'localhost'; 撤销所有权限
>remove delete on *.* from 'test'@'localhost' 仅撤销删除权限
>revoke drop,delete on test.* from 'test'@'localhost'; 解除权限
insert 插入数据
>insert into 表名 (列1,列2) values(值1,值2)
>insert into 表名 set 列1=值,列2=值;
#创建一个表
>create table student(
->Student_ID int(4) not NULL auto_increment,
->name varchar(20) not NULL,
->math float not NULL,
->chinese float not NULL,
);
#插入数据
>insert into student values(2000,'xiaomeng',94,83); //直接插入数据,值的顺序必须和表字段顺序相同
>insert into student (Student_ID,name,math,chinese) values(2001,'xiaoming',98,76); //字段和值相对应插入数据
#insert into ... select ... from 复制表数据
>insert into new-studnet (Student_ID,name,math,chinese) select Student_ID,name,math,chinese from studnet;
update修改表
>update 表名 set 字段1=值 字段2=值 [where 子句];
>update student set math=98; //更新所有行的值
>update student set math=78 where Student_ID=45; //指定条件
drop、delete、truncate
drop database name 删除库
#drop table 删除表
>drop table 表1 表2; //不判断是否存在,不存在时会提示错误
>drop table if exists 表1; //用于判断是否存在
>delete from table; //清空表记录,每次从表中删除一行,删除记录在日志中可以做回滚操作
>truncate table; //删除表中所有行,表结构、约束、索引保持不变
单字段去重
select 重复字段 from 表 group by 重复字段 having count(*)>1;
//count(1)和count(*)等价,count(*)返回表中所有存在的行的总数包含值为null的行,count(字段)表示除去null的所有行的总数
>select name from student group by name having count(*)>1;
全部重复数据
>select * from 表 where 重复字段 in (select 重复字段 from 表 group by 重复字段 having count(*)>1)
alter修改字段
修改表结构
>ALTER TABLE 表名 ORDER BY 字段名 ASC/DESC #表数据按某一字段排序,ASC正序,DESC反序
alter table 表名 [选项]
选项:
add column 列名 类型
change column 旧列名 新列名 新类型
alter column 列名 {set default 默认值 |drop default}
modify column 列名 类型
drop column 列名
rename to 新表名
#添加字段
>alter table 表名 add 新字段 数据类型; //默认新添加的字段在数据表最后列
>alter table student add column math float first; //指定新增字段在表中位置,第一位
>alter table student add column chinese float after math; //指定新增字段位置在math后
#修改字段名称
>alter table 表名 change 旧字段名 新字段名 数据类型;
>alter table student change id student_ID int(10); //将字段id改名为student_ID,同时修改数据类型
#修改字段默认值
>alter table 表名 alter column 字段名 drop default; //删除默认值
>alter table 表名 alter column 字段名 set default 默认值; //设置默认值
#修改字段数据类型
>alter table 表名 modify 字段名 数据类型;
>alter table student modify name varchar(30); //修改字段name的数据长度
#删除字段
>alter table 表名 drop 字段名;
>alter table student drop english; //删除student中的english字段
#修改表名
>alter table 旧表名 rename to 新表名; //to为可选参数,不影响结果
>alter table student rename to Student;
添加约束
alter table 表名 constraint 约束名 约束类型(列名)
alter table student add constraint pk_name primary key (name); #主键约束
alter table student add constraint un_name unique(ID); #唯一约束
alter table student add constraint ch_ID check(ID>0); #范围约束
alter table student add constraint de_name default "test" for name; #默认约束
外键约束
alter table 表名add constraint 约束名 foreign key(关联字段) reference 主表(关联字段);
删除约束
alter table student drop constraint pk_name; #删除主键约束
alter table student drop constraint un_name; #删除唯一约束
alter table student drop constraint ch_ID; #删除范围约束
alter table student drop constraint de_name; #删除默认约束
where条件判断
#mysql中in常用于where表达式中,其作用是查询某个范围内的数据。
select * from where field in(value1,value2,value3,…)
#IN列表项的值是不明确的,而可能是通过一个子查询得到的:
SELECT * FROM article WHERE uid IN(SELECTuidFROMuserWHEREstatus=0)
union
#UNION 操作符用于连接两个以上的 SELECT 语句的结果组合到一个结果集合中。多个 SELECT 语句会删除重复的数据,distinct功能
select country from website union select country from apps;
like
#LIKE 子句中使用百分号 %字符来表示任意字符,类似于UNIX或正则表达式中的星号 *
#like子句代替"="
select * from name where pepole like "%ming"; #以ming结尾的值
数据备份
select into outfile 导出数据,文本形式
fields terminated by '字符' 字段分隔符,默认字符为制表符'\t'
fields [optionally] enclosed by '单字符' 字段引用符,加上optionally后在数字类型上不会有引用符号
fields escaped by '单字符' 转义字符,默认为'\'
lines starting by '字符' 每行前都加此支付,默认为空
lines terminated by '字符' 行结束符,默认为'\n'
#导出一个表中的数据
>select * from test into outfile '/tmp/test.txt';
#将各个值之间用逗号隔开
>select a,b,a+b into outfile '/tmp/test.txt' fields terminated by ',' optionally enclosed by '"' lines terminated by '\n' from test;
>system more /tmp/test.txt //查看数据
load data 导入数据
>load data local infile '/tmp/test.txt' into table test;
>load data local infile '/tmp/test.txt' into table test fields terminated by ':' lines terminated by '\r\n';
mysqlimport 导入文本数据
可选参数:
--fields-terminated-by=name 字段分隔符
--fields-enclosed-by=name 字段引用符
--fields-optionally-enclosed-by=name 字段引用符,只用在 char、varchar 和 text 等字符型字段上
--fields-escaped-by=name 转义字符
--lines-terminated-by=name 记录结束符
-- ignore-lines=number 忽略前几行
mysqlimport -uroot -p --local test test.txt
#指定格式
mysqlimport -uroot -p --local --field-terminated-by=":" --lines-terminated-by="\r\n" test test.txt
mysqldump 系统命令导出一个SQL脚本数据
参数选项:
--host -h 服务器IP地址
--port -P 服务器端口
--user -u mysql用户名
--password -p mysql用户密码
--databases 指定要备份的数据库
--all-databases 备份所有数据库
--compact 压缩模式,减少输出
--comments 添加注释信息
--complete-insert 输出完成的插入语句
--lock-tables 备份前锁定所有数据表
--no-create-db/--no-create-info 禁止生成创建数据库语句
--force 当出现错误时仍然继续备份操作
--default-character-set 指定默认字符集
--add-locks 备份数据表时锁定数据表
--ignore-table 忽略要备份的表
#备份所有数据库
mysqldump -uroot -p --all-databases > /backup/all.sql
#备份指定库,多个库用空格隔开
mysqldump -uroot -p 库名 > /backup/test.sql
#备份指定表,多个表用空格隔开
mysqldump -uroot -p 库名 表1 表2 > /backup/2table.sql
#备份指定数据库排除某些表
mysqldump -uroot -p 库名 --ignore-table=test.table1 -ignore-table=test.table2 > /backup/test.sql
数据导入
mysqladmin 系统命令
mysqladmin -uroot -p create test //创建数据库
mysql -uroot -p test < /backup/test.sql //导入数据
source导入
mysql -uroot -p #先进入sql界面
>source /backup/test.sql